cvs: pear /MDB2/MDB2/Driver/Reverse pgsql.php

From: Date: Tue, 13 May 2008 15:45:46 +0000
Subject: cvs: pear /MDB2/MDB2/Driver/Reverse pgsql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-52318@lists.php.net to get a copy of this message
quipo Tue May 13 15:45:46 2008 UTC Modified files: /pear/MDB2/MDB2/Driver/Reverse pgsql.php Log: fixed bug #13877 in getTableConstraintDefinition(): UNIQUE index not always recognized as constraint http://cvs.php.net/viewvc.cgi/pear/MDB2/MDB2/Driver/Reverse/pgsql.php?r1=1.73&r2=1.74&diff_format=u Index: pear/MDB2/MDB2/Driver/Reverse/pgsql.php diff -u pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.73 pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.74 --- pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.73 Tue May 13 13:37:27 2008 +++ pear/MDB2/MDB2/Driver/Reverse/pgsql.php Tue May 13 15:45:46 2008 @@ -43,7 +43,7 @@ // | Lorenzo Alberton <l.alberton@quipo.it> | // +----------------------------------------------------------------------+ // -// $Id: pgsql.php,v 1.73 2008/05/13 13:37:27 quipo Exp $ +// $Id: pgsql.php,v 1.74 2008/05/13 15:45:46 quipo Exp $ require_once 'MDB2/Driver/Reverse/Common.php'; @@ -299,7 +299,7 @@ LEFT JOIN pg_class t ON c.conrelid = t.oid LEFT JOIN pg_class t2 ON c.confrelid = t2.oid WHERE c.conname = %s - AND t.relname = " . $db->quote($table, 'text'); + AND t.relname = " . $db->quote($table, 'text'); $constraint_name_mdb2 = $db->getIndexName($constraint_name); $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC); if (PEAR::isError($row) || empty($row)) { @@ -310,10 +310,41 @@ if (PEAR::isError($row)) { return $row; } - + $uniqueIndex = false; if (empty($row)) { - return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, - $constraint_name . ' is not an existing table constraint', __FUNCTION__); + // We might be looking for a UNIQUE index that was not created + // as a constraint but should be treated as such. + $query = 'SELECT relname AS constraint_name, + indkey, + 0 AS "check", + 0 AS "foreign", + 0 AS "primary", + 1 AS "unique", + 0 AS deferrable, + 0 AS initiallydeferred, + NULL AS references_table, + NULL AS onupdate, + NULL AS ondelete, + NULL AS match + FROM pg_index, pg_class + WHERE pg_class.oid = pg_index.indexrelid + AND indisunique = \'t\' + AND pg_class.relname = %s'; + $constraint_name_mdb2 = $db->getIndexName($constraint_name); + $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC); + if (PEAR::isError($row) || empty($row)) { + // fallback to the given $index_name, without transformation + $constraint_name_mdb2 = $constraint_name; + $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC); + } + if (PEAR::isError($row)) { + return $row; + } + if (empty($row)) { + return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, + $constraint_name . ' is not an existing table constraint', __FUNCTION__); + } + $uniqueIndex = true; } $row = array_change_key_case($row, CASE_LOWER); @@ -335,12 +366,26 @@ 'match' => $row['match'], ); + if ($uniqueIndex) { + $db->loadModule('Manager', null, true); + $columns = $db->manager->listTableFields($table_name); + $index_column_numbers = explode(' ', $row['indkey']); + $colpos = 1; + foreach ($index_column_numbers as $number) { + $definition['fields'][$columns[($number - 1)]] = array( + 'position' => $colpos++, + 'sorting' => 'ascending', + ); + } + return $definition; + } + $query = 'SELECT a.attname - FROM pg_constraint c - LEFT JOIN pg_class t ON c.conrelid = t.oid - LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.conkey) - WHERE c.conname = %s - AND t.relname = ' . $db->quote($table, 'text'); + FROM pg_constraint c + LEFT JOIN pg_class t ON c.conrelid = t.oid + LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.conkey) + WHERE c.conname = %s + AND t.relname = ' . $db->quote($table, 'text'); $fields = $db->queryCol(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null); if (PEAR::isError($fields)) { return $fields;

« previous php.pear.cvs (#52318) next »