MySqlPlatformTest.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Platforms;
  3. use Doctrine\DBAL\Platforms\MySqlPlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\Schema;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. class MySqlPlatformTest extends AbstractPlatformTestCase
  9. {
  10. public function createPlatform()
  11. {
  12. return new MysqlPlatform;
  13. }
  14. public function testGenerateMixedCaseTableCreate()
  15. {
  16. $table = new Table("Foo");
  17. $table->addColumn("Bar", "integer");
  18. $sql = $this->_platform->getCreateTableSQL($table);
  19. $this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) ENGINE = InnoDB', array_shift($sql));
  20. }
  21. public function getGenerateTableSql()
  22. {
  23. return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB';
  24. }
  25. public function getGenerateTableWithMultiColumnUniqueIndexSql()
  26. {
  27. return array(
  28. 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) ENGINE = InnoDB'
  29. );
  30. }
  31. public function getGenerateAlterTableSql()
  32. {
  33. return array(
  34. "ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL"
  35. );
  36. }
  37. public function testGeneratesSqlSnippets()
  38. {
  39. $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
  40. $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
  41. $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
  42. }
  43. public function testGeneratesTransactionsCommands()
  44. {
  45. $this->assertEquals(
  46. 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
  47. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
  48. ''
  49. );
  50. $this->assertEquals(
  51. 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
  52. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
  53. );
  54. $this->assertEquals(
  55. 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
  56. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
  57. );
  58. $this->assertEquals(
  59. 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
  60. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
  61. );
  62. }
  63. public function testGeneratesDDLSnippets()
  64. {
  65. $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
  66. $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
  67. $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
  68. $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
  69. }
  70. public function testGeneratesTypeDeclarationForIntegers()
  71. {
  72. $this->assertEquals(
  73. 'INT',
  74. $this->_platform->getIntegerTypeDeclarationSQL(array())
  75. );
  76. $this->assertEquals(
  77. 'INT AUTO_INCREMENT',
  78. $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
  79. ));
  80. $this->assertEquals(
  81. 'INT AUTO_INCREMENT',
  82. $this->_platform->getIntegerTypeDeclarationSQL(
  83. array('autoincrement' => true, 'primary' => true)
  84. ));
  85. }
  86. public function testGeneratesTypeDeclarationForStrings()
  87. {
  88. $this->assertEquals(
  89. 'CHAR(10)',
  90. $this->_platform->getVarcharTypeDeclarationSQL(
  91. array('length' => 10, 'fixed' => true)
  92. ));
  93. $this->assertEquals(
  94. 'VARCHAR(50)',
  95. $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
  96. 'Variable string declaration is not correct'
  97. );
  98. $this->assertEquals(
  99. 'VARCHAR(255)',
  100. $this->_platform->getVarcharTypeDeclarationSQL(array()),
  101. 'Long string declaration is not correct'
  102. );
  103. }
  104. public function testPrefersIdentityColumns()
  105. {
  106. $this->assertTrue($this->_platform->prefersIdentityColumns());
  107. }
  108. public function testSupportsIdentityColumns()
  109. {
  110. $this->assertTrue($this->_platform->supportsIdentityColumns());
  111. }
  112. public function testDoesSupportSavePoints()
  113. {
  114. $this->assertTrue($this->_platform->supportsSavepoints());
  115. }
  116. public function getGenerateIndexSql()
  117. {
  118. return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
  119. }
  120. public function getGenerateUniqueIndexSql()
  121. {
  122. return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
  123. }
  124. public function getGenerateForeignKeySql()
  125. {
  126. return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
  127. }
  128. /**
  129. * @group DBAL-126
  130. */
  131. public function testUniquePrimaryKey()
  132. {
  133. $keyTable = new Table("foo");
  134. $keyTable->addColumn("bar", "integer");
  135. $keyTable->addColumn("baz", "string");
  136. $keyTable->setPrimaryKey(array("bar"));
  137. $keyTable->addUniqueIndex(array("baz"));
  138. $oldTable = new Table("foo");
  139. $oldTable->addColumn("bar", "integer");
  140. $oldTable->addColumn("baz", "string");
  141. $c = new \Doctrine\DBAL\Schema\Comparator;
  142. $diff = $c->diffTable($oldTable, $keyTable);
  143. $sql = $this->_platform->getAlterTableSQL($diff);
  144. $this->assertEquals(array(
  145. "ALTER TABLE foo ADD PRIMARY KEY (bar)",
  146. "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
  147. ), $sql);
  148. }
  149. public function testModifyLimitQuery()
  150. {
  151. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
  152. $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
  153. }
  154. public function testModifyLimitQueryWithEmptyOffset()
  155. {
  156. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
  157. $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
  158. }
  159. /**
  160. * @group DDC-118
  161. */
  162. public function testGetDateTimeTypeDeclarationSql()
  163. {
  164. $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
  165. $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
  166. $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
  167. }
  168. public function getCreateTableColumnCommentsSQL()
  169. {
  170. return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) ENGINE = InnoDB");
  171. }
  172. public function getAlterTableColumnCommentsSQL()
  173. {
  174. return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
  175. }
  176. public function getCreateTableColumnTypeCommentsSQL()
  177. {
  178. return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) ENGINE = InnoDB");
  179. }
  180. }