ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilDBPdoReversePostgres.php
Go to the documentation of this file.
1<?php
2
9{
10
11 // {{{ getTableFieldDefinition()
12
21 public function getTableFieldDefinition($table, $field_name)
22 {
28 $result = $this->db_instance->loadModule(ilDBConstants::MODULE_REVERSE);
29
30 $query = "SELECT a.attname AS name,
31 t.typname AS type,
32 CASE a.attlen
33 WHEN -1 THEN
34 CASE t.typname
35 WHEN 'numeric' THEN (a.atttypmod / 65536)
36 WHEN 'decimal' THEN (a.atttypmod / 65536)
37 WHEN 'money' THEN (a.atttypmod / 65536)
38 ELSE CASE a.atttypmod
39 WHEN -1 THEN NULL
40 ELSE a.atttypmod - 4
41 END
42 END
43 ELSE a.attlen
44 END AS length,
45 CASE t.typname
46 WHEN 'numeric' THEN (a.atttypmod % 65536) - 4
47 WHEN 'decimal' THEN (a.atttypmod % 65536) - 4
48 WHEN 'money' THEN (a.atttypmod % 65536) - 4
49 ELSE 0
50 END AS scale,
51 a.attnotnull,
52 a.atttypmod,
53 a.atthasdef,
54 (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
55 FROM pg_attrdef d
56 WHERE d.adrelid = a.attrelid
57 AND d.adnum = a.attnum
58 AND a.atthasdef
59 ) as default
60 FROM pg_attribute a,
61 pg_class c,
62 pg_type t
63 WHERE c.relname = " . $db->quote($table, 'text') . "
64 AND a.atttypid = t.oid
65 AND c.oid = a.attrelid
66 AND NOT a.attisdropped
67 AND a.attnum > 0
68 AND a.attname = " . $db->quote($field_name, 'text') . "
69 ORDER BY a.attnum";
70 $column = $db->queryRow($query, null, ilDBConstants::FETCHMODE_ASSOC);
71
72 if (empty($column)) {
73 throw new ilDatabaseException('it was not specified an existing table column');
74 }
75
76 $column = array_change_key_case($column, CASE_LOWER);
77 $mapped_datatype = $db->getFieldDefinition()->mapNativeDatatype($column);
78
79 list($types, $length, $unsigned, $fixed) = $mapped_datatype;
80 $notnull = false;
81 if (!empty($column['attnotnull']) && $column['attnotnull'] == 't') {
82 $notnull = true;
83 }
84 $default = null;
85 if ($column['atthasdef'] === 't'
86 && !preg_match("/nextval\‍('([^']+)'/", $column['default'])
87 ) {
88 $default = $column['default'];#substr($column['adsrc'], 1, -1);
89 if (is_null($default) && $notnull) {
90 $default = '';
91 }
92 }
93 $autoincrement = false;
94 if (preg_match("/nextval\‍('([^']+)'/", $column['default'], $nextvals)) {
95 $autoincrement = true;
96 }
97 $definition[0] = array( 'notnull' => $notnull, 'nativetype' => $column['type'] );
98 if (!is_null($length)) {
99 $definition[0]['length'] = $length;
100 }
101 if (!is_null($unsigned)) {
102 $definition[0]['unsigned'] = $unsigned;
103 }
104 if (!is_null($fixed)) {
105 $definition[0]['fixed'] = $fixed;
106 }
107 if ($default !== false) {
108 $definition[0]['default'] = $default;
109 }
110 if ($autoincrement !== false) {
111 $definition[0]['autoincrement'] = $autoincrement;
112 }
113 foreach ($types as $key => $type) {
114 $definition[$key] = $definition[0];
115 if ($type == 'clob' || $type == 'blob') {
116 unset($definition[$key]['default']);
117 }
118 $definition[$key]['type'] = $type;
119 $definition[$key]['mdb2type'] = $type;
120 }
121
122 return $definition;
123 }
124
125
132 public function getTableIndexDefinition($table, $index_name)
133 {
134 $db = $this->db_instance;
135 $manager = $db->loadModule(ilDBConstants::MODULE_MANAGER);
140 $query = 'SELECT relname, indkey FROM pg_index, pg_class';
141 $query .= ' WHERE pg_class.oid = pg_index.indexrelid';
142 $query .= " AND indisunique != 't' AND indisprimary != 't'";
143 $query .= ' AND pg_class.relname = %s';
144 $index_name_mdb2 = $db->getIndexName($index_name);
145 $failed = false;
146 try {
147 $row = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2, 'text')), null, ilDBConstants::FETCHMODE_DEFAULT);
148 } catch (Exception $e) {
149 $failed = true;
150 }
151 if ($failed || empty($row)) {
152 $row = $db->queryRow(sprintf($query, $db->quote($index_name, 'text')), null, ilDBConstants::FETCHMODE_DEFAULT);
153 }
154
155 if (empty($row)) {
156 throw new ilDatabaseException('it was not specified an existing table index');
157 }
158
159 $row = array_change_key_case($row, CASE_LOWER);
160
161 $columns = $manager->listTableFields($table);
162
163 $definition = array();
164
165 $index_column_numbers = explode(' ', $row['indkey']);
166
167 $colpos = 1;
168 foreach ($index_column_numbers as $number) {
169 $definition['fields'][$columns[($number - 1)]] = array(
170 'position' => $colpos++,
171 'sorting' => 'ascending',
172 );
173 }
174
175 return $definition;
176 }
177
178 // }}}
179 // {{{ getTableConstraintDefinition()
188 public function getTableConstraintDefinition($table, $constraint_name)
189 {
190 $db = $this->db_instance;
191
192 $query = 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
193 $query .= ' WHERE pg_class.oid = pg_index.indexrelid';
194 $query .= " AND (indisunique = 't' OR indisprimary = 't')";
195 $query .= ' AND pg_class.relname = %s';
196 $constraint_name_mdb2 = $db->getIndexName($constraint_name);
197 try {
198 $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, ilDBConstants::FETCHMODE_ASSOC);
199 } catch (Exception $e) {
200 }
201
202 if ($e instanceof PDOException || empty($row)) {
203 // fallback to the given $index_name, without transformation
204 $row = $db->queryRow(sprintf($query, $db->quote($constraint_name, 'text')), null, ilDBConstants::FETCHMODE_ASSOC);
205 }
206
207 if (empty($row)) {
208 throw new ilDatabaseException($constraint_name . ' is not an existing table constraint');
209 }
210
211 $row = array_change_key_case($row, CASE_LOWER);
212 $columns = $db->loadModule(ilDBConstants::MODULE_MANAGER)->listTableFields($table);
213
214 $definition = array();
215 if ($row['indisprimary'] == 't') {
216 $definition['primary'] = true;
217 } elseif ($row['indisunique'] == 't') {
218 $definition['unique'] = true;
219 }
220
221 $index_column_numbers = explode(' ', $row['indkey']);
222
223 $colpos = 1;
224 foreach ($index_column_numbers as $number) {
225 $definition['fields'][$columns[($number - 1)]] = array(
226 'position' => $colpos++,
227 'sorting' => 'ascending',
228 );
229 }
230
231 return $definition;
232 }
233
234 // }}}
235 // {{{ getTriggerDefinition()
236
251 public function getTriggerDefinition($trigger)
252 {
253 $db = $this->db_instance;
254
255 $query = "SELECT trg.tgname AS trigger_name,
256 tbl.relname AS table_name,
257 CASE
258 WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'
259 ELSE ''
260 END AS trigger_body,
261 CASE trg.tgtype & cast(2 as int2)
262 WHEN 0 THEN 'AFTER'
263 ELSE 'BEFORE'
264 END AS trigger_type,
265 CASE trg.tgtype & cast(28 as int2)
266 WHEN 16 THEN 'UPDATE'
267 WHEN 8 THEN 'DELETE'
268 WHEN 4 THEN 'INSERT'
269 WHEN 20 THEN 'INSERT, UPDATE'
270 WHEN 28 THEN 'INSERT, UPDATE, DELETE'
271 WHEN 24 THEN 'UPDATE, DELETE'
272 WHEN 12 THEN 'INSERT, DELETE'
273 END AS trigger_event,
274 trg.tgenabled AS trigger_enabled,
275 obj_description(trg.oid, 'pg_trigger') AS trigger_comment
276 FROM pg_trigger trg,
277 pg_class tbl,
278 pg_proc p
279 WHERE trg.tgrelid = tbl.oid
280 AND trg.tgfoid = p.oid
281 AND trg.tgname = " . $db->quote($trigger, 'text');
282 $types = array(
283 'trigger_name' => 'text',
284 'table_name' => 'text',
285 'trigger_body' => 'text',
286 'trigger_type' => 'text',
287 'trigger_event' => 'text',
288 'trigger_comment' => 'text',
289 'trigger_enabled' => 'boolean',
290 );
291
292 return $db->queryRow($query, $types, ilDBConstants::FETCHMODE_ASSOC);
293 }
294}
$result
$failed
Definition: Utf8Test.php:85
if(! $in) $columns
Definition: Utf8Test.php:45
An exception for terminatinating execution or to throw for unit testing.
getTriggerDefinition($trigger)
Get the structure of a trigger into an array.
getTableConstraintDefinition($table, $constraint_name)
Get the structure of a constraint into an array.
Class ilDBPdoReverse.
getTableIndexDefinition($table, $index_name)
getTableFieldDefinition($table_name, $field_name)
Class ilDatabaseException.
$query
$type