48require_once
'MDB2/Driver/Reverse/Common.php';
76 $result = $db->loadModule(
'Datatype',
null,
true);
81 $query =
"SELECT a.attname AS name,
86 WHEN 'numeric' THEN (a.atttypmod / 65536)
87 WHEN 'decimal' THEN (a.atttypmod / 65536)
88 WHEN 'money' THEN (a.atttypmod / 65536)
97 WHEN 'numeric' THEN (a.atttypmod % 65536) - 4
98 WHEN 'decimal' THEN (a.atttypmod % 65536) - 4
99 WHEN 'money' THEN (a.atttypmod % 65536) - 4
105 (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
107 WHERE d.adrelid = a.attrelid
108 AND d.adnum = a.attnum
114 WHERE c.relname = ".$db->quote($table,
'text').
"
115 AND a.atttypid = t.oid
116 AND c.oid = a.attrelid
117 AND NOT a.attisdropped
119 AND a.attname = ".$db->quote($field_name,
'text').
"
126 if (empty($column)) {
128 'it was not specified an existing table column', __FUNCTION__);
131 $column = array_change_key_case($column, CASE_LOWER);
132 $mapped_datatype = $db->datatype->mapNativeDatatype($column);
133 if (PEAR::IsError($mapped_datatype)) {
134 return $mapped_datatype;
136 list($types, $length, $unsigned, $fixed) = $mapped_datatype;
138 if (!empty($column[
'attnotnull']) && $column[
'attnotnull'] ==
't') {
142 if ($column[
'atthasdef'] ===
't'
143 && !preg_match(
"/nextval\('([^']+)'/", $column[
'default'])
145 $default = $column[
'default'];#substr($column[
'adsrc'], 1, -1);
146 if (is_null($default) && $notnull) {
150 $autoincrement =
false;
151 if (preg_match(
"/nextval\('([^']+)'/", $column[
'default'], $nextvals)) {
152 $autoincrement =
true;
154 $definition[0] = array(
'notnull' => $notnull,
'nativetype' => $column[
'type']);
155 if (!is_null($length)) {
156 $definition[0][
'length'] = $length;
158 if (!is_null($unsigned)) {
159 $definition[0][
'unsigned'] = $unsigned;
161 if (!is_null($fixed)) {
162 $definition[0][
'fixed'] = $fixed;
164 if ($default !==
false) {
165 $definition[0][
'default'] = $default;
167 if ($autoincrement !==
false) {
168 $definition[0][
'autoincrement'] = $autoincrement;
170 foreach ($types as $key => $type) {
171 $definition[$key] = $definition[0];
172 if ($type ==
'clob' || $type ==
'blob') {
173 unset($definition[$key][
'default']);
175 $definition[$key][
'type'] = $type;
176 $definition[$key][
'mdb2type'] = $type;
198 $query =
'SELECT relname, indkey FROM pg_index, pg_class';
199 $query.=
' WHERE pg_class.oid = pg_index.indexrelid';
200 $query.=
" AND indisunique != 't' AND indisprimary != 't'";
201 $query.=
' AND pg_class.relname = %s';
202 $index_name_mdb2 = $db->getIndexName($index_name);
214 'it was not specified an existing table index', __FUNCTION__);
217 $row = array_change_key_case(
$row, CASE_LOWER);
219 $db->loadModule(
'Manager',
null,
true);
220 $columns = $db->manager->listTableFields($table);
222 $definition = array();
224 $index_column_numbers = explode(
' ',
$row[
'indkey']);
227 foreach ($index_column_numbers as $number) {
228 $definition[
'fields'][
$columns[($number - 1)]] = array(
229 'position' => $colpos++,
230 'sorting' =>
'ascending',
253 $query =
'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
254 $query.=
' WHERE pg_class.oid = pg_index.indexrelid';
255 $query.=
" AND (indisunique = 't' OR indisprimary = 't')";
256 $query.=
' AND pg_class.relname = %s';
257 $constraint_name_mdb2 = $db->getIndexName($constraint_name);
269 $constraint_name .
' is not an existing table constraint', __FUNCTION__);
272 $row = array_change_key_case(
$row, CASE_LOWER);
274 $db->loadModule(
'Manager',
null,
true);
275 $columns = $db->manager->listTableFields($table);
277 $definition = array();
278 if (
$row[
'indisprimary'] ==
't') {
279 $definition[
'primary'] =
true;
280 } elseif (
$row[
'indisunique'] ==
't') {
281 $definition[
'unique'] =
true;
284 $index_column_numbers = explode(
' ',
$row[
'indkey']);
287 foreach ($index_column_numbers as $number) {
288 $definition[
'fields'][
$columns[($number - 1)]] = array(
289 'position' => $colpos++,
290 'sorting' =>
'ascending',
320 $query =
"SELECT trg.tgname AS trigger_name,
321 tbl.relname AS table_name,
323 WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'
326 CASE trg.tgtype & cast(2 as int2)
330 CASE trg.tgtype & cast(28 as int2)
331 WHEN 16 THEN 'UPDATE'
334 WHEN 20 THEN 'INSERT, UPDATE'
335 WHEN 28 THEN 'INSERT, UPDATE, DELETE'
336 WHEN 24 THEN 'UPDATE, DELETE'
337 WHEN 12 THEN 'INSERT, DELETE'
338 END AS trigger_event,
339 trg.tgenabled AS trigger_enabled,
340 obj_description(trg.oid, 'pg_trigger') AS trigger_comment
344 WHERE trg.tgrelid = tbl.oid
345 AND trg.tgfoid = p.oid
346 AND trg.tgname = ". $db->quote($trigger,
'text');
348 'trigger_name' =>
'text',
349 'table_name' =>
'text',
350 'trigger_body' =>
'text',
351 'trigger_type' =>
'text',
352 'trigger_event' =>
'text',
353 'trigger_comment' =>
'text',
354 'trigger_enabled' =>
'boolean',
383 return parent::tableInfo(
$result, $mode);
392 if (!is_resource($resource)) {
394 'Could not generate result resource', __FUNCTION__);
398 if ($db->options[
'field_case'] == CASE_LOWER) {
399 $case_func =
'strtolower';
401 $case_func =
'strtoupper';
404 $case_func =
'strval';
407 $count = @pg_num_fields($resource);
411 $res[
'num_fields'] = $count;
414 $db->loadModule(
'Datatype',
null,
true);
415 for ($i = 0; $i < $count; $i++) {
417 'table' => function_exists(
'pg_field_table') ? @pg_field_table($resource, $i) :
'',
418 'name' => $case_func(@pg_field_name($resource, $i)),
419 'type' => @pg_field_type($resource, $i),
420 'length' => @pg_field_size($resource, $i),
423 $mdb2type_info = $db->datatype->mapNativeDatatype(
$res[$i]);
425 return $mdb2type_info;
427 $res[$i][
'mdb2type'] = $mdb2type_info[0][0];
429 $res[
'order'][
$res[$i][
'name']] = $i;
432 $res[
'ordertable'][
$res[$i][
'table']][
$res[$i][
'name']] = $i;
const MDB2_PORTABILITY_FIX_CASE
Portability: convert names of tables and fields to case defined in the "field_case" option when using...
const MDB2_ERROR_NOT_FOUND
const MDB2_ERROR_NEED_MORE_DATA
const MDB2_FETCHMODE_ASSOC
Column data indexed by column names.
const MDB2_TABLEINFO_ORDERTABLE
const MDB2_TABLEINFO_ORDER
These are constants for the tableInfo-function they are bitwised or'ed.
getTriggerDefinition($trigger)
Get the structure of a trigger into an array.
getTableConstraintDefinition($table, $constraint_name)
Get the structure of a constraint into an array.
tableInfo($result, $mode=null)
Returns information about a table or a result set.
getTableIndexDefinition($table, $index_name)
Get the structure of an index into an array.
getTableFieldDefinition($table, $field_name)
Get the structure of a field into an array.
& getDBInstance()
Get the instance of MDB2 associated with the module instance.
isResultCommon($value)
Tell whether a value is a MDB2 result implementing the common interface.
isError($data, $code=null)
Tell whether a value is a PEAR error.