AbstractMySQLPlatformTestCase.php 34 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
jeroendedauw's avatar
jeroendedauw committed
8
use Doctrine\DBAL\Schema\Index;
9 10
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
11
use Doctrine\DBAL\TransactionIsolationLevel;
12
use function array_shift;
13 14 15 16 17

abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
{
    public function testModifyLimitQueryWitoutLimit()
    {
Sergei Morozov's avatar
Sergei Morozov committed
18
        $sql = $this->platform->modifyLimitQuery('SELECT n FROM Foo', null, 10);
Sergei Morozov's avatar
Sergei Morozov committed
19
        self::assertEquals('SELECT n FROM Foo LIMIT 18446744073709551615 OFFSET 10', $sql);
20 21 22 23
    }

    public function testGenerateMixedCaseTableCreate()
    {
Sergei Morozov's avatar
Sergei Morozov committed
24 25
        $table = new Table('Foo');
        $table->addColumn('Bar', 'integer');
26

Sergei Morozov's avatar
Sergei Morozov committed
27
        $sql = $this->platform->getCreateTableSQL($table);
28
        self::assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', array_shift($sql));
29 30 31 32 33 34 35 36 37
    }

    public function getGenerateTableSql()
    {
        return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB';
    }

    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
Sergei Morozov's avatar
Sergei Morozov committed
38
        return ['CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'];
39 40 41 42
    }

    public function getGenerateAlterTableSql()
    {
Sergei Morozov's avatar
Sergei Morozov committed
43
        return ["ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL, CHANGE bloo bloo TINYINT(1) DEFAULT '0' NOT NULL"];
44 45 46 47
    }

    public function testGeneratesSqlSnippets()
    {
Sergei Morozov's avatar
Sergei Morozov committed
48 49 50
        self::assertEquals('RLIKE', $this->platform->getRegexpExpression(), 'Regular expression operator is not correct');
        self::assertEquals('`', $this->platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
        self::assertEquals('CONCAT(column1, column2, column3)', $this->platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
51 52 53 54
    }

    public function testGeneratesTransactionsCommands()
    {
55
        self::assertEquals(
56
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
Sergei Morozov's avatar
Sergei Morozov committed
57
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_UNCOMMITTED),
58 59
            ''
        );
60
        self::assertEquals(
61
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
Sergei Morozov's avatar
Sergei Morozov committed
62
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED)
63
        );
64
        self::assertEquals(
65
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
Sergei Morozov's avatar
Sergei Morozov committed
66
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
67
        );
68
        self::assertEquals(
69
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
Sergei Morozov's avatar
Sergei Morozov committed
70
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
71 72 73 74 75 76
        );
    }


    public function testGeneratesDDLSnippets()
    {
Sergei Morozov's avatar
Sergei Morozov committed
77 78 79 80
        self::assertEquals('SHOW DATABASES', $this->platform->getListDatabasesSQL());
        self::assertEquals('CREATE DATABASE foobar', $this->platform->getCreateDatabaseSQL('foobar'));
        self::assertEquals('DROP DATABASE foobar', $this->platform->getDropDatabaseSQL('foobar'));
        self::assertEquals('DROP TABLE foobar', $this->platform->getDropTableSQL('foobar'));
81 82 83 84
    }

    public function testGeneratesTypeDeclarationForIntegers()
    {
85
        self::assertEquals(
86
            'INT',
Sergei Morozov's avatar
Sergei Morozov committed
87
            $this->platform->getIntegerTypeDeclarationSQL([])
88
        );
89
        self::assertEquals(
90
            'INT AUTO_INCREMENT',
Sergei Morozov's avatar
Sergei Morozov committed
91
            $this->platform->getIntegerTypeDeclarationSQL(['autoincrement' => true])
Sergei Morozov's avatar
Sergei Morozov committed
92
        );
93
        self::assertEquals(
94
            'INT AUTO_INCREMENT',
Sergei Morozov's avatar
Sergei Morozov committed
95
            $this->platform->getIntegerTypeDeclarationSQL(
Sergei Morozov's avatar
Sergei Morozov committed
96 97 98
                ['autoincrement' => true, 'primary' => true]
            )
        );
99 100 101 102
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
103
        self::assertEquals(
104
            'CHAR(10)',
Sergei Morozov's avatar
Sergei Morozov committed
105
            $this->platform->getVarcharTypeDeclarationSQL(
Sergei Morozov's avatar
Sergei Morozov committed
106 107 108
                ['length' => 10, 'fixed' => true]
            )
        );
109
        self::assertEquals(
110
            'VARCHAR(50)',
Sergei Morozov's avatar
Sergei Morozov committed
111
            $this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
112 113
            'Variable string declaration is not correct'
        );
114
        self::assertEquals(
115
            'VARCHAR(255)',
Sergei Morozov's avatar
Sergei Morozov committed
116
            $this->platform->getVarcharTypeDeclarationSQL([]),
117 118 119 120 121 122
            'Long string declaration is not correct'
        );
    }

    public function testPrefersIdentityColumns()
    {
Sergei Morozov's avatar
Sergei Morozov committed
123
        self::assertTrue($this->platform->prefersIdentityColumns());
124 125 126 127
    }

    public function testSupportsIdentityColumns()
    {
Sergei Morozov's avatar
Sergei Morozov committed
128
        self::assertTrue($this->platform->supportsIdentityColumns());
129 130 131 132
    }

    public function testDoesSupportSavePoints()
    {
Sergei Morozov's avatar
Sergei Morozov committed
133
        self::assertTrue($this->platform->supportsSavepoints());
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }

    public function getGenerateIndexSql()
    {
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
    }

    public function getGenerateUniqueIndexSql()
    {
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
    }

    public function getGenerateForeignKeySql()
    {
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
    }

    /**
     * @group DBAL-126
     */
    public function testUniquePrimaryKey()
    {
Sergei Morozov's avatar
Sergei Morozov committed
156 157 158 159 160
        $keyTable = new Table('foo');
        $keyTable->addColumn('bar', 'integer');
        $keyTable->addColumn('baz', 'string');
        $keyTable->setPrimaryKey(['bar']);
        $keyTable->addUniqueIndex(['baz']);
161

Sergei Morozov's avatar
Sergei Morozov committed
162 163 164
        $oldTable = new Table('foo');
        $oldTable->addColumn('bar', 'integer');
        $oldTable->addColumn('baz', 'string');
165

Sergei Morozov's avatar
Sergei Morozov committed
166
        $c    = new Comparator();
167 168
        $diff = $c->diffTable($oldTable, $keyTable);

Sergei Morozov's avatar
Sergei Morozov committed
169
        $sql = $this->platform->getAlterTableSQL($diff);
170

Sergei Morozov's avatar
Sergei Morozov committed
171 172 173 174
        self::assertEquals([
            'ALTER TABLE foo ADD PRIMARY KEY (bar)',
            'CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)',
        ], $sql);
175 176 177 178
    }

    public function testModifyLimitQuery()
    {
Sergei Morozov's avatar
Sergei Morozov committed
179
        $sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
180
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
181 182 183 184
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
Sergei Morozov's avatar
Sergei Morozov committed
185
        $sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10);
186
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
187 188 189 190 191 192 193
    }

    /**
     * @group DDC-118
     */
    public function testGetDateTimeTypeDeclarationSql()
    {
Sergei Morozov's avatar
Sergei Morozov committed
194 195 196
        self::assertEquals('DATETIME', $this->platform->getDateTimeTypeDeclarationSQL(['version' => false]));
        self::assertEquals('TIMESTAMP', $this->platform->getDateTimeTypeDeclarationSQL(['version' => true]));
        self::assertEquals('DATETIME', $this->platform->getDateTimeTypeDeclarationSQL([]));
197 198 199 200
    }

    public function getCreateTableColumnCommentsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
201
        return ["CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"];
202 203 204 205
    }

    public function getAlterTableColumnCommentsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
206
        return ["ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE foo foo VARCHAR(255) NOT NULL, CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'"];
207 208 209 210
    }

    public function getCreateTableColumnTypeCommentsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
211
        return ["CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"];
212 213 214 215 216 217 218
    }

    /**
     * @group DBAL-237
     */
    public function testChangeIndexWithForeignKeys()
    {
Sergei Morozov's avatar
Sergei Morozov committed
219 220
        $index  = new Index('idx', ['col'], false);
        $unique = new Index('uniq', ['col'], true);
221

Sergei Morozov's avatar
Sergei Morozov committed
222
        $diff = new TableDiff('test', [], [], [], [$unique], [], [$index]);
Sergei Morozov's avatar
Sergei Morozov committed
223
        $sql  = $this->platform->getAlterTableSQL($diff);
Sergei Morozov's avatar
Sergei Morozov committed
224
        self::assertEquals(['ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)'], $sql);
225

Sergei Morozov's avatar
Sergei Morozov committed
226
        $diff = new TableDiff('test', [], [], [], [$index], [], [$unique]);
Sergei Morozov's avatar
Sergei Morozov committed
227
        $sql  = $this->platform->getAlterTableSQL($diff);
Sergei Morozov's avatar
Sergei Morozov committed
228
        self::assertEquals(['ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)'], $sql);
229 230 231 232
    }

    protected function getQuotedColumnInPrimaryKeySQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
233
        return ['CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, PRIMARY KEY(`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'];
234 235 236 237
    }

    protected function getQuotedColumnInIndexSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
238
        return ['CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB (`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'];
239 240
    }

Markus Fasselt's avatar
Markus Fasselt committed
241 242
    protected function getQuotedNameInIndexSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
243
        return ['CREATE TABLE test (column1 VARCHAR(255) NOT NULL, INDEX `key` (column1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'];
Markus Fasselt's avatar
Markus Fasselt committed
244 245
    }

246 247
    protected function getQuotedColumnInForeignKeySQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
248
        return [
249 250 251 252
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL, `bar` VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB',
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foreign` (`create`, bar, `foo-bar`)',
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`) REFERENCES foo (`create`, bar, `foo-bar`)',
            'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY (`create`, foo, `bar`) REFERENCES `foo-bar` (`create`, bar, `foo-bar`)',
Sergei Morozov's avatar
Sergei Morozov committed
253
        ];
254 255 256 257 258 259 260
    }

    public function testCreateTableWithFulltextIndex()
    {
        $table = new Table('fulltext_table');
        $table->addOption('engine', 'MyISAM');
        $table->addColumn('text', 'text');
Sergei Morozov's avatar
Sergei Morozov committed
261
        $table->addIndex(['text'], 'fulltext_text');
262 263 264 265

        $index = $table->getIndex('fulltext_text');
        $index->addFlag('fulltext');

Sergei Morozov's avatar
Sergei Morozov committed
266
        $sql = $this->platform->getCreateTableSQL($table);
Sergei Morozov's avatar
Sergei Morozov committed
267
        self::assertEquals(['CREATE TABLE fulltext_table (text LONGTEXT NOT NULL, FULLTEXT INDEX fulltext_text (text)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'], $sql);
268 269
    }

270 271 272 273 274
    public function testCreateTableWithSpatialIndex()
    {
        $table = new Table('spatial_table');
        $table->addOption('engine', 'MyISAM');
        $table->addColumn('point', 'text'); // This should be a point type
Sergei Morozov's avatar
Sergei Morozov committed
275
        $table->addIndex(['point'], 'spatial_text');
276 277 278 279

        $index = $table->getIndex('spatial_text');
        $index->addFlag('spatial');

Sergei Morozov's avatar
Sergei Morozov committed
280
        $sql = $this->platform->getCreateTableSQL($table);
Sergei Morozov's avatar
Sergei Morozov committed
281
        self::assertEquals(['CREATE TABLE spatial_table (point LONGTEXT NOT NULL, SPATIAL INDEX spatial_text (point)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'], $sql);
282 283
    }

284 285
    public function testClobTypeDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
286 287 288 289 290 291 292 293
        self::assertEquals('TINYTEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 1]));
        self::assertEquals('TINYTEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 255]));
        self::assertEquals('TEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 256]));
        self::assertEquals('TEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 65535]));
        self::assertEquals('MEDIUMTEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 65536]));
        self::assertEquals('MEDIUMTEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 16777215]));
        self::assertEquals('LONGTEXT', $this->platform->getClobTypeDeclarationSQL(['length' => 16777216]));
        self::assertEquals('LONGTEXT', $this->platform->getClobTypeDeclarationSQL([]));
294 295 296 297
    }

    public function testBlobTypeDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
298 299 300 301 302 303 304 305
        self::assertEquals('TINYBLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 1]));
        self::assertEquals('TINYBLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 255]));
        self::assertEquals('BLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 256]));
        self::assertEquals('BLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 65535]));
        self::assertEquals('MEDIUMBLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 65536]));
        self::assertEquals('MEDIUMBLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 16777215]));
        self::assertEquals('LONGBLOB', $this->platform->getBlobTypeDeclarationSQL(['length' => 16777216]));
        self::assertEquals('LONGBLOB', $this->platform->getBlobTypeDeclarationSQL([]));
306 307 308 309 310 311 312 313 314 315
    }

    /**
     * @group DBAL-400
     */
    public function testAlterTableAddPrimaryKey()
    {
        $table = new Table('alter_table_add_pk');
        $table->addColumn('id', 'integer');
        $table->addColumn('foo', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
316
        $table->addIndex(['id'], 'idx_id');
317 318 319 320 321

        $comparator = new Comparator();
        $diffTable  = clone $table;

        $diffTable->dropIndex('idx_id');
Sergei Morozov's avatar
Sergei Morozov committed
322
        $diffTable->setPrimaryKey(['id']);
323

324
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
325
            ['DROP INDEX idx_id ON alter_table_add_pk', 'ALTER TABLE alter_table_add_pk ADD PRIMARY KEY (id)'],
Sergei Morozov's avatar
Sergei Morozov committed
326
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
327 328 329
        );
    }

330
    /**
331
     * @group DBAL-1132
332 333 334
     */
    public function testAlterPrimaryKeyWithAutoincrementColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
335 336
        $table = new Table('alter_primary_key');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
337
        $table->addColumn('foo', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
338
        $table->setPrimaryKey(['id']);
339 340

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
341
        $diffTable  = clone $table;
342 343

        $diffTable->dropPrimaryKey();
Sergei Morozov's avatar
Sergei Morozov committed
344
        $diffTable->setPrimaryKey(['foo']);
345

346
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
347
            [
Steve Müller's avatar
Steve Müller committed
348 349
                'ALTER TABLE alter_primary_key MODIFY id INT NOT NULL',
                'ALTER TABLE alter_primary_key DROP PRIMARY KEY',
Sergei Morozov's avatar
Sergei Morozov committed
350 351
                'ALTER TABLE alter_primary_key ADD PRIMARY KEY (foo)',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
352
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
353 354 355
        );
    }

356 357 358 359 360
    /**
     * @group DBAL-464
     */
    public function testDropPrimaryKeyWithAutoincrementColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
361 362
        $table = new Table('drop_primary_key');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
andig's avatar
andig committed
363
        $table->addColumn('foo', 'integer');
364
        $table->addColumn('bar', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
365
        $table->setPrimaryKey(['id', 'foo']);
366 367

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
368
        $diffTable  = clone $table;
369 370 371

        $diffTable->dropPrimaryKey();

372
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
373
            [
374
                'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
Sergei Morozov's avatar
Sergei Morozov committed
375 376
                'ALTER TABLE drop_primary_key DROP PRIMARY KEY',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
377
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
378 379 380
        );
    }

381 382 383 384 385
    /**
     * @group DBAL-2302
     */
    public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
386 387
        $table = new Table('tbl');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
388 389
        $table->addColumn('foo', 'integer');
        $table->addColumn('bar', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
390
        $table->setPrimaryKey(['id', 'foo']);
391 392

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
393
        $diffTable  = clone $table;
394 395

        $diffTable->dropPrimaryKey();
Sergei Morozov's avatar
Sergei Morozov committed
396
        $diffTable->setPrimaryKey(['id']);
397

398
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
399
            [
400 401 402
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
                'ALTER TABLE tbl DROP PRIMARY KEY',
                'ALTER TABLE tbl ADD PRIMARY KEY (id)',
Sergei Morozov's avatar
Sergei Morozov committed
403
            ],
Sergei Morozov's avatar
Sergei Morozov committed
404
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
405 406 407
        );
    }

408 409 410 411 412
    /**
     * @group DBAL-2302
     */
    public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
413 414
        $table = new Table('tbl');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
415 416
        $table->addColumn('foo', 'integer');
        $table->addColumn('bar', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
417
        $table->setPrimaryKey(['id']);
418 419

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
420
        $diffTable  = clone $table;
421 422

        $diffTable->dropPrimaryKey();
Sergei Morozov's avatar
Sergei Morozov committed
423
        $diffTable->setPrimaryKey(['id', 'foo']);
424

425
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
426
            [
427 428 429
                'ALTER TABLE tbl MODIFY id INT NOT NULL',
                'ALTER TABLE tbl DROP PRIMARY KEY',
                'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)',
Sergei Morozov's avatar
Sergei Morozov committed
430
            ],
Sergei Morozov's avatar
Sergei Morozov committed
431
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
432 433 434
        );
    }

435 436 437 438 439
    /**
     * @group DBAL-586
     */
    public function testAddAutoIncrementPrimaryKey()
    {
Sergei Morozov's avatar
Sergei Morozov committed
440 441 442 443
        $keyTable = new Table('foo');
        $keyTable->addColumn('id', 'integer', ['autoincrement' => true]);
        $keyTable->addColumn('baz', 'string');
        $keyTable->setPrimaryKey(['id']);
444

Sergei Morozov's avatar
Sergei Morozov committed
445 446
        $oldTable = new Table('foo');
        $oldTable->addColumn('baz', 'string');
447

Sergei Morozov's avatar
Sergei Morozov committed
448
        $c    = new Comparator();
449 450
        $diff = $c->diffTable($oldTable, $keyTable);

Sergei Morozov's avatar
Sergei Morozov committed
451
        $sql = $this->platform->getAlterTableSQL($diff);
452

Sergei Morozov's avatar
Sergei Morozov committed
453
        self::assertEquals(['ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)'], $sql);
454 455 456 457
    }

    public function testNamedPrimaryKey()
    {
Sergei Morozov's avatar
Sergei Morozov committed
458 459
        $diff                              = new TableDiff('mytable');
        $diff->changedIndexes['foo_index'] = new Index('foo_index', ['foo'], true, true);
460

Sergei Morozov's avatar
Sergei Morozov committed
461
        $sql = $this->platform->getAlterTableSQL($diff);
462

Sergei Morozov's avatar
Sergei Morozov committed
463 464 465 466
        self::assertEquals([
            'ALTER TABLE mytable DROP PRIMARY KEY',
            'ALTER TABLE mytable ADD PRIMARY KEY (foo)',
        ], $sql);
467
    }
468

469 470
    public function testAlterPrimaryKeyWithNewColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
471
        $table = new Table('yolo');
472 473
        $table->addColumn('pkc1', 'integer');
        $table->addColumn('col_a', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
474
        $table->setPrimaryKey(['pkc1']);
475 476

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
477
        $diffTable  = clone $table;
478

479 480
        $diffTable->addColumn('pkc2', 'integer');
        $diffTable->dropPrimaryKey();
Sergei Morozov's avatar
Sergei Morozov committed
481
        $diffTable->setPrimaryKey(['pkc1', 'pkc2']);
482

483
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
484
            [
485 486 487
                'ALTER TABLE yolo DROP PRIMARY KEY',
                'ALTER TABLE yolo ADD pkc2 INT NOT NULL',
                'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)',
Sergei Morozov's avatar
Sergei Morozov committed
488
            ],
Sergei Morozov's avatar
Sergei Morozov committed
489
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
490
        );
491
    }
492 493 494

    public function testInitializesDoctrineTypeMappings()
    {
Sergei Morozov's avatar
Sergei Morozov committed
495 496
        self::assertTrue($this->platform->hasDoctrineTypeMappingFor('binary'));
        self::assertSame('binary', $this->platform->getDoctrineTypeMapping('binary'));
497

Sergei Morozov's avatar
Sergei Morozov committed
498 499
        self::assertTrue($this->platform->hasDoctrineTypeMappingFor('varbinary'));
        self::assertSame('binary', $this->platform->getDoctrineTypeMapping('varbinary'));
500 501 502 503 504 505 506 507 508
    }

    protected function getBinaryMaxLength()
    {
        return 65535;
    }

    public function testReturnsBinaryTypeDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
509 510 511
        self::assertSame('VARBINARY(255)', $this->platform->getBinaryTypeDeclarationSQL([]));
        self::assertSame('VARBINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
        self::assertSame('VARBINARY(65535)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 65535]));
512
        self::assertSame('VARBINARY(65536)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
513

Sergei Morozov's avatar
Sergei Morozov committed
514
        self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
515 516 517 518 519 520 521 522 523 524 525 526
        self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL([
            'fixed' => true,
            'length' => 0,
        ]));
        self::assertSame('BINARY(65535)', $this->platform->getBinaryTypeDeclarationSQL([
            'fixed' => true,
            'length' => 65535,
        ]));
        self::assertSame('BINARY(65536)', $this->platform->getBinaryTypeDeclarationSQL([
            'fixed' => true,
            'length' => 65536,
        ]));
527 528 529 530
    }

    public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines()
    {
Sergei Morozov's avatar
Sergei Morozov committed
531
        $table = new Table('foreign_table');
532 533
        $table->addColumn('id', 'integer');
        $table->addColumn('fk_id', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
534 535
        $table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
        $table->setPrimaryKey(['id']);
536 537
        $table->addOption('engine', 'MyISAM');

538
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
539
            ['CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'],
Sergei Morozov's avatar
Sergei Morozov committed
540
            $this->platform->getCreateTableSQL(
541 542 543 544 545 546 547 548
                $table,
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
            )
        );

        $table = clone $table;
        $table->addOption('engine', 'InnoDB');

549
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
550
            [
551
                'CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB',
Sergei Morozov's avatar
Sergei Morozov committed
552 553
                'ALTER TABLE foreign_table ADD CONSTRAINT FK_5690FFE2A57719D0 FOREIGN KEY (fk_id) REFERENCES foreign_table (id)',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
554
            $this->platform->getCreateTableSQL(
555 556 557 558 559 560 561 562
                $table,
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
            )
        );
    }

    public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines()
    {
Sergei Morozov's avatar
Sergei Morozov committed
563
        $table = new Table('foreign_table');
564 565
        $table->addColumn('id', 'integer');
        $table->addColumn('fk_id', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
566 567
        $table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
        $table->setPrimaryKey(['id']);
568 569
        $table->addOption('engine', 'MyISAM');

Sergei Morozov's avatar
Sergei Morozov committed
570 571 572
        $addedForeignKeys   = [new ForeignKeyConstraint(['fk_id'], 'foo', ['id'], 'fk_add')];
        $changedForeignKeys = [new ForeignKeyConstraint(['fk_id'], 'bar', ['id'], 'fk_change')];
        $removedForeignKeys = [new ForeignKeyConstraint(['fk_id'], 'baz', ['id'], 'fk_remove')];
573

Sergei Morozov's avatar
Sergei Morozov committed
574 575 576
        $tableDiff                     = new TableDiff('foreign_table');
        $tableDiff->fromTable          = $table;
        $tableDiff->addedForeignKeys   = $addedForeignKeys;
577 578 579
        $tableDiff->changedForeignKeys = $changedForeignKeys;
        $tableDiff->removedForeignKeys = $removedForeignKeys;

Sergei Morozov's avatar
Sergei Morozov committed
580
        self::assertEmpty($this->platform->getAlterTableSQL($tableDiff));
581 582 583

        $table->addOption('engine', 'InnoDB');

Sergei Morozov's avatar
Sergei Morozov committed
584 585 586
        $tableDiff                     = new TableDiff('foreign_table');
        $tableDiff->fromTable          = $table;
        $tableDiff->addedForeignKeys   = $addedForeignKeys;
587 588 589
        $tableDiff->changedForeignKeys = $changedForeignKeys;
        $tableDiff->removedForeignKeys = $removedForeignKeys;

590
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
591
            [
592 593 594 595
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_remove',
                'ALTER TABLE foreign_table DROP FOREIGN KEY fk_change',
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_add FOREIGN KEY (fk_id) REFERENCES foo (id)',
                'ALTER TABLE foreign_table ADD CONSTRAINT fk_change FOREIGN KEY (fk_id) REFERENCES bar (id)',
Sergei Morozov's avatar
Sergei Morozov committed
596
            ],
Sergei Morozov's avatar
Sergei Morozov committed
597
            $this->platform->getAlterTableSQL($tableDiff)
598 599 600 601 602 603 604 605
        );
    }

    /**
     * @group DBAL-234
     */
    protected function getAlterTableRenameIndexSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
606
        return [
607 608
            'DROP INDEX idx_foo ON mytable',
            'CREATE INDEX idx_bar ON mytable (id)',
Sergei Morozov's avatar
Sergei Morozov committed
609
        ];
610 611 612 613 614 615 616
    }

    /**
     * @group DBAL-234
     */
    protected function getQuotedAlterTableRenameIndexSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
617
        return [
618 619 620 621
            'DROP INDEX `create` ON `table`',
            'CREATE INDEX `select` ON `table` (id)',
            'DROP INDEX `foo` ON `table`',
            'CREATE INDEX `bar` ON `table` (id)',
Sergei Morozov's avatar
Sergei Morozov committed
622
        ];
623
    }
624

625 626 627 628 629
    /**
     * @group DBAL-807
     */
    protected function getAlterTableRenameIndexInSchemaSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
630
        return [
631 632
            'DROP INDEX idx_foo ON myschema.mytable',
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
Sergei Morozov's avatar
Sergei Morozov committed
633
        ];
634 635 636 637 638 639 640
    }

    /**
     * @group DBAL-807
     */
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
641
        return [
642 643 644 645
            'DROP INDEX `create` ON `schema`.`table`',
            'CREATE INDEX `select` ON `schema`.`table` (id)',
            'DROP INDEX `foo` ON `schema`.`table`',
            'CREATE INDEX `bar` ON `schema`.`table` (id)',
Sergei Morozov's avatar
Sergei Morozov committed
646
        ];
647 648
    }

649 650 651 652 653 654 655 656 657 658
    protected function getQuotesDropForeignKeySQL()
    {
        return 'ALTER TABLE `table` DROP FOREIGN KEY `select`';
    }

    protected function getQuotesDropConstraintSQL()
    {
        return 'ALTER TABLE `table` DROP CONSTRAINT `select`';
    }

659 660
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
    {
Sergei Morozov's avatar
Sergei Morozov committed
661 662 663 664 665
        $table = new Table('text_blob_default_value');
        $table->addColumn('def_text', 'text', ['default' => 'def']);
        $table->addColumn('def_text_null', 'text', ['notnull' => false, 'default' => 'def']);
        $table->addColumn('def_blob', 'blob', ['default' => 'def']);
        $table->addColumn('def_blob_null', 'blob', ['notnull' => false, 'default' => 'def']);
666

667
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
668
            ['CREATE TABLE text_blob_default_value (def_text LONGTEXT NOT NULL, def_text_null LONGTEXT DEFAULT NULL, def_blob LONGBLOB NOT NULL, def_blob_null LONGBLOB DEFAULT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'],
Sergei Morozov's avatar
Sergei Morozov committed
669
            $this->platform->getCreateTableSQL($table)
670 671 672
        );

        $diffTable = clone $table;
Sergei Morozov's avatar
Sergei Morozov committed
673 674 675 676
        $diffTable->changeColumn('def_text', ['default' => null]);
        $diffTable->changeColumn('def_text_null', ['default' => null]);
        $diffTable->changeColumn('def_blob', ['default' => null]);
        $diffTable->changeColumn('def_blob_null', ['default' => null]);
677 678 679

        $comparator = new Comparator();

Sergei Morozov's avatar
Sergei Morozov committed
680
        self::assertEmpty($this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
681
    }
682 683 684 685 686 687

    /**
     * {@inheritdoc}
     */
    protected function getQuotedAlterTableRenameColumnSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
688
        return ['ALTER TABLE mytable ' .
689 690 691 692 693 694 695 696
            "CHANGE unquoted1 unquoted INT NOT NULL COMMENT 'Unquoted 1', " .
            "CHANGE unquoted2 `where` INT NOT NULL COMMENT 'Unquoted 2', " .
            "CHANGE unquoted3 `foo` INT NOT NULL COMMENT 'Unquoted 3', " .
            "CHANGE `create` reserved_keyword INT NOT NULL COMMENT 'Reserved keyword 1', " .
            "CHANGE `table` `from` INT NOT NULL COMMENT 'Reserved keyword 2', " .
            "CHANGE `select` `bar` INT NOT NULL COMMENT 'Reserved keyword 3', " .
            "CHANGE quoted1 quoted INT NOT NULL COMMENT 'Quoted 1', " .
            "CHANGE quoted2 `and` INT NOT NULL COMMENT 'Quoted 2', " .
Sergei Morozov's avatar
Sergei Morozov committed
697 698
            "CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'",
        ];
699
    }
700 701 702 703 704 705

    /**
     * {@inheritdoc}
     */
    protected function getQuotedAlterTableChangeColumnLengthSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
706
        return ['ALTER TABLE mytable ' .
707 708 709 710 711
            "CHANGE unquoted1 unquoted1 VARCHAR(255) NOT NULL COMMENT 'Unquoted 1', " .
            "CHANGE unquoted2 unquoted2 VARCHAR(255) NOT NULL COMMENT 'Unquoted 2', " .
            "CHANGE unquoted3 unquoted3 VARCHAR(255) NOT NULL COMMENT 'Unquoted 3', " .
            "CHANGE `create` `create` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 1', " .
            "CHANGE `table` `table` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 2', " .
Sergei Morozov's avatar
Sergei Morozov committed
712 713
            "CHANGE `select` `select` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 3'",
        ];
714
    }
715 716 717 718 719 720

    /**
     * @group DBAL-423
     */
    public function testReturnsGuidTypeDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
721
        self::assertSame('CHAR(36)', $this->platform->getGuidTypeDeclarationSQL([]));
722
    }
723 724 725 726 727 728

    /**
     * {@inheritdoc}
     */
    public function getAlterTableRenameColumnSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
729
        return ["ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'"];
730
    }
731 732 733 734 735 736

    /**
     * {@inheritdoc}
     */
    protected function getQuotesTableIdentifiersInAlterTableSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
737
        return [
738 739 740 741 742 743
            'ALTER TABLE `foo` DROP FOREIGN KEY fk1',
            'ALTER TABLE `foo` DROP FOREIGN KEY fk2',
            'ALTER TABLE `foo` RENAME TO `table`, ADD bloo INT NOT NULL, DROP baz, CHANGE bar bar INT DEFAULT NULL, ' .
            'CHANGE id war INT NOT NULL',
            'ALTER TABLE `table` ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
            'ALTER TABLE `table` ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
Sergei Morozov's avatar
Sergei Morozov committed
744
        ];
745
    }
746 747 748 749 750 751

    /**
     * {@inheritdoc}
     */
    protected function getCommentOnColumnSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
752
        return [
753 754 755
            "COMMENT ON COLUMN foo.bar IS 'comment'",
            "COMMENT ON COLUMN `Foo`.`BAR` IS 'comment'",
            "COMMENT ON COLUMN `select`.`from` IS 'comment'",
Sergei Morozov's avatar
Sergei Morozov committed
756
        ];
757
    }
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

    /**
     * {@inheritdoc}
     */
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
    {
        return 'CONSTRAINT `select` UNIQUE (foo)';
    }

    /**
     * {@inheritdoc}
     */
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
    {
        return 'INDEX `select` (foo)';
    }
774

775 776 777 778 779 780 781 782
    /**
     * {@inheritdoc}
     */
    protected function getQuotesReservedKeywordInTruncateTableSQL()
    {
        return 'TRUNCATE `select`';
    }

783 784 785 786 787
    /**
     * {@inheritdoc}
     */
    protected function getAlterStringToFixedStringSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
788
        return ['ALTER TABLE mytable CHANGE name name CHAR(2) NOT NULL'];
789
    }
790 791 792 793 794 795

    /**
     * {@inheritdoc}
     */
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
796
        return [
797 798 799 800
            'ALTER TABLE mytable DROP FOREIGN KEY fk_foo',
            'DROP INDEX idx_foo ON mytable',
            'CREATE INDEX idx_foo_renamed ON mytable (foo)',
            'ALTER TABLE mytable ADD CONSTRAINT fk_foo FOREIGN KEY (foo) REFERENCES foreign_table (id)',
Sergei Morozov's avatar
Sergei Morozov committed
801
        ];
802
    }
803 804 805 806 807 808

    /**
     * {@inheritdoc}
     */
    public function getGeneratesDecimalTypeDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
809 810 811 812 813 814 815 816
        return [
            [[], 'NUMERIC(10, 0)'],
            [['unsigned' => true], 'NUMERIC(10, 0) UNSIGNED'],
            [['unsigned' => false], 'NUMERIC(10, 0)'],
            [['precision' => 5], 'NUMERIC(5, 0)'],
            [['scale' => 5], 'NUMERIC(10, 5)'],
            [['precision' => 8, 'scale' => 2], 'NUMERIC(8, 2)'],
        ];
817 818 819 820 821 822 823
    }

    /**
     * {@inheritdoc}
     */
    public function getGeneratesFloatDeclarationSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
824 825 826 827 828 829 830 831
        return [
            [[], 'DOUBLE PRECISION'],
            [['unsigned' => true], 'DOUBLE PRECISION UNSIGNED'],
            [['unsigned' => false], 'DOUBLE PRECISION'],
            [['precision' => 5], 'DOUBLE PRECISION'],
            [['scale' => 5], 'DOUBLE PRECISION'],
            [['precision' => 8, 'scale' => 2], 'DOUBLE PRECISION'],
        ];
832
    }
833 834 835 836 837 838

    /**
     * @group DBAL-2436
     */
    public function testQuotesTableNameInListTableIndexesSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
839
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\", 'foo_db'), '', true);
840 841 842 843 844 845 846
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesDatabaseNameInListTableIndexesSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
847
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL('foo_table', "Foo'Bar\\"), '', true);
848 849 850 851 852 853 854
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesDatabaseNameInListViewsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
855
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListViewsSQL("Foo'Bar\\"), '', true);
856 857 858 859 860 861 862
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesTableNameInListTableForeignKeysSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
863
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
864 865 866 867 868 869 870
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesDatabaseNameInListTableForeignKeysSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
871
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableForeignKeysSQL('foo_table', "Foo'Bar\\"), '', true);
872 873 874 875 876 877 878
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesTableNameInListTableColumnsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
879
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
880 881 882 883 884 885 886
    }

    /**
     * @group DBAL-2436
     */
    public function testQuotesDatabaseNameInListTableColumnsSQL()
    {
Sergei Morozov's avatar
Sergei Morozov committed
887
        self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
888
    }
889 890 891

    public function testListTableForeignKeysSQLEvaluatesDatabase()
    {
Sergei Morozov's avatar
Sergei Morozov committed
892
        $sql = $this->platform->getListTableForeignKeysSQL('foo');
893

894
        self::assertContains('DATABASE()', $sql);
895

Sergei Morozov's avatar
Sergei Morozov committed
896
        $sql = $this->platform->getListTableForeignKeysSQL('foo', 'bar');
897

898 899
        self::assertContains('bar', $sql);
        self::assertNotContains('DATABASE()', $sql);
900
    }
901 902 903

    public function testSupportsColumnCollation() : void
    {
Sergei Morozov's avatar
Sergei Morozov committed
904
        self::assertTrue($this->platform->supportsColumnCollation());
905 906 907 908 909 910
    }

    public function testColumnCollationDeclarationSQL() : void
    {
        self::assertSame(
            'COLLATE ascii_general_ci',
Sergei Morozov's avatar
Sergei Morozov committed
911
            $this->platform->getColumnCollationDeclarationSQL('ascii_general_ci')
912 913 914 915 916 917 918 919 920 921 922
        );
    }

    public function testGetCreateTableSQLWithColumnCollation() : void
    {
        $table = new Table('foo');
        $table->addColumn('no_collation', 'string');
        $table->addColumn('column_collation', 'string')->setPlatformOption('collation', 'ascii_general_ci');

        self::assertSame(
            ['CREATE TABLE foo (no_collation VARCHAR(255) NOT NULL, column_collation VARCHAR(255) NOT NULL COLLATE ascii_general_ci) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'],
Sergei Morozov's avatar
Sergei Morozov committed
923
            $this->platform->getCreateTableSQL($table),
924 925 926
            'Column "no_collation" will use the default collation from the table/database and "column_collation" overwrites the collation on this column'
        );
    }
927
}