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 ConfigTrait; |
|
|
|
|
18
|
|
|
use ServerTrait; |
19
|
|
|
use TableTrait; |
20
|
|
|
use QueryTrait; |
21
|
|
|
use GrammarTrait; |
22
|
|
|
use ConnectionTrait; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var UtilInterface |
26
|
|
|
*/ |
27
|
|
|
protected $util; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TranslatorInterface |
31
|
|
|
*/ |
32
|
|
|
protected $trans; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ServerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $server; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TableInterface |
41
|
|
|
*/ |
42
|
|
|
protected $table; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var QueryInterface |
46
|
|
|
*/ |
47
|
|
|
protected $query; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var GrammarInterface |
51
|
|
|
*/ |
52
|
|
|
protected $grammar; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ConnectionInterface |
56
|
|
|
*/ |
57
|
|
|
protected $connection; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ConfigEntity |
61
|
|
|
*/ |
62
|
|
|
protected $config; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $options; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* From bootstrap.inc.php |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
public $onActions = "RESTRICT|NO ACTION|CASCADE|SET NULL|SET DEFAULT"; ///< @var string used in foreignKeys() |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* From index.php |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
public $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* From index.php |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
public $inout = "IN|OUT|INOUT"; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
protected $database = ''; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
protected $schema = ''; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The constructor |
99
|
|
|
* |
100
|
|
|
* @param UtilInterface $util |
101
|
|
|
* @param TranslatorInterface $trans |
102
|
|
|
* @param array $options |
103
|
|
|
*/ |
104
|
|
|
public function __construct(UtilInterface $util, TranslatorInterface $trans, array $options) |
105
|
|
|
{ |
106
|
|
|
$this->util = $util; |
107
|
|
|
$this->util->setDriver($this); |
108
|
|
|
$this->trans = $trans; |
109
|
|
|
$this->options = $options; |
110
|
|
|
$this->config = new ConfigEntity(); |
111
|
|
|
$this->initConfig(); |
112
|
|
|
$this->createConnection(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set driver config |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
abstract protected function initConfig(); |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function version() |
126
|
|
|
{ |
127
|
|
|
return "4.8.1-dev"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
|
|
public function options(string $name = '') |
134
|
|
|
{ |
135
|
|
|
if (!($name = trim($name))) { |
136
|
|
|
return $this->options; |
137
|
|
|
} |
138
|
|
|
if (\array_key_exists($name, $this->options)) { |
139
|
|
|
return $this->options[$name]; |
140
|
|
|
} |
141
|
|
|
if ($name === 'server') { |
142
|
|
|
$server = $this->options['host'] ?? ''; |
143
|
|
|
$port = $this->options['port'] ?? ''; // Optional |
144
|
|
|
// Append the port to the host if it is defined. |
145
|
|
|
if (($port)) { |
146
|
|
|
$server .= ":$port"; |
147
|
|
|
} |
148
|
|
|
return $server; |
149
|
|
|
} |
150
|
|
|
// if ($name === 'ssl') { |
151
|
|
|
// return false; // No SSL options yet |
152
|
|
|
// } |
153
|
|
|
// Option not found |
154
|
|
|
return ''; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function connect(string $database, string $schema) |
161
|
|
|
{ |
162
|
|
|
if (!$this->connection->open($database, $schema)) { |
163
|
|
|
throw new AuthException($this->error()); |
164
|
|
|
} |
165
|
|
|
$this->database = $database; |
166
|
|
|
$this->schema = $schema; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
|
|
public function numberRegex() |
173
|
|
|
{ |
174
|
|
|
return '((?<!o)int(?!er)|numeric|real|float|double|decimal|money)'; // not point, not interval |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritDoc |
179
|
|
|
*/ |
180
|
|
|
public function database() |
181
|
|
|
{ |
182
|
|
|
return $this->database; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritDoc |
187
|
|
|
*/ |
188
|
|
|
public function schema() |
189
|
|
|
{ |
190
|
|
|
return $this->schema; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritDoc |
195
|
|
|
*/ |
196
|
|
|
public function config() |
197
|
|
|
{ |
198
|
|
|
return $this->config; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritDoc |
203
|
|
|
*/ |
204
|
|
|
public function minVersion(string $version, string $mariaDb = "", ConnectionInterface $connection = null) |
205
|
|
|
{ |
206
|
|
|
if (!$connection) { |
207
|
|
|
$connection = $this->connection; |
208
|
|
|
} |
209
|
|
|
$info = $connection->serverInfo(); |
210
|
|
|
if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) { |
211
|
|
|
$info = $match[1]; |
212
|
|
|
$version = $mariaDb; |
213
|
|
|
} |
214
|
|
|
return (version_compare($info, $version) >= 0); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritDoc |
219
|
|
|
*/ |
220
|
|
|
public function charset() |
221
|
|
|
{ |
222
|
|
|
// SHOW CHARSET would require an extra query |
223
|
|
|
return ($this->minVersion("5.5.3", 0) ? "utf8mb4" : "utf8"); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritDoc |
228
|
|
|
*/ |
229
|
|
|
public function setUtf8mb4(string $create) |
230
|
|
|
{ |
231
|
|
|
static $set = false; |
232
|
|
|
// possible false positive |
233
|
|
|
if (!$set && preg_match('~\butf8mb4~i', $create)) { |
234
|
|
|
$set = true; |
235
|
|
|
return "SET NAMES " . $this->charset() . ";\n\n"; |
236
|
|
|
} |
237
|
|
|
return ''; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function actions() |
244
|
|
|
{ |
245
|
|
|
return $this->onActions; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function onActions() |
252
|
|
|
{ |
253
|
|
|
return \explode('|', $this->onActions); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|