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