MySqlPlatformTest.php 13.3 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\TableDiff;
9
use Doctrine\DBAL\Schema\Schema;
10
use Doctrine\DBAL\Schema\Index;
Benjamin Eberlei's avatar
Benjamin Eberlei committed
11
use Doctrine\DBAL\Schema\Comparator;
12

13
class MySqlPlatformTest extends AbstractPlatformTestCase
14
{
15
    public function createPlatform()
16
    {
17
        return new MysqlPlatform;
18 19
    }

20 21 22 23 24 25
    public function testModifyLimitQueryWitoutLimit()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT n FROM Foo', null , 10);
        $this->assertEquals('SELECT n FROM Foo LIMIT 18446744073709551615 OFFSET 10',$sql);
    }

26 27
    public function testGenerateMixedCaseTableCreate()
    {
28
        $table = new Table("Foo");
29
        $table->addColumn("Bar", "integer");
30

31
        $sql = $this->_platform->getCreateTableSQL($table);
VladanStef's avatar
VladanStef committed
32
        $this->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()
36
    {
VladanStef's avatar
VladanStef committed
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
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
        return array(
VladanStef's avatar
VladanStef committed
43
            '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'
44 45 46
        );
    }

47
    public function getGenerateAlterTableSql()
48
    {
49
        return array(
50
            "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"
51 52 53
        );
    }

54
    public function testGeneratesSqlSnippets()
55
    {
56 57 58
        $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
        $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
        $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
59 60
    }

61
    public function testGeneratesTransactionsCommands()
62 63 64
    {
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
65
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
66
            ''
67 68 69
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
70
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
71 72 73
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
74
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
75 76 77
        );
        $this->assertEquals(
            'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
78
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
79 80 81
        );
    }

82 83

    public function testGeneratesDDLSnippets()
84
    {
85
        $this->assertEquals('SHOW DATABASES', $this->_platform->getListDatabasesSQL());
86 87 88
        $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
        $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
        $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
89 90
    }

91
    public function testGeneratesTypeDeclarationForIntegers()
92 93 94
    {
        $this->assertEquals(
            'INT',
95
            $this->_platform->getIntegerTypeDeclarationSQL(array())
96 97 98
        );
        $this->assertEquals(
            'INT AUTO_INCREMENT',
99
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
100 101 102
        ));
        $this->assertEquals(
            'INT AUTO_INCREMENT',
103
            $this->_platform->getIntegerTypeDeclarationSQL(
104 105
                array('autoincrement' => true, 'primary' => true)
        ));
106 107 108 109
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
110 111
        $this->assertEquals(
            'CHAR(10)',
112
            $this->_platform->getVarcharTypeDeclarationSQL(
113 114 115 116
                array('length' => 10, 'fixed' => true)
        ));
        $this->assertEquals(
            'VARCHAR(50)',
117
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
118
            'Variable string declaration is not correct'
119 120
        );
        $this->assertEquals(
121
            'VARCHAR(255)',
122
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
123
            'Long string declaration is not correct'
124 125 126
        );
    }

127
    public function testPrefersIdentityColumns()
128 129
    {
        $this->assertTrue($this->_platform->prefersIdentityColumns());
130 131 132 133
    }

    public function testSupportsIdentityColumns()
    {
134 135 136
        $this->assertTrue($this->_platform->supportsIdentityColumns());
    }

137
    public function testDoesSupportSavePoints()
138
    {
139
        $this->assertTrue($this->_platform->supportsSavepoints());
140 141
    }

142
    public function getGenerateIndexSql()
143
    {
144
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
145 146
    }

147
    public function getGenerateUniqueIndexSql()
148
    {
149
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
150 151
    }

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

157 158 159 160
    /**
     * @group DBAL-126
     */
    public function testUniquePrimaryKey()
161
    {
162 163 164 165 166
        $keyTable = new Table("foo");
        $keyTable->addColumn("bar", "integer");
        $keyTable->addColumn("baz", "string");
        $keyTable->setPrimaryKey(array("bar"));
        $keyTable->addUniqueIndex(array("baz"));
167

168 169 170
        $oldTable = new Table("foo");
        $oldTable->addColumn("bar", "integer");
        $oldTable->addColumn("baz", "string");
171

172 173
        $c = new \Doctrine\DBAL\Schema\Comparator;
        $diff = $c->diffTable($oldTable, $keyTable);
174

175
        $sql = $this->_platform->getAlterTableSQL($diff);
176

177 178 179 180 181
        $this->assertEquals(array(
            "ALTER TABLE foo ADD PRIMARY KEY (bar)",
            "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
        ), $sql);
    }
182 183 184 185

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
186
        $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
187 188 189 190 191 192 193
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
    }
194 195 196 197 198 199

    /**
     * @group DDC-118
     */
    public function testGetDateTimeTypeDeclarationSql()
    {
200 201 202
        $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
        $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
        $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
203
    }
204 205 206

    public function getCreateTableColumnCommentsSQL()
    {
VladanStef's avatar
VladanStef committed
207
        return array("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");
208 209 210 211
    }

    public function getAlterTableColumnCommentsSQL()
    {
212
        return array("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'");
213
    }
214 215 216

    public function getCreateTableColumnTypeCommentsSQL()
    {
VladanStef's avatar
VladanStef committed
217
        return array("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");
218
    }
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

    /**
     * @group DBAL-237
     */
    public function testChangeIndexWithForeignKeys()
    {
        $index = new Index("idx", array("col"), false);
        $unique = new Index("uniq", array("col"), true);

        $diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
        $sql = $this->_platform->getAlterTableSQL($diff);
        $this->assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);

        $diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
        $sql = $this->_platform->getAlterTableSQL($diff);
        $this->assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $sql);
    }
236 237 238 239

    protected function getQuotedColumnInPrimaryKeySQL()
    {
        return array(
240
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, PRIMARY KEY(`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
241 242 243 244 245 246
        );
    }

    protected function getQuotedColumnInIndexSQL()
    {
        return array(
247 248 249 250 251 252 253
            'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB (`create`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
        );
    }

    protected function getQuotedColumnInForeignKeySQL()
    {
        return array(
254 255 256 257
            '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`)',
258 259
        );
    }
260 261 262 263 264 265 266 267 268 269 270 271 272 273

    public function testCreateTableWithFulltextIndex()
    {
        $table = new Table('fulltext_table');
        $table->addOption('engine', 'MyISAM');
        $table->addColumn('text', 'text');
        $table->addIndex(array('text'), 'fulltext_text');

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

        $sql = $this->_platform->getCreateTableSQL($table);
        $this->assertEquals(array('CREATE TABLE fulltext_table (text LONGTEXT NOT NULL, FULLTEXT INDEX fulltext_text (text)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM'), $sql);
    }
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

    public function testClobTypeDeclarationSQL()
    {
        $this->assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 1)));
        $this->assertEquals('TINYTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 255)));
        $this->assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 256)));
        $this->assertEquals('TEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65535)));
        $this->assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 65536)));
        $this->assertEquals('MEDIUMTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777215)));
        $this->assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array('length' => 16777216)));
        $this->assertEquals('LONGTEXT', $this->_platform->getClobTypeDeclarationSQL(array()));
    }

    public function testBlobTypeDeclarationSQL()
    {
        $this->assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 1)));
        $this->assertEquals('TINYBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 255)));
        $this->assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 256)));
        $this->assertEquals('BLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65535)));
        $this->assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 65536)));
        $this->assertEquals('MEDIUMBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777215)));
        $this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
        $this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
    }
298 299

    /**
Steve Müller's avatar
Steve Müller committed
300
     * @group DBAL-464
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
     */
    public function testDropPrimaryKeyWithAutoincrementColumn()
    {
        $table = new Table("drop_primary_key");
        $table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
        $table->addColumn('foo', 'integer', array('primary' => true));
        $table->addColumn('bar', 'integer');
        $table->setPrimaryKey(array('id', 'foo'));

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

        $diffTable->dropPrimaryKey();

        $this->assertEquals(
            array(
                'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
                'ALTER TABLE drop_primary_key DROP PRIMARY KEY'
            ),
            $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
        );
    }
323
}