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