MsSqlPlatformTest.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Platforms;
  3. use Doctrine\DBAL\Platforms\MsSqlPlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class MsSqlPlatformTest extends AbstractPlatformTestCase
  7. {
  8. public function createPlatform()
  9. {
  10. return new MsSqlPlatform;
  11. }
  12. public function getGenerateTableSql()
  13. {
  14. return 'CREATE TABLE test (id INT IDENTITY NOT NULL, test NVARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
  15. }
  16. public function getGenerateTableWithMultiColumnUniqueIndexSql()
  17. {
  18. return array(
  19. 'CREATE TABLE test (foo NVARCHAR(255) DEFAULT NULL, bar NVARCHAR(255) DEFAULT NULL)',
  20. 'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar) WHERE foo IS NOT NULL AND bar IS NOT NULL'
  21. );
  22. }
  23. public function getGenerateAlterTableSql()
  24. {
  25. return array(
  26. 'ALTER TABLE mytable RENAME TO userlist',
  27. 'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
  28. 'ALTER TABLE mytable DROP COLUMN foo',
  29. 'ALTER TABLE mytable CHANGE bar baz NVARCHAR(255) DEFAULT \'def\' NOT NULL',
  30. );
  31. }
  32. public function testGeneratesSqlSnippets()
  33. {
  34. $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
  35. $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
  36. $this->assertEquals('(column1 + column2 + column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
  37. }
  38. public function testGeneratesTransactionsCommands()
  39. {
  40. $this->assertEquals(
  41. 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
  42. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
  43. );
  44. $this->assertEquals(
  45. 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
  46. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
  47. );
  48. $this->assertEquals(
  49. 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
  50. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
  51. );
  52. $this->assertEquals(
  53. 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
  54. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
  55. );
  56. }
  57. public function testGeneratesDDLSnippets()
  58. {
  59. $dropDatabaseExpectation = 'DROP DATABASE foobar';
  60. $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
  61. $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
  62. $this->assertEquals($dropDatabaseExpectation, $this->_platform->getDropDatabaseSQL('foobar'));
  63. $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
  64. }
  65. public function testGeneratesTypeDeclarationForIntegers()
  66. {
  67. $this->assertEquals(
  68. 'INT',
  69. $this->_platform->getIntegerTypeDeclarationSQL(array())
  70. );
  71. $this->assertEquals(
  72. 'INT IDENTITY',
  73. $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
  74. ));
  75. $this->assertEquals(
  76. 'INT IDENTITY',
  77. $this->_platform->getIntegerTypeDeclarationSQL(
  78. array('autoincrement' => true, 'primary' => true)
  79. ));
  80. }
  81. public function testGeneratesTypeDeclarationsForStrings()
  82. {
  83. $this->assertEquals(
  84. 'NCHAR(10)',
  85. $this->_platform->getVarcharTypeDeclarationSQL(
  86. array('length' => 10, 'fixed' => true)
  87. ));
  88. $this->assertEquals(
  89. 'NVARCHAR(50)',
  90. $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
  91. 'Variable string declaration is not correct'
  92. );
  93. $this->assertEquals(
  94. 'NVARCHAR(255)',
  95. $this->_platform->getVarcharTypeDeclarationSQL(array()),
  96. 'Long string declaration is not correct'
  97. );
  98. }
  99. public function testPrefersIdentityColumns()
  100. {
  101. $this->assertTrue($this->_platform->prefersIdentityColumns());
  102. }
  103. public function testSupportsIdentityColumns()
  104. {
  105. $this->assertTrue($this->_platform->supportsIdentityColumns());
  106. }
  107. public function testDoesNotSupportSavePoints()
  108. {
  109. $this->assertTrue($this->_platform->supportsSavepoints());
  110. }
  111. public function getGenerateIndexSql()
  112. {
  113. return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
  114. }
  115. public function getGenerateUniqueIndexSql()
  116. {
  117. return 'CREATE UNIQUE INDEX index_name ON test (test, test2) WHERE test IS NOT NULL AND test2 IS NOT NULL';
  118. }
  119. public function getGenerateForeignKeySql()
  120. {
  121. return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
  122. }
  123. public function testModifyLimitQuery()
  124. {
  125. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
  126. $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
  127. }
  128. public function testModifyLimitQueryWithEmptyOffset()
  129. {
  130. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
  131. $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
  132. }
  133. public function testModifyLimitQueryWithOffset()
  134. {
  135. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
  136. $this->assertEquals('WITH outer_tbl AS (SELECT ROW_NUMBER() OVER (ORDER BY username DESC) AS "doctrine_rownum", * FROM (SELECT * FROM user) AS inner_tbl) SELECT * FROM outer_tbl WHERE "doctrine_rownum" BETWEEN 6 AND 15', $sql);
  137. }
  138. public function testModifyLimitQueryWithAscOrderBy()
  139. {
  140. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
  141. $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username ASC', $sql);
  142. }
  143. public function testModifyLimitQueryWithDescOrderBy()
  144. {
  145. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
  146. $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
  147. }
  148. }