AbstractMySQLPlatformTestCase.php 36.7 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Platforms;

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

abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
{
17 18 19
    /** @var MySqlPlatform */
    protected $platform;

20
    public function testModifyLimitQueryWitoutLimit() : void
21
    {
Sergei Morozov's avatar
Sergei Morozov committed
22
        $sql = $this->platform->modifyLimitQuery('SELECT n FROM Foo', null, 10);
Sergei Morozov's avatar
Sergei Morozov committed
23
        self::assertEquals('SELECT n FROM Foo LIMIT 18446744073709551615 OFFSET 10', $sql);
24 25
    }

26
    public function testGenerateMixedCaseTableCreate() : void
27
    {
Sergei Morozov's avatar
Sergei Morozov committed
28 29
        $table = new Table('Foo');
        $table->addColumn('Bar', 'integer');
30

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

35
    public function getGenerateTableSql() : string
36
    {
37
        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';
38 39
    }

40 41 42 43
    /**
     * @return string[]
     */
    public function getGenerateTableWithMultiColumnUniqueIndexSql() : array
44
    {
45
        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'];
46 47
    }

48 49 50 51
    /**
     * {@inheritDoc}
     */
    public function getGenerateAlterTableSql() : array
52
    {
Sergei Morozov's avatar
Sergei Morozov committed
53
        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"];
54 55
    }

56
    public function testGeneratesSqlSnippets() : void
57
    {
Sergei Morozov's avatar
Sergei Morozov committed
58 59 60
        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');
61 62
    }

63
    public function testGeneratesTransactionsCommands() : void
64
    {
65
        self::assertEquals(
66
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
Sergei Morozov's avatar
Sergei Morozov committed
67
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_UNCOMMITTED),
68 69
            ''
        );
70
        self::assertEquals(
71
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
Sergei Morozov's avatar
Sergei Morozov committed
72
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED)
73
        );
74
        self::assertEquals(
75
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
Sergei Morozov's avatar
Sergei Morozov committed
76
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
77
        );
78
        self::assertEquals(
79
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
Sergei Morozov's avatar
Sergei Morozov committed
80
            $this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
81 82 83
        );
    }

84
    public function testGeneratesDDLSnippets() : void
85
    {
Sergei Morozov's avatar
Sergei Morozov committed
86 87 88 89
        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'));
90 91
    }

92
    public function testGeneratesTypeDeclarationForIntegers() : void
93
    {
94
        self::assertEquals(
95
            'INT',
Sergei Morozov's avatar
Sergei Morozov committed
96
            $this->platform->getIntegerTypeDeclarationSQL([])
97
        );
98
        self::assertEquals(
99
            'INT AUTO_INCREMENT',
Sergei Morozov's avatar
Sergei Morozov committed
100
            $this->platform->getIntegerTypeDeclarationSQL(['autoincrement' => true])
Sergei Morozov's avatar
Sergei Morozov committed
101
        );
102
        self::assertEquals(
103
            'INT AUTO_INCREMENT',
Sergei Morozov's avatar
Sergei Morozov committed
104
            $this->platform->getIntegerTypeDeclarationSQL(
Sergei Morozov's avatar
Sergei Morozov committed
105 106 107
                ['autoincrement' => true, 'primary' => true]
            )
        );
108 109
    }

110
    public function testGeneratesTypeDeclarationForStrings() : void
111
    {
112
        self::assertEquals(
113
            'CHAR(10)',
Sergei Morozov's avatar
Sergei Morozov committed
114
            $this->platform->getVarcharTypeDeclarationSQL(
Sergei Morozov's avatar
Sergei Morozov committed
115 116 117
                ['length' => 10, 'fixed' => true]
            )
        );
118
        self::assertEquals(
119
            'VARCHAR(50)',
Sergei Morozov's avatar
Sergei Morozov committed
120
            $this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
121 122
            'Variable string declaration is not correct'
        );
123
        self::assertEquals(
124
            'VARCHAR(255)',
Sergei Morozov's avatar
Sergei Morozov committed
125
            $this->platform->getVarcharTypeDeclarationSQL([]),
126 127 128 129
            'Long string declaration is not correct'
        );
    }

130
    public function testPrefersIdentityColumns() : void
131
    {
Sergei Morozov's avatar
Sergei Morozov committed
132
        self::assertTrue($this->platform->prefersIdentityColumns());
133 134
    }

135
    public function testSupportsIdentityColumns() : void
136
    {
Sergei Morozov's avatar
Sergei Morozov committed
137
        self::assertTrue($this->platform->supportsIdentityColumns());
138 139
    }

140
    public function testDoesSupportSavePoints() : void
141
    {
Sergei Morozov's avatar
Sergei Morozov committed
142
        self::assertTrue($this->platform->supportsSavepoints());
143 144
    }

145
    public function getGenerateIndexSql() : string
146 147 148 149
    {
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
    }

150
    public function getGenerateUniqueIndexSql() : string
151 152 153 154
    {
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
    }

155
    public function getGenerateForeignKeySql() : string
156 157 158 159 160 161 162
    {
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
    }

    /**
     * @group DBAL-126
     */
163
    public function testUniquePrimaryKey() : void
164
    {
Sergei Morozov's avatar
Sergei Morozov committed
165 166 167 168 169
        $keyTable = new Table('foo');
        $keyTable->addColumn('bar', 'integer');
        $keyTable->addColumn('baz', 'string');
        $keyTable->setPrimaryKey(['bar']);
        $keyTable->addUniqueIndex(['baz']);
170

Sergei Morozov's avatar
Sergei Morozov committed
171 172 173
        $oldTable = new Table('foo');
        $oldTable->addColumn('bar', 'integer');
        $oldTable->addColumn('baz', 'string');
174

Sergei Morozov's avatar
Sergei Morozov committed
175
        $c    = new Comparator();
176 177
        $diff = $c->diffTable($oldTable, $keyTable);

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

Sergei Morozov's avatar
Sergei Morozov committed
180 181 182 183
        self::assertEquals([
            'ALTER TABLE foo ADD PRIMARY KEY (bar)',
            'CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)',
        ], $sql);
184 185
    }

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

192
    public function testModifyLimitQueryWithEmptyOffset() : void
193
    {
Sergei Morozov's avatar
Sergei Morozov committed
194
        $sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10);
195
        self::assertEquals('SELECT * FROM user LIMIT 10', $sql);
196 197 198 199 200
    }

    /**
     * @group DDC-118
     */
201
    public function testGetDateTimeTypeDeclarationSql() : void
202
    {
Sergei Morozov's avatar
Sergei Morozov committed
203 204 205
        self::assertEquals('DATETIME', $this->platform->getDateTimeTypeDeclarationSQL(['version' => false]));
        self::assertEquals('TIMESTAMP', $this->platform->getDateTimeTypeDeclarationSQL(['version' => true]));
        self::assertEquals('DATETIME', $this->platform->getDateTimeTypeDeclarationSQL([]));
206 207
    }

208 209 210 211
    /**
     * {@inheritDoc}
     */
    public function getCreateTableColumnCommentsSQL() : array
212
    {
213
        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"];
214 215
    }

216 217 218 219
    /**
     * {@inheritDoc}
     */
    public function getAlterTableColumnCommentsSQL() : array
220
    {
Sergei Morozov's avatar
Sergei Morozov committed
221
        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'"];
222 223
    }

224 225 226 227
    /**
     * {@inheritDoc}
     */
    public function getCreateTableColumnTypeCommentsSQL() : array
228
    {
229
        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"];
230 231 232 233 234
    }

    /**
     * @group DBAL-237
     */
235
    public function testChangeIndexWithForeignKeys() : void
236
    {
Sergei Morozov's avatar
Sergei Morozov committed
237 238
        $index  = new Index('idx', ['col'], false);
        $unique = new Index('uniq', ['col'], true);
239

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

Sergei Morozov's avatar
Sergei Morozov committed
244
        $diff = new TableDiff('test', [], [], [], [$index], [], [$unique]);
Sergei Morozov's avatar
Sergei Morozov committed
245
        $sql  = $this->platform->getAlterTableSQL($diff);
Sergei Morozov's avatar
Sergei Morozov committed
246
        self::assertEquals(['ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)'], $sql);
247 248
    }

249 250 251 252
    /**
     * @return string[]
     */
    protected function getQuotedColumnInPrimaryKeySQL() : array
253
    {
254
        return ['CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, PRIMARY KEY(`create`)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'];
255 256
    }

257 258 259 260
    /**
     * @return string[]
     */
    protected function getQuotedColumnInIndexSQL() : array
261
    {
262
        return ['CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB (`create`)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'];
263 264
    }

265 266 267 268
    /**
     * @return string[]
     */
    protected function getQuotedNameInIndexSQL() : array
Markus Fasselt's avatar
Markus Fasselt committed
269
    {
270
        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
271 272
    }

273 274 275 276
    /**
     * @return string[]
     */
    protected function getQuotedColumnInForeignKeySQL() : array
277
    {
Sergei Morozov's avatar
Sergei Morozov committed
278
        return [
279
            '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',
280 281 282
            '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
283
        ];
284 285
    }

286
    public function testCreateTableWithFulltextIndex() : void
287 288 289 290
    {
        $table = new Table('fulltext_table');
        $table->addOption('engine', 'MyISAM');
        $table->addColumn('text', 'text');
Sergei Morozov's avatar
Sergei Morozov committed
291
        $table->addIndex(['text'], 'fulltext_text');
292 293 294 295

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

Sergei Morozov's avatar
Sergei Morozov committed
296
        $sql = $this->platform->getCreateTableSQL($table);
297
        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);
298 299
    }

300
    public function testCreateTableWithSpatialIndex() : void
301 302 303 304
    {
        $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
305
        $table->addIndex(['point'], 'spatial_text');
306 307 308 309

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

Sergei Morozov's avatar
Sergei Morozov committed
310
        $sql = $this->platform->getCreateTableSQL($table);
311
        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);
312 313
    }

314
    public function testClobTypeDeclarationSQL() : void
315
    {
Sergei Morozov's avatar
Sergei Morozov committed
316 317 318 319 320 321 322 323
        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([]));
324 325
    }

326
    public function testBlobTypeDeclarationSQL() : void
327
    {
Sergei Morozov's avatar
Sergei Morozov committed
328 329 330 331 332 333 334 335
        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([]));
336 337 338 339 340
    }

    /**
     * @group DBAL-400
     */
341
    public function testAlterTableAddPrimaryKey() : void
342 343 344 345
    {
        $table = new Table('alter_table_add_pk');
        $table->addColumn('id', 'integer');
        $table->addColumn('foo', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
346
        $table->addIndex(['id'], 'idx_id');
347 348 349 350 351

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

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

354
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
355
            ['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
356
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
357 358 359
        );
    }

360
    /**
361
     * @group DBAL-1132
362
     */
363
    public function testAlterPrimaryKeyWithAutoincrementColumn() : void
364
    {
Sergei Morozov's avatar
Sergei Morozov committed
365 366
        $table = new Table('alter_primary_key');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
367
        $table->addColumn('foo', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
368
        $table->setPrimaryKey(['id']);
369 370

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
371
        $diffTable  = clone $table;
372 373

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

376
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
377
            [
Steve Müller's avatar
Steve Müller committed
378 379
                '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
380 381
                'ALTER TABLE alter_primary_key ADD PRIMARY KEY (foo)',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
382
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
383 384 385
        );
    }

386 387 388
    /**
     * @group DBAL-464
     */
389
    public function testDropPrimaryKeyWithAutoincrementColumn() : void
390
    {
Sergei Morozov's avatar
Sergei Morozov committed
391 392
        $table = new Table('drop_primary_key');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
andig's avatar
andig committed
393
        $table->addColumn('foo', 'integer');
394
        $table->addColumn('bar', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
395
        $table->setPrimaryKey(['id', 'foo']);
396 397

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
398
        $diffTable  = clone $table;
399 400 401

        $diffTable->dropPrimaryKey();

402
        self::assertEquals(
Sergei Morozov's avatar
Sergei Morozov committed
403
            [
404
                'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
Sergei Morozov's avatar
Sergei Morozov committed
405 406
                'ALTER TABLE drop_primary_key DROP PRIMARY KEY',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
407
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
408 409 410
        );
    }

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

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
423
        $diffTable  = clone $table;
424 425

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

428
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
429
            [
430 431 432
                '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
433
            ],
Sergei Morozov's avatar
Sergei Morozov committed
434
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
435 436 437
        );
    }

438 439 440
    /**
     * @group DBAL-2302
     */
441
    public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn() : void
442
    {
Sergei Morozov's avatar
Sergei Morozov committed
443 444
        $table = new Table('tbl');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
445 446
        $table->addColumn('foo', 'integer');
        $table->addColumn('bar', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
447
        $table->setPrimaryKey(['id']);
448 449

        $comparator = new Comparator();
Sergei Morozov's avatar
Sergei Morozov committed
450
        $diffTable  = clone $table;
451 452

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

455
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
456
            [
457 458 459
                '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
460
            ],
Sergei Morozov's avatar
Sergei Morozov committed
461
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
462 463 464
        );
    }

465 466 467
    /**
     * @group DBAL-586
     */
468
    public function testAddAutoIncrementPrimaryKey() : void
469
    {
Sergei Morozov's avatar
Sergei Morozov committed
470 471 472 473
        $keyTable = new Table('foo');
        $keyTable->addColumn('id', 'integer', ['autoincrement' => true]);
        $keyTable->addColumn('baz', 'string');
        $keyTable->setPrimaryKey(['id']);
474

Sergei Morozov's avatar
Sergei Morozov committed
475 476
        $oldTable = new Table('foo');
        $oldTable->addColumn('baz', 'string');
477

Sergei Morozov's avatar
Sergei Morozov committed
478
        $c    = new Comparator();
479 480
        $diff = $c->diffTable($oldTable, $keyTable);

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

Sergei Morozov's avatar
Sergei Morozov committed
483
        self::assertEquals(['ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)'], $sql);
484 485
    }

486
    public function testNamedPrimaryKey() : void
487
    {
Sergei Morozov's avatar
Sergei Morozov committed
488 489
        $diff                              = new TableDiff('mytable');
        $diff->changedIndexes['foo_index'] = new Index('foo_index', ['foo'], true, true);
490

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

Sergei Morozov's avatar
Sergei Morozov committed
493 494 495 496
        self::assertEquals([
            'ALTER TABLE mytable DROP PRIMARY KEY',
            'ALTER TABLE mytable ADD PRIMARY KEY (foo)',
        ], $sql);
497
    }
498

499
    public function testAlterPrimaryKeyWithNewColumn() : void
500
    {
Sergei Morozov's avatar
Sergei Morozov committed
501
        $table = new Table('yolo');
502 503
        $table->addColumn('pkc1', 'integer');
        $table->addColumn('col_a', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
504
        $table->setPrimaryKey(['pkc1']);
505 506

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

509 510
        $diffTable->addColumn('pkc2', 'integer');
        $diffTable->dropPrimaryKey();
Sergei Morozov's avatar
Sergei Morozov committed
511
        $diffTable->setPrimaryKey(['pkc1', 'pkc2']);
512

513
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
514
            [
515 516 517
                '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
518
            ],
Sergei Morozov's avatar
Sergei Morozov committed
519
            $this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
520
        );
521
    }
522

523
    public function testInitializesDoctrineTypeMappings() : void
524
    {
Sergei Morozov's avatar
Sergei Morozov committed
525 526
        self::assertTrue($this->platform->hasDoctrineTypeMappingFor('binary'));
        self::assertSame('binary', $this->platform->getDoctrineTypeMapping('binary'));
527

Sergei Morozov's avatar
Sergei Morozov committed
528 529
        self::assertTrue($this->platform->hasDoctrineTypeMappingFor('varbinary'));
        self::assertSame('binary', $this->platform->getDoctrineTypeMapping('varbinary'));
530 531
    }

532
    protected function getBinaryMaxLength() : int
533 534 535 536
    {
        return 65535;
    }

537
    public function testReturnsBinaryTypeDeclarationSQL() : void
538
    {
Sergei Morozov's avatar
Sergei Morozov committed
539 540 541
        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]));
542

Sergei Morozov's avatar
Sergei Morozov committed
543 544 545
        self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
        self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
        self::assertSame('BINARY(65535)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 65535]));
546 547 548 549
    }

    /**
     * @group legacy
550 551 552
     * @expectedDeprecation Binary field length 65536 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
     * @expectedDeprecation Binary field length 16777215 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
     * @expectedDeprecation Binary field length 16777216 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
553
     */
554
    public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() : void
555
    {
Sergei Morozov's avatar
Sergei Morozov committed
556 557 558
        self::assertSame('MEDIUMBLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
        self::assertSame('MEDIUMBLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 16777215]));
        self::assertSame('LONGBLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 16777216]));
559

Sergei Morozov's avatar
Sergei Morozov committed
560 561 562
        self::assertSame('MEDIUMBLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 65536]));
        self::assertSame('MEDIUMBLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777215]));
        self::assertSame('LONGBLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777216]));
563 564
    }

565
    public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines() : void
566
    {
Sergei Morozov's avatar
Sergei Morozov committed
567
        $table = new Table('foreign_table');
568 569
        $table->addColumn('id', 'integer');
        $table->addColumn('fk_id', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
570 571
        $table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
        $table->setPrimaryKey(['id']);
572 573
        $table->addOption('engine', 'MyISAM');

574
        self::assertSame(
575
            ['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
576
            $this->platform->getCreateTableSQL(
577 578 579 580 581 582 583 584
                $table,
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
            )
        );

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

585
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
586
            [
587
                '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
588 589
                'ALTER TABLE foreign_table ADD CONSTRAINT FK_5690FFE2A57719D0 FOREIGN KEY (fk_id) REFERENCES foreign_table (id)',
            ],
Sergei Morozov's avatar
Sergei Morozov committed
590
            $this->platform->getCreateTableSQL(
591 592 593 594 595 596
                $table,
                AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS
            )
        );
    }

597
    public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines() : void
598
    {
Sergei Morozov's avatar
Sergei Morozov committed
599
        $table = new Table('foreign_table');
600 601
        $table->addColumn('id', 'integer');
        $table->addColumn('fk_id', 'integer');
Sergei Morozov's avatar
Sergei Morozov committed
602 603
        $table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
        $table->setPrimaryKey(['id']);
604 605
        $table->addOption('engine', 'MyISAM');

Sergei Morozov's avatar
Sergei Morozov committed
606 607 608
        $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')];
609

Sergei Morozov's avatar
Sergei Morozov committed
610 611 612
        $tableDiff                     = new TableDiff('foreign_table');
        $tableDiff->fromTable          = $table;
        $tableDiff->addedForeignKeys   = $addedForeignKeys;
613 614 615
        $tableDiff->changedForeignKeys = $changedForeignKeys;
        $tableDiff->removedForeignKeys = $removedForeignKeys;

Sergei Morozov's avatar
Sergei Morozov committed
616
        self::assertEmpty($this->platform->getAlterTableSQL($tableDiff));
617 618 619

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

Sergei Morozov's avatar
Sergei Morozov committed
620 621 622
        $tableDiff                     = new TableDiff('foreign_table');
        $tableDiff->fromTable          = $table;
        $tableDiff->addedForeignKeys   = $addedForeignKeys;
623 624 625
        $tableDiff->changedForeignKeys = $changedForeignKeys;
        $tableDiff->removedForeignKeys = $removedForeignKeys;

626
        self::assertSame(
Sergei Morozov's avatar
Sergei Morozov committed
627
            [
628 629 630 631
                '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
632
            ],
Sergei Morozov's avatar
Sergei Morozov committed
633
            $this->platform->getAlterTableSQL($tableDiff)
634 635 636 637
        );
    }

    /**
638 639
     * @return string[]
     *
640 641
     * @group DBAL-234
     */
642
    protected function getAlterTableRenameIndexSQL() : array
643
    {
Sergei Morozov's avatar
Sergei Morozov committed
644
        return [
645 646
            'DROP INDEX idx_foo ON mytable',
            'CREATE INDEX idx_bar ON mytable (id)',
Sergei Morozov's avatar
Sergei Morozov committed
647
        ];
648 649 650
    }

    /**
651 652
     * @return string[]
     *
653 654
     * @group DBAL-234
     */
655
    protected function getQuotedAlterTableRenameIndexSQL() : array
656
    {
Sergei Morozov's avatar
Sergei Morozov committed
657
        return [
658 659 660 661
            '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
662
        ];
663
    }
664

665
    /**
666 667
     * @return string[]
     *
668 669
     * @group DBAL-807
     */
670
    protected function getAlterTableRenameIndexInSchemaSQL() : array
671
    {
Sergei Morozov's avatar
Sergei Morozov committed
672
        return [
673 674
            'DROP INDEX idx_foo ON myschema.mytable',
            'CREATE INDEX idx_bar ON myschema.mytable (id)',
Sergei Morozov's avatar
Sergei Morozov committed
675
        ];
676 677 678
    }

    /**
679 680
     * @return string[]
     *
681 682
     * @group DBAL-807
     */
683
    protected function getQuotedAlterTableRenameIndexInSchemaSQL() : array
684
    {
Sergei Morozov's avatar
Sergei Morozov committed
685
        return [
686 687 688 689
            '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
690
        ];
691 692
    }

693
    protected function getQuotesDropForeignKeySQL() : string
694 695 696 697
    {
        return 'ALTER TABLE `table` DROP FOREIGN KEY `select`';
    }

698
    protected function getQuotesDropConstraintSQL() : string
699 700 701 702
    {
        return 'ALTER TABLE `table` DROP CONSTRAINT `select`';
    }

703
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes() : void
704
    {
Sergei Morozov's avatar
Sergei Morozov committed
705 706 707 708 709
        $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']);
710

711
        self::assertSame(
712
            ['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
713
            $this->platform->getCreateTableSQL($table)
714 715 716
        );

        $diffTable = clone $table;
Sergei Morozov's avatar
Sergei Morozov committed
717 718 719 720
        $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]);
721 722 723

        $comparator = new Comparator();

Sergei Morozov's avatar
Sergei Morozov committed
724
        self::assertEmpty($this->platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
725
    }
726 727 728 729

    /**
     * {@inheritdoc}
     */
730
    protected function getQuotedAlterTableRenameColumnSQL() : array
731
    {
Sergei Morozov's avatar
Sergei Morozov committed
732
        return ['ALTER TABLE mytable ' .
733 734 735 736 737 738 739 740
            "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
741 742
            "CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'",
        ];
743
    }
744 745 746 747

    /**
     * {@inheritdoc}
     */
748
    protected function getQuotedAlterTableChangeColumnLengthSQL() : array
749
    {
Sergei Morozov's avatar
Sergei Morozov committed
750
        return ['ALTER TABLE mytable ' .
751 752 753 754 755
            "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
756 757
            "CHANGE `select` `select` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 3'",
        ];
758
    }
759 760 761 762

    /**
     * @group DBAL-423
     */
763
    public function testReturnsGuidTypeDeclarationSQL() : void
764
    {
Sergei Morozov's avatar
Sergei Morozov committed
765
        self::assertSame('CHAR(36)', $this->platform->getGuidTypeDeclarationSQL([]));
766
    }
767 768 769 770

    /**
     * {@inheritdoc}
     */
771
    public function getAlterTableRenameColumnSQL() : array
772
    {
Sergei Morozov's avatar
Sergei Morozov committed
773
        return ["ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'"];
774
    }
775 776 777 778

    /**
     * {@inheritdoc}
     */
779
    protected function getQuotesTableIdentifiersInAlterTableSQL() : array
780
    {
Sergei Morozov's avatar
Sergei Morozov committed
781
        return [
782 783 784 785 786 787
            '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
788
        ];
789
    }
790 791 792 793

    /**
     * {@inheritdoc}
     */
794
    protected function getCommentOnColumnSQL() : array
795
    {
Sergei Morozov's avatar
Sergei Morozov committed
796
        return [
797 798 799
            "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
800
        ];
801
    }
802 803 804 805

    /**
     * {@inheritdoc}
     */
806
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL() : string
807 808 809 810 811 812 813
    {
        return 'CONSTRAINT `select` UNIQUE (foo)';
    }

    /**
     * {@inheritdoc}
     */
814
    protected function getQuotesReservedKeywordInIndexDeclarationSQL() : string
815 816 817
    {
        return 'INDEX `select` (foo)';
    }
818

819 820 821
    /**
     * {@inheritdoc}
     */
822
    protected function getQuotesReservedKeywordInTruncateTableSQL() : string
823 824 825 826
    {
        return 'TRUNCATE `select`';
    }

827 828 829
    /**
     * {@inheritdoc}
     */
830
    protected function getAlterStringToFixedStringSQL() : array
831
    {
Sergei Morozov's avatar
Sergei Morozov committed
832
        return ['ALTER TABLE mytable CHANGE name name CHAR(2) NOT NULL'];
833
    }
834 835 836 837

    /**
     * {@inheritdoc}
     */
838
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL() : array
839
    {
Sergei Morozov's avatar
Sergei Morozov committed
840
        return [
841 842 843 844
            '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
845
        ];
846
    }
847 848 849 850

    /**
     * {@inheritdoc}
     */
851
    public static function getGeneratesDecimalTypeDeclarationSQL() : iterable
852
    {
Sergei Morozov's avatar
Sergei Morozov committed
853 854 855 856 857 858 859 860
        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)'],
        ];
861 862 863 864 865
    }

    /**
     * {@inheritdoc}
     */
866
    public static function getGeneratesFloatDeclarationSQL() : iterable
867
    {
Sergei Morozov's avatar
Sergei Morozov committed
868 869 870 871 872 873 874 875
        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'],
        ];
876
    }
877 878 879 880

    /**
     * @group DBAL-2436
     */
881
    public function testQuotesTableNameInListTableIndexesSQL() : void
882
    {
883 884 885 886
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableIndexesSQL("Foo'Bar\\", 'foo_db')
        );
887 888 889 890 891
    }

    /**
     * @group DBAL-2436
     */
892
    public function testQuotesDatabaseNameInListTableIndexesSQL() : void
893
    {
894 895 896 897
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableIndexesSQL('foo_table', "Foo'Bar\\")
        );
898 899 900 901 902
    }

    /**
     * @group DBAL-2436
     */
903
    public function testQuotesDatabaseNameInListViewsSQL() : void
904
    {
905 906 907 908
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListViewsSQL("Foo'Bar\\")
        );
909 910 911 912 913
    }

    /**
     * @group DBAL-2436
     */
914
    public function testQuotesTableNameInListTableForeignKeysSQL() : void
915
    {
916 917 918 919
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableForeignKeysSQL("Foo'Bar\\")
        );
920 921 922 923 924
    }

    /**
     * @group DBAL-2436
     */
925
    public function testQuotesDatabaseNameInListTableForeignKeysSQL() : void
926
    {
927 928 929 930
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableForeignKeysSQL('foo_table', "Foo'Bar\\")
        );
931 932 933 934 935
    }

    /**
     * @group DBAL-2436
     */
936
    public function testQuotesTableNameInListTableColumnsSQL() : void
937
    {
938 939 940 941
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableColumnsSQL("Foo'Bar\\")
        );
942 943 944 945 946
    }

    /**
     * @group DBAL-2436
     */
947
    public function testQuotesDatabaseNameInListTableColumnsSQL() : void
948
    {
949 950 951 952
        self::assertStringContainsStringIgnoringCase(
            "'Foo''Bar\\\\'",
            $this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\")
        );
953
    }
954

955
    public function testListTableForeignKeysSQLEvaluatesDatabase() : void
956
    {
Sergei Morozov's avatar
Sergei Morozov committed
957
        $sql = $this->platform->getListTableForeignKeysSQL('foo');
958

959
        self::assertStringContainsString('DATABASE()', $sql);
960

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

963 964
        self::assertStringContainsString('bar', $sql);
        self::assertStringNotContainsString('DATABASE()', $sql);
965
    }
966

967 968 969 970 971 972 973 974
    public function testColumnCharsetDeclarationSQL() : void
    {
        self::assertSame(
            'CHARACTER SET ascii',
            $this->platform->getColumnCharsetDeclarationSQL('ascii')
        );
    }

975 976
    public function testSupportsColumnCollation() : void
    {
Sergei Morozov's avatar
Sergei Morozov committed
977
        self::assertTrue($this->platform->supportsColumnCollation());
978 979 980 981 982
    }

    public function testColumnCollationDeclarationSQL() : void
    {
        self::assertSame(
983
            'COLLATE `ascii_general_ci`',
Sergei Morozov's avatar
Sergei Morozov committed
984
            $this->platform->getColumnCollationDeclarationSQL('ascii_general_ci')
985 986 987 988 989 990 991 992 993 994
        );
    }

    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(
995
            ['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
996
            $this->platform->getCreateTableSQL($table),
997 998 999
            'Column "no_collation" will use the default collation from the table/database and "column_collation" overwrites the collation on this column'
        );
    }
1000
}