1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\MariaDb1027Platform; |
8
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
9
|
|
|
use Doctrine\DBAL\Types\Type; |
10
|
|
|
use function array_change_key_case; |
11
|
|
|
use function array_values; |
12
|
|
|
use function assert; |
13
|
|
|
use function explode; |
14
|
|
|
use function is_string; |
15
|
|
|
use function preg_match; |
16
|
|
|
use function strpos; |
17
|
|
|
use function strtok; |
18
|
|
|
use function strtolower; |
19
|
|
|
use function strtr; |
20
|
|
|
use const CASE_LOWER; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Schema manager for the MySql RDBMS. |
24
|
|
|
*/ |
25
|
|
|
class MySqlSchemaManager extends AbstractSchemaManager |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @see https://mariadb.com/kb/en/library/string-literals/#escape-sequences |
29
|
|
|
*/ |
30
|
|
|
private const MARIADB_ESCAPE_SEQUENCES = [ |
31
|
|
|
'\\0' => "\0", |
32
|
|
|
"\\'" => "'", |
33
|
|
|
'\\"' => '"', |
34
|
|
|
'\\b' => "\b", |
35
|
|
|
'\\n' => "\n", |
36
|
|
|
'\\r' => "\r", |
37
|
|
|
'\\t' => "\t", |
38
|
|
|
'\\Z' => "\x1a", |
39
|
|
|
'\\\\' => '\\', |
40
|
|
|
'\\%' => '%', |
41
|
|
|
'\\_' => '_', |
42
|
|
|
|
43
|
|
|
// Internally, MariaDB escapes single quotes using the standard syntax |
44
|
|
|
"''" => "'", |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
12 |
|
protected function _getPortableViewDefinition(array $view) : View |
51
|
|
|
{ |
52
|
12 |
|
return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
protected function _getPortableUserDefinition(array $user) : array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'user' => $user['User'], |
62
|
|
|
'password' => $user['Password'], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
796 |
|
protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array |
70
|
|
|
{ |
71
|
796 |
|
foreach ($tableIndexRows as $k => $v) { |
72
|
288 |
|
$v = array_change_key_case($v, CASE_LOWER); |
73
|
288 |
|
if ($v['key_name'] === 'PRIMARY') { |
74
|
216 |
|
$v['primary'] = true; |
75
|
|
|
} else { |
76
|
192 |
|
$v['primary'] = false; |
77
|
|
|
} |
78
|
|
|
|
79
|
288 |
|
if (strpos($v['index_type'], 'FULLTEXT') !== false) { |
80
|
36 |
|
$v['flags'] = ['FULLTEXT']; |
81
|
276 |
|
} elseif (strpos($v['index_type'], 'SPATIAL') !== false) { |
82
|
36 |
|
$v['flags'] = ['SPATIAL']; |
83
|
|
|
} |
84
|
|
|
|
85
|
288 |
|
$v['length'] = isset($v['sub_part']) ? (int) $v['sub_part'] : null; |
86
|
|
|
|
87
|
288 |
|
$tableIndexRows[$k] = $v; |
88
|
|
|
} |
89
|
|
|
|
90
|
796 |
|
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
24 |
|
protected function _getPortableDatabaseDefinition(array $database) : string |
97
|
|
|
{ |
98
|
24 |
|
return $database['Database']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
832 |
|
protected function _getPortableTableColumnDefinition(array $tableColumn) : Column |
105
|
|
|
{ |
106
|
832 |
|
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
107
|
|
|
|
108
|
832 |
|
$dbType = strtolower($tableColumn['type']); |
109
|
832 |
|
$dbType = strtok($dbType, '(), '); |
110
|
832 |
|
assert(is_string($dbType)); |
111
|
|
|
|
112
|
832 |
|
$length = $tableColumn['length'] ?? strtok('(), '); |
113
|
|
|
|
114
|
832 |
|
$fixed = false; |
115
|
|
|
|
116
|
832 |
|
if (! isset($tableColumn['name'])) { |
117
|
832 |
|
$tableColumn['name'] = ''; |
118
|
|
|
} |
119
|
|
|
|
120
|
832 |
|
$scale = 0; |
121
|
832 |
|
$precision = null; |
122
|
|
|
|
123
|
832 |
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment']) |
124
|
832 |
|
?? $this->_platform->getDoctrineTypeMapping($dbType); |
125
|
|
|
|
126
|
|
|
switch ($dbType) { |
127
|
832 |
|
case 'char': |
128
|
808 |
|
case 'binary': |
129
|
72 |
|
$fixed = true; |
130
|
72 |
|
break; |
131
|
|
|
|
132
|
808 |
|
case 'float': |
133
|
808 |
|
case 'double': |
134
|
796 |
|
case 'real': |
135
|
796 |
|
case 'numeric': |
136
|
796 |
|
case 'decimal': |
137
|
84 |
|
if (preg_match( |
138
|
|
|
'([A-Za-z]+\(([0-9]+),([0-9]+)\))', |
139
|
84 |
|
$tableColumn['type'], |
140
|
84 |
|
$match |
141
|
84 |
|
) === 1) { |
142
|
72 |
|
$precision = (int) $match[1]; |
143
|
72 |
|
$scale = (int) $match[2]; |
144
|
72 |
|
$length = null; |
145
|
|
|
} |
146
|
|
|
|
147
|
84 |
|
break; |
148
|
|
|
|
149
|
784 |
|
case 'tinytext': |
150
|
36 |
|
$length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT; |
151
|
36 |
|
break; |
152
|
|
|
|
153
|
784 |
|
case 'text': |
154
|
36 |
|
$length = MySqlPlatform::LENGTH_LIMIT_TEXT; |
155
|
36 |
|
break; |
156
|
|
|
|
157
|
784 |
|
case 'mediumtext': |
158
|
36 |
|
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT; |
159
|
36 |
|
break; |
160
|
|
|
|
161
|
784 |
|
case 'tinyblob': |
162
|
|
|
$length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB; |
163
|
|
|
break; |
164
|
|
|
|
165
|
784 |
|
case 'blob': |
166
|
36 |
|
$length = MySqlPlatform::LENGTH_LIMIT_BLOB; |
167
|
36 |
|
break; |
168
|
|
|
|
169
|
784 |
|
case 'mediumblob': |
170
|
36 |
|
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB; |
171
|
36 |
|
break; |
172
|
|
|
|
173
|
784 |
|
case 'tinyint': |
174
|
772 |
|
case 'smallint': |
175
|
772 |
|
case 'mediumint': |
176
|
772 |
|
case 'int': |
177
|
556 |
|
case 'integer': |
178
|
556 |
|
case 'bigint': |
179
|
556 |
|
case 'year': |
180
|
720 |
|
$length = null; |
181
|
720 |
|
break; |
182
|
|
|
} |
183
|
|
|
|
184
|
832 |
|
if ($this->_platform instanceof MariaDb1027Platform) { |
185
|
276 |
|
$columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default']); |
186
|
|
|
} else { |
187
|
556 |
|
$columnDefault = $tableColumn['default']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$options = [ |
191
|
832 |
|
'length' => $length !== null ? (int) $length : null, |
192
|
832 |
|
'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false, |
193
|
832 |
|
'fixed' => $fixed, |
194
|
832 |
|
'default' => $columnDefault, |
195
|
832 |
|
'notnull' => $tableColumn['null'] !== 'YES', |
196
|
832 |
|
'scale' => $scale, |
197
|
832 |
|
'precision' => $precision, |
198
|
832 |
|
'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false, |
199
|
|
|
]; |
200
|
|
|
|
201
|
832 |
|
if (isset($tableColumn['comment'])) { |
202
|
832 |
|
$options['comment'] = $tableColumn['comment']; |
203
|
|
|
} |
204
|
|
|
|
205
|
832 |
|
$column = new Column($tableColumn['field'], Type::getType($type), $options); |
206
|
|
|
|
207
|
832 |
|
if (isset($tableColumn['characterset'])) { |
208
|
544 |
|
$column->setPlatformOption('charset', $tableColumn['characterset']); |
209
|
|
|
} |
210
|
|
|
|
211
|
832 |
|
if (isset($tableColumn['collation'])) { |
212
|
544 |
|
$column->setPlatformOption('collation', $tableColumn['collation']); |
213
|
|
|
} |
214
|
|
|
|
215
|
832 |
|
return $column; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers. |
220
|
|
|
* |
221
|
|
|
* - Since MariaDb 10.2.7 column defaults stored in information_schema are now quoted |
222
|
|
|
* to distinguish them from expressions (see MDEV-10134). |
223
|
|
|
* - CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE are stored in information_schema |
224
|
|
|
* as current_timestamp(), currdate(), currtime() |
225
|
|
|
* - Quoted 'NULL' is not enforced by Maria, it is technically possible to have |
226
|
|
|
* null in some circumstances (see https://jira.mariadb.org/browse/MDEV-14053) |
227
|
|
|
* - \' is always stored as '' in information_schema (normalized) |
228
|
|
|
* |
229
|
|
|
* @link https://mariadb.com/kb/en/library/information-schema-columns-table/ |
230
|
|
|
* @link https://jira.mariadb.org/browse/MDEV-13132 |
231
|
|
|
* |
232
|
|
|
* @param string|null $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7 |
233
|
|
|
*/ |
234
|
276 |
|
private function getMariaDb1027ColumnDefault(MariaDb1027Platform $platform, ?string $columnDefault) : ?string |
235
|
|
|
{ |
236
|
276 |
|
if ($columnDefault === 'NULL' || $columnDefault === null) { |
237
|
256 |
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
116 |
|
if (preg_match('/^\'(.*)\'$/', $columnDefault, $matches) === 1) { |
241
|
96 |
|
return strtr($matches[1], self::MARIADB_ESCAPE_SEQUENCES); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
switch ($columnDefault) { |
245
|
36 |
|
case 'current_timestamp()': |
246
|
20 |
|
return $platform->getCurrentTimestampSQL(); |
247
|
|
|
|
248
|
28 |
|
case 'curdate()': |
249
|
12 |
|
return $platform->getCurrentDateSQL(); |
250
|
|
|
|
251
|
28 |
|
case 'curtime()': |
252
|
12 |
|
return $platform->getCurrentTimeSQL(); |
253
|
|
|
} |
254
|
|
|
|
255
|
24 |
|
return $columnDefault; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
770 |
|
protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array |
262
|
|
|
{ |
263
|
770 |
|
$list = []; |
264
|
770 |
|
foreach ($tableForeignKeys as $value) { |
265
|
142 |
|
$value = array_change_key_case($value, CASE_LOWER); |
266
|
142 |
|
if (! isset($list[$value['constraint_name']])) { |
267
|
142 |
|
if (! isset($value['delete_rule']) || $value['delete_rule'] === 'RESTRICT') { |
268
|
112 |
|
$value['delete_rule'] = null; |
269
|
|
|
} |
270
|
|
|
|
271
|
142 |
|
if (! isset($value['update_rule']) || $value['update_rule'] === 'RESTRICT') { |
272
|
122 |
|
$value['update_rule'] = null; |
273
|
|
|
} |
274
|
|
|
|
275
|
142 |
|
$list[$value['constraint_name']] = [ |
276
|
142 |
|
'name' => $value['constraint_name'], |
277
|
|
|
'local' => [], |
278
|
|
|
'foreign' => [], |
279
|
142 |
|
'foreignTable' => $value['referenced_table_name'], |
280
|
142 |
|
'onDelete' => $value['delete_rule'], |
281
|
142 |
|
'onUpdate' => $value['update_rule'], |
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
|
285
|
142 |
|
$list[$value['constraint_name']]['local'][] = $value['column_name']; |
286
|
142 |
|
$list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name']; |
287
|
|
|
} |
288
|
|
|
|
289
|
770 |
|
$result = []; |
290
|
770 |
|
foreach ($list as $constraint) { |
291
|
142 |
|
$result[] = new ForeignKeyConstraint( |
292
|
142 |
|
array_values($constraint['local']), |
293
|
142 |
|
$constraint['foreignTable'], |
294
|
142 |
|
array_values($constraint['foreign']), |
295
|
142 |
|
$constraint['name'], |
296
|
|
|
[ |
297
|
142 |
|
'onDelete' => $constraint['onDelete'], |
298
|
142 |
|
'onUpdate' => $constraint['onUpdate'], |
299
|
|
|
] |
300
|
|
|
); |
301
|
|
|
} |
302
|
|
|
|
303
|
770 |
|
return $result; |
304
|
|
|
} |
305
|
|
|
|
306
|
712 |
|
public function listTableDetails(string $tableName) : Table |
307
|
|
|
{ |
308
|
712 |
|
$table = parent::listTableDetails($tableName); |
309
|
|
|
|
310
|
712 |
|
$platform = $this->_platform; |
311
|
712 |
|
assert($platform instanceof MySqlPlatform); |
312
|
712 |
|
$sql = $platform->getListTableMetadataSQL($tableName); |
313
|
|
|
|
314
|
712 |
|
$tableOptions = $this->_conn->fetchAssoc($sql); |
315
|
|
|
|
316
|
712 |
|
if ($tableOptions === false) { |
317
|
12 |
|
return $table; |
318
|
|
|
} |
319
|
|
|
|
320
|
700 |
|
$table->addOption('engine', $tableOptions['ENGINE']); |
321
|
|
|
|
322
|
700 |
|
if ($tableOptions['TABLE_COLLATION'] !== null) { |
323
|
700 |
|
$table->addOption('collation', $tableOptions['TABLE_COLLATION']); |
324
|
|
|
} |
325
|
|
|
|
326
|
700 |
|
if ($tableOptions['AUTO_INCREMENT'] !== null) { |
327
|
70 |
|
$table->addOption('autoincrement', $tableOptions['AUTO_INCREMENT']); |
328
|
|
|
} |
329
|
|
|
|
330
|
700 |
|
$table->addOption('comment', $tableOptions['TABLE_COMMENT']); |
331
|
700 |
|
$table->addOption('create_options', $this->parseCreateOptions($tableOptions['CREATE_OPTIONS'])); |
332
|
|
|
|
333
|
700 |
|
return $table; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return array<string, string>|array<string, true> |
338
|
|
|
*/ |
339
|
700 |
|
private function parseCreateOptions(?string $string) : array |
340
|
|
|
{ |
341
|
700 |
|
$options = []; |
342
|
|
|
|
343
|
700 |
|
if ($string === null || $string === '') { |
344
|
688 |
|
return $options; |
345
|
|
|
} |
346
|
|
|
|
347
|
36 |
|
foreach (explode(' ', $string) as $pair) { |
348
|
36 |
|
$parts = explode('=', $pair, 2); |
349
|
|
|
|
350
|
36 |
|
$options[$parts[0]] = $parts[1] ?? true; |
351
|
|
|
} |
352
|
|
|
|
353
|
36 |
|
return $options; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|