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