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\TableInterface; |
10
|
|
|
use Lagdo\DbAdmin\Driver\Db\QueryInterface; |
11
|
|
|
use Lagdo\DbAdmin\Driver\Db\GrammarInterface; |
12
|
|
|
|
13
|
|
|
use Lagdo\DbAdmin\Driver\Exception\AuthException; |
14
|
|
|
|
15
|
|
|
abstract class Driver implements DriverInterface |
16
|
|
|
{ |
17
|
|
|
use ServerTrait; |
18
|
|
|
use TableTrait; |
19
|
|
|
use QueryTrait; |
20
|
|
|
use GrammarTrait; |
21
|
|
|
use ConnectionTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var UtilInterface |
25
|
|
|
*/ |
26
|
|
|
protected $util; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TranslatorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $trans; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ServerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $server; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TableInterface |
40
|
|
|
*/ |
41
|
|
|
protected $table; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var QueryInterface |
45
|
|
|
*/ |
46
|
|
|
protected $query; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var GrammarInterface |
50
|
|
|
*/ |
51
|
|
|
protected $grammar; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ConnectionInterface |
55
|
|
|
*/ |
56
|
|
|
protected $connection; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var ConfigEntity |
60
|
|
|
*/ |
61
|
|
|
protected $config; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $options; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* From bootstrap.inc.php |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
public $onActions = 'RESTRICT|NO ACTION|CASCADE|SET NULL|SET DEFAULT'; ///< @var string used in foreignKeys() |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $database = ''; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $schema = ''; |
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->options = $options; |
97
|
|
|
$this->config = new ConfigEntity(); |
98
|
|
|
$this->initConfig(); |
99
|
|
|
$this->createConnection(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set driver config |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
abstract protected function initConfig(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
public function version() |
113
|
|
|
{ |
114
|
|
|
return '4.8.1-dev'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritDoc |
119
|
|
|
*/ |
120
|
|
|
public function options(string $name = '') |
121
|
|
|
{ |
122
|
|
|
if (!($name = trim($name))) { |
123
|
|
|
return $this->options; |
124
|
|
|
} |
125
|
|
|
if (\array_key_exists($name, $this->options)) { |
126
|
|
|
return $this->options[$name]; |
127
|
|
|
} |
128
|
|
|
if ($name === 'server') { |
129
|
|
|
$server = $this->options['host'] ?? ''; |
130
|
|
|
$port = $this->options['port'] ?? ''; // Optional |
131
|
|
|
// Append the port to the host if it is defined. |
132
|
|
|
if (($port)) { |
133
|
|
|
$server .= ":$port"; |
134
|
|
|
} |
135
|
|
|
return $server; |
136
|
|
|
} |
137
|
|
|
// if ($name === 'ssl') { |
138
|
|
|
// return false; // No SSL options yet |
139
|
|
|
// } |
140
|
|
|
// Option not found |
141
|
|
|
return ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function connect(string $database, string $schema) |
148
|
|
|
{ |
149
|
|
|
if (!$this->connection->open($database, $schema)) { |
150
|
|
|
throw new AuthException($this->error()); |
151
|
|
|
} |
152
|
|
|
$this->database = $database; |
153
|
|
|
$this->schema = $schema; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function numberRegex() |
160
|
|
|
{ |
161
|
|
|
return '((?<!o)int(?!er)|numeric|real|float|double|decimal|money)'; // not point, not interval |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function database() |
168
|
|
|
{ |
169
|
|
|
return $this->database; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function schema() |
176
|
|
|
{ |
177
|
|
|
return $this->schema; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
|
|
public function config() |
184
|
|
|
{ |
185
|
|
|
return $this->config; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @inheritDoc |
190
|
|
|
*/ |
191
|
|
|
public function minVersion(string $version, string $mariaDb = '', ConnectionInterface $connection = null) |
192
|
|
|
{ |
193
|
|
|
if (!$connection) { |
194
|
|
|
$connection = $this->connection; |
195
|
|
|
} |
196
|
|
|
$info = $connection->serverInfo(); |
197
|
|
|
if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) { |
198
|
|
|
$info = $match[1]; |
199
|
|
|
$version = $mariaDb; |
200
|
|
|
} |
201
|
|
|
return (version_compare($info, $version) >= 0); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function charset() |
208
|
|
|
{ |
209
|
|
|
// SHOW CHARSET would require an extra query |
210
|
|
|
return ($this->minVersion('5.5.3', 0) ? 'utf8mb4' : 'utf8'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritDoc |
215
|
|
|
*/ |
216
|
|
|
public function setUtf8mb4(string $create) |
217
|
|
|
{ |
218
|
|
|
static $set = false; |
219
|
|
|
// possible false positive |
220
|
|
|
if (!$set && preg_match('~\butf8mb4~i', $create)) { |
221
|
|
|
$set = true; |
222
|
|
|
return 'SET NAMES ' . $this->charset() . ";\n\n"; |
223
|
|
|
} |
224
|
|
|
return ''; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function inout() |
231
|
|
|
{ |
232
|
|
|
// From index.php |
233
|
|
|
return 'IN|OUT|INOUT'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function enumLength() |
240
|
|
|
{ |
241
|
|
|
// From index.php |
242
|
|
|
return "'(?:''|[^'\\\\]|\\\\.)*'"; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function actions() |
249
|
|
|
{ |
250
|
|
|
return $this->onActions; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
|
public function onActions() |
257
|
|
|
{ |
258
|
|
|
return \explode('|', $this->onActions); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the server jush |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function jush() |
267
|
|
|
{ |
268
|
|
|
return $this->config->jush; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
public function unsigned() |
275
|
|
|
{ |
276
|
|
|
return $this->config->unsigned; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function functions() |
283
|
|
|
{ |
284
|
|
|
return $this->config->functions; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
|
|
public function grouping() |
291
|
|
|
{ |
292
|
|
|
return $this->config->grouping; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return array |
297
|
|
|
*/ |
298
|
|
|
public function operators() |
299
|
|
|
{ |
300
|
|
|
return $this->config->operators; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return array |
305
|
|
|
*/ |
306
|
|
|
public function editFunctions() |
307
|
|
|
{ |
308
|
|
|
return $this->config->editFunctions; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function types() |
315
|
|
|
{ |
316
|
|
|
return $this->config->types; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $type |
321
|
|
|
* |
322
|
|
|
* @return bool |
323
|
|
|
*/ |
324
|
|
|
public function typeExists(string $type) |
325
|
|
|
{ |
326
|
|
|
return isset($this->config->types[$type]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param string $type |
331
|
|
|
* |
332
|
|
|
* @return mixed |
333
|
|
|
*/ |
334
|
|
|
public function type(string $type) |
335
|
|
|
{ |
336
|
|
|
return $this->config->types[$type]; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
public function structuredTypes() |
343
|
|
|
{ |
344
|
|
|
return $this->config->structuredTypes; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param string $key |
349
|
|
|
* @param mixed $value |
350
|
|
|
* |
351
|
|
|
* @return void |
352
|
|
|
*/ |
353
|
|
|
public function setStructuredType(string $key, $value) |
354
|
|
|
{ |
355
|
|
|
$this->config->structuredTypes[$key] = $value; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|