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