PostgreSqlPlatformTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Platforms;
  3. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class PostgreSqlPlatformTest extends AbstractPlatformTestCase
  7. {
  8. public function createPlatform()
  9. {
  10. return new PostgreSqlPlatform;
  11. }
  12. public function getGenerateTableSql()
  13. {
  14. return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
  15. }
  16. public function getGenerateTableWithMultiColumnUniqueIndexSql()
  17. {
  18. return array(
  19. 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
  20. 'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)'
  21. );
  22. }
  23. public function getGenerateAlterTableSql()
  24. {
  25. return array(
  26. 'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
  27. 'ALTER TABLE mytable DROP foo',
  28. 'ALTER TABLE mytable ALTER bar TYPE VARCHAR(255)',
  29. "ALTER TABLE mytable ALTER bar SET DEFAULT 'def'",
  30. 'ALTER TABLE mytable ALTER bar SET NOT NULL',
  31. 'ALTER TABLE mytable RENAME TO userlist',
  32. );
  33. }
  34. public function getGenerateIndexSql()
  35. {
  36. return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
  37. }
  38. public function getGenerateForeignKeySql()
  39. {
  40. return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id) NOT DEFERRABLE INITIALLY IMMEDIATE';
  41. }
  42. public function testGeneratesForeignKeySqlForNonStandardOptions()
  43. {
  44. $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
  45. array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
  46. );
  47. $this->assertEquals(
  48. "CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
  49. $this->_platform->getForeignKeyDeclarationSQL($foreignKey)
  50. );
  51. }
  52. public function testGeneratesSqlSnippets()
  53. {
  54. $this->assertEquals('SIMILAR TO', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
  55. $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
  56. $this->assertEquals('column1 || column2 || column3', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
  57. $this->assertEquals('SUBSTR(column, 5)', $this->_platform->getSubstringExpression('column', 5), 'Substring expression without length is not correct');
  58. $this->assertEquals('SUBSTR(column, 0, 5)', $this->_platform->getSubstringExpression('column', 0, 5), 'Substring expression with length is not correct');
  59. }
  60. public function testGeneratesTransactionCommands()
  61. {
  62. $this->assertEquals(
  63. 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
  64. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
  65. );
  66. $this->assertEquals(
  67. 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
  68. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
  69. );
  70. $this->assertEquals(
  71. 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
  72. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
  73. );
  74. $this->assertEquals(
  75. 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
  76. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
  77. );
  78. }
  79. public function testGeneratesDDLSnippets()
  80. {
  81. $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
  82. $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
  83. $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
  84. }
  85. public function testGenerateTableWithAutoincrement()
  86. {
  87. $table = new \Doctrine\DBAL\Schema\Table('autoinc_table');
  88. $column = $table->addColumn('id', 'integer');
  89. $column->setAutoincrement(true);
  90. $this->assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
  91. }
  92. public function testGeneratesTypeDeclarationForIntegers()
  93. {
  94. $this->assertEquals(
  95. 'INT',
  96. $this->_platform->getIntegerTypeDeclarationSQL(array())
  97. );
  98. $this->assertEquals(
  99. 'SERIAL',
  100. $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
  101. ));
  102. $this->assertEquals(
  103. 'SERIAL',
  104. $this->_platform->getIntegerTypeDeclarationSQL(
  105. array('autoincrement' => true, 'primary' => true)
  106. ));
  107. }
  108. public function testGeneratesTypeDeclarationForStrings()
  109. {
  110. $this->assertEquals(
  111. 'CHAR(10)',
  112. $this->_platform->getVarcharTypeDeclarationSQL(
  113. array('length' => 10, 'fixed' => true))
  114. );
  115. $this->assertEquals(
  116. 'VARCHAR(50)',
  117. $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
  118. 'Variable string declaration is not correct'
  119. );
  120. $this->assertEquals(
  121. 'VARCHAR(255)',
  122. $this->_platform->getVarcharTypeDeclarationSQL(array()),
  123. 'Long string declaration is not correct'
  124. );
  125. }
  126. public function getGenerateUniqueIndexSql()
  127. {
  128. return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
  129. }
  130. public function testGeneratesSequenceSqlCommands()
  131. {
  132. $sequence = new \Doctrine\DBAL\Schema\Sequence('myseq', 20, 1);
  133. $this->assertEquals(
  134. 'CREATE SEQUENCE myseq INCREMENT BY 20 MINVALUE 1 START 1',
  135. $this->_platform->getCreateSequenceSQL($sequence)
  136. );
  137. $this->assertEquals(
  138. 'DROP SEQUENCE myseq',
  139. $this->_platform->getDropSequenceSQL('myseq')
  140. );
  141. $this->assertEquals(
  142. "SELECT NEXTVAL('myseq')",
  143. $this->_platform->getSequenceNextValSQL('myseq')
  144. );
  145. }
  146. public function testDoesNotPreferIdentityColumns()
  147. {
  148. $this->assertFalse($this->_platform->prefersIdentityColumns());
  149. }
  150. public function testPrefersSequences()
  151. {
  152. $this->assertTrue($this->_platform->prefersSequences());
  153. }
  154. public function testSupportsIdentityColumns()
  155. {
  156. $this->assertTrue($this->_platform->supportsIdentityColumns());
  157. }
  158. public function testSupportsSavePoints()
  159. {
  160. $this->assertTrue($this->_platform->supportsSavepoints());
  161. }
  162. public function testSupportsSequences()
  163. {
  164. $this->assertTrue($this->_platform->supportsSequences());
  165. }
  166. public function testModifyLimitQuery()
  167. {
  168. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
  169. $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
  170. }
  171. public function testModifyLimitQueryWithEmptyOffset()
  172. {
  173. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
  174. $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
  175. }
  176. public function getCreateTableColumnCommentsSQL()
  177. {
  178. return array(
  179. "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
  180. "COMMENT ON COLUMN test.id IS 'This is a comment'",
  181. );
  182. }
  183. public function getAlterTableColumnCommentsSQL()
  184. {
  185. return array(
  186. "ALTER TABLE mytable ADD quota INT NOT NULL",
  187. "COMMENT ON COLUMN mytable.quota IS 'A comment'",
  188. "COMMENT ON COLUMN mytable.baz IS 'B comment'",
  189. );
  190. }
  191. public function getCreateTableColumnTypeCommentsSQL()
  192. {
  193. return array(
  194. "CREATE TABLE test (id INT NOT NULL, data TEXT NOT NULL, PRIMARY KEY(id))",
  195. "COMMENT ON COLUMN test.data IS '(DC2Type:array)'"
  196. );
  197. }
  198. }