3 namespace Doctrine\Tests\DBAL\Platforms;
5 use Doctrine\DBAL\Platforms\MySqlPlatform;
6 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;
12 class MySqlPlatformTest extends AbstractPlatformTestCase
14 public function createPlatform()
16 return new MysqlPlatform;
19 public function testGenerateMixedCaseTableCreate()
21 $table = new Table("Foo");
22 $table->addColumn("Bar", "integer");
24 $sql = $this->_platform->getCreateTableSQL($table);
25 $this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', array_shift($sql));
28 public function getGenerateTableSql()
30 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';
33 public function getGenerateTableWithMultiColumnUniqueIndexSql()
36 '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'
40 public function getGenerateAlterTableSql()
43 "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"
47 public function testGeneratesSqlSnippets()
49 $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
50 $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
51 $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
54 public function testGeneratesTransactionsCommands()
57 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
58 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
62 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
63 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
66 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
67 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
70 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
71 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
76 public function testGeneratesDDLSnippets()
78 $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
79 $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
80 $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
81 $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
84 public function testGeneratesTypeDeclarationForIntegers()
88 $this->_platform->getIntegerTypeDeclarationSQL(array())
92 $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
96 $this->_platform->getIntegerTypeDeclarationSQL(
97 array('autoincrement' => true, 'primary' => true)
101 public function testGeneratesTypeDeclarationForStrings()
105 $this->_platform->getVarcharTypeDeclarationSQL(
106 array('length' => 10, 'fixed' => true)
110 $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
111 'Variable string declaration is not correct'
115 $this->_platform->getVarcharTypeDeclarationSQL(array()),
116 'Long string declaration is not correct'
120 public function testPrefersIdentityColumns()
122 $this->assertTrue($this->_platform->prefersIdentityColumns());
125 public function testSupportsIdentityColumns()
127 $this->assertTrue($this->_platform->supportsIdentityColumns());
130 public function testDoesSupportSavePoints()
132 $this->assertTrue($this->_platform->supportsSavepoints());
135 public function getGenerateIndexSql()
137 return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
140 public function getGenerateUniqueIndexSql()
142 return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
145 public function getGenerateForeignKeySql()
147 return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
153 public function testUniquePrimaryKey()
155 $keyTable = new Table("foo");
156 $keyTable->addColumn("bar", "integer");
157 $keyTable->addColumn("baz", "string");
158 $keyTable->setPrimaryKey(array("bar"));
159 $keyTable->addUniqueIndex(array("baz"));
161 $oldTable = new Table("foo");
162 $oldTable->addColumn("bar", "integer");
163 $oldTable->addColumn("baz", "string");
165 $c = new \Doctrine\DBAL\Schema\Comparator;
166 $diff = $c->diffTable($oldTable, $keyTable);
168 $sql = $this->_platform->getAlterTableSQL($diff);
170 $this->assertEquals(array(
171 "ALTER TABLE foo ADD PRIMARY KEY (bar)",
172 "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
176 public function testModifyLimitQuery()
178 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
179 $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
182 public function testModifyLimitQueryWithEmptyOffset()
184 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
185 $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
191 public function testGetDateTimeTypeDeclarationSql()
193 $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
194 $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
195 $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
198 public function getCreateTableColumnCommentsSQL()
200 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");
203 public function getAlterTableColumnCommentsSQL()
205 return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
208 public function getCreateTableColumnTypeCommentsSQL()
210 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");
216 public function testChangeIndexWithForeignKeys()
218 $index = new Index("idx", array("col"), false);
219 $unique = new Index("uniq", array("col"), true);
221 $diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
222 $sql = $this->_platform->getAlterTableSQL($diff);
223 $this->assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);
225 $diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
226 $sql = $this->_platform->getAlterTableSQL($diff);
227 $this->assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $sql);