MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
PostgreSqlPlatform.php
Ir para a documentação deste ficheiro.
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
21
24
35{
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
61 public function getNowExpression()
62 {
63 return 'LOCALTIMESTAMP(0)';
64 }
65
72 public function getRegexpExpression()
73 {
74 return 'SIMILAR TO';
75 }
76
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
102 /*public function parseBoolean($value)
103 {
104 return $value;
105 }*/
106
113 public function supportsSequences()
114 {
115 return true;
116 }
117
123 public function supportsSchemas()
124 {
125 return true;
126 }
127
134 public function supportsIdentityColumns()
135 {
136 return true;
137 }
138
144 public function prefersSequences()
145 {
146 return true;
147 }
148
149 public function getListDatabasesSQL()
150 {
151 return 'SELECT datname FROM pg_database';
152 }
153
154 public function getListSequencesSQL($database)
155 {
156 return "SELECT
157 c.relname, n.nspname AS schemaname
158 FROM
159 pg_class c, pg_namespace n
160 WHERE relkind = 'S' AND n.oid = c.relnamespace AND
161 (n.nspname NOT LIKE 'pg_%' AND n.nspname != 'information_schema')";
162 }
163
164 public function getListTablesSQL()
165 {
166 return "SELECT tablename AS table_name, schemaname AS schema_name
167 FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'information_schema'";
168 }
169
170 public function getListViewsSQL($database)
171 {
172 return 'SELECT viewname, definition FROM pg_views';
173 }
174
175 public function getListTableForeignKeysSQL($table, $database = null)
176 {
177 return "SELECT r.conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
178 FROM pg_catalog.pg_constraint r
179 WHERE r.conrelid =
180 (
181 SELECT c.oid
182 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
183 WHERE " .$this->getTableWhereClause($table) ."
184 AND n.oid = c.relnamespace
185 )
186 AND r.contype = 'f'";
187 }
188
189 public function getCreateViewSQL($name, $sql)
190 {
191 return 'CREATE VIEW ' . $name . ' AS ' . $sql;
192 }
193
194 public function getDropViewSQL($name)
195 {
196 return 'DROP VIEW '. $name;
197 }
198
199 public function getListTableConstraintsSQL($table)
200 {
201 return "SELECT
202 relname
203 FROM
204 pg_class
205 WHERE oid IN (
206 SELECT indexrelid
207 FROM pg_index, pg_class
208 WHERE pg_class.relname = '$table'
209 AND pg_class.oid = pg_index.indrelid
210 AND (indisunique = 't' OR indisprimary = 't')
211 )";
212 }
213
220 public function getListTableIndexesSQL($table)
221 {
222 return "SELECT relname, pg_index.indisunique, pg_index.indisprimary,
223 pg_index.indkey, pg_index.indrelid
224 FROM pg_class, pg_index
225 WHERE oid IN (
226 SELECT indexrelid
227 FROM pg_index si, pg_class sc, pg_namespace sn
228 WHERE " . $this->getTableWhereClause($table, 'sc', 'sn')." AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid
229 ) AND pg_index.indexrelid = oid";
230 }
231
232 private function getTableWhereClause($table, $classAlias = 'c', $namespaceAlias = 'n')
233 {
234 $whereClause = "";
235 if (strpos($table, ".") !== false) {
236 list($schema, $table) = explode(".", $table);
237 $whereClause = "$classAlias.relname = '" . $table . "' AND $namespaceAlias.nspname = '" . $schema . "'";
238 } else {
239 $whereClause = "$classAlias.relname = '" . $table . "'";
240 }
241 return $whereClause;
242 }
243
244 public function getListTableColumnsSQL($table)
245 {
246 return "SELECT
247 a.attnum,
248 a.attname AS field,
249 t.typname AS type,
250 format_type(a.atttypid, a.atttypmod) AS complete_type,
251 (SELECT t1.typname FROM pg_catalog.pg_type t1 WHERE t1.oid = t.typbasetype) AS domain_type,
252 (SELECT format_type(t2.typbasetype, t2.typtypmod) FROM pg_catalog.pg_type t2
253 WHERE t2.typtype = 'd' AND t2.typname = format_type(a.atttypid, a.atttypmod)) AS domain_complete_type,
254 a.attnotnull AS isnotnull,
255 (SELECT 't'
256 FROM pg_index
257 WHERE c.oid = pg_index.indrelid
258 AND pg_index.indkey[0] = a.attnum
259 AND pg_index.indisprimary = 't'
260 ) AS pri,
261 (SELECT pg_attrdef.adsrc
262 FROM pg_attrdef
263 WHERE c.oid = pg_attrdef.adrelid
264 AND pg_attrdef.adnum=a.attnum
265 ) AS default
266 FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
267 WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
268 AND a.attnum > 0
269 AND a.attrelid = c.oid
270 AND a.atttypid = t.oid
271 AND n.oid = c.relnamespace
272 ORDER BY a.attnum";
273 }
274
283 public function getCreateDatabaseSQL($name)
284 {
285 return 'CREATE DATABASE ' . $name;
286 }
287
295 public function getDropDatabaseSQL($name)
296 {
297 return 'DROP DATABASE ' . $name;
298 }
299
308 public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
309 {
310 $query = '';
311 if ($foreignKey->hasOption('match')) {
312 $query .= ' MATCH ' . $foreignKey->getOption('match');
313 }
314 $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
315 if ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false) {
316 $query .= ' DEFERRABLE';
317 } else {
318 $query .= ' NOT DEFERRABLE';
319 }
320 if ($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false) {
321 $query .= ' INITIALLY DEFERRED';
322 } else {
323 $query .= ' INITIALLY IMMEDIATE';
324 }
325 return $query;
326 }
327
340 public function getAlterTableSQL(TableDiff $diff)
341 {
342 $sql = array();
343
344 foreach ($diff->addedColumns as $column) {
345 $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
346 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
347 }
348
349 foreach ($diff->removedColumns as $column) {
350 $query = 'DROP ' . $column->getQuotedName($this);
351 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
352 }
353
354 foreach ($diff->changedColumns AS $columnDiff) {
355 $oldColumnName = $columnDiff->oldColumnName;
356 $column = $columnDiff->column;
357
358 if ($columnDiff->hasChanged('type')) {
359 $type = $column->getType();
360
361 // here was a server version check before, but DBAL API does not support this anymore.
362 $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
363 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
364 }
365 if ($columnDiff->hasChanged('default')) {
366 $query = 'ALTER ' . $oldColumnName . ' SET ' . $this->getDefaultValueDeclarationSQL($column->toArray());
367 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
368 }
369 if ($columnDiff->hasChanged('notnull')) {
370 $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
371 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
372 }
373 if ($columnDiff->hasChanged('autoincrement')) {
374 if ($column->getAutoincrement()) {
375 // add autoincrement
376 $seqName = $diff->name . '_' . $oldColumnName . '_seq';
377
378 $sql[] = "CREATE SEQUENCE " . $seqName;
379 $sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
380 $query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
381 $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
382 } else {
383 // Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
384 $query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
385 $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
386 }
387 }
388 }
389
390 foreach ($diff->renamedColumns as $oldColumnName => $column) {
391 $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
392 }
393
394 if ($diff->newName !== false) {
395 $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
396 }
397
398 $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
399
400 return $sql;
401 }
402
409 public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
410 {
411 return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
412 ' INCREMENT BY ' . $sequence->getAllocationSize() .
413 ' MINVALUE ' . $sequence->getInitialValue() .
414 ' START ' . $sequence->getInitialValue();
415 }
416
422 public function getDropSequenceSQL($sequence)
423 {
424 if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
425 $sequence = $sequence->getQuotedName($this);
426 }
427 return 'DROP SEQUENCE ' . $sequence;
428 }
429
435 public function getDropForeignKeySQL($foreignKey, $table)
436 {
437 return $this->getDropConstraintSQL($foreignKey, $table);
438 }
439
448 protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
449 {
450 $queryFields = $this->getColumnDeclarationListSQL($columns);
451
452 if (isset($options['primary']) && ! empty($options['primary'])) {
453 $keyColumns = array_unique(array_values($options['primary']));
454 $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
455 }
456
457 $query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
458
459 $sql[] = $query;
460
461 if (isset($options['indexes']) && ! empty($options['indexes'])) {
462 foreach ($options['indexes'] AS $index) {
463 $sql[] = $this->getCreateIndexSQL($index, $tableName);
464 }
465 }
466
467 if (isset($options['foreignKeys'])) {
468 foreach ((array) $options['foreignKeys'] as $definition) {
469 $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
470 }
471 }
472
473 return $sql;
474 }
475
482 public function convertBooleans($item)
483 {
484 if (is_array($item)) {
485 foreach ($item as $key => $value) {
486 if (is_bool($value) || is_numeric($item)) {
487 $item[$key] = ($value) ? 'true' : 'false';
488 }
489 }
490 } else {
491 if (is_bool($item) || is_numeric($item)) {
492 $item = ($item) ? 'true' : 'false';
493 }
494 }
495 return $item;
496 }
497
498 public function getSequenceNextValSQL($sequenceName)
499 {
500 return "SELECT NEXTVAL('" . $sequenceName . "')";
501 }
502
503 public function getSetTransactionIsolationSQL($level)
504 {
505 return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL '
506 . $this->_getTransactionIsolationLevelSQL($level);
507 }
508
512 public function getBooleanTypeDeclarationSQL(array $field)
513 {
514 return 'BOOLEAN';
515 }
516
520 public function getIntegerTypeDeclarationSQL(array $field)
521 {
522 if ( ! empty($field['autoincrement'])) {
523 return 'SERIAL';
524 }
525
526 return 'INT';
527 }
528
532 public function getBigIntTypeDeclarationSQL(array $field)
533 {
534 if ( ! empty($field['autoincrement'])) {
535 return 'BIGSERIAL';
536 }
537 return 'BIGINT';
538 }
539
543 public function getSmallIntTypeDeclarationSQL(array $field)
544 {
545 return 'SMALLINT';
546 }
547
551 public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
552 {
553 return 'TIMESTAMP(0) WITHOUT TIME ZONE';
554 }
555
559 public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
560 {
561 return 'TIMESTAMP(0) WITH TIME ZONE';
562 }
563
567 public function getDateTypeDeclarationSQL(array $fieldDeclaration)
568 {
569 return 'DATE';
570 }
571
575 public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
576 {
577 return 'TIME(0) WITHOUT TIME ZONE';
578 }
579
583 protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
584 {
585 return '';
586 }
587
594 public function getVarcharTypeDeclarationSQL(array $field)
595 {
596 if ( ! isset($field['length'])) {
597 if (array_key_exists('default', $field)) {
598 $field['length'] = $this->getVarcharDefaultLength();
599 } else {
600 $field['length'] = false;
601 }
602 }
603
604 $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
605 $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
606
607 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
608 : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
609 }
610
612 public function getClobTypeDeclarationSQL(array $field)
613 {
614 return 'TEXT';
615 }
616
622 public function getName()
623 {
624 return 'postgresql';
625 }
626
635 public function getSQLResultCasing($column)
636 {
637 return strtolower($column);
638 }
639
641 {
642 return 'Y-m-d H:i:sO';
643 }
644
652 public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
653 {
654 return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
655 }
656
660 public function getTruncateTableSQL($tableName, $cascade = false)
661 {
662 return 'TRUNCATE '.$tableName.' '.($cascade)?'CASCADE':'';
663 }
664
665 public function getReadLockSQL()
666 {
667 return 'FOR SHARE';
668 }
669
671 {
672 $this->doctrineTypeMapping = array(
673 'smallint' => 'smallint',
674 'int2' => 'smallint',
675 'serial' => 'integer',
676 'serial4' => 'integer',
677 'int' => 'integer',
678 'int4' => 'integer',
679 'integer' => 'integer',
680 'bigserial' => 'bigint',
681 'serial8' => 'bigint',
682 'bigint' => 'bigint',
683 'int8' => 'bigint',
684 'bool' => 'boolean',
685 'boolean' => 'boolean',
686 'text' => 'text',
687 'varchar' => 'string',
688 'interval' => 'string',
689 '_varchar' => 'string',
690 'char' => 'string',
691 'bpchar' => 'string',
692 'date' => 'date',
693 'datetime' => 'datetime',
694 'timestamp' => 'datetime',
695 'timestamptz' => 'datetimetz',
696 'time' => 'time',
697 'timetz' => 'time',
698 'float' => 'float',
699 'float4' => 'float',
700 'float8' => 'float',
701 'double' => 'float',
702 'double precision' => 'float',
703 'real' => 'float',
704 'decimal' => 'decimal',
705 'money' => 'decimal',
706 'numeric' => 'decimal',
707 'year' => 'date',
708 );
709 }
710}
getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
getLocateExpression($str, $substr, $startPos=false)
getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
getTruncateTableSQL($tableName, $cascade=false)
getSubstringExpression($value, $from, $len=null)
_getCreateTableSQL($tableName, array $columns, array $options=array())
getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)