PostgreSqlPlatform.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Platforms;
  20. use Doctrine\DBAL\Schema\TableDiff,
  21. Doctrine\DBAL\Schema\Table;
  22. /**
  23. * PostgreSqlPlatform.
  24. *
  25. * @since 2.0
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @todo Rename: PostgreSQLPlatform
  30. */
  31. class PostgreSqlPlatform extends AbstractPlatform
  32. {
  33. /**
  34. * Returns part of a string.
  35. *
  36. * Note: Not SQL92, but common functionality.
  37. *
  38. * @param string $value the target $value the string or the string column.
  39. * @param int $from extract from this characeter.
  40. * @param int $len extract this amount of characters.
  41. * @return string sql that extracts part of a string.
  42. * @override
  43. */
  44. public function getSubstringExpression($value, $from, $len = null)
  45. {
  46. if ($len === null) {
  47. return 'SUBSTR(' . $value . ', ' . $from . ')';
  48. } else {
  49. return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
  50. }
  51. }
  52. /**
  53. * Returns the SQL string to return the current system date and time.
  54. *
  55. * @return string
  56. */
  57. public function getNowExpression()
  58. {
  59. return 'LOCALTIMESTAMP(0)';
  60. }
  61. /**
  62. * regexp
  63. *
  64. * @return string the regular expression operator
  65. * @override
  66. */
  67. public function getRegexpExpression()
  68. {
  69. return 'SIMILAR TO';
  70. }
  71. /**
  72. * returns the position of the first occurrence of substring $substr in string $str
  73. *
  74. * @param string $substr literal string to find
  75. * @param string $str literal string
  76. * @param int $pos position to start at, beginning of string by default
  77. * @return integer
  78. */
  79. public function getLocateExpression($str, $substr, $startPos = false)
  80. {
  81. if ($startPos !== false) {
  82. $str = $this->getSubstringExpression($str, $startPos);
  83. return 'CASE WHEN (POSITION('.$substr.' IN '.$str.') = 0) THEN 0 ELSE (POSITION('.$substr.' IN '.$str.') + '.($startPos-1).') END';
  84. } else {
  85. return 'POSITION('.$substr.' IN '.$str.')';
  86. }
  87. }
  88. public function getDateDiffExpression($date1, $date2)
  89. {
  90. return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
  91. }
  92. public function getDateAddDaysExpression($date, $days)
  93. {
  94. return "(" . $date . "+ interval '" . $days . " day')";
  95. }
  96. public function getDateSubDaysExpression($date, $days)
  97. {
  98. return "(" . $date . "- interval '" . $days . " day')";
  99. }
  100. public function getDateAddMonthExpression($date, $months)
  101. {
  102. return "(" . $date . "+ interval '" . $months . " month')";
  103. }
  104. public function getDateSubMonthExpression($date, $months)
  105. {
  106. return "(" . $date . "- interval '" . $months . " month')";
  107. }
  108. /**
  109. * parses a literal boolean value and returns
  110. * proper sql equivalent
  111. *
  112. * @param string $value boolean value to be parsed
  113. * @return string parsed boolean value
  114. */
  115. /*public function parseBoolean($value)
  116. {
  117. return $value;
  118. }*/
  119. /**
  120. * Whether the platform supports sequences.
  121. * Postgres has native support for sequences.
  122. *
  123. * @return boolean
  124. */
  125. public function supportsSequences()
  126. {
  127. return true;
  128. }
  129. /**
  130. * Whether the platform supports database schemas.
  131. *
  132. * @return boolean
  133. */
  134. public function supportsSchemas()
  135. {
  136. return true;
  137. }
  138. /**
  139. * Whether the platform supports identity columns.
  140. * Postgres supports these through the SERIAL keyword.
  141. *
  142. * @return boolean
  143. */
  144. public function supportsIdentityColumns()
  145. {
  146. return true;
  147. }
  148. public function supportsCommentOnStatement()
  149. {
  150. return true;
  151. }
  152. /**
  153. * Whether the platform prefers sequences for ID generation.
  154. *
  155. * @return boolean
  156. */
  157. public function prefersSequences()
  158. {
  159. return true;
  160. }
  161. public function getListDatabasesSQL()
  162. {
  163. return 'SELECT datname FROM pg_database';
  164. }
  165. public function getListSequencesSQL($database)
  166. {
  167. return "SELECT
  168. c.relname, n.nspname AS schemaname
  169. FROM
  170. pg_class c, pg_namespace n
  171. WHERE relkind = 'S' AND n.oid = c.relnamespace AND
  172. (n.nspname NOT LIKE 'pg_%' AND n.nspname != 'information_schema')";
  173. }
  174. public function getListTablesSQL()
  175. {
  176. return "SELECT tablename AS table_name, schemaname AS schema_name
  177. FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'information_schema'";
  178. }
  179. public function getListViewsSQL($database)
  180. {
  181. return 'SELECT viewname, definition FROM pg_views';
  182. }
  183. public function getListTableForeignKeysSQL($table, $database = null)
  184. {
  185. return "SELECT r.conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
  186. FROM pg_catalog.pg_constraint r
  187. WHERE r.conrelid =
  188. (
  189. SELECT c.oid
  190. FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
  191. WHERE " .$this->getTableWhereClause($table) ." AND n.oid = c.relnamespace
  192. )
  193. AND r.contype = 'f'";
  194. }
  195. public function getCreateViewSQL($name, $sql)
  196. {
  197. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  198. }
  199. public function getDropViewSQL($name)
  200. {
  201. return 'DROP VIEW '. $name;
  202. }
  203. public function getListTableConstraintsSQL($table)
  204. {
  205. return "SELECT
  206. relname
  207. FROM
  208. pg_class
  209. WHERE oid IN (
  210. SELECT indexrelid
  211. FROM pg_index, pg_class
  212. WHERE pg_class.relname = '$table'
  213. AND pg_class.oid = pg_index.indrelid
  214. AND (indisunique = 't' OR indisprimary = 't')
  215. )";
  216. }
  217. /**
  218. * @license New BSD License
  219. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  220. * @param string $table
  221. * @return string
  222. */
  223. public function getListTableIndexesSQL($table, $currentDatabase = null)
  224. {
  225. return "SELECT relname, pg_index.indisunique, pg_index.indisprimary,
  226. pg_index.indkey, pg_index.indrelid
  227. FROM pg_class, pg_index
  228. WHERE oid IN (
  229. SELECT indexrelid
  230. FROM pg_index si, pg_class sc, pg_namespace sn
  231. WHERE " . $this->getTableWhereClause($table, 'sc', 'sn')." AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid
  232. ) AND pg_index.indexrelid = oid";
  233. }
  234. /**
  235. * @param string $table
  236. * @param string $classAlias
  237. * @param string $namespaceAlias
  238. * @return string
  239. */
  240. private function getTableWhereClause($table, $classAlias = 'c', $namespaceAlias = 'n')
  241. {
  242. $whereClause = $namespaceAlias.".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND ";
  243. if (strpos($table, ".") !== false) {
  244. list($schema, $table) = explode(".", $table);
  245. $schema = "'" . $schema . "'";
  246. } else {
  247. $schema = "ANY(string_to_array((select setting from pg_catalog.pg_settings where name = 'search_path'),','))";
  248. }
  249. $whereClause .= "$classAlias.relname = '" . $table . "' AND $namespaceAlias.nspname = $schema";
  250. return $whereClause;
  251. }
  252. public function getListTableColumnsSQL($table, $database = null)
  253. {
  254. return "SELECT
  255. a.attnum,
  256. a.attname AS field,
  257. t.typname AS type,
  258. format_type(a.atttypid, a.atttypmod) AS complete_type,
  259. (SELECT t1.typname FROM pg_catalog.pg_type t1 WHERE t1.oid = t.typbasetype) AS domain_type,
  260. (SELECT format_type(t2.typbasetype, t2.typtypmod) FROM pg_catalog.pg_type t2
  261. WHERE t2.typtype = 'd' AND t2.typname = format_type(a.atttypid, a.atttypmod)) AS domain_complete_type,
  262. a.attnotnull AS isnotnull,
  263. (SELECT 't'
  264. FROM pg_index
  265. WHERE c.oid = pg_index.indrelid
  266. AND pg_index.indkey[0] = a.attnum
  267. AND pg_index.indisprimary = 't'
  268. ) AS pri,
  269. (SELECT pg_attrdef.adsrc
  270. FROM pg_attrdef
  271. WHERE c.oid = pg_attrdef.adrelid
  272. AND pg_attrdef.adnum=a.attnum
  273. ) AS default,
  274. (SELECT pg_description.description
  275. FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid
  276. ) AS comment
  277. FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
  278. WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
  279. AND a.attnum > 0
  280. AND a.attrelid = c.oid
  281. AND a.atttypid = t.oid
  282. AND n.oid = c.relnamespace
  283. ORDER BY a.attnum";
  284. }
  285. /**
  286. * create a new database
  287. *
  288. * @param string $name name of the database that should be created
  289. * @throws PDOException
  290. * @return void
  291. * @override
  292. */
  293. public function getCreateDatabaseSQL($name)
  294. {
  295. return 'CREATE DATABASE ' . $name;
  296. }
  297. /**
  298. * drop an existing database
  299. *
  300. * @param string $name name of the database that should be dropped
  301. * @throws PDOException
  302. * @access public
  303. */
  304. public function getDropDatabaseSQL($name)
  305. {
  306. return 'DROP DATABASE ' . $name;
  307. }
  308. /**
  309. * Return the FOREIGN KEY query section dealing with non-standard options
  310. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  311. *
  312. * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey foreign key definition
  313. * @return string
  314. * @override
  315. */
  316. public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
  317. {
  318. $query = '';
  319. if ($foreignKey->hasOption('match')) {
  320. $query .= ' MATCH ' . $foreignKey->getOption('match');
  321. }
  322. $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
  323. if ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false) {
  324. $query .= ' DEFERRABLE';
  325. } else {
  326. $query .= ' NOT DEFERRABLE';
  327. }
  328. if ($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false) {
  329. $query .= ' INITIALLY DEFERRED';
  330. } else {
  331. $query .= ' INITIALLY IMMEDIATE';
  332. }
  333. return $query;
  334. }
  335. /**
  336. * generates the sql for altering an existing table on postgresql
  337. *
  338. * @param string $name name of the table that is intended to be changed.
  339. * @param array $changes associative array that contains the details of each type *
  340. * @param boolean $check indicates whether the function should just check if the DBMS driver
  341. * can perform the requested table alterations if the value is true or
  342. * actually perform them otherwise.
  343. * @see Doctrine_Export::alterTable()
  344. * @return array
  345. * @override
  346. */
  347. public function getAlterTableSQL(TableDiff $diff)
  348. {
  349. $sql = array();
  350. $commentsSQL = array();
  351. foreach ($diff->addedColumns as $column) {
  352. $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
  353. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  354. if ($comment = $this->getColumnComment($column)) {
  355. $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
  356. }
  357. }
  358. foreach ($diff->removedColumns as $column) {
  359. $query = 'DROP ' . $column->getQuotedName($this);
  360. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  361. }
  362. foreach ($diff->changedColumns AS $columnDiff) {
  363. $oldColumnName = $columnDiff->oldColumnName;
  364. $column = $columnDiff->column;
  365. if ($columnDiff->hasChanged('type')) {
  366. $type = $column->getType();
  367. // here was a server version check before, but DBAL API does not support this anymore.
  368. $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
  369. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  370. }
  371. if ($columnDiff->hasChanged('default')) {
  372. $query = 'ALTER ' . $oldColumnName . ' SET ' . $this->getDefaultValueDeclarationSQL($column->toArray());
  373. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  374. }
  375. if ($columnDiff->hasChanged('notnull')) {
  376. $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
  377. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  378. }
  379. if ($columnDiff->hasChanged('autoincrement')) {
  380. if ($column->getAutoincrement()) {
  381. // add autoincrement
  382. $seqName = $diff->name . '_' . $oldColumnName . '_seq';
  383. $sql[] = "CREATE SEQUENCE " . $seqName;
  384. $sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
  385. $query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
  386. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  387. } else {
  388. // Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
  389. $query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
  390. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  391. }
  392. }
  393. if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) {
  394. $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
  395. }
  396. }
  397. foreach ($diff->renamedColumns as $oldColumnName => $column) {
  398. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
  399. }
  400. if ($diff->newName !== false) {
  401. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
  402. }
  403. return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
  404. }
  405. /**
  406. * Gets the SQL to create a sequence on this platform.
  407. *
  408. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  409. * @return string
  410. */
  411. public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  412. {
  413. return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
  414. ' INCREMENT BY ' . $sequence->getAllocationSize() .
  415. ' MINVALUE ' . $sequence->getInitialValue() .
  416. ' START ' . $sequence->getInitialValue();
  417. }
  418. public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  419. {
  420. return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
  421. ' INCREMENT BY ' . $sequence->getAllocationSize();
  422. }
  423. /**
  424. * Drop existing sequence
  425. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  426. * @return string
  427. */
  428. public function getDropSequenceSQL($sequence)
  429. {
  430. if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
  431. $sequence = $sequence->getQuotedName($this);
  432. }
  433. return 'DROP SEQUENCE ' . $sequence;
  434. }
  435. /**
  436. * @param ForeignKeyConstraint|string $foreignKey
  437. * @param Table|string $table
  438. * @return string
  439. */
  440. public function getDropForeignKeySQL($foreignKey, $table)
  441. {
  442. return $this->getDropConstraintSQL($foreignKey, $table);
  443. }
  444. /**
  445. * Gets the SQL used to create a table.
  446. *
  447. * @param unknown_type $tableName
  448. * @param array $columns
  449. * @param array $options
  450. * @return unknown
  451. */
  452. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  453. {
  454. $queryFields = $this->getColumnDeclarationListSQL($columns);
  455. if (isset($options['primary']) && ! empty($options['primary'])) {
  456. $keyColumns = array_unique(array_values($options['primary']));
  457. $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
  458. }
  459. $query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
  460. $sql[] = $query;
  461. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  462. foreach ($options['indexes'] AS $index) {
  463. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  464. }
  465. }
  466. if (isset($options['foreignKeys'])) {
  467. foreach ((array) $options['foreignKeys'] as $definition) {
  468. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  469. }
  470. }
  471. return $sql;
  472. }
  473. /**
  474. * Postgres wants boolean values converted to the strings 'true'/'false'.
  475. *
  476. * @param array $item
  477. * @override
  478. */
  479. public function convertBooleans($item)
  480. {
  481. if (is_array($item)) {
  482. foreach ($item as $key => $value) {
  483. if (is_bool($value) || is_numeric($item)) {
  484. $item[$key] = ($value) ? 'true' : 'false';
  485. }
  486. }
  487. } else {
  488. if (is_bool($item) || is_numeric($item)) {
  489. $item = ($item) ? 'true' : 'false';
  490. }
  491. }
  492. return $item;
  493. }
  494. public function getSequenceNextValSQL($sequenceName)
  495. {
  496. return "SELECT NEXTVAL('" . $sequenceName . "')";
  497. }
  498. public function getSetTransactionIsolationSQL($level)
  499. {
  500. return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL '
  501. . $this->_getTransactionIsolationLevelSQL($level);
  502. }
  503. /**
  504. * @override
  505. */
  506. public function getBooleanTypeDeclarationSQL(array $field)
  507. {
  508. return 'BOOLEAN';
  509. }
  510. /**
  511. * @override
  512. */
  513. public function getIntegerTypeDeclarationSQL(array $field)
  514. {
  515. if ( ! empty($field['autoincrement'])) {
  516. return 'SERIAL';
  517. }
  518. return 'INT';
  519. }
  520. /**
  521. * @override
  522. */
  523. public function getBigIntTypeDeclarationSQL(array $field)
  524. {
  525. if ( ! empty($field['autoincrement'])) {
  526. return 'BIGSERIAL';
  527. }
  528. return 'BIGINT';
  529. }
  530. /**
  531. * @override
  532. */
  533. public function getSmallIntTypeDeclarationSQL(array $field)
  534. {
  535. return 'SMALLINT';
  536. }
  537. /**
  538. * @override
  539. */
  540. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  541. {
  542. return 'TIMESTAMP(0) WITHOUT TIME ZONE';
  543. }
  544. /**
  545. * @override
  546. */
  547. public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
  548. {
  549. return 'TIMESTAMP(0) WITH TIME ZONE';
  550. }
  551. /**
  552. * @override
  553. */
  554. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  555. {
  556. return 'DATE';
  557. }
  558. /**
  559. * @override
  560. */
  561. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  562. {
  563. return 'TIME(0) WITHOUT TIME ZONE';
  564. }
  565. /**
  566. * @override
  567. */
  568. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  569. {
  570. return '';
  571. }
  572. /**
  573. * Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
  574. *
  575. * @params array $field
  576. * @override
  577. */
  578. protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
  579. {
  580. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  581. : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
  582. }
  583. /** @override */
  584. public function getClobTypeDeclarationSQL(array $field)
  585. {
  586. return 'TEXT';
  587. }
  588. /**
  589. * Get the platform name for this instance
  590. *
  591. * @return string
  592. */
  593. public function getName()
  594. {
  595. return 'postgresql';
  596. }
  597. /**
  598. * Gets the character casing of a column in an SQL result set.
  599. *
  600. * PostgreSQL returns all column names in SQL result sets in lowercase.
  601. *
  602. * @param string $column The column name for which to get the correct character casing.
  603. * @return string The column name in the character casing used in SQL result sets.
  604. */
  605. public function getSQLResultCasing($column)
  606. {
  607. return strtolower($column);
  608. }
  609. public function getDateTimeTzFormatString()
  610. {
  611. return 'Y-m-d H:i:sO';
  612. }
  613. /**
  614. * Get the insert sql for an empty insert statement
  615. *
  616. * @param string $tableName
  617. * @param string $identifierColumnName
  618. * @return string $sql
  619. */
  620. public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
  621. {
  622. return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
  623. }
  624. /**
  625. * @inheritdoc
  626. */
  627. public function getTruncateTableSQL($tableName, $cascade = false)
  628. {
  629. return 'TRUNCATE '.$tableName.' '.(($cascade)?'CASCADE':'');
  630. }
  631. public function getReadLockSQL()
  632. {
  633. return 'FOR SHARE';
  634. }
  635. protected function initializeDoctrineTypeMappings()
  636. {
  637. $this->doctrineTypeMapping = array(
  638. 'smallint' => 'smallint',
  639. 'int2' => 'smallint',
  640. 'serial' => 'integer',
  641. 'serial4' => 'integer',
  642. 'int' => 'integer',
  643. 'int4' => 'integer',
  644. 'integer' => 'integer',
  645. 'bigserial' => 'bigint',
  646. 'serial8' => 'bigint',
  647. 'bigint' => 'bigint',
  648. 'int8' => 'bigint',
  649. 'bool' => 'boolean',
  650. 'boolean' => 'boolean',
  651. 'text' => 'text',
  652. 'varchar' => 'string',
  653. 'interval' => 'string',
  654. '_varchar' => 'string',
  655. 'char' => 'string',
  656. 'bpchar' => 'string',
  657. 'date' => 'date',
  658. 'datetime' => 'datetime',
  659. 'timestamp' => 'datetime',
  660. 'timestamptz' => 'datetimetz',
  661. 'time' => 'time',
  662. 'timetz' => 'time',
  663. 'float' => 'float',
  664. 'float4' => 'float',
  665. 'float8' => 'float',
  666. 'double' => 'float',
  667. 'double precision' => 'float',
  668. 'real' => 'float',
  669. 'decimal' => 'decimal',
  670. 'money' => 'decimal',
  671. 'numeric' => 'decimal',
  672. 'year' => 'date',
  673. );
  674. }
  675. public function getVarcharMaxLength()
  676. {
  677. return 65535;
  678. }
  679. protected function getReservedKeywordsClass()
  680. {
  681. return 'Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords';
  682. }
  683. }