Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Database\Drivers;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Supplemental PostgreSQL database driver.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class PgSqlDriver extends Nette\Object implements Nette\Database\ISupplementalDriver
 19: {
 20:     /** @var Nette\Database\Connection */
 21:     private $connection;
 22: 
 23: 
 24:     public function __construct(Nette\Database\Connection $connection, array $options)
 25:     {
 26:         $this->connection = $connection;
 27:     }
 28: 
 29: 
 30:     /********************* SQL ****************d*g**/
 31: 
 32: 
 33:     /**
 34:      * Delimites identifier for use in a SQL statement.
 35:      */
 36:     public function delimite($name)
 37:     {
 38:         // @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
 39:         return '"' . str_replace('"', '""', $name) . '"';
 40:     }
 41: 
 42: 
 43:     /**
 44:      * Formats boolean for use in a SQL statement.
 45:      */
 46:     public function formatBool($value)
 47:     {
 48:         return $value ? 'TRUE' : 'FALSE';
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Formats date-time for use in a SQL statement.
 54:      */
 55:     public function formatDateTime(\DateTime $value)
 56:     {
 57:         return $value->format("'Y-m-d H:i:s'");
 58:     }
 59: 
 60: 
 61:     /**
 62:      * Encodes string for use in a LIKE statement.
 63:      */
 64:     public function formatLike($value, $pos)
 65:     {
 66:         $value = strtr($value, array("'" => "''", '\\' => '\\\\', '%' => '\\\\%', '_' => '\\\\_'));
 67:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Injects LIMIT/OFFSET to the SQL query.
 73:      */
 74:     public function applyLimit(& $sql, $limit, $offset)
 75:     {
 76:         if ($limit >= 0) {
 77:             $sql .= ' LIMIT ' . (int) $limit;
 78:         }
 79:         if ($offset > 0) {
 80:             $sql .= ' OFFSET ' . (int) $offset;
 81:         }
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Normalizes result row.
 87:      */
 88:     public function normalizeRow($row, $statement)
 89:     {
 90:         return $row;
 91:     }
 92: 
 93: 
 94:     /********************* reflection ****************d*g**/
 95: 
 96: 
 97:     /**
 98:      * Returns list of tables.
 99:      */
100:     public function getTables()
101:     {
102:         $tables = array();
103:         foreach ($this->connection->query("
104:             SELECT
105:                 c.relname::varchar AS name,
106:                 c.relkind = 'v' AS view
107:             FROM
108:                 pg_catalog.pg_class AS c
109:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
110:             WHERE
111:                 c.relkind IN ('r', 'v')
112:                 AND ARRAY[n.nspname] <@ pg_catalog.current_schemas(FALSE)
113:             ORDER BY
114:                 c.relname
115:         ") as $row) {
116:             $tables[] = (array) $row;
117:         }
118: 
119:         return $tables;
120:     }
121: 
122: 
123:     /**
124:      * Returns metadata for all columns in a table.
125:      */
126:     public function getColumns($table)
127:     {
128:         $columns = array();
129:         foreach ($this->connection->query("
130:             SELECT
131:                 a.attname::varchar AS name,
132:                 c.relname::varchar AS table,
133:                 upper(t.typname) AS nativetype,
134:                 NULL AS size,
135:                 FALSE AS unsigned,
136:                 NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
137:                 pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
138:                 coalesce(co.contype = 'p' AND strpos(ad.adsrc, 'nextval') = 1, FALSE) AS autoincrement,
139:                 coalesce(co.contype = 'p', FALSE) AS primary,
140:                 substring(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) from 'nextval[(]''\"?([^''\"]+)') AS sequence
141:             FROM
142:                 pg_catalog.pg_attribute AS a
143:                 JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid
144:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
145:                 JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid
146:                 LEFT JOIN pg_catalog.pg_attrdef AS ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
147:                 LEFT JOIN pg_catalog.pg_constraint AS co ON co.connamespace = n.oid AND contype = 'p' AND co.conrelid = c.oid AND a.attnum = ANY(co.conkey)
148:             WHERE
149:                 c.relkind IN ('r', 'v')
150:                 AND c.relname::varchar = {$this->connection->quote($table)}
151:                 AND ARRAY[n.nspname] <@ pg_catalog.current_schemas(FALSE)
152:                 AND a.attnum > 0
153:                 AND NOT a.attisdropped
154:             ORDER BY
155:                 a.attnum
156:         ") as $row) {
157:             $column = (array) $row;
158:             $column['vendor'] = $column;
159:             unset($column['sequence']);
160: 
161:             $columns[] = $column;
162:         }
163: 
164:         return $columns;
165:     }
166: 
167: 
168:     /**
169:      * Returns metadata for all indexes in a table.
170:      */
171:     public function getIndexes($table)
172:     {
173:         $indexes = array();
174:         foreach ($this->connection->query("
175:             SELECT
176:                 c2.relname::varchar AS name,
177:                 i.indisunique AS unique,
178:                 i.indisprimary AS primary,
179:                 a.attname::varchar AS column
180:             FROM
181:                 pg_catalog.pg_class AS c1
182:                 JOIN pg_catalog.pg_namespace AS n ON c1.relnamespace = n.oid
183:                 JOIN pg_catalog.pg_index AS i ON c1.oid = i.indrelid
184:                 JOIN pg_catalog.pg_class AS c2 ON i.indexrelid = c2.oid
185:                 LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
186:             WHERE
187:                 ARRAY[n.nspname] <@ pg_catalog.current_schemas(FALSE)
188:                 AND c1.relkind = 'r'
189:                 AND c1.relname = {$this->connection->quote($table)}
190:         ") as $row) {
191:             $indexes[$row['name']]['name'] = $row['name'];
192:             $indexes[$row['name']]['unique'] = $row['unique'];
193:             $indexes[$row['name']]['primary'] = $row['primary'];
194:             $indexes[$row['name']]['columns'][] = $row['column'];
195:         }
196: 
197:         return array_values($indexes);
198:     }
199: 
200: 
201:     /**
202:      * Returns metadata for all foreign keys in a table.
203:      */
204:     public function getForeignKeys($table)
205:     {
206:         /* Does't work with multicolumn foreign keys */
207:         return $this->connection->query("
208:             SELECT
209:                 co.conname::varchar AS name,
210:                 al.attname::varchar AS local,
211:                 cf.relname::varchar AS table,
212:                 af.attname::varchar AS foreign
213:             FROM
214:                 pg_catalog.pg_constraint AS co
215:                 JOIN pg_catalog.pg_namespace AS n ON co.connamespace = n.oid
216:                 JOIN pg_catalog.pg_class AS cl ON co.conrelid = cl.oid
217:                 JOIN pg_catalog.pg_class AS cf ON co.confrelid = cf.oid
218:                 JOIN pg_catalog.pg_attribute AS al ON al.attrelid = cl.oid AND al.attnum = co.conkey[1]
219:                 JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
220:             WHERE
221:                 ARRAY[n.nspname] <@ pg_catalog.current_schemas(FALSE)
222:                 AND co.contype = 'f'
223:                 AND cl.relname = {$this->connection->quote($table)}
224:         ")->fetchAll();
225:     }
226: 
227: 
228:     /**
229:      * @return bool
230:      */
231:     public function isSupported($item)
232:     {
233:         return $item === self::SUPPORT_COLUMNS_META || $item === self::SUPPORT_SEQUENCE;
234:     }
235: 
236: }
237: 
Nette 2.0 API documentation generated by ApiGen 2.8.0