3 namespace Doctrine\Tests\DBAL\Platforms;
5 use Doctrine\DBAL\Platforms\SQLServer2008Platform;
6 use Doctrine\DBAL\Types\Type;
8 class SQLServerPlatformTest extends AbstractPlatformTestCase
10 public function createPlatform()
12 return new SQLServer2008Platform;
15 public function getGenerateTableSql()
17 return 'CREATE TABLE test (id INT IDENTITY NOT NULL, test NVARCHAR(255) DEFAULT NULL, PRIMARY KEY (id))';
20 public function getGenerateTableWithMultiColumnUniqueIndexSql()
23 'CREATE TABLE test (foo NVARCHAR(255) DEFAULT NULL, bar NVARCHAR(255) DEFAULT NULL)',
24 'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar) WHERE foo IS NOT NULL AND bar IS NOT NULL'
28 public function getGenerateAlterTableSql()
31 'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
32 'ALTER TABLE mytable DROP COLUMN foo',
33 'ALTER TABLE mytable ALTER COLUMN baz NVARCHAR(255) DEFAULT \'def\' NOT NULL',
34 'ALTER TABLE mytable ALTER COLUMN bloo BIT DEFAULT \'0\' NOT NULL',
35 "sp_RENAME 'mytable', 'userlist'",
39 public function testGeneratesSqlSnippets()
41 $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
42 $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
43 $this->assertEquals('(column1 + column2 + column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
46 public function testGeneratesTransactionsCommands()
49 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
50 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
53 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
54 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
57 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
58 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
61 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
62 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
66 public function testGeneratesDDLSnippets()
68 $dropDatabaseExpectation = 'DROP DATABASE foobar';
70 $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
71 $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
72 $this->assertEquals($dropDatabaseExpectation, $this->_platform->getDropDatabaseSQL('foobar'));
73 $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
76 public function testGeneratesTypeDeclarationForIntegers()
80 $this->_platform->getIntegerTypeDeclarationSQL(array())
84 $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
88 $this->_platform->getIntegerTypeDeclarationSQL(
89 array('autoincrement' => true, 'primary' => true)
93 public function testGeneratesTypeDeclarationsForStrings()
97 $this->_platform->getVarcharTypeDeclarationSQL(
98 array('length' => 10, 'fixed' => true)
102 $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
103 'Variable string declaration is not correct'
107 $this->_platform->getVarcharTypeDeclarationSQL(array()),
108 'Long string declaration is not correct'
112 public function testPrefersIdentityColumns()
114 $this->assertTrue($this->_platform->prefersIdentityColumns());
117 public function testSupportsIdentityColumns()
119 $this->assertTrue($this->_platform->supportsIdentityColumns());
122 public function testDoesNotSupportSavePoints()
124 $this->assertTrue($this->_platform->supportsSavepoints());
127 public function getGenerateIndexSql()
129 return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
132 public function getGenerateUniqueIndexSql()
134 return 'CREATE UNIQUE INDEX index_name ON test (test, test2) WHERE test IS NOT NULL AND test2 IS NOT NULL';
137 public function getGenerateForeignKeySql()
139 return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
142 public function testModifyLimitQuery()
144 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
145 $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
148 public function testModifyLimitQueryWithEmptyOffset()
150 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
151 $this->assertEquals('SELECT TOP 10 * FROM user', $sql);
154 public function testModifyLimitQueryWithOffset()
156 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
157 $this->assertEquals('SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum, * FROM user) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15', $sql);
160 public function testModifyLimitQueryWithAscOrderBy()
162 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
163 $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username ASC', $sql);
166 public function testModifyLimitQueryWithDescOrderBy()
168 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
169 $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
175 public function testQuoteIdentifier()
177 $this->assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
178 $this->assertEquals('[test]', $this->_platform->quoteIdentifier('test'));
179 $this->assertEquals('[test].[test]', $this->_platform->quoteIdentifier('test.test'));
185 public function testQuoteSingleIdentifier()
187 $this->assertEquals('[fo][o]', $this->_platform->quoteSingleIdentifier('fo]o'));
188 $this->assertEquals('[test]', $this->_platform->quoteSingleIdentifier('test'));
189 $this->assertEquals('[test.test]', $this->_platform->quoteSingleIdentifier('test.test'));
195 public function testCreateClusteredIndex()
197 $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'));
198 $idx->addFlag('clustered');
199 $this->assertEquals('CREATE CLUSTERED INDEX idx ON tbl (id)', $this->_platform->getCreateIndexSQL($idx, 'tbl'));
205 public function testCreateNonClusteredPrimaryKeyInTable()
207 $table = new \Doctrine\DBAL\Schema\Table("tbl");
208 $table->addColumn("id", "integer");
209 $table->setPrimaryKey(Array("id"));
210 $table->getIndex('primary')->addFlag('nonclustered');
212 $this->assertEquals(array('CREATE TABLE tbl (id INT NOT NULL, PRIMARY KEY NONCLUSTERED (id))'), $this->_platform->getCreateTableSQL($table));
218 public function testCreateNonClusteredPrimaryKey()
220 $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'), false, true);
221 $idx->addFlag('nonclustered');
222 $this->assertEquals('ALTER TABLE tbl ADD PRIMARY KEY NONCLUSTERED (id)', $this->_platform->getCreatePrimaryKeySQL($idx, 'tbl'));