Passed
Push — main ( 3310d4...08974e )
by Thierry
02:20
created

SelectFacade::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Driver\Facades;
4
5
use Lagdo\DbAdmin\Db\Page\Dql\SelectEntity;
6
use Lagdo\DbAdmin\Db\Page\Dql\SelectQuery;
7
use Lagdo\DbAdmin\Db\Page\Dql\SelectResult;
8
use Lagdo\DbAdmin\Db\Service\TimerService;
9
use Exception;
10
11
use function array_keys;
12
use function count;
13
14
/**
15
 * Facade to table select functions
16
 */
17
class SelectFacade extends AbstractFacade
18
{
19
    /**
20
     * @var SelectQuery|null
21
     */
22
    private SelectQuery|null $selectQuery = null;
23
24
    /**
25
     * @var SelectResult|null
26
     */
27
    private SelectResult|null $selectResult = null;
28
29
    /**
30
     * @param AbstractFacade $dbFacade
31
     * @param TimerService $timer
32
     */
33
    public function __construct(AbstractFacade $dbFacade, protected TimerService $timer)
34
    {
35
        parent::__construct($dbFacade);
36
    }
37
38
    /**
39
     * @return SelectQuery
40
     */
41
    private function query(): SelectQuery
42
    {
43
        return $this->selectQuery ??= new SelectQuery($this->page, $this->driver, $this->utils);
0 ignored issues
show
Bug introduced by
It seems like $this->driver can also be of type null; however, parameter $driver of Lagdo\DbAdmin\Db\Page\Dq...ectQuery::__construct() does only seem to accept Lagdo\DbAdmin\Driver\DriverInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        return $this->selectQuery ??= new SelectQuery($this->page, /** @scrutinizer ignore-type */ $this->driver, $this->utils);
Loading history...
44
    }
45
46
    /**
47
     * @return SelectResult
48
     */
49
    private function result(): SelectResult
50
    {
51
        return $this->selectResult ??= new SelectResult($this->page, $this->driver, $this->utils);
0 ignored issues
show
Bug introduced by
It seems like $this->driver can also be of type null; however, parameter $driver of Lagdo\DbAdmin\Db\Page\Dq...ctResult::__construct() does only seem to accept Lagdo\DbAdmin\Driver\DriverInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        return $this->selectResult ??= new SelectResult($this->page, /** @scrutinizer ignore-type */ $this->driver, $this->utils);
Loading history...
52
    }
53
54
    /**
55
     * @param string $table The table name
56
     * @param array $queryOptions The query options
57
     *
58
     * @return SelectEntity
59
     * @throws Exception
60
     */
61
    private function prepareSelect(string $table, array $queryOptions = []): SelectEntity
62
    {
63
        $tableStatus = $this->driver->tableStatusOrName($table);
0 ignored issues
show
Bug introduced by
The method tableStatusOrName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $tableStatus = $this->driver->tableStatusOrName($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        $tableName = $this->page->tableName($tableStatus);
65
        $selectEntity = new SelectEntity($table, $tableName,
66
            $tableStatus, $queryOptions);
67
        return $this->query()->prepareSelect($selectEntity);
68
    }
69
70
    /**
71
     * Get required data for create/update on tables
72
     *
73
     * @param string $table The table name
74
     * @param array $queryOptions The query options
75
     *
76
     * @return SelectEntity
77
     * @throws Exception
78
     */
79
    public function getSelectData(string $table, array $queryOptions = []): SelectEntity
80
    {
81
        return $this->prepareSelect($table, $queryOptions);
82
    }
83
84
    /**
85
     * Get required data for create/update on tables
86
     *
87
     * @param string $table The table name
88
     * @param array $queryOptions The query options
89
     *
90
     * @return int
91
     */
92
    public function countSelect(string $table, array $queryOptions): int
93
    {
94
        $selectEntity = $this->prepareSelect($table, $queryOptions);
95
        $hasGroupsInFields = count($selectEntity->group) < count($selectEntity->select);
96
97
        try {
98
            $query = $this->driver->getRowCountQuery($table, $selectEntity->where,
99
                $hasGroupsInFields, $selectEntity->group);
100
            return (int)$this->driver->result($query);
101
        } catch(Exception) {
102
            return -1;
103
        }
104
    }
105
106
    /**
107
     * @param SelectEntity $selectEntity
108
     *
109
     * @return void
110
     */
111
    private function executeQuery(SelectEntity $selectEntity): void
112
    {
113
        $this->timer->start();
114
115
        // From driver.inc.php
116
        $statement = $this->driver->execute($selectEntity->query);
117
        $selectEntity->duration = $this->timer->duration();
118
        $selectEntity->rows = [];
119
120
        // From adminer.inc.php
121
        if (!$statement) {
122
            $selectEntity->error = $this->driver->error();
123
            return;
124
        }
125
126
        // From select.inc.php
127
        $selectEntity->rows = [];
128
        while (($row = $statement->fetchAssoc())) {
129
            if ($selectEntity->page && $this->driver->jush() === "oracle") {
130
                unset($row["RNUM"]);
131
            }
132
            $selectEntity->rows[] = $row;
133
        }
134
    }
135
136
    /**
137
     * Get required data for create/update on tables
138
     *
139
     * @param string $table The table name
140
     * @param array $queryOptions The query options
141
     *
142
     * @return array
143
     * @throws Exception
144
     */
145
    public function execSelect(string $table, array $queryOptions): array
146
    {
147
        $selectEntity = $this->prepareSelect($table, $queryOptions);
148
        $this->executeQuery($selectEntity);
149
150
        if ($selectEntity->error !== null) {
151
            return [
152
                'message' => $selectEntity->error,
153
            ];
154
        }
155
        if (count($selectEntity->rows) === 0) {
156
            return [
157
                'message' => $this->utils->trans->lang('No rows.'),
158
            ];
159
        }
160
161
        // $backward_keys = $this->driver->backwardKeys($table, $tableName);
162
        // lengths = $this->getValuesLengths($rows, $selectEntity->queryOptions);
163
164
        $queryFields = array_keys($selectEntity->rows[0]);
165
        $this->result()->setResultHeaders($selectEntity, $queryFields);
166
167
        return [
168
            'headers' => $selectEntity->headers,
169
            'query' => $selectEntity->query,
170
            'limit' => $selectEntity->limit,
171
            'duration' => $selectEntity->duration,
172
            'rows' => $this->result()->getRows($selectEntity),
173
        ];
174
    }
175
}
176