Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • SqlsrvDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://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:     public function convertException(\PDOException $e)
 31:     {
 32:         $code = isset($e->errorInfo[0]) ? $e->errorInfo[0] : NULL;
 33:         if ($code === '0A000' && strpos($e->getMessage(), 'truncate') !== FALSE) {
 34:             return Nette\Database\ForeignKeyConstraintViolationException::from($e);
 35: 
 36:         } elseif ($code === '23502') {
 37:             return Nette\Database\NotNullConstraintViolationException::from($e);
 38: 
 39:         } elseif ($code === '23503') {
 40:             return Nette\Database\ForeignKeyConstraintViolationException::from($e);
 41: 
 42:         } elseif ($code === '23505') {
 43:             return Nette\Database\UniqueConstraintViolationException::from($e);
 44: 
 45:         } elseif ($code === '08006') {
 46:             return Nette\Database\ConnectionException::from($e);
 47: 
 48:         } else {
 49:             return Nette\Database\DriverException::from($e);
 50:         }
 51:     }
 52: 
 53: 
 54:     /********************* SQL ****************d*g**/
 55: 
 56: 
 57:     /**
 58:      * Delimites identifier for use in a SQL statement.
 59:      */
 60:     public function delimite($name)
 61:     {
 62:         // @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
 63:         return '"' . str_replace('"', '""', $name) . '"';
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Formats boolean for use in a SQL statement.
 69:      */
 70:     public function formatBool($value)
 71:     {
 72:         return $value ? 'TRUE' : 'FALSE';
 73:     }
 74: 
 75: 
 76:     /**
 77:      * Formats date-time for use in a SQL statement.
 78:      */
 79:     public function formatDateTime(/*\DateTimeInterface*/ $value)
 80:     {
 81:         return $value->format("'Y-m-d H:i:s'");
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Formats date-time interval for use in a SQL statement.
 87:      */
 88:     public function formatDateInterval(\DateInterval $value)
 89:     {
 90:         throw new Nette\NotSupportedException;
 91:     }
 92: 
 93: 
 94:     /**
 95:      * Encodes string for use in a LIKE statement.
 96:      */
 97:     public function formatLike($value, $pos)
 98:     {
 99:         $bs = substr($this->connection->quote('\\', \PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off
100:         $value = substr($this->connection->quote($value, \PDO::PARAM_STR), 1, -1);
101:         $value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\'));
102:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
103:     }
104: 
105: 
106:     /**
107:      * Injects LIMIT/OFFSET to the SQL query.
108:      */
109:     public function applyLimit(& $sql, $limit, $offset)
110:     {
111:         if ($limit >= 0) {
112:             $sql .= ' LIMIT ' . (int) $limit;
113:         }
114:         if ($offset > 0) {
115:             $sql .= ' OFFSET ' . (int) $offset;
116:         }
117:     }
118: 
119: 
120:     /**
121:      * Normalizes result row.
122:      */
123:     public function normalizeRow($row)
124:     {
125:         return $row;
126:     }
127: 
128: 
129:     /********************* reflection ****************d*g**/
130: 
131: 
132:     /**
133:      * Returns list of tables.
134:      */
135:     public function getTables()
136:     {
137:         $tables = array();
138:         foreach ($this->connection->query("
139:             SELECT DISTINCT ON (c.relname)
140:                 c.relname::varchar AS name,
141:                 c.relkind = 'v' AS view,
142:                 n.nspname::varchar || '.' || c.relname::varchar AS \"fullName\"
143:             FROM
144:                 pg_catalog.pg_class AS c
145:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
146:             WHERE
147:                 c.relkind IN ('r', 'v')
148:                 AND n.nspname = ANY (pg_catalog.current_schemas(FALSE))
149:             ORDER BY
150:                 c.relname
151:         ") as $row) {
152:             $tables[] = (array) $row;
153:         }
154: 
155:         return $tables;
156:     }
157: 
158: 
159:     /**
160:      * Returns metadata for all columns in a table.
161:      */
162:     public function getColumns($table)
163:     {
164:         $columns = array();
165:         foreach ($this->connection->query("
166:             SELECT
167:                 a.attname::varchar AS name,
168:                 c.relname::varchar AS table,
169:                 upper(t.typname) AS nativetype,
170:                 CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size,
171:                 FALSE AS unsigned,
172:                 NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
173:                 pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
174:                 coalesce(co.contype = 'p' AND strpos(ad.adsrc, 'nextval') = 1, FALSE) AS autoincrement,
175:                 coalesce(co.contype = 'p', FALSE) AS primary,
176:                 substring(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) from 'nextval[(]''\"?([^''\"]+)') AS sequence
177:             FROM
178:                 pg_catalog.pg_attribute AS a
179:                 JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid
180:                 JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid
181:                 LEFT JOIN pg_catalog.pg_attrdef AS ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
182:                 LEFT JOIN pg_catalog.pg_constraint AS co ON co.connamespace = c.relnamespace AND contype = 'p' AND co.conrelid = c.oid AND a.attnum = ANY(co.conkey)
183:             WHERE
184:                 c.relkind IN ('r', 'v')
185:                 AND c.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
186:                 AND a.attnum > 0
187:                 AND NOT a.attisdropped
188:             ORDER BY
189:                 a.attnum
190:         ") as $row) {
191:             $column = (array) $row;
192:             $column['vendor'] = $column;
193:             unset($column['sequence']);
194: 
195:             $columns[] = $column;
196:         }
197: 
198:         return $columns;
199:     }
200: 
201: 
202:     /**
203:      * Returns metadata for all indexes in a table.
204:      */
205:     public function getIndexes($table)
206:     {
207:         $indexes = array();
208:         foreach ($this->connection->query("
209:             SELECT
210:                 c2.relname::varchar AS name,
211:                 i.indisunique AS unique,
212:                 i.indisprimary AS primary,
213:                 a.attname::varchar AS column
214:             FROM
215:                 pg_catalog.pg_class AS c1
216:                 JOIN pg_catalog.pg_index AS i ON c1.oid = i.indrelid
217:                 JOIN pg_catalog.pg_class AS c2 ON i.indexrelid = c2.oid
218:                 LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
219:             WHERE
220:                 c1.relkind = 'r'
221:                 AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
222:         ") as $row) {
223:             $indexes[$row['name']]['name'] = $row['name'];
224:             $indexes[$row['name']]['unique'] = $row['unique'];
225:             $indexes[$row['name']]['primary'] = $row['primary'];
226:             $indexes[$row['name']]['columns'][] = $row['column'];
227:         }
228: 
229:         return array_values($indexes);
230:     }
231: 
232: 
233:     /**
234:      * Returns metadata for all foreign keys in a table.
235:      */
236:     public function getForeignKeys($table)
237:     {
238:         /* Does't work with multicolumn foreign keys */
239:         return $this->connection->query("
240:             SELECT
241:                 co.conname::varchar AS name,
242:                 al.attname::varchar AS local,
243:                 nf.nspname || '.' || cf.relname::varchar AS table,
244:                 af.attname::varchar AS foreign
245:             FROM
246:                 pg_catalog.pg_constraint AS co
247:                 JOIN pg_catalog.pg_class AS cl ON co.conrelid = cl.oid
248:                 JOIN pg_catalog.pg_class AS cf ON co.confrelid = cf.oid
249:                 JOIN pg_catalog.pg_namespace AS nf ON nf.oid = cf.relnamespace
250:                 JOIN pg_catalog.pg_attribute AS al ON al.attrelid = cl.oid AND al.attnum = co.conkey[1]
251:                 JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
252:             WHERE
253:                 co.contype = 'f'
254:                 AND cl.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
255:                 AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
256:         ")->fetchAll();
257:     }
258: 
259: 
260:     /**
261:      * Returns associative array of detected types (IReflection::FIELD_*) in result set.
262:      */
263:     public function getColumnTypes(\PDOStatement $statement)
264:     {
265:         return Nette\Database\Helpers::detectTypes($statement);
266:     }
267: 
268: 
269:     /**
270:      * @param  string
271:      * @return bool
272:      */
273:     public function isSupported($item)
274:     {
275:         return $item === self::SUPPORT_SEQUENCE || $item === self::SUPPORT_SUBSELECT || $item === self::SUPPORT_SCHEMA;
276:     }
277: 
278: 
279:     /**
280:      * Converts: schema.name => "schema"."name"
281:      * @param  string
282:      * @return string
283:      */
284:     private function delimiteFQN($name)
285:     {
286:         return implode('.', array_map(array($this, 'delimite'), explode('.', $name)));
287:     }
288: 
289: }
290: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0