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