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: throw new NotImplementedException;
66: }
67:
68:
69:
70: 71: 72:
73: public function applyLimit(&$sql, $limit, $offset)
74: {
75: if ($limit >= 0)
76: $sql .= ' LIMIT ' . (int) $limit;
77:
78: if ($offset > 0)
79: $sql .= ' OFFSET ' . (int) $offset;
80: }
81:
82:
83:
84: 85: 86:
87: public function normalizeRow($row, $statement)
88: {
89: return $row;
90: }
91:
92:
93:
94:
95:
96:
97:
98: 99: 100:
101: public function getTables()
102: {
103: return $this->connection->query("
104: SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view
105: FROM information_schema.tables
106: WHERE table_schema = current_schema()
107: ")->fetchAll();
108: }
109:
110:
111:
112: 113: 114:
115: public function getColumns($table)
116: {
117: $primary = (int) $this->connection->query("
118: SELECT indkey
119: FROM pg_class
120: LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
121: WHERE pg_class.relname = {$this->connection->quote($table)}
122: ")->fetchColumn(0);
123:
124: $columns = array();
125: foreach ($this->connection->query("
126: SELECT *
127: FROM information_schema.columns
128: WHERE table_name = {$this->connection->quote($table)} AND table_schema = current_schema()
129: ORDER BY ordinal_position
130: ") as $row) {
131: $size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
132: $columns[] = array(
133: 'name' => $row['column_name'],
134: 'table' => $table,
135: 'nativetype' => strtoupper($row['udt_name']),
136: 'size' => $size ? $size : NULL,
137: 'nullable' => $row['is_nullable'] === 'YES',
138: 'default' => $row['column_default'],
139: 'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval',
140: 'primary' => (int) $row['ordinal_position'] === $primary,
141: 'vendor' => (array) $row,
142: );
143: }
144: return $columns;
145: }
146:
147:
148:
149: 150: 151:
152: public function getIndexes($table)
153: {
154: $columns = array();
155: foreach ($this->connection->query("
156: SELECT ordinal_position, column_name
157: FROM information_schema.columns
158: WHERE table_name = {$this->connection->quote($table)} AND table_schema = current_schema()
159: ORDER BY ordinal_position
160: ") as $row) {
161: $columns[$row['ordinal_position']] = $row['column_name'];
162: }
163:
164: $indexes = array();
165: foreach ($this->connection->query("
166: SELECT pg_class2.relname, indisunique, indisprimary, indkey
167: FROM pg_class
168: LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
169: INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
170: WHERE pg_class.relname = {$this->connection->quote($table)}
171: ") as $row) {
172: $indexes[$row['relname']]['name'] = $row['relname'];
173: $indexes[$row['relname']]['unique'] = $row['indisunique'] === 't';
174: $indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't';
175: foreach (explode(' ', $row['indkey']) as $index) {
176: $indexes[$row['relname']]['columns'][] = $columns[$index];
177: }
178: }
179: return array_values($indexes);
180: }
181:
182:
183:
184: 185: 186:
187: public function getForeignKeys($table)
188: {
189: throw new NotImplementedException;
190: }
191:
192:
193:
194: 195: 196:
197: public function isSupported($item)
198: {
199: return $item === self::META;
200: }
201:
202: }
203: