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