1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Platforms; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use Doctrine\DBAL\Schema\Column; |
9
|
|
|
use Doctrine\DBAL\Schema\Constraint; |
10
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
11
|
|
|
use Doctrine\DBAL\Schema\Identifier; |
12
|
|
|
use Doctrine\DBAL\Schema\Index; |
13
|
|
|
use Doctrine\DBAL\Schema\Table; |
14
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
15
|
|
|
use Doctrine\DBAL\TransactionIsolationLevel; |
16
|
|
|
use Doctrine\DBAL\Types; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use function array_merge; |
19
|
|
|
use function array_unique; |
20
|
|
|
use function array_values; |
21
|
|
|
use function implode; |
22
|
|
|
use function sprintf; |
23
|
|
|
use function sqrt; |
24
|
|
|
use function str_replace; |
25
|
|
|
use function strpos; |
26
|
|
|
use function strtolower; |
27
|
|
|
use function trim; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The SqlitePlatform class describes the specifics and dialects of the SQLite |
31
|
|
|
* database platform. |
32
|
|
|
* |
33
|
|
|
* @todo Rename: SQLitePlatform |
34
|
|
|
*/ |
35
|
|
|
class SqlitePlatform extends AbstractPlatform |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
3 |
|
public function getRegexpExpression() : string |
41
|
|
|
{ |
42
|
3 |
|
return 'REGEXP'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function getNowExpression(string $type = 'timestamp') : string |
49
|
|
|
{ |
50
|
|
|
switch ($type) { |
51
|
|
|
case 'time': |
52
|
|
|
return 'time(\'now\')'; |
53
|
|
|
case 'date': |
54
|
|
|
return 'date(\'now\')'; |
55
|
|
|
case 'timestamp': |
56
|
|
|
default: |
57
|
|
|
return 'datetime(\'now\')'; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
547 |
|
public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null) : string |
65
|
|
|
{ |
66
|
547 |
|
switch ($mode) { |
67
|
|
|
case TrimMode::UNSPECIFIED: |
68
|
|
|
case TrimMode::BOTH: |
69
|
547 |
|
$trimFn = 'TRIM'; |
70
|
547 |
|
break; |
71
|
|
|
|
72
|
|
|
case TrimMode::LEADING: |
73
|
545 |
|
$trimFn = 'LTRIM'; |
74
|
545 |
|
break; |
75
|
|
|
|
76
|
|
|
case TrimMode::TRAILING: |
77
|
543 |
|
$trimFn = 'RTRIM'; |
78
|
543 |
|
break; |
79
|
|
|
|
80
|
|
|
default: |
81
|
519 |
|
throw new InvalidArgumentException( |
82
|
519 |
|
sprintf( |
83
|
|
|
'The value of $mode is expected to be one of the TrimMode constants, %d given.', |
84
|
519 |
|
$mode |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
547 |
|
$arguments = [$str]; |
90
|
|
|
|
91
|
547 |
|
if ($char !== null) { |
92
|
539 |
|
$arguments[] = $char; |
93
|
|
|
} |
94
|
|
|
|
95
|
547 |
|
return sprintf('%s(%s)', $trimFn, implode(', ', $arguments)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
452 |
|
public function getSubstringExpression(string $string, string $start, ?string $length = null) : string |
102
|
|
|
{ |
103
|
452 |
|
if ($length === null) { |
104
|
452 |
|
return sprintf('SUBSTR(%s, %s)', $string, $start); |
105
|
|
|
} |
106
|
|
|
|
107
|
450 |
|
return sprintf('SUBSTR(%s, %s, %s)', $string, $start, $length); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
451 |
|
public function getLocateExpression(string $string, string $substring, ?string $start = null) : string |
114
|
|
|
{ |
115
|
451 |
|
if ($start === null) { |
116
|
451 |
|
return sprintf('LOCATE(%s, %s)', $string, $substring); |
117
|
|
|
} |
118
|
|
|
|
119
|
451 |
|
return sprintf('LOCATE(%s, %s, %s)', $string, $substring, $start); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
1678 |
|
protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string |
126
|
|
|
{ |
127
|
|
|
switch ($unit) { |
128
|
1678 |
|
case DateIntervalUnit::WEEK: |
129
|
485 |
|
$interval = $this->multiplyInterval($interval, 7); |
130
|
485 |
|
$unit = DateIntervalUnit::DAY; |
131
|
485 |
|
break; |
132
|
|
|
|
133
|
1678 |
|
case DateIntervalUnit::QUARTER: |
134
|
469 |
|
$interval = $this->multiplyInterval($interval, 3); |
135
|
469 |
|
$unit = DateIntervalUnit::MONTH; |
136
|
469 |
|
break; |
137
|
|
|
} |
138
|
|
|
|
139
|
1678 |
|
return 'DATETIME(' . $date . ',' . $this->getConcatExpression( |
140
|
1678 |
|
$this->quoteStringLiteral($operator), |
141
|
|
|
$interval, |
142
|
1678 |
|
$this->quoteStringLiteral(' ' . $unit) |
143
|
1678 |
|
) . ')'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
399 |
|
public function getDateDiffExpression(string $date1, string $date2) : string |
150
|
|
|
{ |
151
|
399 |
|
return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
* |
157
|
|
|
* The SQLite platform doesn't support the concept of a database, therefore, it always returns an empty string |
158
|
|
|
* as an indicator of an implicitly selected database. |
159
|
|
|
* |
160
|
|
|
* @see \Doctrine\DBAL\Connection::getDatabase() |
161
|
|
|
*/ |
162
|
450 |
|
public function getCurrentDatabaseExpression() : string |
163
|
|
|
{ |
164
|
450 |
|
return "''"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritDoc} |
169
|
|
|
*/ |
170
|
1705 |
|
protected function _getTransactionIsolationLevelSQL(int $level) : string |
171
|
|
|
{ |
172
|
|
|
switch ($level) { |
173
|
1705 |
|
case TransactionIsolationLevel::READ_UNCOMMITTED: |
174
|
1705 |
|
return '0'; |
175
|
1705 |
|
case TransactionIsolationLevel::READ_COMMITTED: |
176
|
1705 |
|
case TransactionIsolationLevel::REPEATABLE_READ: |
177
|
1705 |
|
case TransactionIsolationLevel::SERIALIZABLE: |
178
|
1705 |
|
return '1'; |
179
|
|
|
default: |
180
|
|
|
return parent::_getTransactionIsolationLevelSQL($level); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritDoc} |
186
|
|
|
*/ |
187
|
1705 |
|
public function getSetTransactionIsolationSQL(int $level) : string |
188
|
|
|
{ |
189
|
1705 |
|
return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritDoc} |
194
|
|
|
*/ |
195
|
1698 |
|
public function prefersIdentityColumns() : bool |
196
|
|
|
{ |
197
|
1698 |
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritDoc} |
202
|
|
|
*/ |
203
|
1177 |
|
public function getBooleanTypeDeclarationSQL(array $columnDef) : string |
204
|
|
|
{ |
205
|
1177 |
|
return 'BOOLEAN'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritDoc} |
210
|
|
|
*/ |
211
|
2148 |
|
public function getIntegerTypeDeclarationSQL(array $columnDef) : string |
212
|
|
|
{ |
213
|
2148 |
|
return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritDoc} |
218
|
|
|
*/ |
219
|
1754 |
|
public function getBigIntTypeDeclarationSQL(array $columnDef) : string |
220
|
|
|
{ |
221
|
|
|
// SQLite autoincrement is implicit for INTEGER PKs, but not for BIGINT fields. |
222
|
1754 |
|
if (! empty($columnDef['autoincrement'])) { |
223
|
1754 |
|
return $this->getIntegerTypeDeclarationSQL($columnDef); |
224
|
|
|
} |
225
|
|
|
|
226
|
1641 |
|
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritDoc} |
231
|
|
|
*/ |
232
|
1639 |
|
public function getTinyIntTypeDeclarationSql(array $field) : string |
233
|
|
|
{ |
234
|
|
|
// SQLite autoincrement is implicit for INTEGER PKs, but not for TINYINT fields. |
235
|
1639 |
|
if (! empty($field['autoincrement'])) { |
236
|
1639 |
|
return $this->getIntegerTypeDeclarationSQL($field); |
237
|
|
|
} |
238
|
|
|
|
239
|
1636 |
|
return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritDoc} |
244
|
|
|
*/ |
245
|
1804 |
|
public function getSmallIntTypeDeclarationSQL(array $columnDef) : string |
246
|
|
|
{ |
247
|
|
|
// SQLite autoincrement is implicit for INTEGER PKs, but not for SMALLINT fields. |
248
|
1804 |
|
if (! empty($columnDef['autoincrement'])) { |
249
|
1804 |
|
return $this->getIntegerTypeDeclarationSQL($columnDef); |
250
|
|
|
} |
251
|
|
|
|
252
|
1758 |
|
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritDoc} |
257
|
|
|
*/ |
258
|
3 |
|
public function getMediumIntTypeDeclarationSql(array $field) : string |
259
|
|
|
{ |
260
|
|
|
// SQLite autoincrement is implicit for INTEGER PKs, but not for MEDIUMINT fields. |
261
|
3 |
|
if (! empty($field['autoincrement'])) { |
262
|
3 |
|
return $this->getIntegerTypeDeclarationSQL($field); |
263
|
|
|
} |
264
|
|
|
|
265
|
3 |
|
return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritDoc} |
270
|
|
|
*/ |
271
|
549 |
|
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) : string |
272
|
|
|
{ |
273
|
549 |
|
return 'DATETIME'; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritDoc} |
278
|
|
|
*/ |
279
|
453 |
|
public function getDateTypeDeclarationSQL(array $fieldDeclaration) : string |
280
|
|
|
{ |
281
|
453 |
|
return 'DATE'; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritDoc} |
286
|
|
|
*/ |
287
|
393 |
|
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) : string |
288
|
|
|
{ |
289
|
393 |
|
return 'TIME'; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritDoc} |
294
|
|
|
*/ |
295
|
2148 |
|
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) : string |
296
|
|
|
{ |
297
|
|
|
// sqlite autoincrement is only possible for the primary key |
298
|
2148 |
|
if (! empty($columnDef['autoincrement'])) { |
299
|
1968 |
|
return ' PRIMARY KEY AUTOINCREMENT'; |
300
|
|
|
} |
301
|
|
|
|
302
|
2118 |
|
return ! empty($columnDef['unsigned']) ? ' UNSIGNED' : ''; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritDoc} |
307
|
|
|
*/ |
308
|
1553 |
|
public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey) : string |
309
|
|
|
{ |
310
|
1553 |
|
return parent::getForeignKeyDeclarationSQL(new ForeignKeyConstraint( |
311
|
1553 |
|
$foreignKey->getQuotedLocalColumns($this), |
312
|
1553 |
|
str_replace('.', '__', $foreignKey->getQuotedForeignTableName($this)), |
313
|
1553 |
|
$foreignKey->getQuotedForeignColumns($this), |
314
|
1553 |
|
$foreignKey->getName(), |
315
|
1553 |
|
$foreignKey->getOptions() |
316
|
|
|
)); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* {@inheritDoc} |
321
|
|
|
*/ |
322
|
1968 |
|
protected function _getCreateTableSQL(string $tableName, array $columns, array $options = []) : array |
323
|
|
|
{ |
324
|
1968 |
|
$tableName = str_replace('.', '__', $tableName); |
325
|
1968 |
|
$queryFields = $this->getColumnDeclarationListSQL($columns); |
326
|
|
|
|
327
|
1968 |
|
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { |
328
|
|
|
foreach ($options['uniqueConstraints'] as $name => $definition) { |
329
|
|
|
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
1968 |
|
$queryFields .= $this->getNonAutoincrementPrimaryKeyDefinition($columns, $options); |
334
|
|
|
|
335
|
1968 |
|
if (isset($options['foreignKeys'])) { |
336
|
1965 |
|
foreach ($options['foreignKeys'] as $foreignKey) { |
337
|
1553 |
|
$queryFields .= ', ' . $this->getForeignKeyDeclarationSQL($foreignKey); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
1968 |
|
$tableComment = ''; |
342
|
1968 |
|
if (isset($options['comment'])) { |
343
|
256 |
|
$comment = trim($options['comment'], " '"); |
344
|
|
|
|
345
|
256 |
|
$tableComment = $this->getInlineTableCommentSQL($comment); |
346
|
|
|
} |
347
|
|
|
|
348
|
1968 |
|
$query = ['CREATE TABLE ' . $tableName . ' ' . $tableComment . '(' . $queryFields . ')']; |
349
|
|
|
|
350
|
1968 |
|
if (isset($options['alter']) && $options['alter'] === true) { |
351
|
1553 |
|
return $query; |
352
|
|
|
} |
353
|
|
|
|
354
|
1932 |
|
if (isset($options['indexes']) && ! empty($options['indexes'])) { |
355
|
1686 |
|
foreach ($options['indexes'] as $indexDef) { |
356
|
1686 |
|
$query[] = $this->getCreateIndexSQL($indexDef, $tableName); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
1932 |
|
if (isset($options['unique']) && ! empty($options['unique'])) { |
361
|
|
|
foreach ($options['unique'] as $indexDef) { |
362
|
|
|
$query[] = $this->getCreateIndexSQL($indexDef, $tableName); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
1932 |
|
return $query; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Generate a PRIMARY KEY definition if no autoincrement value is used |
371
|
|
|
* |
372
|
|
|
* @param mixed[][] $columns |
373
|
|
|
* @param mixed[] $options |
374
|
|
|
*/ |
375
|
1968 |
|
private function getNonAutoincrementPrimaryKeyDefinition(array $columns, array $options) : string |
376
|
|
|
{ |
377
|
1968 |
|
if (empty($options['primary'])) { |
378
|
1573 |
|
return ''; |
379
|
|
|
} |
380
|
|
|
|
381
|
1926 |
|
$keyColumns = array_unique(array_values($options['primary'])); |
382
|
|
|
|
383
|
1926 |
|
foreach ($keyColumns as $keyColumn) { |
384
|
1926 |
|
foreach ($columns as $column) { |
385
|
1926 |
|
if ($column['name'] === $keyColumn && ! empty($column['autoincrement'])) { |
386
|
1761 |
|
return ''; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
1833 |
|
return ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* {@inheritdoc} |
396
|
|
|
*/ |
397
|
868 |
|
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string |
398
|
|
|
{ |
399
|
868 |
|
return 'BLOB'; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* {@inheritdoc} |
404
|
|
|
*/ |
405
|
1896 |
|
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string |
406
|
|
|
{ |
407
|
1896 |
|
$sql = 'VARCHAR'; |
408
|
|
|
|
409
|
1896 |
|
if ($length !== null) { |
410
|
1893 |
|
$sql .= sprintf('(%d)', $length); |
411
|
|
|
} |
412
|
|
|
|
413
|
1896 |
|
return $sql; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* {@inheritDoc} |
418
|
|
|
*/ |
419
|
826 |
|
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string |
420
|
|
|
{ |
421
|
826 |
|
return 'BLOB'; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* {@inheritDoc} |
426
|
|
|
*/ |
427
|
1011 |
|
public function getClobTypeDeclarationSQL(array $field) : string |
428
|
|
|
{ |
429
|
1011 |
|
return 'CLOB'; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* {@inheritDoc} |
434
|
|
|
*/ |
435
|
1314 |
|
public function getListTableConstraintsSQL(string $table) : string |
436
|
|
|
{ |
437
|
1314 |
|
$table = str_replace('.', '__', $table); |
438
|
|
|
|
439
|
1314 |
|
return sprintf( |
440
|
3 |
|
"SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = %s AND sql NOT NULL ORDER BY name", |
441
|
1314 |
|
$this->quoteStringLiteral($table) |
442
|
|
|
); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* {@inheritDoc} |
447
|
|
|
*/ |
448
|
1542 |
|
public function getListTableColumnsSQL(string $table, ?string $database = null) : string |
449
|
|
|
{ |
450
|
1542 |
|
$table = str_replace('.', '__', $table); |
451
|
|
|
|
452
|
1542 |
|
return sprintf('PRAGMA table_info(%s)', $this->quoteStringLiteral($table)); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* {@inheritDoc} |
457
|
|
|
*/ |
458
|
429 |
|
public function getListTableIndexesSQL(string $table, ?string $currentDatabase = null) : string |
459
|
|
|
{ |
460
|
429 |
|
$table = str_replace('.', '__', $table); |
461
|
|
|
|
462
|
429 |
|
return sprintf('PRAGMA index_list(%s)', $this->quoteStringLiteral($table)); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* {@inheritDoc} |
467
|
|
|
*/ |
468
|
508 |
|
public function getListTablesSQL() : string |
469
|
|
|
{ |
470
|
|
|
return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' AND name != 'geometry_columns' AND name != 'spatial_ref_sys' " |
471
|
|
|
. 'UNION ALL SELECT name FROM sqlite_temp_master ' |
472
|
508 |
|
. "WHERE type = 'table' ORDER BY name"; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* {@inheritDoc} |
477
|
|
|
*/ |
478
|
297 |
|
public function getListViewsSQL(string $database) : string |
479
|
|
|
{ |
480
|
297 |
|
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL"; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* {@inheritDoc} |
485
|
|
|
*/ |
486
|
297 |
|
public function getCreateViewSQL(string $name, string $sql) : string |
487
|
|
|
{ |
488
|
297 |
|
return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* {@inheritDoc} |
493
|
|
|
*/ |
494
|
297 |
|
public function getDropViewSQL(string $name) : string |
495
|
|
|
{ |
496
|
297 |
|
return 'DROP VIEW ' . $name; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* {@inheritDoc} |
501
|
|
|
*/ |
502
|
1553 |
|
public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey) : string |
503
|
|
|
{ |
504
|
1553 |
|
$query = parent::getAdvancedForeignKeyOptionsSQL($foreignKey); |
505
|
|
|
|
506
|
1553 |
|
$query .= ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false ? ' ' : ' NOT ') . 'DEFERRABLE'; |
507
|
1553 |
|
$query .= ' INITIALLY ' . ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false ? 'DEFERRED' : 'IMMEDIATE'); |
508
|
|
|
|
509
|
1553 |
|
return $query; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* {@inheritDoc} |
514
|
|
|
*/ |
515
|
295 |
|
public function supportsIdentityColumns() : bool |
516
|
|
|
{ |
517
|
295 |
|
return true; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* {@inheritDoc} |
522
|
|
|
*/ |
523
|
1482 |
|
public function supportsColumnCollation() : bool |
524
|
|
|
{ |
525
|
1482 |
|
return true; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* {@inheritDoc} |
530
|
|
|
*/ |
531
|
1980 |
|
public function supportsInlineColumnComments() : bool |
532
|
|
|
{ |
533
|
1980 |
|
return true; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* {@inheritDoc} |
538
|
|
|
*/ |
539
|
1632 |
|
public function getName() : string |
540
|
|
|
{ |
541
|
1632 |
|
return 'sqlite'; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* {@inheritDoc} |
546
|
|
|
*/ |
547
|
1062 |
|
public function getTruncateTableSQL(string $tableName, bool $cascade = false) : string |
548
|
|
|
{ |
549
|
1062 |
|
$tableIdentifier = new Identifier($tableName); |
550
|
1062 |
|
$tableName = str_replace('.', '__', $tableIdentifier->getQuotedName($this)); |
551
|
|
|
|
552
|
1062 |
|
return 'DELETE FROM ' . $tableName; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* User-defined function for Sqlite that is used with PDO::sqliteCreateFunction(). |
557
|
|
|
* |
558
|
|
|
* @param int|float $value |
559
|
|
|
*/ |
560
|
|
|
public static function udfSqrt($value) : float |
561
|
|
|
{ |
562
|
|
|
return sqrt($value); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* User-defined function for Sqlite that implements MOD(a, b). |
567
|
|
|
*/ |
568
|
|
|
public static function udfMod(int $a, int $b) : int |
569
|
|
|
{ |
570
|
|
|
return $a % $b; |
571
|
|
|
} |
572
|
|
|
|
573
|
451 |
|
public static function udfLocate(string $str, string $substr, int $offset = 0) : int |
574
|
|
|
{ |
575
|
|
|
// SQL's LOCATE function works on 1-based positions, while PHP's strpos works on 0-based positions. |
576
|
|
|
// So we have to make them compatible if an offset is given. |
577
|
451 |
|
if ($offset > 0) { |
578
|
451 |
|
$offset -= 1; |
579
|
|
|
} |
580
|
|
|
|
581
|
451 |
|
$pos = strpos($str, $substr, $offset); |
582
|
|
|
|
583
|
451 |
|
if ($pos !== false) { |
584
|
451 |
|
return $pos + 1; |
585
|
|
|
} |
586
|
|
|
|
587
|
451 |
|
return 0; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* {@inheritDoc} |
592
|
|
|
*/ |
593
|
|
|
public function getForUpdateSql() : string |
594
|
|
|
{ |
595
|
|
|
return ''; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* {@inheritDoc} |
600
|
|
|
*/ |
601
|
687 |
|
public function getInlineColumnCommentSQL(?string $comment) : string |
602
|
|
|
{ |
603
|
687 |
|
if ($comment === null || $comment === '') { |
604
|
279 |
|
return ''; |
605
|
|
|
} |
606
|
|
|
|
607
|
684 |
|
return '--' . str_replace("\n", "\n--", $comment) . "\n"; |
608
|
|
|
} |
609
|
|
|
|
610
|
256 |
|
private function getInlineTableCommentSQL(string $comment) : string |
611
|
|
|
{ |
612
|
256 |
|
return $this->getInlineColumnCommentSQL($comment); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* {@inheritDoc} |
617
|
|
|
*/ |
618
|
1404 |
|
protected function initializeDoctrineTypeMappings() : void |
619
|
|
|
{ |
620
|
1404 |
|
$this->doctrineTypeMapping = [ |
621
|
|
|
'bigint' => 'bigint', |
622
|
|
|
'bigserial' => 'bigint', |
623
|
|
|
'blob' => 'blob', |
624
|
|
|
'boolean' => 'boolean', |
625
|
|
|
'char' => 'string', |
626
|
|
|
'clob' => 'text', |
627
|
|
|
'date' => 'date', |
628
|
|
|
'datetime' => 'datetime', |
629
|
|
|
'decimal' => 'decimal', |
630
|
|
|
'double' => 'float', |
631
|
|
|
'double precision' => 'float', |
632
|
|
|
'float' => 'float', |
633
|
|
|
'image' => 'string', |
634
|
|
|
'int' => 'integer', |
635
|
|
|
'integer' => 'integer', |
636
|
|
|
'longtext' => 'text', |
637
|
|
|
'longvarchar' => 'string', |
638
|
|
|
'mediumint' => 'integer', |
639
|
|
|
'mediumtext' => 'text', |
640
|
|
|
'ntext' => 'string', |
641
|
|
|
'numeric' => 'decimal', |
642
|
|
|
'nvarchar' => 'string', |
643
|
|
|
'real' => 'float', |
644
|
|
|
'serial' => 'integer', |
645
|
|
|
'smallint' => 'smallint', |
646
|
|
|
'string' => 'string', |
647
|
|
|
'text' => 'text', |
648
|
|
|
'time' => 'time', |
649
|
|
|
'timestamp' => 'datetime', |
650
|
|
|
'tinyint' => 'boolean', |
651
|
|
|
'tinytext' => 'text', |
652
|
|
|
'varchar' => 'string', |
653
|
|
|
'varchar2' => 'string', |
654
|
|
|
]; |
655
|
1404 |
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* {@inheritDoc} |
659
|
|
|
*/ |
660
|
2010 |
|
protected function getReservedKeywordsClass() : string |
661
|
|
|
{ |
662
|
2010 |
|
return Keywords\SQLiteKeywords::class; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* {@inheritDoc} |
667
|
|
|
*/ |
668
|
1553 |
|
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) : array |
669
|
|
|
{ |
670
|
1553 |
|
if (! $diff->fromTable instanceof Table) { |
671
|
|
|
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema.'); |
672
|
|
|
} |
673
|
|
|
|
674
|
1553 |
|
$sql = []; |
675
|
1553 |
|
foreach ($diff->fromTable->getIndexes() as $index) { |
676
|
1535 |
|
if ($index->isPrimary()) { |
677
|
1529 |
|
continue; |
678
|
|
|
} |
679
|
|
|
|
680
|
1526 |
|
$sql[] = $this->getDropIndexSQL($index, $diff->name); |
681
|
|
|
} |
682
|
|
|
|
683
|
1553 |
|
return $sql; |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* {@inheritDoc} |
688
|
|
|
*/ |
689
|
1553 |
|
protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff) : array |
690
|
|
|
{ |
691
|
1553 |
|
if (! $diff->fromTable instanceof Table) { |
692
|
|
|
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema.'); |
693
|
|
|
} |
694
|
|
|
|
695
|
1553 |
|
$sql = []; |
696
|
1553 |
|
$tableName = $diff->getNewName(); |
697
|
|
|
|
698
|
1553 |
|
if ($tableName === null) { |
699
|
1103 |
|
$tableName = $diff->getName($this); |
700
|
|
|
} |
701
|
|
|
|
702
|
1553 |
|
foreach ($this->getIndexesInAlteredTable($diff) as $index) { |
703
|
1535 |
|
if ($index->isPrimary()) { |
704
|
1529 |
|
continue; |
705
|
|
|
} |
706
|
|
|
|
707
|
1532 |
|
$sql[] = $this->getCreateIndexSQL($index, $tableName->getQuotedName($this)); |
708
|
|
|
} |
709
|
|
|
|
710
|
1553 |
|
return $sql; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* {@inheritDoc} |
715
|
|
|
*/ |
716
|
1806 |
|
protected function doModifyLimitQuery(string $query, ?int $limit, int $offset) : string |
717
|
|
|
{ |
718
|
1806 |
|
if ($limit === null && $offset > 0) { |
719
|
1776 |
|
$limit = -1; |
720
|
|
|
} |
721
|
|
|
|
722
|
1806 |
|
return parent::doModifyLimitQuery($query, $limit, $offset); |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* {@inheritDoc} |
727
|
|
|
*/ |
728
|
567 |
|
public function getBlobTypeDeclarationSQL(array $field) : string |
729
|
|
|
{ |
730
|
567 |
|
return 'BLOB'; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* {@inheritDoc} |
735
|
|
|
*/ |
736
|
216 |
|
public function getTemporaryTableName(string $tableName) : string |
737
|
|
|
{ |
738
|
216 |
|
$tableName = str_replace('.', '__', $tableName); |
739
|
|
|
|
740
|
216 |
|
return $tableName; |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* {@inheritDoc} |
745
|
|
|
* |
746
|
|
|
* Sqlite Platform emulates schema by underscoring each dot and generating tables |
747
|
|
|
* into the default database. |
748
|
|
|
* |
749
|
|
|
* This hack is implemented to be able to use SQLite as testdriver when |
750
|
|
|
* using schema supporting databases. |
751
|
|
|
*/ |
752
|
|
|
public function canEmulateSchemas() : bool |
753
|
|
|
{ |
754
|
|
|
return true; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* {@inheritDoc} |
759
|
|
|
*/ |
760
|
1707 |
|
public function supportsForeignKeyConstraints() : bool |
761
|
|
|
{ |
762
|
1707 |
|
return false; |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* {@inheritDoc} |
767
|
|
|
*/ |
768
|
|
|
public function getCreatePrimaryKeySQL(Index $index, $table) : string |
769
|
|
|
{ |
770
|
|
|
throw new DBALException('Sqlite platform does not support alter primary key.'); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* {@inheritdoc} |
775
|
|
|
*/ |
776
|
1547 |
|
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table) : string |
777
|
|
|
{ |
778
|
1547 |
|
throw new DBALException('Sqlite platform does not support alter foreign key.'); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* {@inheritdoc} |
783
|
|
|
*/ |
784
|
|
|
public function getDropForeignKeySQL($foreignKey, $table) : string |
785
|
|
|
{ |
786
|
|
|
throw new DBALException('Sqlite platform does not support alter foreign key.'); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* {@inheritDoc} |
791
|
|
|
*/ |
792
|
1521 |
|
public function getCreateConstraintSQL(Constraint $constraint, $table) : string |
793
|
|
|
{ |
794
|
1521 |
|
throw new DBALException('Sqlite platform does not support alter constraint.'); |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
/** |
798
|
|
|
* {@inheritDoc} |
799
|
|
|
*/ |
800
|
1971 |
|
public function getCreateTableSQL(Table $table, int $createFlags = self::CREATE_INDEXES | self::CREATE_FOREIGNKEYS) : array |
801
|
|
|
{ |
802
|
1971 |
|
return parent::getCreateTableSQL($table, $createFlags); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* {@inheritDoc} |
807
|
|
|
*/ |
808
|
344 |
|
public function getListTableForeignKeysSQL(string $table, ?string $database = null) : string |
809
|
|
|
{ |
810
|
344 |
|
$table = str_replace('.', '__', $table); |
811
|
|
|
|
812
|
344 |
|
return sprintf('PRAGMA foreign_key_list(%s)', $this->quoteStringLiteral($table)); |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
/** |
816
|
|
|
* {@inheritDoc} |
817
|
|
|
*/ |
818
|
1690 |
|
public function getAlterTableSQL(TableDiff $diff) : array |
819
|
|
|
{ |
820
|
1690 |
|
$sql = $this->getSimpleAlterTableSQL($diff); |
821
|
1690 |
|
if ($sql !== false) { |
|
|
|
|
822
|
1648 |
|
return $sql; |
823
|
|
|
} |
824
|
|
|
|
825
|
1622 |
|
$fromTable = $diff->fromTable; |
826
|
1622 |
|
if (! $fromTable instanceof Table) { |
827
|
1409 |
|
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema.'); |
828
|
|
|
} |
829
|
|
|
|
830
|
1553 |
|
$table = clone $fromTable; |
831
|
|
|
|
832
|
1553 |
|
$columns = []; |
833
|
1553 |
|
$oldColumnNames = []; |
834
|
1553 |
|
$newColumnNames = []; |
835
|
1553 |
|
$columnSql = []; |
836
|
|
|
|
837
|
1553 |
|
foreach ($table->getColumns() as $columnName => $column) { |
838
|
1550 |
|
$columnName = strtolower($columnName); |
839
|
1550 |
|
$columns[$columnName] = $column; |
840
|
1550 |
|
$oldColumnNames[$columnName] = $newColumnNames[$columnName] = $column->getQuotedName($this); |
841
|
|
|
} |
842
|
|
|
|
843
|
1553 |
|
foreach ($diff->removedColumns as $columnName => $column) { |
844
|
1529 |
|
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
845
|
|
|
continue; |
846
|
|
|
} |
847
|
|
|
|
848
|
1529 |
|
$columnName = strtolower($columnName); |
849
|
1529 |
|
if (! isset($columns[$columnName])) { |
850
|
|
|
continue; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
unset( |
854
|
1529 |
|
$columns[$columnName], |
855
|
1529 |
|
$oldColumnNames[$columnName], |
856
|
1529 |
|
$newColumnNames[$columnName] |
857
|
|
|
); |
858
|
|
|
} |
859
|
|
|
|
860
|
1553 |
|
foreach ($diff->renamedColumns as $oldColumnName => $column) { |
861
|
1349 |
|
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
862
|
|
|
continue; |
863
|
|
|
} |
864
|
|
|
|
865
|
1349 |
|
$oldColumnName = strtolower($oldColumnName); |
866
|
1349 |
|
if (isset($columns[$oldColumnName])) { |
867
|
1349 |
|
unset($columns[$oldColumnName]); |
868
|
|
|
} |
869
|
|
|
|
870
|
1349 |
|
$columns[strtolower($column->getName())] = $column; |
871
|
|
|
|
872
|
1349 |
|
if (! isset($newColumnNames[$oldColumnName])) { |
873
|
|
|
continue; |
874
|
|
|
} |
875
|
|
|
|
876
|
1349 |
|
$newColumnNames[$oldColumnName] = $column->getQuotedName($this); |
877
|
|
|
} |
878
|
|
|
|
879
|
1553 |
|
foreach ($diff->changedColumns as $oldColumnName => $columnDiff) { |
880
|
1128 |
|
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
881
|
|
|
continue; |
882
|
|
|
} |
883
|
|
|
|
884
|
1128 |
|
if (isset($columns[$oldColumnName])) { |
885
|
1125 |
|
unset($columns[$oldColumnName]); |
886
|
|
|
} |
887
|
|
|
|
888
|
1128 |
|
$columns[strtolower($columnDiff->column->getName())] = $columnDiff->column; |
889
|
|
|
|
890
|
1128 |
|
if (! isset($newColumnNames[$oldColumnName])) { |
891
|
647 |
|
continue; |
892
|
|
|
} |
893
|
|
|
|
894
|
1125 |
|
$newColumnNames[$oldColumnName] = $columnDiff->column->getQuotedName($this); |
895
|
|
|
} |
896
|
|
|
|
897
|
1553 |
|
foreach ($diff->addedColumns as $columnName => $column) { |
898
|
1127 |
|
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
899
|
|
|
continue; |
900
|
|
|
} |
901
|
|
|
|
902
|
1127 |
|
$columns[strtolower($columnName)] = $column; |
903
|
|
|
} |
904
|
|
|
|
905
|
1553 |
|
$sql = []; |
906
|
1553 |
|
$tableSql = []; |
907
|
|
|
|
908
|
1553 |
|
if (! $this->onSchemaAlterTable($diff, $tableSql)) { |
909
|
1553 |
|
$dataTable = new Table('__temp__' . $table->getName()); |
910
|
|
|
|
911
|
1553 |
|
$newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), [], $this->getForeignKeysInAlteredTable($diff), $table->getOptions()); |
912
|
1553 |
|
$newTable->addOption('alter', true); |
913
|
|
|
|
914
|
1553 |
|
$sql = $this->getPreAlterTableIndexForeignKeySQL($diff); |
915
|
|
|
//$sql = array_merge($sql, $this->getCreateTableSQL($dataTable, 0)); |
916
|
1553 |
|
$sql[] = sprintf('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s', $dataTable->getQuotedName($this), implode(', ', $oldColumnNames), $table->getQuotedName($this)); |
917
|
1553 |
|
$sql[] = $this->getDropTableSQL($fromTable); |
918
|
|
|
|
919
|
1553 |
|
$sql = array_merge($sql, $this->getCreateTableSQL($newTable)); |
920
|
1553 |
|
$sql[] = sprintf('INSERT INTO %s (%s) SELECT %s FROM %s', $newTable->getQuotedName($this), implode(', ', $newColumnNames), implode(', ', $oldColumnNames), $dataTable->getQuotedName($this)); |
921
|
1553 |
|
$sql[] = $this->getDropTableSQL($dataTable); |
922
|
|
|
|
923
|
1553 |
|
$newName = $diff->getNewName(); |
924
|
|
|
|
925
|
1553 |
|
if ($newName !== null) { |
926
|
1343 |
|
$sql[] = sprintf( |
927
|
9 |
|
'ALTER TABLE %s RENAME TO %s', |
928
|
1343 |
|
$newTable->getQuotedName($this), |
929
|
1343 |
|
$newName->getQuotedName($this) |
930
|
|
|
); |
931
|
|
|
} |
932
|
|
|
|
933
|
1553 |
|
$sql = array_merge($sql, $this->getPostAlterTableIndexForeignKeySQL($diff)); |
934
|
|
|
} |
935
|
|
|
|
936
|
1553 |
|
return array_merge($sql, $tableSql, $columnSql); |
937
|
|
|
} |
938
|
|
|
|
939
|
|
|
/** |
940
|
|
|
* @return string[]|false |
941
|
|
|
*/ |
942
|
1690 |
|
private function getSimpleAlterTableSQL(TableDiff $diff) |
943
|
|
|
{ |
944
|
|
|
// Suppress changes on integer type autoincrement columns. |
945
|
1690 |
|
foreach ($diff->changedColumns as $oldColumnName => $columnDiff) { |
946
|
1168 |
|
if (! $columnDiff->fromColumn instanceof Column || |
947
|
1156 |
|
! $columnDiff->column instanceof Column || |
948
|
1156 |
|
! $columnDiff->column->getAutoincrement() || |
949
|
1168 |
|
! $columnDiff->column->getType() instanceof Types\IntegerType |
950
|
|
|
) { |
951
|
1128 |
|
continue; |
952
|
|
|
} |
953
|
|
|
|
954
|
331 |
|
if (! $columnDiff->hasChanged('type') && $columnDiff->hasChanged('unsigned')) { |
955
|
325 |
|
unset($diff->changedColumns[$oldColumnName]); |
956
|
|
|
|
957
|
325 |
|
continue; |
958
|
|
|
} |
959
|
|
|
|
960
|
331 |
|
$fromColumnType = $columnDiff->fromColumn->getType(); |
961
|
|
|
|
962
|
331 |
|
if (! ($fromColumnType instanceof Types\SmallIntType) && ! ($fromColumnType instanceof Types\BigIntType)) { |
963
|
|
|
continue; |
964
|
|
|
} |
965
|
|
|
|
966
|
331 |
|
unset($diff->changedColumns[$oldColumnName]); |
967
|
|
|
} |
968
|
|
|
|
969
|
1690 |
|
if (! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes) |
970
|
1675 |
|
|| ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes) |
971
|
1663 |
|
|| ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes) |
972
|
1690 |
|
|| ! empty($diff->renamedIndexes) |
973
|
|
|
) { |
974
|
1553 |
|
return false; |
975
|
|
|
} |
976
|
|
|
|
977
|
1654 |
|
$table = new Table($diff->name); |
978
|
|
|
|
979
|
1654 |
|
$sql = []; |
980
|
1654 |
|
$tableSql = []; |
981
|
1654 |
|
$columnSql = []; |
982
|
|
|
|
983
|
1654 |
|
foreach ($diff->addedColumns as $column) { |
984
|
1435 |
|
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
985
|
|
|
continue; |
986
|
|
|
} |
987
|
|
|
|
988
|
1435 |
|
$field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray()); |
989
|
1435 |
|
$type = $field['type']; |
990
|
|
|
switch (true) { |
991
|
1435 |
|
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']: |
992
|
1432 |
|
case $type instanceof Types\DateTimeType && $field['default'] === $this->getCurrentTimestampSQL(): |
993
|
1432 |
|
case $type instanceof Types\DateType && $field['default'] === $this->getCurrentDateSQL(): |
994
|
1429 |
|
case $type instanceof Types\TimeType && $field['default'] === $this->getCurrentTimeSQL(): |
995
|
1409 |
|
return false; |
996
|
|
|
} |
997
|
|
|
|
998
|
1429 |
|
$field['name'] = $column->getQuotedName($this); |
999
|
1429 |
|
if ($type instanceof Types\StringType && $field['length'] === null) { |
1000
|
1429 |
|
$field['length'] = 255; |
1001
|
|
|
} |
1002
|
|
|
|
1003
|
1429 |
|
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' ADD COLUMN ' . $this->getColumnDeclarationSQL($field['name'], $field); |
1004
|
|
|
} |
1005
|
|
|
|
1006
|
1648 |
|
if (! $this->onSchemaAlterTable($diff, $tableSql)) { |
1007
|
1648 |
|
if ($diff->newName !== null) { |
1008
|
343 |
|
$newTable = new Identifier($diff->newName); |
1009
|
343 |
|
$sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' RENAME TO ' . $newTable->getQuotedName($this); |
1010
|
|
|
} |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
1648 |
|
return array_merge($sql, $tableSql, $columnSql); |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
|
/** |
1017
|
|
|
* @return string[] |
1018
|
|
|
*/ |
1019
|
1553 |
|
private function getColumnNamesInAlteredTable(TableDiff $diff) : array |
1020
|
|
|
{ |
1021
|
1553 |
|
$columns = []; |
1022
|
|
|
|
1023
|
1553 |
|
foreach ($diff->fromTable->getColumns() as $columnName => $column) { |
|
|
|
|
1024
|
1550 |
|
$columns[strtolower($columnName)] = $column->getName(); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
1553 |
|
foreach ($diff->removedColumns as $columnName => $column) { |
1028
|
1529 |
|
$columnName = strtolower($columnName); |
1029
|
1529 |
|
if (! isset($columns[$columnName])) { |
1030
|
|
|
continue; |
1031
|
|
|
} |
1032
|
|
|
|
1033
|
1529 |
|
unset($columns[$columnName]); |
1034
|
|
|
} |
1035
|
|
|
|
1036
|
1553 |
|
foreach ($diff->renamedColumns as $oldColumnName => $column) { |
1037
|
1349 |
|
$columnName = $column->getName(); |
1038
|
1349 |
|
$columns[strtolower($oldColumnName)] = $columnName; |
1039
|
1349 |
|
$columns[strtolower($columnName)] = $columnName; |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
1553 |
|
foreach ($diff->changedColumns as $oldColumnName => $columnDiff) { |
1043
|
1128 |
|
$columnName = $columnDiff->column->getName(); |
1044
|
1128 |
|
$columns[strtolower($oldColumnName)] = $columnName; |
1045
|
1128 |
|
$columns[strtolower($columnName)] = $columnName; |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
1553 |
|
foreach ($diff->addedColumns as $column) { |
1049
|
1127 |
|
$columnName = $column->getName(); |
1050
|
1127 |
|
$columns[strtolower($columnName)] = $columnName; |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
1553 |
|
return $columns; |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
/** |
1057
|
|
|
* @return Index[] |
1058
|
|
|
*/ |
1059
|
1553 |
|
private function getIndexesInAlteredTable(TableDiff $diff) : array |
1060
|
|
|
{ |
1061
|
1553 |
|
$indexes = $diff->fromTable->getIndexes(); |
1062
|
1553 |
|
$columnNames = $this->getColumnNamesInAlteredTable($diff); |
1063
|
|
|
|
1064
|
1553 |
|
foreach ($indexes as $key => $index) { |
1065
|
1535 |
|
foreach ($diff->renamedIndexes as $oldIndexName => $renamedIndex) { |
1066
|
728 |
|
if (strtolower($key) !== strtolower($oldIndexName)) { |
1067
|
728 |
|
continue; |
1068
|
|
|
} |
1069
|
|
|
|
1070
|
470 |
|
unset($indexes[$key]); |
1071
|
|
|
} |
1072
|
|
|
|
1073
|
1535 |
|
$changed = false; |
1074
|
1535 |
|
$indexColumns = []; |
1075
|
1535 |
|
foreach ($index->getColumns() as $columnName) { |
1076
|
1535 |
|
$normalizedColumnName = strtolower($columnName); |
1077
|
1535 |
|
if (! isset($columnNames[$normalizedColumnName])) { |
1078
|
1337 |
|
unset($indexes[$key]); |
1079
|
1337 |
|
continue 2; |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
1535 |
|
$indexColumns[] = $columnNames[$normalizedColumnName]; |
1083
|
1535 |
|
if ($columnName === $columnNames[$normalizedColumnName]) { |
1084
|
1535 |
|
continue; |
1085
|
|
|
} |
1086
|
|
|
|
1087
|
1337 |
|
$changed = true; |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
1535 |
|
if (! $changed) { |
1091
|
1535 |
|
continue; |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
1337 |
|
$indexes[$key] = new Index($index->getName(), $indexColumns, $index->isUnique(), $index->isPrimary(), $index->getFlags()); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
1553 |
|
foreach ($diff->removedIndexes as $index) { |
1098
|
1520 |
|
$indexName = $index->getName(); |
1099
|
|
|
|
1100
|
1520 |
|
if ($indexName === '') { |
1101
|
|
|
continue; |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
1520 |
|
unset($indexes[strtolower($indexName)]); |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
1553 |
|
foreach (array_merge($diff->changedIndexes, $diff->addedIndexes, $diff->renamedIndexes) as $index) { |
1108
|
728 |
|
$indexName = $index->getName(); |
1109
|
|
|
|
1110
|
728 |
|
if ($indexName !== '') { |
1111
|
728 |
|
$indexes[strtolower($indexName)] = $index; |
1112
|
|
|
} else { |
1113
|
|
|
$indexes[] = $index; |
1114
|
|
|
} |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
1553 |
|
return $indexes; |
1118
|
|
|
} |
1119
|
|
|
|
1120
|
|
|
/** |
1121
|
|
|
* @return ForeignKeyConstraint[] |
1122
|
|
|
*/ |
1123
|
1553 |
|
private function getForeignKeysInAlteredTable(TableDiff $diff) : array |
1124
|
|
|
{ |
1125
|
1553 |
|
$foreignKeys = $diff->fromTable->getForeignKeys(); |
1126
|
1553 |
|
$columnNames = $this->getColumnNamesInAlteredTable($diff); |
1127
|
|
|
|
1128
|
1553 |
|
foreach ($foreignKeys as $key => $constraint) { |
1129
|
1343 |
|
$changed = false; |
1130
|
1343 |
|
$localColumns = []; |
1131
|
1343 |
|
foreach ($constraint->getLocalColumns() as $columnName) { |
1132
|
1343 |
|
$normalizedColumnName = strtolower($columnName); |
1133
|
1343 |
|
if (! isset($columnNames[$normalizedColumnName])) { |
1134
|
1337 |
|
unset($foreignKeys[$key]); |
1135
|
1337 |
|
continue 2; |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
1343 |
|
$localColumns[] = $columnNames[$normalizedColumnName]; |
1139
|
1343 |
|
if ($columnName === $columnNames[$normalizedColumnName]) { |
1140
|
1343 |
|
continue; |
1141
|
|
|
} |
1142
|
|
|
|
1143
|
1337 |
|
$changed = true; |
1144
|
|
|
} |
1145
|
|
|
|
1146
|
1343 |
|
if (! $changed) { |
1147
|
1343 |
|
continue; |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
1337 |
|
$foreignKeys[$key] = new ForeignKeyConstraint($localColumns, $constraint->getForeignTableName(), $constraint->getForeignColumns(), $constraint->getName(), $constraint->getOptions()); |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
1553 |
|
foreach ($diff->removedForeignKeys as $constraint) { |
1154
|
233 |
|
if (! $constraint instanceof ForeignKeyConstraint) { |
1155
|
|
|
$constraint = new Identifier($constraint); |
1156
|
|
|
} |
1157
|
|
|
|
1158
|
233 |
|
$constraintName = $constraint->getName(); |
1159
|
|
|
|
1160
|
233 |
|
if ($constraintName === '') { |
1161
|
|
|
continue; |
1162
|
|
|
} |
1163
|
|
|
|
1164
|
233 |
|
unset($foreignKeys[strtolower($constraintName)]); |
1165
|
|
|
} |
1166
|
|
|
|
1167
|
1553 |
|
foreach (array_merge($diff->changedForeignKeys, $diff->addedForeignKeys) as $constraint) { |
1168
|
512 |
|
$constraintName = $constraint->getName(); |
1169
|
|
|
|
1170
|
512 |
|
if ($constraintName !== '') { |
1171
|
233 |
|
$foreignKeys[strtolower($constraintName)] = $constraint; |
1172
|
|
|
} else { |
1173
|
299 |
|
$foreignKeys[] = $constraint; |
1174
|
|
|
} |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
1553 |
|
return $foreignKeys; |
1178
|
|
|
} |
1179
|
|
|
|
1180
|
|
|
/** |
1181
|
|
|
* @return Index[] |
1182
|
|
|
*/ |
1183
|
1553 |
|
private function getPrimaryIndexInAlteredTable(TableDiff $diff) : array |
1184
|
|
|
{ |
1185
|
1553 |
|
$primaryIndex = []; |
1186
|
|
|
|
1187
|
1553 |
|
foreach ($this->getIndexesInAlteredTable($diff) as $index) { |
1188
|
1535 |
|
if (! $index->isPrimary()) { |
1189
|
1532 |
|
continue; |
1190
|
|
|
} |
1191
|
|
|
|
1192
|
1529 |
|
$primaryIndex = [$index->getName() => $index]; |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
1553 |
|
return $primaryIndex; |
1196
|
|
|
} |
1197
|
|
|
} |
1198
|
|
|
|