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