Passed
Push — main ( d1bd15...446e40 )
by Thierry
07:44
created

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