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

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • 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 PgSqlDriver extends Object implements ISupplementalDriver
 22: {
 23:     /** @var Connection */
 24:     private $connection;
 25: 
 26: 
 27: 
 28:     public function __construct(Connection $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:      * Normalizes result row.
 97:      */
 98:     public function normalizeRow($row, $statement)
 99:     {
100:         return $row;
101:     }
102: 
103: 
104: 
105:     /********************* reflection ****************d*g**/
106: 
107: 
108: 
109:     /**
110:      * Returns list of tables.
111:      */
112:     public function getTables()
113:     {
114:         $tables = array();
115:         foreach ($this->connection->query("
116:             SELECT
117:                 c.relname::varchar AS name,
118:                 c.relkind = 'v' AS view
119:             FROM
120:                 pg_catalog.pg_class AS c
121:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
122:             WHERE
123:                 c.relkind IN ('r', 'v')
124:                 AND n.nspname = current_schema()
125:             ORDER BY
126:                 c.relname
127:         ") as $row) {
128:             $tables[] = (array) $row;
129:         }
130: 
131:         return $tables;
132:     }
133: 
134: 
135: 
136:     /**
137:      * Returns metadata for all columns in a table.
138:      */
139:     public function getColumns($table)
140:     {
141:         $columns = array();
142:         foreach ($this->connection->query("
143:             SELECT
144:                 a.attname::varchar AS name,
145:                 c.relname::varchar AS table,
146:                 upper(t.typname) AS nativetype,
147:                 NULL AS size,
148:                 FALSE AS unsigned,
149:                 NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS nullable,
150:                 ad.adsrc::varchar AS default,
151:                 coalesce(co.contype = 'p' AND strpos(ad.adsrc, 'nextval') = 1, FALSE) AS autoincrement,
152:                 coalesce(co.contype = 'p', FALSE) AS primary,
153:                 substring(ad.adsrc from 'nextval[(]''\"?([^''\"]+)') AS sequence
154:             FROM
155:                 pg_catalog.pg_attribute AS a
156:                 JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid
157:                 JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
158:                 JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid
159:                 LEFT JOIN pg_catalog.pg_attrdef AS ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
160:                 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)
161:             WHERE
162:                 c.relkind IN ('r', 'v')
163:                 AND c.relname::varchar = {$this->connection->quote($table)}
164:                 AND n.nspname = current_schema()
165:                 AND a.attnum > 0
166:                 AND NOT a.attisdropped
167:             ORDER BY
168:                 a.attnum
169:         ") as $row) {
170:             $column = (array) $row;
171:             $column['vendor'] = $column;
172:             unset($column['sequence']);
173: 
174:             $columns[] = $column;
175:         }
176: 
177:         return $columns;
178:     }
179: 
180: 
181: 
182:     /**
183:      * Returns metadata for all indexes in a table.
184:      */
185:     public function getIndexes($table)
186:     {
187:         $indexes = array();
188:         foreach ($this->connection->query("
189:             SELECT
190:                 c2.relname::varchar AS name,
191:                 i.indisunique AS unique,
192:                 i.indisprimary AS primary,
193:                 a.attname::varchar AS column
194:             FROM
195:                 pg_catalog.pg_class AS c1
196:                 JOIN pg_catalog.pg_namespace AS n ON c1.relnamespace = n.oid
197:                 JOIN pg_catalog.pg_index AS i ON c1.oid = i.indrelid
198:                 JOIN pg_catalog.pg_class AS c2 ON i.indexrelid = c2.oid
199:                 LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
200:             WHERE
201:                 n.nspname = current_schema()
202:                 AND c1.relkind = 'r'
203:                 AND c1.relname = {$this->connection->quote($table)}
204:         ") as $row) {
205:             $indexes[$row['name']]['name'] = $row['name'];
206:             $indexes[$row['name']]['unique'] = $row['unique'];
207:             $indexes[$row['name']]['primary'] = $row['primary'];
208:             $indexes[$row['name']]['columns'][] = $row['column'];
209:         }
210: 
211:         return array_values($indexes);
212:     }
213: 
214: 
215: 
216:     /**
217:      * Returns metadata for all foreign keys in a table.
218:      */
219:     public function getForeignKeys($table)
220:     {
221:         /* Does't work with multicolumn foreign keys */
222:         return $this->connection->query("
223:             SELECT
224:                 co.conname::varchar AS name,
225:                 al.attname::varchar AS local,
226:                 cf.relname::varchar AS table,
227:                 af.attname::varchar AS foreign
228:             FROM
229:                 pg_catalog.pg_constraint AS co
230:                 JOIN pg_catalog.pg_namespace AS n ON co.connamespace = n.oid
231:                 JOIN pg_catalog.pg_class AS cl ON co.conrelid = cl.oid
232:                 JOIN pg_catalog.pg_class AS cf ON co.confrelid = cf.oid
233:                 JOIN pg_catalog.pg_attribute AS al ON al.attrelid = cl.oid AND al.attnum = co.conkey[1]
234:                 JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
235:             WHERE
236:                 n.nspname = current_schema()
237:                 AND co.contype = 'f'
238:                 AND cl.relname = {$this->connection->quote($table)}
239:         ")->fetchAll();
240:     }
241: 
242: 
243: 
244:     /**
245:      * @return bool
246:      */
247:     public function isSupported($item)
248:     {
249:         return $item === self::SUPPORT_COLUMNS_META || $item === self::SUPPORT_SEQUENCE;
250:     }
251: 
252: }
253: 
Nette Framework 2.0.8 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0