Packages

  • 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
  • PHP

Classes

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