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 formatDateTime(DateTime $value)
54: {
55: return $value->format("'Y-m-d H:i:s'");
56: }
57:
58:
59:
60: 61: 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: 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: 87:
88: public function normalizeRow($row, $statement)
89: {
90: return $row;
91: }
92:
93:
94:
95:
96:
97:
98:
99: 100: 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: 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: 163:
164: public function getIndexes($table)
165: {
166:
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: 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: 219:
220: public function isSupported($item)
221: {
222: return $item === self::META;
223: }
224:
225: }
226: