PostgreSqlPlatformTest.php 7.94 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Doctrine\Tests\DBAL\Platforms;

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

require_once __DIR__ . '/../../TestInit.php';
 
10
class PostgreSqlPlatformTest extends AbstractPlatformTestCase
11
{
12
    public function createPlatform()
13
    {
14
        return new PostgreSqlPlatform;
15 16
    }

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

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

30
    public function getGenerateAlterTableSql()
31
    {
32
        return array(
33
            'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
34 35 36 37
            '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',
38 39 40 41
            'ALTER TABLE mytable RENAME TO userlist',
        );
    }

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

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

52 53
    public function testGeneratesForeignKeySqlForNonStandardOptions()
    {
54 55
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
                array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
56 57
        );
        $this->assertEquals(
58
            "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
59
            $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
60 61 62
        );
    }

63 64 65 66 67
    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');
68 69
        $this->assertEquals('SUBSTR(column, 5)', $this->_platform->getSubstringExpression('column', 5), 'Substring expression without length is not correct');
        $this->assertEquals('SUBSTR(column, 0, 5)', $this->_platform->getSubstringExpression('column', 0, 5), 'Substring expression with length is not correct');
70 71 72
    }

    public function testGeneratesTransactionCommands()
73 74 75
    {
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
76
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
77 78 79
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
80
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
81 82 83
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
84
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
85 86 87
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
88
            $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
89 90 91
        );
    }

92
    public function testGeneratesDDLSnippets()
93
    {
94 95 96
        $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'));
97 98
    }

99 100 101 102 103 104 105 106 107
    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));
    }

108
    public function testGeneratesTypeDeclarationForIntegers()
109 110 111
    {
        $this->assertEquals(
            'INT',
112
            $this->_platform->getIntegerTypeDeclarationSQL(array())
113 114 115
        );
        $this->assertEquals(
            'SERIAL',
116
            $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
117 118 119
        ));
        $this->assertEquals(
            'SERIAL',
120
            $this->_platform->getIntegerTypeDeclarationSQL(
121 122
                array('autoincrement' => true, 'primary' => true)
        ));
123 124 125 126
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
127 128
        $this->assertEquals(
            'CHAR(10)',
129
            $this->_platform->getVarcharTypeDeclarationSQL(
130 131
                array('length' => 10, 'fixed' => true))
        );
132 133
        $this->assertEquals(
            'VARCHAR(50)',
134
            $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
135
            'Variable string declaration is not correct'
136 137
        );
        $this->assertEquals(
138
            'VARCHAR(255)',
139
            $this->_platform->getVarcharTypeDeclarationSQL(array()),
140
            'Long string declaration is not correct'
141 142 143
        );
    }

144 145 146 147 148
    public function getGenerateUniqueIndexSql()
    {
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
    }

149
    public function testGeneratesSequenceSqlCommands()
150
    {
151
        $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
152
        $this->assertEquals(
153
            'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
154
            $this->_platform->getCreateSequenceSQL($sequence)
155 156 157
        );
        $this->assertEquals(
            'DROP SEQUENCE myseq',
158
            $this->_platform->getDropSequenceSQL('myseq')
159 160 161
        );
        $this->assertEquals(
            "SELECT NEXTVAL('myseq')",
162
            $this->_platform->getSequenceNextValSQL('myseq')
163 164 165
        );
    }

166
    public function testDoesNotPreferIdentityColumns()
167 168
    {
        $this->assertFalse($this->_platform->prefersIdentityColumns());
169 170 171 172
    }

    public function testPrefersSequences()
    {
173
        $this->assertTrue($this->_platform->prefersSequences());
174 175 176 177
    }

    public function testSupportsIdentityColumns()
    {
178
        $this->assertTrue($this->_platform->supportsIdentityColumns());
179 180 181 182
    }

    public function testSupportsSavePoints()
    {
183
        $this->assertTrue($this->_platform->supportsSavepoints());
184 185 186 187
    }

    public function testSupportsSequences()
    {
188 189
        $this->assertTrue($this->_platform->supportsSequences());
    }
190 191 192 193

    public function testModifyLimitQuery()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
194
        $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
195 196 197 198 199 200 201
    }

    public function testModifyLimitQueryWithEmptyOffset()
    {
        $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
        $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
    }
202 203 204 205 206

    public function getCreateTableColumnCommentsSQL()
    {
        return array(
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
207
            "COMMENT ON COLUMN test.id IS 'This is a comment'",
208 209 210 211 212 213 214
        );
    }

    public function getAlterTableColumnCommentsSQL()
    {
        return array(
            "ALTER TABLE mytable ADD quota INT NOT NULL",
215
            "COMMENT ON COLUMN mytable.quota IS 'A comment'",
216
            "COMMENT ON COLUMN mytable.baz IS 'B comment'",
217 218
        );
    }
219
}