1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class PgSqlDriver extends Object implements ISupplementalDriver
22: {
23:
24: private $connection;
25:
26:
27:
28: public function __construct(Connection $connection, array $options)
29: {
30: $this->connection = $connection;
31: }
32:
33:
34:
35:
36:
37:
38:
39: 40: 41:
42: public function delimite($name)
43: {
44:
45: return '"' . str_replace('"', '""', $name) . '"';
46: }
47:
48:
49:
50: 51: 52:
53: public function formatBool($value)
54: {
55: return $value ? 'TRUE' : 'FALSE';
56: }
57:
58:
59:
60: 61: 62:
63: public function formatDateTime(DateTime $value)
64: {
65: return $value->format("'Y-m-d H:i:s'");
66: }
67:
68:
69:
70: 71: 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: 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:
98: public function normalizeRow($row, $statement)
99: {
100: return $row;
101: }
102:
103:
104:
105:
106:
107:
108:
109: 110: 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: 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: 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: 218:
219: public function getForeignKeys($table)
220: {
221:
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: 246:
247: public function isSupported($item)
248: {
249: return $item === self::SUPPORT_COLUMNS_META || $item === self::SUPPORT_SEQUENCE;
250: }
251:
252: }
253: