SqlitePlatform.php 34 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Platforms;
4

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Schema\Constraint;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
use Doctrine\DBAL\Schema\Identifier;
10 11 12
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
13
use Doctrine\DBAL\TransactionIsolationLevel;
14
use Doctrine\DBAL\Types;
15 16 17 18 19 20 21 22 23 24 25
use function array_merge;
use function array_unique;
use function array_values;
use function implode;
use function is_numeric;
use function sprintf;
use function sqrt;
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
26

27
/**
28 29
 * The SqlitePlatform class describes the specifics and dialects of the SQLite
 * database platform.
30
 *
Benjamin Morel's avatar
Benjamin Morel committed
31
 * @todo   Rename: SQLitePlatform
32
 */
33
class SqlitePlatform extends AbstractPlatform
34 35
{
    /**
36
     * {@inheritDoc}
37 38 39
     */
    public function getRegexpExpression()
    {
40
        return 'REGEXP';
41 42
    }

43 44
    /**
     * {@inheritDoc}
45 46
     *
     * @deprecated Use application-generated UUIDs instead
47 48 49 50 51 52 53 54 55
     */
    public function getGuidExpression()
    {
        return "HEX(RANDOMBLOB(4)) || '-' || HEX(RANDOMBLOB(2)) || '-4' || "
            . "SUBSTR(HEX(RANDOMBLOB(2)), 2) || '-' || "
            . "SUBSTR('89AB', 1 + (ABS(RANDOM()) % 4), 1) || "
            . "SUBSTR(HEX(RANDOMBLOB(2)), 2) || '-' || HEX(RANDOMBLOB(6))";
    }

56
    /**
57
     * {@inheritDoc}
58 59 60 61 62 63 64 65 66 67 68 69 70 71
     */
    public function getNowExpression($type = 'timestamp')
    {
        switch ($type) {
            case 'time':
                return 'time(\'now\')';
            case 'date':
                return 'date(\'now\')';
            case 'timestamp':
            default:
                return 'datetime(\'now\')';
        }
    }

72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
76
    {
77
        $trimChar = $char !== false ? (', ' . $char) : '';
78

79
        switch ($pos) {
80
            case TrimMode::LEADING:
81 82 83
                $trimFn = 'LTRIM';
                break;

84
            case TrimMode::TRAILING:
85 86 87 88 89
                $trimFn = 'RTRIM';
                break;

            default:
                $trimFn = 'TRIM';
90 91 92 93 94
        }

        return $trimFn . '(' . $str . $trimChar . ')';
    }

95
    /**
96
     * {@inheritDoc}
97 98 99 100 101 102 103 104
     *
     * SQLite only supports the 2 parameter variant of this function
     */
    public function getSubstringExpression($value, $position, $length = null)
    {
        if ($length !== null) {
            return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
        }
105

106 107 108
        return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
    }

109
    /**
110
     * {@inheritDoc}
111 112 113
     */
    public function getLocateExpression($str, $substr, $startPos = false)
    {
114 115
        if ($startPos === false) {
            return 'LOCATE(' . $str . ', ' . $substr . ')';
116
        }
117

118
        return 'LOCATE(' . $str . ', ' . $substr . ', ' . $startPos . ')';
119 120
    }

121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
125
    {
126
        switch ($unit) {
127 128 129
            case DateIntervalUnit::SECOND:
            case DateIntervalUnit::MINUTE:
            case DateIntervalUnit::HOUR:
130
                return 'DATETIME(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
131

132 133
            default:
                switch ($unit) {
134
                    case DateIntervalUnit::WEEK:
135
                        $interval *= 7;
136
                        $unit      = DateIntervalUnit::DAY;
137 138
                        break;

139
                    case DateIntervalUnit::QUARTER:
140
                        $interval *= 3;
141
                        $unit      = DateIntervalUnit::MONTH;
142 143 144
                        break;
                }

Fogs's avatar
Fogs committed
145
                if (! is_numeric($interval)) {
146 147 148
                    $interval = "' || " . $interval . " || '";
                }

149
                return 'DATE(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
150
        }
151 152
    }

153 154 155
    /**
     * {@inheritDoc}
     */
156
    public function getDateDiffExpression($date1, $date2)
157
    {
158
        return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2);
159 160
    }

161 162 163
    /**
     * {@inheritDoc}
     */
164
    protected function _getTransactionIsolationLevelSQL($level)
romanb's avatar
romanb committed
165 166
    {
        switch ($level) {
167
            case TransactionIsolationLevel::READ_UNCOMMITTED:
romanb's avatar
romanb committed
168
                return 0;
169 170 171
            case TransactionIsolationLevel::READ_COMMITTED:
            case TransactionIsolationLevel::REPEATABLE_READ:
            case TransactionIsolationLevel::SERIALIZABLE:
romanb's avatar
romanb committed
172 173
                return 1;
            default:
174
                return parent::_getTransactionIsolationLevelSQL($level);
romanb's avatar
romanb committed
175 176
        }
    }
177

178 179 180
    /**
     * {@inheritDoc}
     */
181
    public function getSetTransactionIsolationSQL($level)
romanb's avatar
romanb committed
182
    {
183
        return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level);
romanb's avatar
romanb committed
184
    }
185

186
    /**
187
     * {@inheritDoc}
188
     */
189 190
    public function prefersIdentityColumns()
    {
191 192
        return true;
    }
193 194

    /**
195
     * {@inheritDoc}
196
     */
197
    public function getBooleanTypeDeclarationSQL(array $field)
198 199 200
    {
        return 'BOOLEAN';
    }
201

202
    /**
203
     * {@inheritDoc}
204
     */
205
    public function getIntegerTypeDeclarationSQL(array $field)
206
    {
207
        return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($field);
208 209
    }

210
    /**
211
     * {@inheritDoc}
212
     */
213
    public function getBigIntTypeDeclarationSQL(array $field)
214
    {
215
        //  SQLite autoincrement is implicit for INTEGER PKs, but not for BIGINT fields.
216
        if (! empty($field['autoincrement'])) {
217 218 219
            return $this->getIntegerTypeDeclarationSQL($field);
        }

220
        return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
221 222
    }

223
    /**
224
     * {@inheritDoc}
225
     */
226 227
    public function getTinyIntTypeDeclarationSql(array $field)
    {
228
        //  SQLite autoincrement is implicit for INTEGER PKs, but not for TINYINT fields.
229
        if (! empty($field['autoincrement'])) {
230 231 232
            return $this->getIntegerTypeDeclarationSQL($field);
        }

233
        return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
234 235
    }

236
    /**
237
     * {@inheritDoc}
238
     */
239
    public function getSmallIntTypeDeclarationSQL(array $field)
240
    {
241
        //  SQLite autoincrement is implicit for INTEGER PKs, but not for SMALLINT fields.
242
        if (! empty($field['autoincrement'])) {
243 244 245
            return $this->getIntegerTypeDeclarationSQL($field);
        }

246
        return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
247 248
    }

249
    /**
250
     * {@inheritDoc}
251
     */
252 253
    public function getMediumIntTypeDeclarationSql(array $field)
    {
254
        //  SQLite autoincrement is implicit for INTEGER PKs, but not for MEDIUMINT fields.
255
        if (! empty($field['autoincrement'])) {
256 257 258
            return $this->getIntegerTypeDeclarationSQL($field);
        }

259
        return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
260 261
    }

262
    /**
263
     * {@inheritDoc}
264
     */
265
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
266 267 268
    {
        return 'DATETIME';
    }
269

270
    /**
271
     * {@inheritDoc}
272
     */
273
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
274 275 276
    {
        return 'DATE';
    }
277

278
    /**
279
     * {@inheritDoc}
280
     */
281
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
282 283 284 285
    {
        return 'TIME';
    }

286
    /**
287
     * {@inheritDoc}
288
     */
289
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
290
    {
291
        // sqlite autoincrement is only possible for the primary key
292
        if (! empty($columnDef['autoincrement'])) {
293
            return ' PRIMARY KEY AUTOINCREMENT';
294 295
        }

296
        return ! empty($columnDef['unsigned']) ? ' UNSIGNED' : '';
297 298
    }

299 300 301 302 303 304
    /**
     * {@inheritDoc}
     */
    public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
    {
        return parent::getForeignKeyDeclarationSQL(new ForeignKeyConstraint(
305 306 307
            $foreignKey->getQuotedLocalColumns($this),
            str_replace('.', '__', $foreignKey->getQuotedForeignTableName($this)),
            $foreignKey->getQuotedForeignColumns($this),
308 309 310 311 312
            $foreignKey->getName(),
            $foreignKey->getOptions()
        ));
    }

313
    /**
314
     * {@inheritDoc}
315
     */
316
    protected function _getCreateTableSQL($name, array $columns, array $options = [])
317
    {
318
        $name        = str_replace('.', '__', $name);
319
        $queryFields = $this->getColumnDeclarationListSQL($columns);
320

321 322 323 324 325 326
        if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
            foreach ($options['uniqueConstraints'] as $name => $definition) {
                $queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
            }
        }

Timo Bakx's avatar
Timo Bakx committed
327
        $queryFields .= $this->getNonAutoincrementPrimaryKeyDefinition($columns, $options);
328

329 330
        if (isset($options['foreignKeys'])) {
            foreach ($options['foreignKeys'] as $foreignKey) {
331
                $queryFields .= ', ' . $this->getForeignKeyDeclarationSQL($foreignKey);
332 333 334
            }
        }

335
        $query = ['CREATE TABLE ' . $name . ' (' . $queryFields . ')'];
336

337
        if (isset($options['alter']) && $options['alter'] === true) {
338
            return $query;
339
        }
340

341
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
Benjamin Morel's avatar
Benjamin Morel committed
342
            foreach ($options['indexes'] as $indexDef) {
343
                $query[] = $this->getCreateIndexSQL($indexDef, $name);
344 345
            }
        }
346

347
        if (isset($options['unique']) && ! empty($options['unique'])) {
Benjamin Morel's avatar
Benjamin Morel committed
348
            foreach ($options['unique'] as $indexDef) {
349 350 351 352
                $query[] = $this->getCreateIndexSQL($indexDef, $name);
            }
        }

353 354 355
        return $query;
    }

Timo Bakx's avatar
Timo Bakx committed
356 357 358
    /**
     * Generate a PRIMARY KEY definition if no autoincrement value is used
     *
359 360
     * @param mixed[][] $columns
     * @param mixed[]   $options
Timo Bakx's avatar
Timo Bakx committed
361 362 363 364 365 366 367 368 369 370
     */
    private function getNonAutoincrementPrimaryKeyDefinition(array $columns, array $options) : string
    {
        if (empty($options['primary'])) {
            return '';
        }

        $keyColumns = array_unique(array_values($options['primary']));

        foreach ($keyColumns as $keyColumn) {
371
            if (! empty($columns[$keyColumn]['autoincrement'])) {
Timo Bakx's avatar
Timo Bakx committed
372 373 374 375 376 377 378
                return '';
            }
        }

        return ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
    }

379
    /**
380
     * {@inheritDoc}
381
     */
382
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
383 384 385 386
    {
        return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
                : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
    }
387

Steve Müller's avatar
Steve Müller committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    /**
     * {@inheritdoc}
     */
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
    {
        return 'BLOB';
    }

    /**
     * {@inheritdoc}
     */
    public function getBinaryMaxLength()
    {
        return 0;
    }

    /**
     * {@inheritdoc}
     */
    public function getBinaryDefaultLength()
    {
        return 0;
    }

412 413 414
    /**
     * {@inheritDoc}
     */
415
    public function getClobTypeDeclarationSQL(array $field)
416 417 418
    {
        return 'CLOB';
    }
419

420 421 422
    /**
     * {@inheritDoc}
     */
423
    public function getListTableConstraintsSQL($table)
424
    {
425
        $table = str_replace('.', '__', $table);
426

427 428 429 430
        return sprintf(
            "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = %s AND sql NOT NULL ORDER BY name",
            $this->quoteStringLiteral($table)
        );
431 432
    }

433 434 435
    /**
     * {@inheritDoc}
     */
436
    public function getListTableColumnsSQL($table, $currentDatabase = null)
437
    {
438
        $table = str_replace('.', '__', $table);
439

440
        return sprintf('PRAGMA table_info(%s)', $this->quoteStringLiteral($table));
441 442
    }

443 444 445
    /**
     * {@inheritDoc}
     */
446
    public function getListTableIndexesSQL($table, $currentDatabase = null)
447
    {
448
        $table = str_replace('.', '__', $table);
449

450
        return sprintf('PRAGMA index_list(%s)', $this->quoteStringLiteral($table));
451 452
    }

453 454 455
    /**
     * {@inheritDoc}
     */
456
    public function getListTablesSQL()
457
    {
jsor's avatar
jsor committed
458
        return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' AND name != 'geometry_columns' AND name != 'spatial_ref_sys' "
459
             . 'UNION ALL SELECT name FROM sqlite_temp_master '
460 461 462
             . "WHERE type = 'table' ORDER BY name";
    }

463 464 465
    /**
     * {@inheritDoc}
     */
466
    public function getListViewsSQL($database)
467 468 469 470
    {
        return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
    }

471 472 473
    /**
     * {@inheritDoc}
     */
474
    public function getCreateViewSQL($name, $sql)
475 476 477 478
    {
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
    }

479 480 481
    /**
     * {@inheritDoc}
     */
482
    public function getDropViewSQL($name)
483
    {
484
        return 'DROP VIEW ' . $name;
485 486
    }

487 488 489
    /**
     * {@inheritDoc}
     */
490 491 492 493
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
    {
        $query = parent::getAdvancedForeignKeyOptionsSQL($foreignKey);

494 495
        $query .= ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false ? ' ' : ' NOT ') . 'DEFERRABLE';
        $query .= ' INITIALLY ' . ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false ? 'DEFERRED' : 'IMMEDIATE');
496 497 498 499

        return $query;
    }

500 501 502
    /**
     * {@inheritDoc}
     */
503 504 505 506 507
    public function supportsIdentityColumns()
    {
        return true;
    }

508 509 510 511 512 513 514 515
    /**
     * {@inheritDoc}
     */
    public function supportsColumnCollation()
    {
        return true;
    }

516 517 518 519 520 521 522 523
    /**
     * {@inheritDoc}
     */
    public function supportsInlineColumnComments()
    {
        return true;
    }

524
    /**
525
     * {@inheritDoc}
526 527 528 529 530
     */
    public function getName()
    {
        return 'sqlite';
    }
531 532

    /**
533
     * {@inheritDoc}
534
     */
535
    public function getTruncateTableSQL($tableName, $cascade = false)
536
    {
537
        $tableIdentifier = new Identifier($tableName);
538
        $tableName       = str_replace('.', '__', $tableIdentifier->getQuotedName($this));
539

540
        return 'DELETE FROM ' . $tableName;
541
    }
542 543

    /**
Benjamin Morel's avatar
Benjamin Morel committed
544
     * User-defined function for Sqlite that is used with PDO::sqliteCreateFunction().
545
     *
546
     * @param int|float $value
547
     *
548 549
     * @return float
     */
550
    public static function udfSqrt($value)
551 552 553 554 555
    {
        return sqrt($value);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
556
     * User-defined function for Sqlite that implements MOD(a, b).
557
     *
558 559
     * @param int $a
     * @param int $b
560
     *
561
     * @return int
562
     */
563
    public static function udfMod($a, $b)
564
    {
565
        return $a % $b;
566
    }
567 568

    /**
569 570 571
     * @param string $str
     * @param string $substr
     * @param int    $offset
572
     *
573
     * @return int
574
     */
575
    public static function udfLocate($str, $substr, $offset = 0)
576
    {
577 578 579
        // SQL's LOCATE function works on 1-based positions, while PHP's strpos works on 0-based positions.
        // So we have to make them compatible if an offset is given.
        if ($offset > 0) {
Steve Müller's avatar
Steve Müller committed
580
            $offset -= 1;
581 582
        }

583
        $pos = strpos($str, $substr, $offset);
584

585
        if ($pos !== false) {
586
            return $pos + 1;
587
        }
588

589 590
        return 0;
    }
591

592 593 594
    /**
     * {@inheritDoc}
     */
595 596 597 598
    public function getForUpdateSql()
    {
        return '';
    }
599

600 601 602 603 604
    /**
     * {@inheritDoc}
     */
    public function getInlineColumnCommentSQL($comment)
    {
Steve Müller's avatar
Steve Müller committed
605
        return '--' . str_replace("\n", "\n--", $comment) . "\n";
606 607
    }

608 609 610
    /**
     * {@inheritDoc}
     */
611 612
    protected function initializeDoctrineTypeMappings()
    {
613
        $this->doctrineTypeMapping = [
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
            'boolean'          => 'boolean',
            'tinyint'          => 'boolean',
            'smallint'         => 'smallint',
            'mediumint'        => 'integer',
            'int'              => 'integer',
            'integer'          => 'integer',
            'serial'           => 'integer',
            'bigint'           => 'bigint',
            'bigserial'        => 'bigint',
            'clob'             => 'text',
            'tinytext'         => 'text',
            'mediumtext'       => 'text',
            'longtext'         => 'text',
            'text'             => 'text',
            'varchar'          => 'string',
629
            'longvarchar'      => 'string',
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
            'varchar2'         => 'string',
            'nvarchar'         => 'string',
            'image'            => 'string',
            'ntext'            => 'string',
            'char'             => 'string',
            'date'             => 'date',
            'datetime'         => 'datetime',
            'timestamp'        => 'datetime',
            'time'             => 'time',
            'float'            => 'float',
            'double'           => 'float',
            'double precision' => 'float',
            'real'             => 'float',
            'decimal'          => 'decimal',
            'numeric'          => 'decimal',
645
            'blob'             => 'blob',
646
        ];
647
    }
648

649 650 651
    /**
     * {@inheritDoc}
     */
652 653
    protected function getReservedKeywordsClass()
    {
654
        return Keywords\SQLiteKeywords::class;
655
    }
656

657 658 659 660 661
    /**
     * {@inheritDoc}
     */
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
    {
662
        if (! $diff->fromTable instanceof Table) {
663 664 665
            throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
        }

666
        $sql = [];
667
        foreach ($diff->fromTable->getIndexes() as $index) {
668 669
            if ($index->isPrimary()) {
                continue;
670
            }
671 672

            $sql[] = $this->getDropIndexSQL($index, $diff->name);
673 674 675 676 677 678 679 680 681 682
        }

        return $sql;
    }

    /**
     * {@inheritDoc}
     */
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
    {
683
        if (! $diff->fromTable instanceof Table) {
684 685 686
            throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
        }

687
        $sql       = [];
Sergei Morozov's avatar
Sergei Morozov committed
688 689 690 691 692 693
        $tableName = $diff->getNewName();

        if ($tableName === false) {
            $tableName = $diff->getName($this);
        }

Benjamin Morel's avatar
Benjamin Morel committed
694
        foreach ($this->getIndexesInAlteredTable($diff) as $index) {
695 696 697 698
            if ($index->isPrimary()) {
                continue;
            }

699
            $sql[] = $this->getCreateIndexSQL($index, $tableName->getQuotedName($this));
700 701 702 703
        }

        return $sql;
    }
704 705 706 707 708 709

    /**
     * {@inheritDoc}
     */
    protected function doModifyLimitQuery($query, $limit, $offset)
    {
710
        if ($limit === null && $offset > 0) {
711 712 713 714 715
            return $query . ' LIMIT -1 OFFSET ' . $offset;
        }

        return parent::doModifyLimitQuery($query, $limit, $offset);
    }
716

717
    /**
718
     * {@inheritDoc}
719 720 721 722 723
     */
    public function getBlobTypeDeclarationSQL(array $field)
    {
        return 'BLOB';
    }
724

725 726 727
    /**
     * {@inheritDoc}
     */
728 729
    public function getTemporaryTableName($tableName)
    {
730
        $tableName = str_replace('.', '__', $tableName);
731

732 733
        return $tableName;
    }
734 735

    /**
736 737
     * {@inheritDoc}
     *
738 739 740 741 742 743 744 745 746 747
     * Sqlite Platform emulates schema by underscoring each dot and generating tables
     * into the default database.
     *
     * This hack is implemented to be able to use SQLite as testdriver when
     * using schema supporting databases.
     */
    public function canEmulateSchemas()
    {
        return true;
    }
748

749 750 751 752 753 754 755 756
    /**
     * {@inheritDoc}
     */
    public function supportsForeignKeyConstraints()
    {
        return false;
    }

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    /**
     * {@inheritDoc}
     */
    public function getCreatePrimaryKeySQL(Index $index, $table)
    {
        throw new DBALException('Sqlite platform does not support alter primary key.');
    }

    /**
     * {@inheritdoc}
     */
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
    {
        throw new DBALException('Sqlite platform does not support alter foreign key.');
    }

    /**
     * {@inheritdoc}
     */
    public function getDropForeignKeySQL($foreignKey, $table)
    {
        throw new DBALException('Sqlite platform does not support alter foreign key.');
    }

781 782 783 784 785 786 787
    /**
     * {@inheritDoc}
     */
    public function getCreateConstraintSQL(Constraint $constraint, $table)
    {
        throw new DBALException('Sqlite platform does not support alter constraint.');
    }
788 789 790 791 792 793

    /**
     * {@inheritDoc}
     */
    public function getCreateTableSQL(Table $table, $createFlags = null)
    {
794
        $createFlags = $createFlags ?? self::CREATE_INDEXES | self::CREATE_FOREIGNKEYS;
795 796 797

        return parent::getCreateTableSQL($table, $createFlags);
    }
798

799 800 801 802 803 804 805
    /**
     * {@inheritDoc}
     */
    public function getListTableForeignKeysSQL($table, $database = null)
    {
        $table = str_replace('.', '__', $table);

806
        return sprintf('PRAGMA foreign_key_list(%s)', $this->quoteStringLiteral($table));
807 808 809 810 811 812 813
    }

    /**
     * {@inheritDoc}
     */
    public function getAlterTableSQL(TableDiff $diff)
    {
814
        $sql = $this->getSimpleAlterTableSQL($diff);
815
        if ($sql !== false) {
816 817 818
            return $sql;
        }

819
        $fromTable = $diff->fromTable;
820
        if (! $fromTable instanceof Table) {
821 822 823 824 825
            throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
        }

        $table = clone $fromTable;

826
        $columns        = [];
827 828
        $oldColumnNames = [];
        $newColumnNames = [];
829
        $columnSql      = [];
830 831

        foreach ($table->getColumns() as $columnName => $column) {
832 833
            $columnName                  = strtolower($columnName);
            $columns[$columnName]        = $column;
834 835 836
            $oldColumnNames[$columnName] = $newColumnNames[$columnName] = $column->getQuotedName($this);
        }

837 838 839 840 841
        foreach ($diff->removedColumns as $columnName => $column) {
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
            }

842
            $columnName = strtolower($columnName);
843 844
            if (! isset($columns[$columnName])) {
                continue;
845
            }
846 847 848 849 850 851

            unset(
                $columns[$columnName],
                $oldColumnNames[$columnName],
                $newColumnNames[$columnName]
            );
852 853 854 855 856 857 858
        }

        foreach ($diff->renamedColumns as $oldColumnName => $column) {
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
                continue;
            }

859 860 861 862 863 864 865
            $oldColumnName = strtolower($oldColumnName);
            if (isset($columns[$oldColumnName])) {
                unset($columns[$oldColumnName]);
            }

            $columns[strtolower($column->getName())] = $column;

866 867
            if (! isset($newColumnNames[$oldColumnName])) {
                continue;
868
            }
869 870

            $newColumnNames[$oldColumnName] = $column->getQuotedName($this);
871 872 873 874 875 876 877
        }

        foreach ($diff->changedColumns as $oldColumnName => $columnDiff) {
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
            }

878 879
            if (isset($columns[$oldColumnName])) {
                unset($columns[$oldColumnName]);
880 881
            }

882
            $columns[strtolower($columnDiff->column->getName())] = $columnDiff->column;
883

884 885
            if (! isset($newColumnNames[$oldColumnName])) {
                continue;
886
            }
887 888

            $newColumnNames[$oldColumnName] = $columnDiff->column->getQuotedName($this);
889 890
        }

891 892 893 894
        foreach ($diff->addedColumns as $columnName => $column) {
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
            }
895

896
            $columns[strtolower($columnName)] = $column;
897 898
        }

899
        $sql      = [];
900
        $tableSql = [];
901 902
        if (! $this->onSchemaAlterTable($diff, $tableSql)) {
            $dataTable = new Table('__temp__' . $table->getName());
903

904
            $newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
905
            $newTable->addOption('alter', true);
906

907 908 909
            $sql = $this->getPreAlterTableIndexForeignKeySQL($diff);
            //$sql = array_merge($sql, $this->getCreateTableSQL($dataTable, 0));
            $sql[] = sprintf('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s', $dataTable->getQuotedName($this), implode(', ', $oldColumnNames), $table->getQuotedName($this));
910
            $sql[] = $this->getDropTableSQL($fromTable);
911

912
            $sql   = array_merge($sql, $this->getCreateTableSQL($newTable));
913 914 915
            $sql[] = sprintf('INSERT INTO %s (%s) SELECT %s FROM %s', $newTable->getQuotedName($this), implode(', ', $newColumnNames), implode(', ', $oldColumnNames), $dataTable->getQuotedName($this));
            $sql[] = $this->getDropTableSQL($dataTable);

Sergei Morozov's avatar
Sergei Morozov committed
916 917 918 919 920 921 922 923
            $newName = $diff->getNewName();

            if ($newName !== false) {
                $sql[] = sprintf(
                    'ALTER TABLE %s RENAME TO %s',
                    $newTable->getQuotedName($this),
                    $newName->getQuotedName($this)
                );
924 925
            }

926 927
            $sql = array_merge($sql, $this->getPostAlterTableIndexForeignKeySQL($diff));
        }
928 929 930 931

        return array_merge($sql, $tableSql, $columnSql);
    }

Benjamin Morel's avatar
Benjamin Morel committed
932
    /**
933
     * @return string[]|false
Benjamin Morel's avatar
Benjamin Morel committed
934
     */
935 936
    private function getSimpleAlterTableSQL(TableDiff $diff)
    {
937 938
        // Suppress changes on integer type autoincrement columns.
        foreach ($diff->changedColumns as $oldColumnName => $columnDiff) {
939
            if (! $columnDiff->fromColumn instanceof Column ||
940 941
                ! $columnDiff->column instanceof Column ||
                ! $columnDiff->column->getAutoincrement() ||
942
                ! $columnDiff->column->getType() instanceof Types\IntegerType
943 944 945 946
            ) {
                continue;
            }

947
            if (! $columnDiff->hasChanged('type') && $columnDiff->hasChanged('unsigned')) {
948 949 950 951 952
                unset($diff->changedColumns[$oldColumnName]);

                continue;
            }

953
            $fromColumnType = $columnDiff->fromColumn->getType();
954

955 956
            if (! ($fromColumnType instanceof Types\SmallIntType) && ! ($fromColumnType instanceof Types\BigIntType)) {
                continue;
957
            }
958 959

            unset($diff->changedColumns[$oldColumnName]);
960 961
        }

962
        if (! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes)
963 964
                || ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes)
                || ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes)
965
                || ! empty($diff->renamedIndexes)
966 967 968 969 970 971
        ) {
            return false;
        }

        $table = new Table($diff->name);

972 973
        $sql       = [];
        $tableSql  = [];
974
        $columnSql = [];
975

Benjamin Morel's avatar
Benjamin Morel committed
976
        foreach ($diff->addedColumns as $column) {
977 978 979 980
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
            }

981
            $field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray());
982
            $type  = $field['type'];
983 984
            switch (true) {
                case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
985 986 987
                case $type instanceof Types\DateTimeType && $field['default'] === $this->getCurrentTimestampSQL():
                case $type instanceof Types\DateType && $field['default'] === $this->getCurrentDateSQL():
                case $type instanceof Types\TimeType && $field['default'] === $this->getCurrentTimeSQL():
988 989 990 991
                    return false;
            }

            $field['name'] = $column->getQuotedName($this);
992
            if ($type instanceof Types\StringType && $field['length'] === null) {
993 994 995
                $field['length'] = 255;
            }

996
            $sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' ADD COLUMN ' . $this->getColumnDeclarationSQL($field['name'], $field);
997 998
        }

999
        if (! $this->onSchemaAlterTable($diff, $tableSql)) {
1000
            if ($diff->newName !== false) {
1001
                $newTable = new Identifier($diff->newName);
1002
                $sql[]    = 'ALTER TABLE ' . $table->getQuotedName($this) . ' RENAME TO ' . $newTable->getQuotedName($this);
1003 1004
            }
        }
1005 1006 1007 1008

        return array_merge($sql, $tableSql, $columnSql);
    }

Benjamin Morel's avatar
Benjamin Morel committed
1009
    /**
1010
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
1011
     */
1012
    private function getColumnNamesInAlteredTable(TableDiff $diff)
1013
    {
1014
        $columns = [];
1015

1016 1017 1018 1019 1020 1021
        foreach ($diff->fromTable->getColumns() as $columnName => $column) {
            $columns[strtolower($columnName)] = $column->getName();
        }

        foreach ($diff->removedColumns as $columnName => $column) {
            $columnName = strtolower($columnName);
1022 1023
            if (! isset($columns[$columnName])) {
                continue;
1024
            }
1025 1026

            unset($columns[$columnName]);
1027 1028 1029
        }

        foreach ($diff->renamedColumns as $oldColumnName => $column) {
1030
            $columnName                          = $column->getName();
1031
            $columns[strtolower($oldColumnName)] = $columnName;
1032
            $columns[strtolower($columnName)]    = $columnName;
1033 1034 1035
        }

        foreach ($diff->changedColumns as $oldColumnName => $columnDiff) {
1036
            $columnName                          = $columnDiff->column->getName();
1037
            $columns[strtolower($oldColumnName)] = $columnName;
1038
            $columns[strtolower($columnName)]    = $columnName;
1039 1040
        }

Sergei Morozov's avatar
Sergei Morozov committed
1041 1042
        foreach ($diff->addedColumns as $column) {
            $columnName                       = $column->getName();
1043 1044 1045 1046 1047 1048
            $columns[strtolower($columnName)] = $columnName;
        }

        return $columns;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1049
    /**
1050
     * @return Index[]
Benjamin Morel's avatar
Benjamin Morel committed
1051
     */
1052 1053
    private function getIndexesInAlteredTable(TableDiff $diff)
    {
1054
        $indexes     = $diff->fromTable->getIndexes();
1055 1056 1057
        $columnNames = $this->getColumnNamesInAlteredTable($diff);

        foreach ($indexes as $key => $index) {
1058
            foreach ($diff->renamedIndexes as $oldIndexName => $renamedIndex) {
1059 1060
                if (strtolower($key) !== strtolower($oldIndexName)) {
                    continue;
1061
                }
1062 1063

                unset($indexes[$key]);
1064 1065
            }

1066
            $changed      = false;
1067
            $indexColumns = [];
1068 1069
            foreach ($index->getColumns() as $columnName) {
                $normalizedColumnName = strtolower($columnName);
1070
                if (! isset($columnNames[$normalizedColumnName])) {
1071 1072 1073
                    unset($indexes[$key]);
                    continue 2;
                }
1074 1075 1076 1077 1078 1079 1080

                $indexColumns[] = $columnNames[$normalizedColumnName];
                if ($columnName === $columnNames[$normalizedColumnName]) {
                    continue;
                }

                $changed = true;
1081 1082
            }

1083 1084
            if (! $changed) {
                continue;
1085
            }
1086 1087

            $indexes[$key] = new Index($index->getName(), $indexColumns, $index->isUnique(), $index->isPrimary(), $index->getFlags());
1088 1089 1090
        }

        foreach ($diff->removedIndexes as $index) {
1091
            $indexName = strtolower($index->getName());
1092 1093
            if (! strlen($indexName) || ! isset($indexes[$indexName])) {
                continue;
1094
            }
1095 1096

            unset($indexes[$indexName]);
1097 1098
        }

1099
        foreach (array_merge($diff->changedIndexes, $diff->addedIndexes, $diff->renamedIndexes) as $index) {
1100 1101 1102 1103 1104
            $indexName = strtolower($index->getName());
            if (strlen($indexName)) {
                $indexes[$indexName] = $index;
            } else {
                $indexes[] = $index;
1105 1106 1107
            }
        }

1108 1109 1110
        return $indexes;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1111
    /**
1112
     * @return ForeignKeyConstraint[]
Benjamin Morel's avatar
Benjamin Morel committed
1113
     */
1114 1115 1116 1117 1118 1119
    private function getForeignKeysInAlteredTable(TableDiff $diff)
    {
        $foreignKeys = $diff->fromTable->getForeignKeys();
        $columnNames = $this->getColumnNamesInAlteredTable($diff);

        foreach ($foreignKeys as $key => $constraint) {
1120
            $changed      = false;
1121
            $localColumns = [];
1122 1123
            foreach ($constraint->getLocalColumns() as $columnName) {
                $normalizedColumnName = strtolower($columnName);
1124
                if (! isset($columnNames[$normalizedColumnName])) {
1125 1126 1127
                    unset($foreignKeys[$key]);
                    continue 2;
                }
1128 1129 1130 1131 1132 1133 1134

                $localColumns[] = $columnNames[$normalizedColumnName];
                if ($columnName === $columnNames[$normalizedColumnName]) {
                    continue;
                }

                $changed = true;
1135 1136
            }

1137 1138
            if (! $changed) {
                continue;
1139
            }
1140 1141

            $foreignKeys[$key] = new ForeignKeyConstraint($localColumns, $constraint->getForeignTableName(), $constraint->getForeignColumns(), $constraint->getName(), $constraint->getOptions());
1142 1143 1144
        }

        foreach ($diff->removedForeignKeys as $constraint) {
Sergei Morozov's avatar
Sergei Morozov committed
1145 1146 1147 1148
            if (! $constraint instanceof ForeignKeyConstraint) {
                $constraint = new Identifier($constraint);
            }

1149
            $constraintName = strtolower($constraint->getName());
1150 1151
            if (! strlen($constraintName) || ! isset($foreignKeys[$constraintName])) {
                continue;
1152
            }
1153 1154

            unset($foreignKeys[$constraintName]);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
        }

        foreach (array_merge($diff->changedForeignKeys, $diff->addedForeignKeys) as $constraint) {
            $constraintName = strtolower($constraint->getName());
            if (strlen($constraintName)) {
                $foreignKeys[$constraintName] = $constraint;
            } else {
                $foreignKeys[] = $constraint;
            }
        }

        return $foreignKeys;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1169
    /**
1170
     * @return Index[]
Benjamin Morel's avatar
Benjamin Morel committed
1171
     */
1172 1173
    private function getPrimaryIndexInAlteredTable(TableDiff $diff)
    {
1174
        $primaryIndex = [];
1175 1176

        foreach ($this->getIndexesInAlteredTable($diff) as $index) {
1177 1178
            if (! $index->isPrimary()) {
                continue;
1179
            }
1180 1181

            $primaryIndex = [$index->getName() => $index];
1182 1183 1184 1185
        }

        return $primaryIndex;
    }
1186
}