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; |
|
|
|
|
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($options); |
97
|
|
|
$this->history = new History($trans); |
98
|
|
|
$this->initConfig(); |
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 initConfig(); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
* @throws AuthException |
134
|
|
|
*/ |
135
|
|
|
public function connect(string $database, string $schema) |
136
|
|
|
{ |
137
|
|
|
if (!$this->connection->open($database, $schema)) { |
138
|
|
|
throw new AuthException($this->error()); |
139
|
|
|
} |
140
|
|
|
$this->config->database = $database; |
141
|
|
|
$this->config->schema = $schema; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function minVersion(string $version, string $mariaDb = '') |
148
|
|
|
{ |
149
|
|
|
$info = $this->connection->serverInfo(); |
150
|
|
|
if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) { |
151
|
|
|
$info = $match[1]; |
152
|
|
|
$version = $mariaDb; |
153
|
|
|
} |
154
|
|
|
return (version_compare($info, $version) >= 0); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function charset() |
161
|
|
|
{ |
162
|
|
|
// SHOW CHARSET would require an extra query |
163
|
|
|
return ($this->minVersion('5.5.3', 0) ? 'utf8mb4' : 'utf8'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
|
|
public function begin() |
170
|
|
|
{ |
171
|
|
|
$result = $this->connection->query("BEGIN"); |
172
|
|
|
return $result !== false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
public function commit() |
179
|
|
|
{ |
180
|
|
|
$result = $this->connection->query("COMMIT"); |
181
|
|
|
return $result !== false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritDoc |
186
|
|
|
*/ |
187
|
|
|
public function rollback() |
188
|
|
|
{ |
189
|
|
|
$result = $this->connection->query("ROLLBACK"); |
190
|
|
|
return $result !== false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritDoc |
195
|
|
|
*/ |
196
|
|
|
public function setUtf8mb4(string $create) |
197
|
|
|
{ |
198
|
|
|
static $set = false; |
199
|
|
|
// possible false positive |
200
|
|
|
if (!$set && preg_match('~\butf8mb4~i', $create)) { |
201
|
|
|
$set = true; |
202
|
|
|
return 'SET NAMES ' . $this->charset() . ";\n\n"; |
203
|
|
|
} |
204
|
|
|
return ''; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritDoc |
209
|
|
|
*/ |
210
|
|
|
public function execute(string $query) |
211
|
|
|
{ |
212
|
|
|
$this->history->save($query); |
213
|
|
|
return $this->connection->query($query); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritDoc |
218
|
|
|
*/ |
219
|
|
|
public function queries() |
220
|
|
|
{ |
221
|
|
|
return $this->history->queries(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritDoc |
226
|
|
|
*/ |
227
|
|
|
public function applyQueries(string $query, array $tables, $escape = null) |
228
|
|
|
{ |
229
|
|
|
if (!$escape) { |
230
|
|
|
$escape = function ($table) { |
231
|
|
|
return $this->table($table); |
232
|
|
|
}; |
233
|
|
|
} |
234
|
|
|
foreach ($tables as $table) { |
235
|
|
|
if (!$this->execute("$query " . $escape($table))) { |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
return true; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritDoc |
244
|
|
|
*/ |
245
|
|
|
public function values(string $query, int $column = 0) |
246
|
|
|
{ |
247
|
|
|
$statement = $this->execute($query); |
248
|
|
|
if (!is_object($statement)) { |
249
|
|
|
return []; |
250
|
|
|
} |
251
|
|
|
$values = []; |
252
|
|
|
while ($row = $statement->fetchRow()) { |
253
|
|
|
$values[] = $row[$column]; |
254
|
|
|
} |
255
|
|
|
return $values; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @inheritDoc |
260
|
|
|
*/ |
261
|
|
|
public function colValues(string $query, string $column) |
262
|
|
|
{ |
263
|
|
|
$statement = $this->execute($query); |
264
|
|
|
if (!is_object($statement)) { |
265
|
|
|
return []; |
266
|
|
|
} |
267
|
|
|
$values = []; |
268
|
|
|
while ($row = $statement->fetchAssoc()) { |
269
|
|
|
$values[] = $row[$column]; |
270
|
|
|
} |
271
|
|
|
return $values; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @inheritDoc |
276
|
|
|
*/ |
277
|
|
|
public function keyValues(string $query, int $keyColumn = 0, int $valueColumn = 1) |
278
|
|
|
{ |
279
|
|
|
$statement = $this->execute($query); |
280
|
|
|
if (!is_object($statement)) { |
281
|
|
|
return []; |
282
|
|
|
} |
283
|
|
|
$values = []; |
284
|
|
|
while ($row = $statement->fetchRow()) { |
285
|
|
|
$values[$row[$keyColumn]] = $row[$valueColumn]; |
286
|
|
|
} |
287
|
|
|
return $values; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @inheritDoc |
292
|
|
|
*/ |
293
|
|
|
public function rows(string $query) |
294
|
|
|
{ |
295
|
|
|
$statement = $this->execute($query); |
296
|
|
|
if (!is_object($statement)) { // can return true |
297
|
|
|
return []; |
298
|
|
|
} |
299
|
|
|
$rows = []; |
300
|
|
|
while ($row = $statement->fetchAssoc()) { |
301
|
|
|
$rows[] = $row; |
302
|
|
|
} |
303
|
|
|
return $rows; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|