PostgreSqlPlatformTest.php 11.8 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Types\Type;

8
class PostgreSqlPlatformTest extends AbstractPlatformTestCase
9
{
10
    public function createPlatform()
11
    {
12
        return new PostgreSqlPlatform;
13 14
    }

15
    public function getGenerateTableSql()
16
    {
17
        return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
18 19
    }

20 21 22 23
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
    {
        return array(
            'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
24
            'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)'
25 26 27
        );
    }

28
    public function getGenerateAlterTableSql()
29
    {
30
        return array(
31
            'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
32 33 34 35
            'ALTER TABLE mytable DROP foo',
            'ALTER TABLE mytable ALTER bar TYPE VARCHAR(255)',
            "ALTER TABLE mytable ALTER bar SET  DEFAULT 'def'",
            'ALTER TABLE mytable ALTER bar SET NOT NULL',
36 37 38
            'ALTER TABLE mytable ALTER bloo TYPE BOOLEAN',
            "ALTER TABLE mytable ALTER bloo SET  DEFAULT 'false'",
            'ALTER TABLE mytable ALTER bloo SET NOT NULL',
39 40 41 42
            'ALTER TABLE mytable RENAME TO userlist',
        );
    }

43
    public function getGenerateIndexSql()
44
    {
45 46
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
    }
47

48 49
    public function getGenerateForeignKeySql()
    {
50
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id) NOT DEFERRABLE INITIALLY IMMEDIATE';
51 52
    }

53 54
    public function testGeneratesForeignKeySqlForNonStandardOptions()
    {
55 56
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
                array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
57 58
        );
        $this->assertEquals(
59
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
60
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
61
        );
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('match' => 'full')
        );
        $this->assertEquals(
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) MATCH full NOT DEFERRABLE INITIALLY IMMEDIATE",
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
        );

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferrable' => true)
        );
        $this->assertEquals(
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) DEFERRABLE INITIALLY IMMEDIATE",
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
        );

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferred' => true)
        );
        $this->assertEquals(
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) NOT DEFERRABLE INITIALLY DEFERRED",
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
        );

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('feferred' => true)
        );
        $this->assertEquals(
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) NOT DEFERRABLE INITIALLY DEFERRED",
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
        );

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('foreign_id'), 'my_table', array('id'), 'my_fk', array('deferrable' => true, 'deferred' => true, 'match' => 'full')
        );
        $this->assertEquals(
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) MATCH full DEFERRABLE INITIALLY DEFERRED",
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
        );
102 103
    }

104 105 106 107 108
    public function testGeneratesSqlSnippets()
    {
        $this->assertEquals('SIMILAR TO', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
        $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
        $this->assertEquals('column1 || column2 || column3', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
109 110
        $this->assertEquals('SUBSTRING(column FROM 5)', $this->_platform->getSubstringExpression('column', 5), 'Substring expression without length is not correct');
        $this->assertEquals('SUBSTRING(column FROM 1 FOR 5)', $this->_platform->getSubstringExpression('column', 1, 5), 'Substring expression with length is not correct');
111 112 113
    }

    public function testGeneratesTransactionCommands()
114 115 116
    {
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
117
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
118 119 120
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
121
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
122 123 124
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
125
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
126 127 128
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
129
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
130 131 132
        );
    }

133
    public function testGeneratesDDLSnippets()
134
    {
135 136 137
        $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
        $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
        $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
138 139
    }

140 141 142 143 144 145 146 147 148
    public function testGenerateTableWithAutoincrement()
    {
        $table = new \Doctrine\DBAL\Schema\Table('autoinc_table');
        $column = $table->addColumn('id', 'integer');
        $column->setAutoincrement(true);

        $this->assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
    }

149
    public function testGeneratesTypeDeclarationForIntegers()
150 151 152
    {
        $this->assertEquals(
            'INT',
153
            $this->_platform->getIntegerTypeDeclarationSQL(array())
154 155 156
        );
        $this->assertEquals(
            'SERIAL',
157
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
158 159 160
        ));
        $this->assertEquals(
            'SERIAL',
161
            $this->_platform->getIntegerTypeDeclarationSQL(
162 163
                array('autoincrement' => true, 'primary' => true)
        ));
164 165 166 167
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
168 169
        $this->assertEquals(
            'CHAR(10)',
170
            $this->_platform->getVarcharTypeDeclarationSQL(
171 172
                array('length' => 10, 'fixed' => true))
        );
173 174
        $this->assertEquals(
            'VARCHAR(50)',
175
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
176
            'Variable string declaration is not correct'
177 178
        );
        $this->assertEquals(
179
            'VARCHAR(255)',
180
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
181
            'Long string declaration is not correct'
182 183 184
        );
    }

185 186 187 188 189
    public function getGenerateUniqueIndexSql()
    {
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
    }

190
    public function testGeneratesSequenceSqlCommands()
191
    {
192
        $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
193
        $this->assertEquals(
194
            'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
195
            $this->_platform->getCreateSequenceSQL($sequence)
196 197
        );
        $this->assertEquals(
198
            'DROP SEQUENCE myseq CASCADE',
199
            $this->_platform->getDropSequenceSQL('myseq')
200 201 202
        );
        $this->assertEquals(
            "SELECT NEXTVAL('myseq')",
203
            $this->_platform->getSequenceNextValSQL('myseq')
204 205 206
        );
    }

207
    public function testDoesNotPreferIdentityColumns()
208 209
    {
        $this->assertFalse($this->_platform->prefersIdentityColumns());
210 211 212 213
    }

    public function testPrefersSequences()
    {
214
        $this->assertTrue($this->_platform->prefersSequences());
215 216 217 218
    }

    public function testSupportsIdentityColumns()
    {
219
        $this->assertTrue($this->_platform->supportsIdentityColumns());
220 221 222 223
    }

    public function testSupportsSavePoints()
    {
224
        $this->assertTrue($this->_platform->supportsSavepoints());
225 226 227 228
    }

    public function testSupportsSequences()
    {
229 230
        $this->assertTrue($this->_platform->supportsSequences());
    }
231 232 233 234

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
235
        $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
236 237 238 239 240 241 242
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
    }
243 244 245 246 247

    public function getCreateTableColumnCommentsSQL()
    {
        return array(
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
248
            "COMMENT ON COLUMN test.id IS 'This is a comment'",
249 250 251 252 253 254 255
        );
    }

    public function getAlterTableColumnCommentsSQL()
    {
        return array(
            "ALTER TABLE mytable ADD quota INT NOT NULL",
256
            "COMMENT ON COLUMN mytable.quota IS 'A comment'",
257
            "COMMENT ON COLUMN mytable.foo IS NULL",
258
            "COMMENT ON COLUMN mytable.baz IS 'B comment'",
259 260
        );
    }
261 262 263 264 265 266 267 268

    public function getCreateTableColumnTypeCommentsSQL()
    {
        return array(
            "CREATE TABLE test (id INT NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))",
            "COMMENT ON COLUMN test.data IS '(DC2Type:array)'"
        );
    }
269 270 271

    protected function getQuotedColumnInPrimaryKeySQL()
    {
272
        return array(
273
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, PRIMARY KEY("create"))',
274
        );
275 276 277 278
    }

    protected function getQuotedColumnInIndexSQL()
    {
279
        return array(
280 281 282 283 284 285 286 287 288 289
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL)',
            'CREATE INDEX IDX_22660D028FD6E0FB ON "quoted" ("create")',
        );
    }

    protected function getQuotedColumnInForeignKeySQL()
    {
        return array(
            'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL)',
            'ALTER TABLE "quoted" ADD CONSTRAINT FK_22660D028FD6E0FB8C736521 FOREIGN KEY ("create", foo) REFERENCES "foreign" ("create", bar) NOT DEFERRABLE INITIALLY IMMEDIATE',
290
        );
291
    }
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

    /**
     * @group DBAL-457
     */
    public function testConvertBooleanAsStrings()
    {
        $platform = $this->createPlatform();

        $this->assertEquals('true', $platform->convertBooleans(true));
        $this->assertEquals('false', $platform->convertBooleans(false));
    }

    /**
     * @group DBAL-457
     */
    public function testConvertBooleanAsIntegers()
    {
        $platform = $this->createPlatform();
        $platform->setUseBooleanTrueFalseStrings(false);

        $this->assertEquals('1', $platform->convertBooleans(true));
        $this->assertEquals('0', $platform->convertBooleans(false));
    }
315
}
316