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
  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 date-time for use in a SQL statement.
 52:      */
 53:     public function formatDateTime(DateTime $value)
 54:     {
 55:         return $value->format("'Y-m-d H:i:s'");
 56:     }
 57: 
 58: 
 59: 
 60:     /**
 61:      * Encodes string for use in a LIKE statement.
 62:      */
 63:     public function formatLike($value, $pos)
 64:     {
 65:         throw new NotImplementedException;
 66:     }
 67: 
 68: 
 69: 
 70:     /**
 71:      * Injects LIMIT/OFFSET to the SQL query.
 72:      */
 73:     public function applyLimit(&$sql, $limit, $offset)
 74:     {
 75:         if ($limit >= 0)
 76:             $sql .= ' LIMIT ' . (int) $limit;
 77: 
 78:         if ($offset > 0)
 79:             $sql .= ' OFFSET ' . (int) $offset;
 80:     }
 81: 
 82: 
 83: 
 84:     /**
 85:      * Normalizes result row.
 86:      */
 87:     public function normalizeRow($row, $statement)
 88:     {
 89:         return $row;
 90:     }
 91: 
 92: 
 93: 
 94:     /********************* reflection ****************d*g**/
 95: 
 96: 
 97: 
 98:     /**
 99:      * Returns list of tables.
100:      */
101:     public function getTables()
102:     {
103:         return $this->connection->query("
104:             SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view
105:             FROM information_schema.tables
106:             WHERE table_schema = current_schema()
107:         ")->fetchAll();
108:     }
109: 
110: 
111: 
112:     /**
113:      * Returns metadata for all columns in a table.
114:      */
115:     public function getColumns($table)
116:     {
117:         $primary = (int) $this->connection->query("
118:             SELECT indkey
119:             FROM pg_class
120:             LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
121:             WHERE pg_class.relname = {$this->connection->quote($table)}
122:         ")->fetchColumn(0);
123: 
124:         $columns = array();
125:         foreach ($this->connection->query("
126:             SELECT *
127:             FROM information_schema.columns
128:             WHERE table_name = {$this->connection->quote($table)} AND table_schema = current_schema()
129:             ORDER BY ordinal_position
130:         ") as $row) {
131:             $size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
132:             $columns[] = array(
133:                 'name' => $row['column_name'],
134:                 'table' => $table,
135:                 'nativetype' => strtoupper($row['udt_name']),
136:                 'size' => $size ? $size : NULL,
137:                 'nullable' => $row['is_nullable'] === 'YES',
138:                 'default' => $row['column_default'],
139:                 'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval',
140:                 'primary' => (int) $row['ordinal_position'] === $primary,
141:                 'vendor' => (array) $row,
142:             );
143:         }
144:         return $columns;
145:     }
146: 
147: 
148: 
149:     /**
150:      * Returns metadata for all indexes in a table.
151:      */
152:     public function getIndexes($table)
153:     {
154:         $columns = array();
155:         foreach ($this->connection->query("
156:             SELECT ordinal_position, column_name
157:             FROM information_schema.columns
158:             WHERE table_name = {$this->connection->quote($table)} AND table_schema = current_schema()
159:             ORDER BY ordinal_position
160:         ") as $row) {
161:             $columns[$row['ordinal_position']] = $row['column_name'];
162:         }
163: 
164:         $indexes = array();
165:         foreach ($this->connection->query("
166:             SELECT pg_class2.relname, indisunique, indisprimary, indkey
167:             FROM pg_class
168:             LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
169:             INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
170:             WHERE pg_class.relname = {$this->connection->quote($table)}
171:         ") as $row) {
172:             $indexes[$row['relname']]['name'] = $row['relname'];
173:             $indexes[$row['relname']]['unique'] = $row['indisunique'] === 't';
174:             $indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't';
175:             foreach (explode(' ', $row['indkey']) as $index) {
176:                 $indexes[$row['relname']]['columns'][] = $columns[$index];
177:             }
178:         }
179:         return array_values($indexes);
180:     }
181: 
182: 
183: 
184:     /**
185:      * Returns metadata for all foreign keys in a table.
186:      */
187:     public function getForeignKeys($table)
188:     {
189:         throw new NotImplementedException;
190:     }
191: 
192: 
193: 
194:     /**
195:      * @return bool
196:      */
197:     public function isSupported($item)
198:     {
199:         return $item === self::META;
200:     }
201: 
202: }
203: 
Nette Framework 2.0.3 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0