Passed
Push — main ( 28b00c...2eb7a0 )
by Thierry
20:47 queued 09:28
created

QueryHistory::getQueries()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 24
c 1
b 0
f 0
nc 14
nop 0
dl 0
loc 36
rs 8.6026
1
<?php
2
3
namespace Lagdo\DbAdmin\Service\DbAdmin;
4
5
use Lagdo\DbAdmin\Config\AuthInterface;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
use Lagdo\DbAdmin\Service\Options;
8
use Lagdo\Facades\Logger;
9
10
/**
11
 * SQL queries logging and storage.
12
 */
13
class QueryHistory
14
{
15
    use ConnectionTrait;
16
17
    /**
18
     * @var bool
19
     */
20
    private bool $historyEnabled;
21
22
    /**
23
     * @var bool
24
     */
25
    private bool $historyDistinct;
26
27
    /**
28
     * @var int
29
     */
30
    private int $historyLimit;
31
32
    /**
33
     * The constructor
34
     *
35
     * @param AuthInterface $auth
36
     * @param DriverInterface $driver
37
     * @param array $database
38
     * @param array $options
39
     */
40
    public function __construct(private AuthInterface $auth,
41
        private DriverInterface $driver, array $database, array $options)
42
    {
43
        $this->historyEnabled = (bool)($options['history']['enabled'] ?? false);
44
        $this->historyDistinct = (bool)($options['history']['distinct'] ?? false);
45
        $this->historyLimit = (int)($options['history']['limit'] ?? 15);
46
        if (!$this->historyEnabled) {
47
            return;
48
        }
49
50
        // Connect to the logging database.
51
        $this->connect($driver, $database);
52
    }
53
54
    /**
55
     * @var string
56
     */
57
    protected function user(): string
58
    {
59
        return $this->auth->user();
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getQueries(): array
66
    {
67
        if (!$this->historyEnabled) {
68
            return [];
69
        }
70
        $ownerId = $this->getOwnerId(false);
71
        if ($ownerId === 0) {
72
            return [];
73
        }
74
75
        // PostgreSQL doesn't allow the use of distinct and order by
76
        // a field not in the select clause in the same SQL query.
77
        $category = Options::CAT_EDITOR;
78
        $select = $this->historyDistinct && $this->driver->jush() !== 'pgsql' ?
79
            'select distinct' : 'select';
80
        $query = "$select query from dbadmin_runned_commands c " .
81
            "where c.owner_id=:owner_id and c.category=:category " .
82
            "order by c.last_update desc limit {$this->historyLimit}";
83
        $values = [
84
            'owner_id' => $ownerId,
85
            'category' => $category,
86
        ];
87
        $statement = $this->executeQuery($query, $values);
88
        if ($statement !== false) {
89
            $commands = [];
90
            $id = 1;
91
            while (($row = $statement->fetchAssoc())) {
92
                $commands[$id++] = $row['query'];
93
            }
94
            return $commands;
95
        }
96
97
        Logger::warning('Unable to read commands from the query logging database.', [
98
            'error' => $this->connection->error(),
99
        ]);
100
        return [];
101
    }
102
}
103