3 namespace Doctrine\Tests\DBAL\Platforms;
5 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
6 use Doctrine\DBAL\Types\Type;
8 class PostgreSqlPlatformTest extends AbstractPlatformTestCase
10 public function createPlatform()
12 return new PostgreSqlPlatform;
15 public function getGenerateTableSql()
17 return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
20 public function getGenerateTableWithMultiColumnUniqueIndexSql()
23 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
24 'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)'
28 public function getGenerateAlterTableSql()
31 'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
32 'ALTER TABLE mytable DROP foo',
33 'ALTER TABLE mytable ALTER bar TYPE VARCHAR(255)',
34 "ALTER TABLE mytable ALTER bar SET DEFAULT 'def'",
35 'ALTER TABLE mytable ALTER bar SET NOT NULL',
36 'ALTER TABLE mytable ALTER bloo TYPE BOOLEAN',
37 "ALTER TABLE mytable ALTER bloo SET DEFAULT 'false'",
38 'ALTER TABLE mytable ALTER bloo SET NOT NULL',
39 'ALTER TABLE mytable RENAME TO userlist',
43 public function getGenerateIndexSql()
45 return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
48 public function getGenerateForeignKeySql()
50 return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id) NOT DEFERRABLE INITIALLY IMMEDIATE';
53 public function testGeneratesForeignKeySqlForNonStandardOptions()
55 $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
56 array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
59 "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
60 $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
64 public function testGeneratesSqlSnippets()
66 $this->assertEquals('SIMILAR TO', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
67 $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
68 $this->assertEquals('column1 || column2 || column3', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
69 $this->assertEquals('SUBSTR(column, 5)', $this->_platform->getSubstringExpression('column', 5), 'Substring expression without length is not correct');
70 $this->assertEquals('SUBSTR(column, 0, 5)', $this->_platform->getSubstringExpression('column', 0, 5), 'Substring expression with length is not correct');
73 public function testGeneratesTransactionCommands()
76 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
77 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
80 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
81 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
84 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
85 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
88 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
89 $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
93 public function testGeneratesDDLSnippets()
95 $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
96 $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
97 $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
100 public function testGenerateTableWithAutoincrement()
102 $table = new \Doctrine\DBAL\Schema\Table('autoinc_table');
103 $column = $table->addColumn('id', 'integer');
104 $column->setAutoincrement(true);
106 $this->assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
109 public function testGeneratesTypeDeclarationForIntegers()
113 $this->_platform->getIntegerTypeDeclarationSQL(array())
117 $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
121 $this->_platform->getIntegerTypeDeclarationSQL(
122 array('autoincrement' => true, 'primary' => true)
126 public function testGeneratesTypeDeclarationForStrings()
130 $this->_platform->getVarcharTypeDeclarationSQL(
131 array('length' => 10, 'fixed' => true))
135 $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
136 'Variable string declaration is not correct'
140 $this->_platform->getVarcharTypeDeclarationSQL(array()),
141 'Long string declaration is not correct'
145 public function getGenerateUniqueIndexSql()
147 return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
150 public function testGeneratesSequenceSqlCommands()
152 $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
154 'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
155 $this->_platform->getCreateSequenceSQL($sequence)
158 'DROP SEQUENCE myseq',
159 $this->_platform->getDropSequenceSQL('myseq')
162 "SELECT NEXTVAL('myseq')",
163 $this->_platform->getSequenceNextValSQL('myseq')
167 public function testDoesNotPreferIdentityColumns()
169 $this->assertFalse($this->_platform->prefersIdentityColumns());
172 public function testPrefersSequences()
174 $this->assertTrue($this->_platform->prefersSequences());
177 public function testSupportsIdentityColumns()
179 $this->assertTrue($this->_platform->supportsIdentityColumns());
182 public function testSupportsSavePoints()
184 $this->assertTrue($this->_platform->supportsSavepoints());
187 public function testSupportsSequences()
189 $this->assertTrue($this->_platform->supportsSequences());
192 public function testModifyLimitQuery()
194 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
195 $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
198 public function testModifyLimitQueryWithEmptyOffset()
200 $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
201 $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
204 public function getCreateTableColumnCommentsSQL()
207 "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
208 "COMMENT ON COLUMN test.id IS 'This is a comment'",
212 public function getAlterTableColumnCommentsSQL()
215 "ALTER TABLE mytable ADD quota INT NOT NULL",
216 "COMMENT ON COLUMN mytable.quota IS 'A comment'",
217 "COMMENT ON COLUMN mytable.baz IS 'B comment'",
221 public function getCreateTableColumnTypeCommentsSQL()
224 "CREATE TABLE test (id INT NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))",
225 "COMMENT ON COLUMN test.data IS '(DC2Type:array)'"