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
  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 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:         $value = strtr($value, array("'" => "''", '\\' => '\\\\', '%' => '\\\\%', '_' => '\\\\_'));
 66:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
 67:     }
 68: 
 69: 
 70: 
 71:     /**
 72:      * Injects LIMIT/OFFSET to the SQL query.
 73:      */
 74:     public function applyLimit(&$sql, $limit, $offset)
 75:     {
 76:         if ($limit >= 0)
 77:             $sql .= ' LIMIT ' . (int) $limit;
 78: 
 79:         if ($offset > 0)
 80:             $sql .= ' OFFSET ' . (int) $offset;
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Normalizes result row.
 87:      */
 88:     public function normalizeRow($row, $statement)
 89:     {
 90:         return $row;
 91:     }
 92: 
 93: 
 94: 
 95:     /********************* reflection ****************d*g**/
 96: 
 97: 
 98: 
 99:     /**
100:      * Returns list of tables.
101:      */
102:     public function getTables()
103:     {
104:         $tables = array();
105:         foreach ($this->connection->query("
106:             SELECT
107:                 table_name AS name,
108:                 table_type = 'VIEW' AS view
109:             FROM
110:                 information_schema.tables
111:             WHERE
112:                 table_schema = current_schema()
113:         ") as $row) {
114:             $tables[] = (array) $row;
115:         }
116: 
117:         return $tables;
118:     }
119: 
120: 
121: 
122:     /**
123:      * Returns metadata for all columns in a table.
124:      */
125:     public function getColumns($table)
126:     {
127:         $columns = array();
128:         foreach ($this->connection->query("
129:             SELECT
130:                 c.column_name AS name,
131:                 c.table_name AS table,
132:                 upper(c.udt_name) AS nativetype,
133:                 greatest(c.character_maximum_length, c.numeric_precision) AS size,
134:                 FALSE AS unsigned,
135:                 c.is_nullable = 'YES' AS nullable,
136:                 c.column_default AS default,
137:                 coalesce(tc.constraint_type = 'PRIMARY KEY', FALSE) AND strpos(c.column_default, 'nextval') = 1 AS autoincrement,
138:                 coalesce(tc.constraint_type = 'PRIMARY KEY', FALSE) AS primary
139:             FROM
140:                 information_schema.columns AS c
141:                 LEFT JOIN information_schema.constraint_column_usage AS ccu USING(table_catalog, table_schema, table_name, column_name)
142:                 LEFT JOIN information_schema.table_constraints AS tc USING(constraint_catalog, constraint_schema, constraint_name)
143:             WHERE
144:                 c.table_name = {$this->connection->quote($table)}
145:                 AND
146:                 c.table_schema = current_schema()
147:                 AND
148:                 (tc.constraint_type IS NULL OR tc.constraint_type = 'PRIMARY KEY')
149:             ORDER BY
150:                 c.ordinal_position
151:         ") as $row) {
152:             $row['vendor'] = array();
153:             $columns[] = (array) $row;
154:         }
155: 
156:         return $columns;
157:     }
158: 
159: 
160: 
161:     /**
162:      * Returns metadata for all indexes in a table.
163:      */
164:     public function getIndexes($table)
165:     {
166:         /* There is no information about all indexes in information_schema, so pg catalog must be used */
167:         $indexes = array();
168:         foreach ($this->connection->query("
169:             SELECT
170:                 c2.relname AS name,
171:                 indisunique AS unique,
172:                 indisprimary AS primary,
173:                 attname AS column
174:             FROM
175:                 pg_class AS c1
176:                 JOIN pg_namespace ON c1.relnamespace = pg_namespace.oid
177:                 JOIN pg_index ON c1.oid = indrelid
178:                 JOIN pg_class AS c2 ON indexrelid = c2.oid
179:                 LEFT JOIN pg_attribute ON c1.oid = attrelid AND attnum = ANY(indkey)
180:             WHERE
181:                 nspname = current_schema()
182:                 AND
183:                 c1.relkind = 'r'
184:                 AND
185:                 c1.relname = {$this->connection->quote($table)}
186:         ") as $row) {
187:             $indexes[$row['name']]['name'] = $row['name'];
188:             $indexes[$row['name']]['unique'] = $row['unique'];
189:             $indexes[$row['name']]['primary'] = $row['primary'];
190:             $indexes[$row['name']]['columns'][] = $row['column'];
191:         }
192: 
193:         return array_values($indexes);
194:     }
195: 
196: 
197: 
198:     /**
199:      * Returns metadata for all foreign keys in a table.
200:      */
201:     public function getForeignKeys($table)
202:     {
203:         return $this->connection->query("
204:             SELECT tc.table_name AS name, kcu.column_name AS local, ccu.table_name AS table, ccu.column_name AS foreign
205:             FROM information_schema.table_constraints AS tc
206:             JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.constraint_schema = kcu.constraint_schema
207:             JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.constraint_schema = tc.constraint_schema
208:             WHERE
209:                 constraint_type = 'FOREIGN KEY' AND
210:                 tc.table_name = {$this->connection->quote($table)} AND
211:                 tc.constraint_schema = current_schema()
212:         ")->fetchAll();
213:     }
214: 
215: 
216: 
217:     /**
218:      * @return bool
219:      */
220:     public function isSupported($item)
221:     {
222:         return $item === self::META;
223:     }
224: 
225: }
226: 
Nette Framework 2.0.4 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0