Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

Classes

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • SqlsrvDriver
  • 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(/*\DateTimeInterface*/ $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:         $bs = substr($this->connection->quote('\\', \PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off
 67:         $value = substr($this->connection->quote($value, \PDO::PARAM_STR), 1, -1);
 68:         $value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\'));
 69:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Injects LIMIT/OFFSET to the SQL query.
 75:      */
 76:     public function applyLimit(& $sql, $limit, $offset)
 77:     {
 78:         if ($limit >= 0) {
 79:             $sql .= ' LIMIT ' . (int) $limit;
 80:         }
 81:         if ($offset > 0) {
 82:             $sql .= ' OFFSET ' . (int) $offset;
 83:         }
 84:     }
 85: 
 86: 
 87:     /**
 88:      * Normalizes result row.
 89:      */
 90:     public function normalizeRow($row)
 91:     {
 92:         return $row;
 93:     }
 94: 
 95: 
 96:     /********************* reflection ****************d*g**/
 97: 
 98: 
 99:     /**
100:      * Returns list of tables.
101:      */
102:     public function getTables()
103:     {
104:         $tables = array();
105:         foreach ($this->connection->query("
106:             SELECT DISTINCT ON (c.relname)
107:                 c.relname::varchar AS name,
108:                 c.relkind = 'v' AS view
109:             FROM
110:                 pg_catalog.pg_class AS c
111:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
112:             WHERE
113:                 c.relkind IN ('r', 'v')
114:                 AND n.nspname = ANY (pg_catalog.current_schemas(FALSE))
115:             ORDER BY
116:                 c.relname
117:         ") as $row) {
118:             $tables[] = (array) $row;
119:         }
120: 
121:         return $tables;
122:     }
123: 
124: 
125:     /**
126:      * Returns metadata for all columns in a table.
127:      */
128:     public function getColumns($table)
129:     {
130:         $columns = array();
131:         foreach ($this->connection->query("
132:             SELECT
133:                 a.attname::varchar AS name,
134:                 c.relname::varchar AS table,
135:                 upper(t.typname) AS nativetype,
136:                 NULL AS size,
137:                 FALSE AS unsigned,
138:                 NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
139:                 pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
140:                 coalesce(co.contype = 'p' AND strpos(ad.adsrc, 'nextval') = 1, FALSE) AS autoincrement,
141:                 coalesce(co.contype = 'p', FALSE) AS primary,
142:                 substring(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) from 'nextval[(]''\"?([^''\"]+)') AS sequence
143:             FROM
144:                 pg_catalog.pg_attribute AS a
145:                 JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid
146:                 JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid
147:                 LEFT JOIN pg_catalog.pg_attrdef AS ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
148:                 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)
149:             WHERE
150:                 c.relkind IN ('r', 'v')
151:                 AND c.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
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_index AS i ON c1.oid = i.indrelid
183:                 JOIN pg_catalog.pg_class AS c2 ON i.indexrelid = c2.oid
184:                 LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
185:             WHERE
186:                 c1.relkind = 'r'
187:                 AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
188:         ") as $row) {
189:             $indexes[$row['name']]['name'] = $row['name'];
190:             $indexes[$row['name']]['unique'] = $row['unique'];
191:             $indexes[$row['name']]['primary'] = $row['primary'];
192:             $indexes[$row['name']]['columns'][] = $row['column'];
193:         }
194: 
195:         return array_values($indexes);
196:     }
197: 
198: 
199:     /**
200:      * Returns metadata for all foreign keys in a table.
201:      */
202:     public function getForeignKeys($table)
203:     {
204:         /* Does't work with multicolumn foreign keys */
205:         return $this->connection->query("
206:             SELECT
207:                 co.conname::varchar AS name,
208:                 al.attname::varchar AS local,
209:                 cf.relname::varchar AS table,
210:                 af.attname::varchar AS foreign
211:             FROM
212:                 pg_catalog.pg_constraint AS co
213:                 JOIN pg_catalog.pg_class AS cl ON co.conrelid = cl.oid
214:                 JOIN pg_catalog.pg_class AS cf ON co.confrelid = cf.oid
215:                 JOIN pg_catalog.pg_namespace AS nf ON nf.oid = cf.relnamespace
216:                 JOIN pg_catalog.pg_attribute AS al ON al.attrelid = cl.oid AND al.attnum = co.conkey[1]
217:                 JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
218:             WHERE
219:                 co.contype = 'f'
220:                 AND cl.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
221:                 AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
222:         ")->fetchAll();
223:     }
224: 
225: 
226:     /**
227:      * Returns associative array of detected types (IReflection::FIELD_*) in result set.
228:      */
229:     public function getColumnTypes(\PDOStatement $statement)
230:     {
231:         return Nette\Database\Helpers::detectTypes($statement);
232:     }
233: 
234: 
235:     /**
236:      * @param  string
237:      * @return bool
238:      */
239:     public function isSupported($item)
240:     {
241:         return $item === self::SUPPORT_SEQUENCE || $item === self::SUPPORT_SUBSELECT;
242:     }
243: 
244: 
245:     /**
246:      * Converts: schema.name => "schema"."name"
247:      * @param  string
248:      * @return string
249:      */
250:     private function delimiteFQN($name)
251:     {
252:         return implode('.', array_map(array($this, 'delimite'), explode('.', $name)));
253:     }
254: 
255: }
256: 
Nette 2.2 API documentation generated by ApiGen 2.8.0