1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Platforms; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\ColumnDiff; |
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
7
|
|
|
use Doctrine\DBAL\Schema\Identifier; |
8
|
|
|
use Doctrine\DBAL\Schema\Index; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
11
|
|
|
use Doctrine\DBAL\TransactionIsolationLevel; |
12
|
|
|
use Doctrine\DBAL\Types\BlobType; |
13
|
|
|
use Doctrine\DBAL\Types\TextType; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use function array_diff_key; |
16
|
|
|
use function array_merge; |
17
|
|
|
use function array_unique; |
18
|
|
|
use function array_shift; |
19
|
|
|
use function array_values; |
20
|
|
|
use function count; |
21
|
|
|
use function func_get_args; |
22
|
|
|
use function implode; |
23
|
|
|
use function in_array; |
24
|
|
|
use function is_numeric; |
25
|
|
|
use function is_string; |
26
|
|
|
use function sprintf; |
27
|
|
|
use function str_replace; |
28
|
|
|
use function strtoupper; |
29
|
|
|
use function trim; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The MySqlPlatform provides the behavior, features and SQL dialect of the |
33
|
|
|
* MySQL database platform. This platform represents a MySQL 5.0 or greater platform that |
34
|
|
|
* uses the InnoDB storage engine. |
35
|
|
|
* |
36
|
|
|
* @todo Rename: MySQLPlatform |
37
|
|
|
*/ |
38
|
|
|
class MySqlPlatform extends AbstractPlatform |
39
|
|
|
{ |
40
|
|
|
public const LENGTH_LIMIT_TINYTEXT = 255; |
41
|
|
|
public const LENGTH_LIMIT_TEXT = 65535; |
42
|
|
|
public const LENGTH_LIMIT_MEDIUMTEXT = 16777215; |
43
|
|
|
|
44
|
|
|
public const LENGTH_LIMIT_TINYBLOB = 255; |
45
|
|
|
public const LENGTH_LIMIT_BLOB = 65535; |
46
|
|
|
public const LENGTH_LIMIT_MEDIUMBLOB = 16777215; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
292 |
|
*/ |
51
|
|
|
protected function doModifyLimitQuery($query, $limit, $offset) |
52
|
292 |
|
{ |
53
|
170 |
|
if ($limit !== null) { |
54
|
|
|
$query .= ' LIMIT ' . $limit; |
55
|
170 |
|
|
56
|
170 |
|
if ($offset > 0) { |
57
|
|
|
$query .= ' OFFSET ' . $offset; |
58
|
130 |
|
} |
59
|
|
|
} elseif ($offset > 0) { |
60
|
65 |
|
// 2^64-1 is the maximum of unsigned BIGINT, the biggest limit possible |
61
|
|
|
$query .= ' LIMIT 18446744073709551615 OFFSET ' . $offset; |
62
|
|
|
} |
63
|
292 |
|
|
64
|
|
|
return $query; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
1574 |
|
*/ |
70
|
|
|
public function getIdentifierQuoteCharacter() |
71
|
1574 |
|
{ |
72
|
|
|
return '`'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
57 |
|
*/ |
78
|
|
|
public function getRegexpExpression() |
79
|
57 |
|
{ |
80
|
|
|
return 'RLIKE'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
* |
86
|
|
|
* @deprecated Use application-generated UUIDs instead |
87
|
16 |
|
*/ |
88
|
|
|
public function getGuidExpression() |
89
|
16 |
|
{ |
90
|
|
|
return 'UUID()'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
8 |
|
*/ |
96
|
|
|
public function getLocateExpression($str, $substr, $startPos = false) |
97
|
8 |
|
{ |
98
|
8 |
|
if ($startPos === false) { |
99
|
|
|
return 'LOCATE(' . $substr . ', ' . $str . ')'; |
100
|
|
|
} |
101
|
8 |
|
|
102
|
|
|
return 'LOCATE(' . $substr . ', ' . $str . ', ' . $startPos . ')'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
57 |
|
*/ |
108
|
|
|
public function getConcatExpression() |
109
|
57 |
|
{ |
110
|
|
|
return sprintf('CONCAT(%s)', implode(', ', func_get_args())); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
8 |
|
*/ |
116
|
|
|
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) |
117
|
8 |
|
{ |
118
|
|
|
$function = $operator === '+' ? 'DATE_ADD' : 'DATE_SUB'; |
119
|
8 |
|
|
120
|
|
|
return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit . ')'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
24 |
|
*/ |
126
|
|
|
public function getDateDiffExpression($date1, $date2) |
127
|
24 |
|
{ |
128
|
|
|
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
73 |
|
*/ |
134
|
|
|
public function getListDatabasesSQL() |
135
|
73 |
|
{ |
136
|
|
|
return 'SHOW DATABASES'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
|
|
public function getListTableConstraintsSQL($table) |
143
|
|
|
{ |
144
|
|
|
return 'SHOW INDEX FROM ' . $table; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
* |
150
|
|
|
* Two approaches to listing the table indexes. The information_schema is |
151
|
|
|
* preferred, because it doesn't cause problems with SQL keywords such as "order" or "table". |
152
|
408 |
|
*/ |
153
|
|
|
public function getListTableIndexesSQL($table, $currentDatabase = null) |
154
|
408 |
|
{ |
155
|
408 |
|
if ($currentDatabase) { |
156
|
408 |
|
$currentDatabase = $this->quoteStringLiteral($currentDatabase); |
157
|
|
|
$table = $this->quoteStringLiteral($table); |
158
|
|
|
|
159
|
|
|
return 'SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ' . |
160
|
|
|
'SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ' . |
161
|
|
|
'CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, ' . |
162
|
408 |
|
'NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment ' . |
163
|
|
|
'FROM information_schema.STATISTICS WHERE TABLE_NAME = ' . $table . ' AND TABLE_SCHEMA = ' . $currentDatabase; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return 'SHOW INDEX FROM ' . $table; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritDoc} |
171
|
65 |
|
*/ |
172
|
|
|
public function getListViewsSQL($database) |
173
|
65 |
|
{ |
174
|
|
|
$database = $this->quoteStringLiteral($database); |
175
|
65 |
|
|
176
|
|
|
return 'SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = ' . $database; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
433 |
|
*/ |
182
|
|
|
public function getListTableForeignKeysSQL($table, $database = null) |
183
|
433 |
|
{ |
184
|
|
|
$table = $this->quoteStringLiteral($table); |
185
|
433 |
|
|
186
|
376 |
|
if ($database !== null) { |
187
|
|
|
$database = $this->quoteStringLiteral($database); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$sql = 'SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ' . |
191
|
|
|
'k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ' . |
192
|
|
|
'FROM information_schema.key_column_usage k /*!50116 ' . |
193
|
|
|
'INNER JOIN information_schema.referential_constraints c ON ' . |
194
|
433 |
|
' c.constraint_name = k.constraint_name AND ' . |
195
|
|
|
' c.table_name = ' . $table . ' */ WHERE k.table_name = ' . $table; |
196
|
433 |
|
|
197
|
|
|
$databaseNameSql = $database ?? 'DATABASE()'; |
198
|
433 |
|
|
199
|
433 |
|
$sql .= ' AND k.table_schema = ' . $databaseNameSql . ' /*!50116 AND c.constraint_schema = ' . $databaseNameSql . ' */'; |
200
|
|
|
$sql .= ' AND k.`REFERENCED_COLUMN_NAME` is not NULL'; |
201
|
433 |
|
|
202
|
|
|
return $sql; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
8 |
|
*/ |
208
|
|
|
public function getCreateViewSQL($name, $sql) |
209
|
8 |
|
{ |
210
|
|
|
return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritDoc} |
215
|
8 |
|
*/ |
216
|
|
|
public function getDropViewSQL($name) |
217
|
8 |
|
{ |
218
|
|
|
return 'DROP VIEW ' . $name; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritDoc} |
223
|
1496 |
|
*/ |
224
|
|
|
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
225
|
1496 |
|
{ |
226
|
1496 |
|
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') |
227
|
|
|
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
73 |
|
*/ |
233
|
|
|
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed) |
234
|
73 |
|
{ |
235
|
|
|
return $fixed ? 'BINARY(' . ($length ?: 255) . ')' : 'VARBINARY(' . ($length ?: 255) . ')'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Gets the SQL snippet used to declare a CLOB column type. |
240
|
|
|
* TINYTEXT : 2 ^ 8 - 1 = 255 |
241
|
|
|
* TEXT : 2 ^ 16 - 1 = 65535 |
242
|
|
|
* MEDIUMTEXT : 2 ^ 24 - 1 = 16777215 |
243
|
|
|
* LONGTEXT : 2 ^ 32 - 1 = 4294967295 |
244
|
|
|
* |
245
|
|
|
* {@inheritDoc} |
246
|
692 |
|
*/ |
247
|
|
|
public function getClobTypeDeclarationSQL(array $field) |
248
|
692 |
|
{ |
249
|
84 |
|
if (! empty($field['length']) && is_numeric($field['length'])) { |
250
|
|
|
$length = $field['length']; |
251
|
84 |
|
|
252
|
65 |
|
if ($length <= static::LENGTH_LIMIT_TINYTEXT) { |
253
|
|
|
return 'TINYTEXT'; |
254
|
|
|
} |
255
|
84 |
|
|
256
|
84 |
|
if ($length <= static::LENGTH_LIMIT_TEXT) { |
257
|
|
|
return 'TEXT'; |
258
|
|
|
} |
259
|
65 |
|
|
260
|
65 |
|
if ($length <= static::LENGTH_LIMIT_MEDIUMTEXT) { |
261
|
|
|
return 'MEDIUMTEXT'; |
262
|
|
|
} |
263
|
|
|
} |
264
|
673 |
|
|
265
|
|
|
return 'LONGTEXT'; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritDoc} |
270
|
251 |
|
*/ |
271
|
|
|
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
272
|
251 |
|
{ |
273
|
57 |
|
if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] === true) { |
274
|
|
|
return 'TIMESTAMP'; |
275
|
|
|
} |
276
|
251 |
|
|
277
|
|
|
return 'DATETIME'; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritDoc} |
282
|
154 |
|
*/ |
283
|
|
|
public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
284
|
154 |
|
{ |
285
|
|
|
return 'DATE'; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritDoc} |
290
|
146 |
|
*/ |
291
|
|
|
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
292
|
146 |
|
{ |
293
|
|
|
return 'TIME'; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* {@inheritDoc} |
298
|
177 |
|
*/ |
299
|
|
|
public function getBooleanTypeDeclarationSQL(array $field) |
300
|
177 |
|
{ |
301
|
|
|
return 'TINYINT(1)'; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Obtain DBMS specific SQL code portion needed to set the COLLATION |
306
|
|
|
* of a field declaration to be used in statements like CREATE TABLE. |
307
|
|
|
* |
308
|
|
|
* @deprecated Deprecated since version 2.5, Use {@link self::getColumnCollationDeclarationSQL()} instead. |
309
|
|
|
* |
310
|
|
|
* @param string $collation name of the collation |
311
|
|
|
* |
312
|
|
|
* @return string DBMS specific SQL code portion needed to set the COLLATION |
313
|
|
|
* of a field declaration. |
314
|
|
|
*/ |
315
|
|
|
public function getCollationFieldDeclaration($collation) |
316
|
|
|
{ |
317
|
|
|
return $this->getColumnCollationDeclarationSQL($collation); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* {@inheritDoc} |
322
|
|
|
* |
323
|
|
|
* MySql prefers "autoincrement" identity columns since sequences can only |
324
|
|
|
* be emulated with a table. |
325
|
65 |
|
*/ |
326
|
|
|
public function prefersIdentityColumns() |
327
|
65 |
|
{ |
328
|
|
|
return true; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* {@inheritDoc} |
333
|
|
|
* |
334
|
|
|
* MySql supports this through AUTO_INCREMENT columns. |
335
|
81 |
|
*/ |
336
|
|
|
public function supportsIdentityColumns() |
337
|
81 |
|
{ |
338
|
|
|
return true; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritDoc} |
343
|
3178 |
|
*/ |
344
|
|
|
public function supportsInlineColumnComments() |
345
|
3178 |
|
{ |
346
|
|
|
return true; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* {@inheritDoc} |
351
|
179 |
|
*/ |
352
|
|
|
public function supportsColumnCollation() |
353
|
179 |
|
{ |
354
|
|
|
return true; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritDoc} |
359
|
582 |
|
*/ |
360
|
|
|
public function getListTablesSQL() |
361
|
582 |
|
{ |
362
|
|
|
return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritDoc} |
367
|
472 |
|
*/ |
368
|
|
|
public function getListTableColumnsSQL($table, $database = null) |
369
|
472 |
|
{ |
370
|
|
|
$table = $this->quoteStringLiteral($table); |
371
|
472 |
|
|
372
|
415 |
|
if ($database) { |
373
|
|
|
$database = $this->quoteStringLiteral($database); |
374
|
57 |
|
} else { |
375
|
|
|
$database = 'DATABASE()'; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return 'SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ' . |
379
|
|
|
'COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, ' . |
380
|
472 |
|
'CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS Collation ' . |
381
|
|
|
'FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ' . $database . ' AND TABLE_NAME = ' . $table; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* {@inheritDoc} |
386
|
73 |
|
*/ |
387
|
|
|
public function getCreateDatabaseSQL($name) |
388
|
73 |
|
{ |
389
|
|
|
return 'CREATE DATABASE ' . $name; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* {@inheritDoc} |
394
|
73 |
|
*/ |
395
|
|
|
public function getDropDatabaseSQL($name) |
396
|
73 |
|
{ |
397
|
|
|
return 'DROP DATABASE ' . $name; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* {@inheritDoc} |
402
|
2152 |
|
*/ |
403
|
|
|
protected function _getCreateTableSQL($tableName, array $columns, array $options = []) |
404
|
2152 |
|
{ |
405
|
|
|
$queryFields = $this->getColumnDeclarationListSQL($columns); |
406
|
2152 |
|
|
407
|
|
|
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { |
408
|
|
|
foreach ($options['uniqueConstraints'] as $index => $definition) { |
409
|
|
|
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
2152 |
|
// add all indexes |
414
|
508 |
|
if (isset($options['indexes']) && ! empty($options['indexes'])) { |
415
|
508 |
|
foreach ($options['indexes'] as $index => $definition) { |
416
|
|
|
$queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
2152 |
|
// attach all primary keys |
421
|
1101 |
|
if (isset($options['primary']) && ! empty($options['primary'])) { |
422
|
1101 |
|
$keyColumns = array_unique(array_values($options['primary'])); |
423
|
|
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
424
|
|
|
} |
425
|
2152 |
|
|
426
|
|
|
$query = 'CREATE '; |
427
|
2152 |
|
|
428
|
|
|
if (! empty($options['temporary'])) { |
429
|
|
|
$query .= 'TEMPORARY '; |
430
|
|
|
} |
431
|
2152 |
|
|
432
|
2152 |
|
$query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') '; |
433
|
2152 |
|
$query .= $this->buildTableOptions($options); |
434
|
|
|
$query .= $this->buildPartitionOptions($options); |
435
|
2152 |
|
|
436
|
2152 |
|
$sql = [$query]; |
437
|
|
|
$engine = 'INNODB'; |
438
|
2152 |
|
|
439
|
187 |
|
if (isset($options['engine'])) { |
440
|
|
|
$engine = strtoupper(trim($options['engine'])); |
441
|
|
|
} |
442
|
|
|
|
443
|
2152 |
|
// Propagate foreign key constraints only for InnoDB. |
444
|
1301 |
|
if (isset($options['foreignKeys']) && $engine === 'INNODB') { |
445
|
178 |
|
foreach ((array) $options['foreignKeys'] as $definition) { |
446
|
|
|
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName); |
447
|
|
|
} |
448
|
|
|
} |
449
|
2152 |
|
|
450
|
|
|
return $sql; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* {@inheritdoc} |
455
|
3235 |
|
*/ |
456
|
|
|
public function getDefaultValueDeclarationSQL($field) |
457
|
|
|
{ |
458
|
3235 |
|
// Unset the default value if the given field definition does not allow default values. |
459
|
547 |
|
if ($field['type'] instanceof TextType || $field['type'] instanceof BlobType) { |
460
|
|
|
$field['default'] = null; |
461
|
|
|
} |
462
|
3235 |
|
|
463
|
|
|
return parent::getDefaultValueDeclarationSQL($field); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Build SQL for table options |
468
|
|
|
* |
469
|
|
|
* @param mixed[] $options |
470
|
|
|
* |
471
|
|
|
* @return string |
472
|
2152 |
|
*/ |
473
|
|
|
private function buildTableOptions(array $options) |
474
|
2152 |
|
{ |
475
|
|
|
if (isset($options['table_options'])) { |
476
|
|
|
return $options['table_options']; |
477
|
|
|
} |
478
|
2152 |
|
|
479
|
|
|
$tableOptions = []; |
480
|
|
|
|
481
|
2152 |
|
// Charset |
482
|
2144 |
|
if (! isset($options['charset'])) { |
483
|
|
|
$options['charset'] = 'utf8'; |
484
|
|
|
} |
485
|
2152 |
|
|
486
|
|
|
$tableOptions[] = sprintf('DEFAULT CHARACTER SET %s', $options['charset']); |
487
|
|
|
|
488
|
2152 |
|
// Collate |
489
|
2144 |
|
if (! isset($options['collate'])) { |
490
|
|
|
$options['collate'] = $options['charset'] . '_unicode_ci'; |
491
|
|
|
} |
492
|
2152 |
|
|
493
|
|
|
$tableOptions[] = sprintf('COLLATE %s', $options['collate']); |
494
|
|
|
|
495
|
2152 |
|
// Engine |
496
|
1965 |
|
if (! isset($options['engine'])) { |
497
|
|
|
$options['engine'] = 'InnoDB'; |
498
|
|
|
} |
499
|
2152 |
|
|
500
|
|
|
$tableOptions[] = sprintf('ENGINE = %s', $options['engine']); |
501
|
|
|
|
502
|
2152 |
|
// Auto increment |
503
|
|
|
if (isset($options['auto_increment'])) { |
504
|
|
|
$tableOptions[] = sprintf('AUTO_INCREMENT = %s', $options['auto_increment']); |
505
|
|
|
} |
506
|
|
|
|
507
|
2152 |
|
// Comment |
508
|
|
|
if (isset($options['comment'])) { |
509
|
|
|
$comment = trim($options['comment'], " '"); |
510
|
|
|
|
511
|
|
|
$tableOptions[] = sprintf('COMMENT = %s ', $this->quoteStringLiteral($comment)); |
512
|
|
|
} |
513
|
|
|
|
514
|
2152 |
|
// Row format |
515
|
|
|
if (isset($options['row_format'])) { |
516
|
|
|
$tableOptions[] = sprintf('ROW_FORMAT = %s', $options['row_format']); |
517
|
|
|
} |
518
|
2152 |
|
|
519
|
|
|
return implode(' ', $tableOptions); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Build SQL for partition options. |
524
|
|
|
* |
525
|
|
|
* @param mixed[] $options |
526
|
|
|
* |
527
|
|
|
* @return string |
528
|
2152 |
|
*/ |
529
|
|
|
private function buildPartitionOptions(array $options) |
530
|
2152 |
|
{ |
531
|
|
|
return isset($options['partition_options']) |
532
|
2152 |
|
? ' ' . $options['partition_options'] |
533
|
|
|
: ''; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* {@inheritDoc} |
538
|
1651 |
|
*/ |
539
|
|
|
public function getAlterTableSQL(TableDiff $diff) |
540
|
1651 |
|
{ |
541
|
1651 |
|
$columnSql = []; |
542
|
1651 |
|
$queryParts = []; |
543
|
114 |
|
if ($diff->newName !== false) { |
544
|
|
|
$queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this); |
545
|
|
|
} |
546
|
1651 |
|
|
547
|
350 |
|
foreach ($diff->addedColumns as $column) { |
548
|
|
|
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
549
|
|
|
continue; |
550
|
|
|
} |
551
|
350 |
|
|
552
|
350 |
|
$columnArray = $column->toArray(); |
553
|
350 |
|
$columnArray['comment'] = $this->getColumnComment($column); |
554
|
|
|
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
555
|
|
|
} |
556
|
1651 |
|
|
557
|
179 |
|
foreach ($diff->removedColumns as $column) { |
558
|
|
|
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
559
|
|
|
continue; |
560
|
|
|
} |
561
|
179 |
|
|
562
|
|
|
$queryParts[] = 'DROP ' . $column->getQuotedName($this); |
563
|
|
|
} |
564
|
1651 |
|
|
565
|
539 |
|
foreach ($diff->changedColumns as $columnDiff) { |
566
|
|
|
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
567
|
|
|
continue; |
568
|
|
|
} |
569
|
|
|
|
570
|
539 |
|
/** @var ColumnDiff $columnDiff */ |
571
|
539 |
|
$column = $columnDiff->column; |
572
|
|
|
$columnArray = $column->toArray(); |
573
|
|
|
|
574
|
539 |
|
// Don't propagate default value changes for unsupported column types. |
575
|
539 |
|
if ($columnDiff->hasChanged('default') && |
576
|
539 |
|
count($columnDiff->changedProperties) === 1 && |
577
|
|
|
($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType) |
578
|
44 |
|
) { |
579
|
|
|
continue; |
580
|
|
|
} |
581
|
495 |
|
|
582
|
495 |
|
$columnArray['comment'] = $this->getColumnComment($column); |
583
|
495 |
|
$queryParts[] = 'CHANGE ' . ($columnDiff->getOldColumnName()->getQuotedName($this)) . ' ' |
584
|
|
|
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
585
|
|
|
} |
586
|
1651 |
|
|
587
|
236 |
|
foreach ($diff->renamedColumns as $oldColumnName => $column) { |
588
|
|
|
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
589
|
|
|
continue; |
590
|
|
|
} |
591
|
236 |
|
|
592
|
236 |
|
$oldColumnName = new Identifier($oldColumnName); |
593
|
236 |
|
$columnArray = $column->toArray(); |
594
|
236 |
|
$columnArray['comment'] = $this->getColumnComment($column); |
595
|
236 |
|
$queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' ' |
596
|
|
|
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
597
|
|
|
} |
598
|
1651 |
|
|
599
|
206 |
|
if (isset($diff->addedIndexes['primary'])) { |
600
|
206 |
|
$keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns())); |
601
|
206 |
|
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')'; |
602
|
|
|
unset($diff->addedIndexes['primary']); |
603
|
|
|
} |
604
|
1651 |
|
|
605
|
1651 |
|
$sql = []; |
606
|
|
|
$tableSql = []; |
607
|
1651 |
|
|
608
|
1651 |
|
if (! $this->onSchemaAlterTable($diff, $tableSql)) { |
609
|
888 |
|
if (count($queryParts) > 0) { |
610
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(', ', $queryParts); |
611
|
1651 |
|
} |
612
|
1651 |
|
$sql = array_merge( |
613
|
1651 |
|
$this->getPreAlterTableIndexForeignKeySQL($diff), |
614
|
1651 |
|
$sql, |
615
|
|
|
$this->getPostAlterTableIndexForeignKeySQL($diff) |
616
|
|
|
); |
617
|
|
|
} |
618
|
1651 |
|
|
619
|
|
|
return array_merge($sql, $tableSql, $columnSql); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* {@inheritDoc} |
624
|
1651 |
|
*/ |
625
|
|
|
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) |
626
|
1651 |
|
{ |
627
|
1651 |
|
$sql = []; |
628
|
|
|
$table = $diff->getName($this)->getQuotedName($this); |
629
|
1651 |
|
|
630
|
320 |
|
foreach ($diff->changedIndexes as $changedIndex) { |
631
|
|
|
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex)); |
632
|
|
|
} |
633
|
1651 |
|
|
634
|
195 |
|
foreach ($diff->removedIndexes as $remKey => $remIndex) { |
635
|
|
|
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex)); |
636
|
195 |
|
|
637
|
57 |
|
foreach ($diff->addedIndexes as $addKey => $addIndex) { |
638
|
57 |
|
if ($remIndex->getColumns() === $addIndex->getColumns()) { |
639
|
|
|
$indexClause = 'INDEX ' . $addIndex->getName(); |
640
|
57 |
|
|
641
|
|
|
if ($addIndex->isPrimary()) { |
642
|
57 |
|
$indexClause = 'PRIMARY KEY'; |
643
|
57 |
|
} elseif ($addIndex->isUnique()) { |
644
|
|
|
$indexClause = 'UNIQUE INDEX ' . $addIndex->getName(); |
645
|
|
|
} |
646
|
57 |
|
|
647
|
57 |
|
$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', '; |
648
|
57 |
|
$query .= 'ADD ' . $indexClause; |
649
|
|
|
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex) . ')'; |
650
|
57 |
|
|
651
|
|
|
$sql[] = $query; |
652
|
57 |
|
|
653
|
|
|
unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]); |
654
|
195 |
|
|
655
|
|
|
break; |
656
|
|
|
} |
657
|
|
|
} |
658
|
|
|
} |
659
|
1651 |
|
|
660
|
|
|
$engine = 'INNODB'; |
661
|
1651 |
|
|
662
|
57 |
|
if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) { |
663
|
|
|
$engine = strtoupper(trim($diff->fromTable->getOption('engine'))); |
664
|
|
|
} |
665
|
|
|
|
666
|
1651 |
|
// Suppress foreign key constraint propagation on non-supporting engines. |
667
|
57 |
|
if ($engine !== 'INNODB') { |
668
|
57 |
|
$diff->addedForeignKeys = []; |
669
|
57 |
|
$diff->changedForeignKeys = []; |
670
|
|
|
$diff->removedForeignKeys = []; |
671
|
|
|
} |
672
|
1651 |
|
|
673
|
1651 |
|
$sql = array_merge( |
674
|
1651 |
|
$sql, |
675
|
1651 |
|
$this->getPreAlterTableAlterIndexForeignKeySQL($diff), |
676
|
1651 |
|
parent::getPreAlterTableIndexForeignKeySQL($diff), |
677
|
|
|
$this->getPreAlterTableRenameIndexForeignKeySQL($diff) |
678
|
|
|
); |
679
|
1651 |
|
|
680
|
|
|
return $sql; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* @return string[] |
685
|
507 |
|
*/ |
686
|
|
|
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index) |
687
|
507 |
|
{ |
688
|
|
|
$sql = []; |
689
|
507 |
|
|
690
|
195 |
|
if (! $index->isPrimary() || ! $diff->fromTable instanceof Table) { |
691
|
|
|
return $sql; |
692
|
|
|
} |
693
|
312 |
|
|
694
|
|
|
$tableName = $diff->getName($this)->getQuotedName($this); |
695
|
|
|
|
696
|
312 |
|
// Dropping primary keys requires to unset autoincrement attribute on the particular column first. |
697
|
312 |
|
foreach ($index->getColumns() as $columnName) { |
698
|
57 |
|
if (! $diff->fromTable->hasColumn($columnName)) { |
699
|
|
|
continue; |
700
|
|
|
} |
701
|
312 |
|
|
702
|
|
|
$column = $diff->fromTable->getColumn($columnName); |
703
|
312 |
|
|
704
|
255 |
|
if ($column->getAutoincrement() !== true) { |
705
|
|
|
continue; |
706
|
|
|
} |
707
|
179 |
|
|
708
|
|
|
$column->setAutoincrement(false); |
709
|
179 |
|
|
710
|
179 |
|
$sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' . |
711
|
|
|
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
712
|
|
|
|
713
|
179 |
|
// original autoincrement information might be needed later on by other parts of the table alteration |
714
|
|
|
$column->setAutoincrement(true); |
715
|
|
|
} |
716
|
312 |
|
|
717
|
|
|
return $sql; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* @param TableDiff $diff The table diff to gather the SQL for. |
722
|
|
|
* |
723
|
|
|
* @return string[] |
724
|
1651 |
|
*/ |
725
|
|
|
private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff) |
726
|
1651 |
|
{ |
727
|
1651 |
|
$sql = []; |
728
|
|
|
$table = $diff->getName($this)->getQuotedName($this); |
729
|
1651 |
|
|
730
|
|
|
foreach ($diff->changedIndexes as $changedIndex) { |
731
|
320 |
|
// Changed primary key |
732
|
73 |
|
if (! $changedIndex->isPrimary() || ! ($diff->fromTable instanceof Table)) { |
733
|
|
|
continue; |
734
|
|
|
} |
735
|
247 |
|
|
736
|
247 |
|
foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) { |
737
|
|
|
$column = $diff->fromTable->getColumn($columnName); |
738
|
|
|
|
739
|
247 |
|
// Check if an autoincrement column was dropped from the primary key. |
740
|
190 |
|
if (! $column->getAutoincrement() || in_array($columnName, $changedIndex->getColumns())) { |
741
|
|
|
continue; |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
// The autoincrement attribute needs to be removed from the dropped column |
745
|
57 |
|
// before we can drop and recreate the primary key. |
746
|
|
|
$column->setAutoincrement(false); |
747
|
57 |
|
|
748
|
57 |
|
$sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' . |
749
|
|
|
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
750
|
|
|
|
751
|
|
|
// Restore the autoincrement attribute as it might be needed later on |
752
|
247 |
|
// by other parts of the table alteration. |
753
|
|
|
$column->setAutoincrement(true); |
754
|
|
|
} |
755
|
|
|
} |
756
|
1651 |
|
|
757
|
|
|
return $sql; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @param TableDiff $diff The table diff to gather the SQL for. |
762
|
|
|
* |
763
|
|
|
* @return string[] |
764
|
1119 |
|
*/ |
765
|
|
|
protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff) |
766
|
1119 |
|
{ |
767
|
1119 |
|
$sql = []; |
768
|
|
|
$tableName = $diff->getName($this)->getQuotedName($this); |
769
|
1119 |
|
|
770
|
44 |
|
foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { |
771
|
|
|
if (in_array($foreignKey, $diff->changedForeignKeys, true)) { |
772
|
|
|
continue; |
773
|
|
|
} |
774
|
44 |
|
|
775
|
|
|
$sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName); |
776
|
|
|
} |
777
|
1119 |
|
|
778
|
|
|
return $sql; |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* Returns the remaining foreign key constraints that require one of the renamed indexes. |
783
|
|
|
* |
784
|
|
|
* "Remaining" here refers to the diff between the foreign keys currently defined in the associated |
785
|
|
|
* table and the foreign keys to be removed. |
786
|
|
|
* |
787
|
|
|
* @param TableDiff $diff The table diff to evaluate. |
788
|
|
|
* |
789
|
|
|
* @return ForeignKeyConstraint[] |
790
|
1119 |
|
*/ |
791
|
|
|
private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff) |
792
|
1119 |
|
{ |
793
|
923 |
|
if (empty($diff->renamedIndexes) || ! $diff->fromTable instanceof Table) { |
794
|
|
|
return []; |
795
|
|
|
} |
796
|
202 |
|
|
797
|
|
|
$foreignKeys = []; |
798
|
202 |
|
/** @var ForeignKeyConstraint[] $remainingForeignKeys */ |
799
|
202 |
|
$remainingForeignKeys = array_diff_key( |
800
|
202 |
|
$diff->fromTable->getForeignKeys(), |
801
|
|
|
$diff->removedForeignKeys |
802
|
|
|
); |
803
|
202 |
|
|
804
|
44 |
|
foreach ($remainingForeignKeys as $foreignKey) { |
805
|
44 |
|
foreach ($diff->renamedIndexes as $index) { |
806
|
44 |
|
if ($foreignKey->intersectsIndexColumns($index)) { |
807
|
|
|
$foreignKeys[] = $foreignKey; |
808
|
44 |
|
|
809
|
|
|
break; |
810
|
|
|
} |
811
|
|
|
} |
812
|
|
|
} |
813
|
202 |
|
|
814
|
|
|
return $foreignKeys; |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* {@inheritdoc} |
819
|
1651 |
|
*/ |
820
|
|
|
protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff) |
821
|
1651 |
|
{ |
822
|
1651 |
|
return array_merge( |
823
|
1651 |
|
parent::getPostAlterTableIndexForeignKeySQL($diff), |
824
|
|
|
$this->getPostAlterTableRenameIndexForeignKeySQL($diff) |
825
|
|
|
); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* @param TableDiff $diff The table diff to gather the SQL for. |
830
|
|
|
* |
831
|
|
|
* @return string[] |
832
|
1119 |
|
*/ |
833
|
|
|
protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff) |
834
|
1119 |
|
{ |
835
|
1119 |
|
$sql = []; |
836
|
76 |
|
$tableName = $diff->newName !== false |
837
|
1119 |
|
? $diff->getNewName()->getQuotedName($this) |
838
|
|
|
: $diff->getName($this)->getQuotedName($this); |
839
|
1119 |
|
|
840
|
44 |
|
foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { |
841
|
|
|
if (in_array($foreignKey, $diff->changedForeignKeys, true)) { |
842
|
|
|
continue; |
843
|
|
|
} |
844
|
44 |
|
|
845
|
|
|
$sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName); |
846
|
|
|
} |
847
|
1119 |
|
|
848
|
|
|
return $sql; |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* {@inheritDoc} |
853
|
991 |
|
*/ |
854
|
|
|
protected function getCreateIndexSQLFlags(Index $index) |
855
|
991 |
|
{ |
856
|
991 |
|
$type = ''; |
857
|
211 |
|
if ($index->isUnique()) { |
858
|
804 |
|
$type .= 'UNIQUE '; |
859
|
65 |
|
} elseif ($index->hasFlag('fulltext')) { |
860
|
739 |
|
$type .= 'FULLTEXT '; |
861
|
65 |
|
} elseif ($index->hasFlag('spatial')) { |
862
|
|
|
$type .= 'SPATIAL '; |
863
|
|
|
} |
864
|
991 |
|
|
865
|
|
|
return $type; |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
/** |
869
|
|
|
* {@inheritDoc} |
870
|
2216 |
|
*/ |
871
|
|
|
public function getIntegerTypeDeclarationSQL(array $field) |
872
|
2216 |
|
{ |
873
|
|
|
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
/** |
877
|
|
|
* {@inheritDoc} |
878
|
120 |
|
*/ |
879
|
|
|
public function getBigIntTypeDeclarationSQL(array $field) |
880
|
120 |
|
{ |
881
|
|
|
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
/** |
885
|
|
|
* {@inheritDoc} |
886
|
8 |
|
*/ |
887
|
|
|
public function getSmallIntTypeDeclarationSQL(array $field) |
888
|
8 |
|
{ |
889
|
|
|
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* {@inheritdoc} |
894
|
470 |
|
*/ |
895
|
|
|
public function getFloatDeclarationSQL(array $field) |
896
|
470 |
|
{ |
897
|
|
|
return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($field); |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
/** |
901
|
|
|
* {@inheritdoc} |
902
|
502 |
|
*/ |
903
|
|
|
public function getDecimalTypeDeclarationSQL(array $columnDef) |
904
|
502 |
|
{ |
905
|
|
|
return parent::getDecimalTypeDeclarationSQL($columnDef) . $this->getUnsignedDeclaration($columnDef); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
/** |
909
|
|
|
* Get unsigned declaration for a column. |
910
|
|
|
* |
911
|
|
|
* @param mixed[] $columnDef |
912
|
|
|
* |
913
|
|
|
* @return string |
914
|
2916 |
|
*/ |
915
|
|
|
private function getUnsignedDeclaration(array $columnDef) |
916
|
2916 |
|
{ |
917
|
|
|
return ! empty($columnDef['unsigned']) ? ' UNSIGNED' : ''; |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* {@inheritDoc} |
922
|
2216 |
|
*/ |
923
|
|
|
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
924
|
2216 |
|
{ |
925
|
2216 |
|
$autoinc = ''; |
926
|
347 |
|
if (! empty($columnDef['autoincrement'])) { |
927
|
|
|
$autoinc = ' AUTO_INCREMENT'; |
928
|
|
|
} |
929
|
2216 |
|
|
930
|
|
|
return $this->getUnsignedDeclaration($columnDef) . $autoinc; |
931
|
|
|
} |
932
|
|
|
|
933
|
|
|
/** |
934
|
|
|
* {@inheritDoc} |
935
|
487 |
|
*/ |
936
|
|
|
public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey) |
937
|
487 |
|
{ |
938
|
487 |
|
$query = ''; |
939
|
|
|
if ($foreignKey->hasOption('match')) { |
940
|
|
|
$query .= ' MATCH ' . $foreignKey->getOption('match'); |
941
|
487 |
|
} |
942
|
|
|
$query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey); |
943
|
487 |
|
|
944
|
|
|
return $query; |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
/** |
948
|
|
|
* {@inheritDoc} |
949
|
654 |
|
*/ |
950
|
|
|
public function getDropIndexSQL($index, $table = null) |
951
|
654 |
|
{ |
952
|
450 |
|
if ($index instanceof Index) { |
953
|
210 |
|
$indexName = $index->getQuotedName($this); |
954
|
210 |
|
} elseif (is_string($index)) { |
955
|
|
|
$indexName = $index; |
956
|
|
|
} else { |
957
|
|
|
throw new InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); |
958
|
|
|
} |
959
|
654 |
|
|
960
|
8 |
|
if ($table instanceof Table) { |
961
|
646 |
|
$table = $table->getQuotedName($this); |
962
|
|
|
} elseif (! is_string($table)) { |
963
|
|
|
throw new InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
964
|
|
|
} |
965
|
654 |
|
|
966
|
|
|
if ($index instanceof Index && $index->isPrimary()) { |
967
|
|
|
// mysql primary keys are always named "PRIMARY", |
968
|
369 |
|
// so we cannot use them in statements because of them being keyword. |
969
|
|
|
return $this->getDropPrimaryKeySQL($table); |
970
|
|
|
} |
971
|
285 |
|
|
972
|
|
|
return 'DROP INDEX ' . $indexName . ' ON ' . $table; |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
/** |
976
|
|
|
* @param string $table |
977
|
|
|
* |
978
|
|
|
* @return string |
979
|
369 |
|
*/ |
980
|
|
|
protected function getDropPrimaryKeySQL($table) |
981
|
369 |
|
{ |
982
|
|
|
return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'; |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
/** |
986
|
|
|
* {@inheritDoc} |
987
|
57 |
|
*/ |
988
|
|
|
public function getSetTransactionIsolationSQL($level) |
989
|
57 |
|
{ |
990
|
|
|
return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* {@inheritDoc} |
995
|
840 |
|
*/ |
996
|
|
|
public function getName() |
997
|
840 |
|
{ |
998
|
|
|
return 'mysql'; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
/** |
1002
|
|
|
* {@inheritDoc} |
1003
|
|
|
*/ |
1004
|
|
|
public function getReadLockSQL() |
1005
|
|
|
{ |
1006
|
|
|
return 'LOCK IN SHARE MODE'; |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
/** |
1010
|
|
|
* {@inheritDoc} |
1011
|
331 |
|
*/ |
1012
|
|
|
protected function initializeDoctrineTypeMappings() |
1013
|
331 |
|
{ |
1014
|
|
|
$this->doctrineTypeMapping = [ |
1015
|
|
|
'tinyint' => 'boolean', |
1016
|
|
|
'smallint' => 'smallint', |
1017
|
|
|
'mediumint' => 'integer', |
1018
|
|
|
'int' => 'integer', |
1019
|
|
|
'integer' => 'integer', |
1020
|
|
|
'bigint' => 'bigint', |
1021
|
|
|
'tinytext' => 'text', |
1022
|
|
|
'mediumtext' => 'text', |
1023
|
|
|
'longtext' => 'text', |
1024
|
|
|
'text' => 'text', |
1025
|
|
|
'varchar' => 'string', |
1026
|
|
|
'string' => 'string', |
1027
|
|
|
'char' => 'string', |
1028
|
|
|
'date' => 'date', |
1029
|
|
|
'datetime' => 'datetime', |
1030
|
|
|
'timestamp' => 'datetime', |
1031
|
|
|
'time' => 'time', |
1032
|
|
|
'float' => 'float', |
1033
|
|
|
'double' => 'float', |
1034
|
|
|
'real' => 'float', |
1035
|
|
|
'decimal' => 'decimal', |
1036
|
|
|
'numeric' => 'decimal', |
1037
|
|
|
'year' => 'date', |
1038
|
|
|
'longblob' => 'blob', |
1039
|
|
|
'blob' => 'blob', |
1040
|
|
|
'mediumblob' => 'blob', |
1041
|
|
|
'tinyblob' => 'blob', |
1042
|
|
|
'binary' => 'binary', |
1043
|
|
|
'varbinary' => 'binary', |
1044
|
|
|
'set' => 'simple_array', |
1045
|
331 |
|
]; |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
|
|
/** |
1049
|
|
|
* {@inheritDoc} |
1050
|
1496 |
|
*/ |
1051
|
|
|
public function getVarcharMaxLength() |
1052
|
1496 |
|
{ |
1053
|
|
|
return 65535; |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
/** |
1057
|
|
|
* {@inheritdoc} |
1058
|
187 |
|
*/ |
1059
|
|
|
public function getBinaryMaxLength() |
1060
|
187 |
|
{ |
1061
|
|
|
return 65535; |
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
/** |
1065
|
|
|
* {@inheritDoc} |
1066
|
1248 |
|
*/ |
1067
|
|
|
protected function getReservedKeywordsClass() |
1068
|
1248 |
|
{ |
1069
|
|
|
return Keywords\MySQLKeywords::class; |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* {@inheritDoc} |
1074
|
|
|
* |
1075
|
|
|
* MySQL commits a transaction implicitly when DROP TABLE is executed, however not |
1076
|
|
|
* if DROP TEMPORARY TABLE is executed. |
1077
|
16 |
|
*/ |
1078
|
|
|
public function getDropTemporaryTableSQL($table) |
1079
|
16 |
|
{ |
1080
|
|
|
if ($table instanceof Table) { |
1081
|
16 |
|
$table = $table->getQuotedName($this); |
1082
|
|
|
} elseif (! is_string($table)) { |
1083
|
|
|
throw new InvalidArgumentException('getDropTemporaryTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
1084
|
|
|
} |
1085
|
16 |
|
|
1086
|
|
|
return 'DROP TEMPORARY TABLE ' . $table; |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
/** |
1090
|
|
|
* Gets the SQL Snippet used to declare a BLOB column type. |
1091
|
|
|
* TINYBLOB : 2 ^ 8 - 1 = 255 |
1092
|
|
|
* BLOB : 2 ^ 16 - 1 = 65535 |
1093
|
|
|
* MEDIUMBLOB : 2 ^ 24 - 1 = 16777215 |
1094
|
|
|
* LONGBLOB : 2 ^ 32 - 1 = 4294967295 |
1095
|
|
|
* |
1096
|
|
|
* {@inheritDoc} |
1097
|
230 |
|
*/ |
1098
|
|
|
public function getBlobTypeDeclarationSQL(array $field) |
1099
|
230 |
|
{ |
1100
|
130 |
|
if (! empty($field['length']) && is_numeric($field['length'])) { |
1101
|
|
|
$length = $field['length']; |
1102
|
130 |
|
|
1103
|
65 |
|
if ($length <= static::LENGTH_LIMIT_TINYBLOB) { |
1104
|
|
|
return 'TINYBLOB'; |
1105
|
|
|
} |
1106
|
130 |
|
|
1107
|
65 |
|
if ($length <= static::LENGTH_LIMIT_BLOB) { |
1108
|
|
|
return 'BLOB'; |
1109
|
|
|
} |
1110
|
130 |
|
|
1111
|
122 |
|
if ($length <= static::LENGTH_LIMIT_MEDIUMBLOB) { |
1112
|
|
|
return 'MEDIUMBLOB'; |
1113
|
|
|
} |
1114
|
|
|
} |
1115
|
230 |
|
|
1116
|
|
|
return 'LONGBLOB'; |
1117
|
|
|
} |
1118
|
|
|
|
1119
|
|
|
/** |
1120
|
|
|
* {@inheritdoc} |
1121
|
1755 |
|
*/ |
1122
|
|
|
public function quoteStringLiteral($str) |
1123
|
1755 |
|
{ |
1124
|
|
|
$str = str_replace('\\', '\\\\', $str); // MySQL requires backslashes to be escaped aswell. |
1125
|
1755 |
|
|
1126
|
|
|
return parent::quoteStringLiteral($str); |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
/** |
1130
|
|
|
* {@inheritdoc} |
1131
|
19 |
|
*/ |
1132
|
|
|
public function getDefaultTransactionIsolationLevel() |
1133
|
19 |
|
{ |
1134
|
|
|
return TransactionIsolationLevel::REPEATABLE_READ; |
1135
|
|
|
} |
1136
|
|
|
|
1137
|
|
|
/** |
1138
|
|
|
* {@inheritdoc} |
1139
|
|
|
*/ |
1140
|
|
|
public function supportsColumnLengthIndexes() : bool |
1141
|
|
|
{ |
1142
|
|
|
return true; |
1143
|
|
|
} |
1144
|
|
|
} |
1145
|
|
|
|