Passed
Push — main ( 148bfb...4ebd52 )
by Thierry
01:32
created

Driver::grouping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver;
4
5
use Lagdo\DbAdmin\Driver\Entity\ConfigEntity;
6
7
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
8
use Lagdo\DbAdmin\Driver\Db\ServerInterface;
9
use Lagdo\DbAdmin\Driver\Db\DatabaseInterface;
10
use Lagdo\DbAdmin\Driver\Db\TableInterface;
11
use Lagdo\DbAdmin\Driver\Db\QueryInterface;
12
use Lagdo\DbAdmin\Driver\Db\GrammarInterface;
13
14
use Lagdo\DbAdmin\Driver\Exception\AuthException;
15
16
abstract class Driver implements DriverInterface
17
{
18
    use ConfigTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Driver\ConfigTrait requires some properties which are not provided by Lagdo\DbAdmin\Driver\Driver: $onActions, $jush, $numberRegex, $grouping, $editFunctions, $inout, $version, $unsigned, $enumLength, $functions, $types, $operators
Loading history...
19
    use ServerTrait;
20
    use TableTrait;
21
    use DatabaseTrait;
22
    use QueryTrait;
23
    use GrammarTrait;
24
    use ConnectionTrait;
25
26
    /**
27
     * @var UtilInterface
28
     */
29
    protected $util;
30
31
    /**
32
     * @var TranslatorInterface
33
     */
34
    protected $trans;
35
36
    /**
37
     * @var ServerInterface
38
     */
39
    protected $server;
40
41
    /**
42
     * @var DatabaseInterface
43
     */
44
    protected $database;
45
46
    /**
47
     * @var TableInterface
48
     */
49
    protected $table;
50
51
    /**
52
     * @var QueryInterface
53
     */
54
    protected $query;
55
56
    /**
57
     * @var GrammarInterface
58
     */
59
    protected $grammar;
60
61
    /**
62
     * @var ConnectionInterface
63
     */
64
    protected $connection;
65
66
    /**
67
     * @var ConfigEntity
68
     */
69
    protected $config;
70
71
    /**
72
     * Executed queries
73
     *
74
     * @var array
75
     */
76
    protected $queries = [];
77
78
    /**
79
     * Query start timestamp
80
     *
81
     * @var int
82
     */
83
    protected $start = 0;
84
85
    /**
86
     * The last error code
87
     *
88
     * @var int
89
     */
90
    protected $errno = 0;
91
92
    /**
93
     * The last error message
94
     *
95
     * @var string
96
     */
97
    protected $error = '';
98
99
    /**
100
     * The constructor
101
     *
102
     * @param UtilInterface $util
103
     * @param TranslatorInterface $trans
104
     * @param array $options
105
     */
106
    public function __construct(UtilInterface $util, TranslatorInterface $trans, array $options)
107
    {
108
        $this->util = $util;
109
        $this->util->setDriver($this);
110
        $this->trans = $trans;
111
        $this->config = new ConfigEntity($options);
112
        $this->initConfig();
113
        $this->createConnection();
114
    }
115
116
    /**
117
     * Set driver config
118
     *
119
     * @return void
120
     */
121
    abstract protected function initConfig();
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function connect(string $database, string $schema)
127
    {
128
        if (!$this->connection->open($database, $schema)) {
129
            throw new AuthException($this->error());
130
        }
131
        $this->config->database = $database;
132
        $this->config->schema = $schema;
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function minVersion(string $version, string $mariaDb = '', ConnectionInterface $connection = null)
139
    {
140
        if (!$connection) {
141
            $connection = $this->connection;
142
        }
143
        $info = $connection->serverInfo();
144
        if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) {
145
            $info = $match[1];
146
            $version = $mariaDb;
147
        }
148
        return (version_compare($info, $version) >= 0);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function charset()
155
    {
156
        // SHOW CHARSET would require an extra query
157
        return ($this->minVersion('5.5.3', 0) ? 'utf8mb4' : 'utf8');
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function begin()
164
    {
165
        $result = $this->connection->query("BEGIN");
166
        return $result !== false;
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function commit()
173
    {
174
        $result = $this->connection->query("COMMIT");
175
        return $result !== false;
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function rollback()
182
    {
183
        $result = $this->connection->query("ROLLBACK");
184
        return $result !== false;
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    public function setUtf8mb4(string $create)
191
    {
192
        static $set = false;
193
        // possible false positive
194
        if (!$set && preg_match('~\butf8mb4~i', $create)) {
195
            $set = true;
196
            return 'SET NAMES ' . $this->charset() . ";\n\n";
197
        }
198
        return '';
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    public function setError(string $error = '')
205
    {
206
        $this->error = $error;
207
    }
208
209
    /**
210
     * @inheritDoc
211
     */
212
    public function error()
213
    {
214
        return $this->error;
215
    }
216
217
    /**
218
     * @inheritDoc
219
     */
220
    public function hasError()
221
    {
222
        return $this->error !== '';
223
    }
224
225
    /**
226
     * @inheritDoc
227
     */
228
    public function setErrno($errno)
229
    {
230
        $this->errno = $errno;
231
    }
232
233
    /**
234
     * @inheritDoc
235
     */
236
    public function errno()
237
    {
238
        return $this->errno;
239
    }
240
241
    /**
242
     * @inheritDoc
243
     */
244
    public function hasErrno()
245
    {
246
        return $this->errno !== 0;
247
    }
248
249
    /**
250
     * @inheritDoc
251
     */
252
    public function execute(string $query)
253
    {
254
        if (!$this->start) {
255
            $this->start = intval(microtime(true));
256
        }
257
        $this->queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
258
        return $this->connection->query($query);
259
    }
260
261
    /**
262
     * @inheritDoc
263
     */
264
    public function queries()
265
    {
266
        return [implode("\n", $this->queries), $this->trans->formatTime($this->start)];
267
    }
268
269
    /**
270
     * @inheritDoc
271
     */
272
    public function applyQueries(string $query, array $tables, $escape = null)
273
    {
274
        if (!$escape) {
275
            $escape = function ($table) {
276
                return $this->table($table);
277
            };
278
        }
279
        foreach ($tables as $table) {
280
            if (!$this->execute("$query " . $escape($table))) {
281
                return false;
282
            }
283
        }
284
        return true;
285
    }
286
287
    /**
288
     * @inheritDoc
289
     */
290
    public function values(string $query, $column = 0)
291
    {
292
        $values = [];
293
        $statement = $this->connection->query($query);
294
        if (is_object($statement)) {
295
            while ($row = $statement->fetchRow()) {
296
                $values[] = $row[$column];
297
            }
298
        }
299
        return $values;
300
    }
301
302
    /**
303
     * @inheritDoc
304
     */
305
    public function keyValues(string $query, ConnectionInterface $connection = null, bool $setKeys = true)
306
    {
307
        if (!is_object($connection)) {
308
            $connection = $this->connection;
309
        }
310
        $values = [];
311
        $statement = $connection->query($query);
312
        if (is_object($statement)) {
313
            while ($row = $statement->fetchRow()) {
314
                if ($setKeys) {
315
                    $values[$row[0]] = $row[1];
316
                } else {
317
                    $values[] = $row[0];
318
                }
319
            }
320
        }
321
        return $values;
322
    }
323
324
    /**
325
     * @inheritDoc
326
     */
327
    public function rows(string $query, ConnectionInterface $connection = null)
328
    {
329
        if (!$connection) {
330
            $connection = $this->connection;
331
        }
332
        $statement = $connection->query($query);
333
        if (!is_object($statement)) { // can return true
334
            return [];
335
        }
336
        $rows = [];
337
        while ($row = $statement->fetchAssoc()) {
338
            $rows[] = $row;
339
        }
340
        return $rows;
341
    }
342
}
343