Passed
Push — main ( 3626c9...5c7dd3 )
by Thierry
01:38
created

Driver::error()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 ErrorTrait;
19
    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...
20
    use ServerTrait;
21
    use TableTrait;
22
    use DatabaseTrait;
23
    use QueryTrait;
24
    use GrammarTrait;
25
    use ConnectionTrait;
26
27
    /**
28
     * @var UtilInterface
29
     */
30
    protected $util;
31
32
    /**
33
     * @var TranslatorInterface
34
     */
35
    protected $trans;
36
37
    /**
38
     * @var ServerInterface
39
     */
40
    protected $server;
41
42
    /**
43
     * @var DatabaseInterface
44
     */
45
    protected $database;
46
47
    /**
48
     * @var TableInterface
49
     */
50
    protected $table;
51
52
    /**
53
     * @var QueryInterface
54
     */
55
    protected $query;
56
57
    /**
58
     * @var GrammarInterface
59
     */
60
    protected $grammar;
61
62
    /**
63
     * @var ConnectionInterface
64
     */
65
    protected $connection;
66
67
    /**
68
     * @var ConfigEntity
69
     */
70
    protected $config;
71
72
    /**
73
     * Executed queries
74
     *
75
     * @var array
76
     */
77
    protected $queries = [];
78
79
    /**
80
     * Query start timestamp
81
     *
82
     * @var int
83
     */
84
    protected $start = 0;
85
86
    /**
87
     * The constructor
88
     *
89
     * @param UtilInterface $util
90
     * @param TranslatorInterface $trans
91
     * @param array $options
92
     */
93
    public function __construct(UtilInterface $util, TranslatorInterface $trans, array $options)
94
    {
95
        $this->util = $util;
96
        $this->util->setDriver($this);
97
        $this->trans = $trans;
98
        $this->config = new ConfigEntity($options);
99
        $this->initConfig();
100
        $this->createConnection();
101
    }
102
103
    /**
104
     * Set driver config
105
     *
106
     * @return void
107
     */
108
    abstract protected function initConfig();
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function connect(string $database, string $schema)
114
    {
115
        if (!$this->connection->open($database, $schema)) {
116
            throw new AuthException($this->error());
117
        }
118
        $this->config->database = $database;
119
        $this->config->schema = $schema;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function minVersion(string $version, string $mariaDb = '', ConnectionInterface $connection = null)
126
    {
127
        if (!$connection) {
128
            $connection = $this->connection;
129
        }
130
        $info = $connection->serverInfo();
131
        if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) {
132
            $info = $match[1];
133
            $version = $mariaDb;
134
        }
135
        return (version_compare($info, $version) >= 0);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function charset()
142
    {
143
        // SHOW CHARSET would require an extra query
144
        return ($this->minVersion('5.5.3', 0) ? 'utf8mb4' : 'utf8');
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function begin()
151
    {
152
        $result = $this->connection->query("BEGIN");
153
        return $result !== false;
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function commit()
160
    {
161
        $result = $this->connection->query("COMMIT");
162
        return $result !== false;
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function rollback()
169
    {
170
        $result = $this->connection->query("ROLLBACK");
171
        return $result !== false;
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function setUtf8mb4(string $create)
178
    {
179
        static $set = false;
180
        // possible false positive
181
        if (!$set && preg_match('~\butf8mb4~i', $create)) {
182
            $set = true;
183
            return 'SET NAMES ' . $this->charset() . ";\n\n";
184
        }
185
        return '';
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function execute(string $query)
192
    {
193
        if (!$this->start) {
194
            $this->start = intval(microtime(true));
195
        }
196
        $this->queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
197
        return $this->connection->query($query);
198
    }
199
200
    /**
201
     * @inheritDoc
202
     */
203
    public function queries()
204
    {
205
        return [implode("\n", $this->queries), $this->trans->formatTime($this->start)];
206
    }
207
208
    /**
209
     * @inheritDoc
210
     */
211
    public function applyQueries(string $query, array $tables, $escape = null)
212
    {
213
        if (!$escape) {
214
            $escape = function ($table) {
215
                return $this->table($table);
216
            };
217
        }
218
        foreach ($tables as $table) {
219
            if (!$this->execute("$query " . $escape($table))) {
220
                return false;
221
            }
222
        }
223
        return true;
224
    }
225
226
    /**
227
     * @inheritDoc
228
     */
229
    public function values(string $query, $column = 0)
230
    {
231
        $values = [];
232
        $statement = $this->connection->query($query);
233
        if (is_object($statement)) {
234
            while ($row = $statement->fetchRow()) {
235
                $values[] = $row[$column];
236
            }
237
        }
238
        return $values;
239
    }
240
241
    /**
242
     * @inheritDoc
243
     */
244
    public function keyValues(string $query, ConnectionInterface $connection = null, bool $setKeys = true)
245
    {
246
        if (!is_object($connection)) {
247
            $connection = $this->connection;
248
        }
249
        $values = [];
250
        $statement = $connection->query($query);
251
        if (is_object($statement)) {
252
            while ($row = $statement->fetchRow()) {
253
                if ($setKeys) {
254
                    $values[$row[0]] = $row[1];
255
                } else {
256
                    $values[] = $row[0];
257
                }
258
            }
259
        }
260
        return $values;
261
    }
262
263
    /**
264
     * @inheritDoc
265
     */
266
    public function rows(string $query, ConnectionInterface $connection = null)
267
    {
268
        if (!$connection) {
269
            $connection = $this->connection;
270
        }
271
        $statement = $connection->query($query);
272
        if (!is_object($statement)) { // can return true
273
            return [];
274
        }
275
        $rows = [];
276
        while ($row = $statement->fetchAssoc()) {
277
            $rows[] = $row;
278
        }
279
        return $rows;
280
    }
281
}
282