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