PostgreSqlPlatform.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 ." + (" . $days . " || ' day')::interval)";
  95. }
  96. public function getDateSubDaysExpression($date, $days)
  97. {
  98. return "(" . $date ." - (" . $days . " || ' day')::interval)";
  99. }
  100. public function getDateAddMonthExpression($date, $months)
  101. {
  102. return "(" . $date ." + (" . $months . " || ' month')::interval)";
  103. }
  104. public function getDateSubMonthExpression($date, $months)
  105. {
  106. return "(" . $date ." - (" . $months . " || ' month')::interval)";
  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' AND tablename != 'geometry_columns' AND tablename != 'spatial_ref_sys'";
  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. $columnSql = array();
  352. foreach ($diff->addedColumns as $column) {
  353. if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
  354. continue;
  355. }
  356. $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
  357. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  358. if ($comment = $this->getColumnComment($column)) {
  359. $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
  360. }
  361. }
  362. foreach ($diff->removedColumns as $column) {
  363. if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
  364. continue;
  365. }
  366. $query = 'DROP ' . $column->getQuotedName($this);
  367. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  368. }
  369. foreach ($diff->changedColumns AS $columnDiff) {
  370. if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
  371. continue;
  372. }
  373. $oldColumnName = $columnDiff->oldColumnName;
  374. $column = $columnDiff->column;
  375. if ($columnDiff->hasChanged('type')) {
  376. $type = $column->getType();
  377. // here was a server version check before, but DBAL API does not support this anymore.
  378. $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
  379. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  380. }
  381. if ($columnDiff->hasChanged('default')) {
  382. $query = 'ALTER ' . $oldColumnName . ' SET ' . $this->getDefaultValueDeclarationSQL($column->toArray());
  383. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  384. }
  385. if ($columnDiff->hasChanged('notnull')) {
  386. $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
  387. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  388. }
  389. if ($columnDiff->hasChanged('autoincrement')) {
  390. if ($column->getAutoincrement()) {
  391. // add autoincrement
  392. $seqName = $diff->name . '_' . $oldColumnName . '_seq';
  393. $sql[] = "CREATE SEQUENCE " . $seqName;
  394. $sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
  395. $query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
  396. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  397. } else {
  398. // Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
  399. $query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
  400. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  401. }
  402. }
  403. if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) {
  404. $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
  405. }
  406. }
  407. foreach ($diff->renamedColumns as $oldColumnName => $column) {
  408. if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
  409. continue;
  410. }
  411. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
  412. }
  413. $tableSql = array();
  414. if (!$this->onSchemaAlterTable($diff, $tableSql)) {
  415. if ($diff->newName !== false) {
  416. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
  417. }
  418. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
  419. }
  420. return array_merge($sql, $tableSql, $columnSql);
  421. }
  422. /**
  423. * Gets the SQL to create a sequence on this platform.
  424. *
  425. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  426. * @return string
  427. */
  428. public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  429. {
  430. return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
  431. ' INCREMENT BY ' . $sequence->getAllocationSize() .
  432. ' MINVALUE ' . $sequence->getInitialValue() .
  433. ' START ' . $sequence->getInitialValue();
  434. }
  435. public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  436. {
  437. return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
  438. ' INCREMENT BY ' . $sequence->getAllocationSize();
  439. }
  440. /**
  441. * Drop existing sequence
  442. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  443. * @return string
  444. */
  445. public function getDropSequenceSQL($sequence)
  446. {
  447. if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
  448. $sequence = $sequence->getQuotedName($this);
  449. }
  450. return 'DROP SEQUENCE ' . $sequence;
  451. }
  452. /**
  453. * @param ForeignKeyConstraint|string $foreignKey
  454. * @param Table|string $table
  455. * @return string
  456. */
  457. public function getDropForeignKeySQL($foreignKey, $table)
  458. {
  459. return $this->getDropConstraintSQL($foreignKey, $table);
  460. }
  461. /**
  462. * Gets the SQL used to create a table.
  463. *
  464. * @param unknown_type $tableName
  465. * @param array $columns
  466. * @param array $options
  467. * @return unknown
  468. */
  469. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  470. {
  471. $queryFields = $this->getColumnDeclarationListSQL($columns);
  472. if (isset($options['primary']) && ! empty($options['primary'])) {
  473. $keyColumns = array_unique(array_values($options['primary']));
  474. $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
  475. }
  476. $query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
  477. $sql[] = $query;
  478. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  479. foreach ($options['indexes'] AS $index) {
  480. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  481. }
  482. }
  483. if (isset($options['foreignKeys'])) {
  484. foreach ((array) $options['foreignKeys'] as $definition) {
  485. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  486. }
  487. }
  488. return $sql;
  489. }
  490. /**
  491. * Postgres wants boolean values converted to the strings 'true'/'false'.
  492. *
  493. * @param array $item
  494. * @override
  495. */
  496. public function convertBooleans($item)
  497. {
  498. if (is_array($item)) {
  499. foreach ($item as $key => $value) {
  500. if (is_bool($value) || is_numeric($item)) {
  501. $item[$key] = ($value) ? 'true' : 'false';
  502. }
  503. }
  504. } else {
  505. if (is_bool($item) || is_numeric($item)) {
  506. $item = ($item) ? 'true' : 'false';
  507. }
  508. }
  509. return $item;
  510. }
  511. public function getSequenceNextValSQL($sequenceName)
  512. {
  513. return "SELECT NEXTVAL('" . $sequenceName . "')";
  514. }
  515. public function getSetTransactionIsolationSQL($level)
  516. {
  517. return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL '
  518. . $this->_getTransactionIsolationLevelSQL($level);
  519. }
  520. /**
  521. * @override
  522. */
  523. public function getBooleanTypeDeclarationSQL(array $field)
  524. {
  525. return 'BOOLEAN';
  526. }
  527. /**
  528. * @override
  529. */
  530. public function getIntegerTypeDeclarationSQL(array $field)
  531. {
  532. if ( ! empty($field['autoincrement'])) {
  533. return 'SERIAL';
  534. }
  535. return 'INT';
  536. }
  537. /**
  538. * @override
  539. */
  540. public function getBigIntTypeDeclarationSQL(array $field)
  541. {
  542. if ( ! empty($field['autoincrement'])) {
  543. return 'BIGSERIAL';
  544. }
  545. return 'BIGINT';
  546. }
  547. /**
  548. * @override
  549. */
  550. public function getSmallIntTypeDeclarationSQL(array $field)
  551. {
  552. return 'SMALLINT';
  553. }
  554. /**
  555. * @override
  556. */
  557. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  558. {
  559. return 'TIMESTAMP(0) WITHOUT TIME ZONE';
  560. }
  561. /**
  562. * @override
  563. */
  564. public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
  565. {
  566. return 'TIMESTAMP(0) WITH TIME ZONE';
  567. }
  568. /**
  569. * @override
  570. */
  571. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  572. {
  573. return 'DATE';
  574. }
  575. /**
  576. * @override
  577. */
  578. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  579. {
  580. return 'TIME(0) WITHOUT TIME ZONE';
  581. }
  582. /**
  583. * @override
  584. */
  585. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  586. {
  587. return '';
  588. }
  589. /**
  590. * Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
  591. *
  592. * @params array $field
  593. * @override
  594. */
  595. protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
  596. {
  597. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  598. : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
  599. }
  600. /** @override */
  601. public function getClobTypeDeclarationSQL(array $field)
  602. {
  603. return 'TEXT';
  604. }
  605. /**
  606. * Get the platform name for this instance
  607. *
  608. * @return string
  609. */
  610. public function getName()
  611. {
  612. return 'postgresql';
  613. }
  614. /**
  615. * Gets the character casing of a column in an SQL result set.
  616. *
  617. * PostgreSQL returns all column names in SQL result sets in lowercase.
  618. *
  619. * @param string $column The column name for which to get the correct character casing.
  620. * @return string The column name in the character casing used in SQL result sets.
  621. */
  622. public function getSQLResultCasing($column)
  623. {
  624. return strtolower($column);
  625. }
  626. public function getDateTimeTzFormatString()
  627. {
  628. return 'Y-m-d H:i:sO';
  629. }
  630. /**
  631. * Get the insert sql for an empty insert statement
  632. *
  633. * @param string $tableName
  634. * @param string $identifierColumnName
  635. * @return string $sql
  636. */
  637. public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
  638. {
  639. return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
  640. }
  641. /**
  642. * @inheritdoc
  643. */
  644. public function getTruncateTableSQL($tableName, $cascade = false)
  645. {
  646. return 'TRUNCATE '.$tableName.' '.(($cascade)?'CASCADE':'');
  647. }
  648. public function getReadLockSQL()
  649. {
  650. return 'FOR SHARE';
  651. }
  652. protected function initializeDoctrineTypeMappings()
  653. {
  654. $this->doctrineTypeMapping = array(
  655. 'smallint' => 'smallint',
  656. 'int2' => 'smallint',
  657. 'serial' => 'integer',
  658. 'serial4' => 'integer',
  659. 'int' => 'integer',
  660. 'int4' => 'integer',
  661. 'integer' => 'integer',
  662. 'bigserial' => 'bigint',
  663. 'serial8' => 'bigint',
  664. 'bigint' => 'bigint',
  665. 'int8' => 'bigint',
  666. 'bool' => 'boolean',
  667. 'boolean' => 'boolean',
  668. 'text' => 'text',
  669. 'varchar' => 'string',
  670. 'interval' => 'string',
  671. '_varchar' => 'string',
  672. 'char' => 'string',
  673. 'bpchar' => 'string',
  674. 'date' => 'date',
  675. 'datetime' => 'datetime',
  676. 'timestamp' => 'datetime',
  677. 'timestamptz' => 'datetimetz',
  678. 'time' => 'time',
  679. 'timetz' => 'time',
  680. 'float' => 'float',
  681. 'float4' => 'float',
  682. 'float8' => 'float',
  683. 'double' => 'float',
  684. 'double precision' => 'float',
  685. 'real' => 'float',
  686. 'decimal' => 'decimal',
  687. 'money' => 'decimal',
  688. 'numeric' => 'decimal',
  689. 'year' => 'date',
  690. 'bytea' => 'blob',
  691. );
  692. }
  693. public function getVarcharMaxLength()
  694. {
  695. return 65535;
  696. }
  697. protected function getReservedKeywordsClass()
  698. {
  699. return 'Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords';
  700. }
  701. /**
  702. * Gets the SQL Snippet used to declare a BLOB column type.
  703. */
  704. public function getBlobTypeDeclarationSQL(array $field)
  705. {
  706. return 'BYTEA';
  707. }
  708. }