fix duplicate constraints in the constraints page
authorioguix <ioguix>
Fri, 28 Dec 2007 16:21:25 +0000 (16:21 +0000)
committerioguix <ioguix>
Fri, 28 Dec 2007 16:21:25 +0000 (16:21 +0000)
classes/database/Postgres73.php
classes/database/Postgres74.php
classes/database/Postgres82.php
tblproperties.php

index 9bf26a09bb2f30908afa6426a1a0ebffe469bf58..9e3d789a9953d128142bdf7e87bb75c96451d9cc 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres73.php,v 1.184 2007/12/28 15:39:45 ioguix Exp $
+ * $Id: Postgres73.php,v 1.185 2007/12/28 16:21:25 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1739,6 +1739,67 @@ class Postgres73 extends Postgres72 {
 
        // Constraint functions
 
+       /**
+        * Returns a list of all constraints on a table
+        * @param $table The table to find rules for
+        * @return A recordset
+        */
+       function getConstraints($table) {
+               $this->clean($table);
+
+               /* This query finds all foreign key and check constraints in the pg_constraint
+                * table, and unions that with all indexes that are the basis for unique or
+                * primary key constraints. */
+               $sql = "
+                       SELECT conname, consrc, contype, indkey, indisclustered FROM (
+                               SELECT
+                                       conname,
+                                       CASE WHEN contype='f' THEN
+                                               pg_catalog.pg_get_constraintdef(oid)
+                                       ELSE
+                                               'CHECK (' || consrc || ')'
+                                       END AS consrc,
+                                       contype,
+                                       conrelid AS relid,
+                                       NULL AS indkey,
+                                       FALSE AS indisclustered
+                               FROM
+                                       pg_catalog.pg_constraint
+                               WHERE
+                                       contype IN ('f', 'c')
+                               UNION ALL
+                               SELECT
+                                       pc.relname,
+                                       NULL,
+                                       CASE WHEN indisprimary THEN
+                                               'p'
+                                       ELSE
+                                               'u'
+                                       END,
+                                       pi.indrelid,
+                                       indkey,
+                                       pi.indisclustered
+                               FROM
+                                       pg_catalog.pg_class pc,
+                                       pg_catalog.pg_index pi
+                               WHERE
+                                       pc.oid=pi.indexrelid
+                                       AND EXISTS (
+                                               SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
+                                               ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
+                                               WHERE d.classid = pc.tableoid AND d.objid = pc.oid AND d.deptype = 'i' AND c.contype IN ('u', 'p')
+                               )
+                       ) AS sub
+                       WHERE relid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
+                                       AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
+                                       WHERE nspname='{$this->_schema}'))
+                       ORDER BY
+                               1
+               ";
+
+               return $this->selectSet($sql);
+       }
+
        /**
         * Returns a list of all constraints on a table,
         * including constraint name, definition, related col and referenced namespace,
@@ -1746,7 +1807,7 @@ class Postgres73 extends Postgres72 {
         * @param $table the table where we are looking for fk
         * @return a recordset
         */
-       function getConstraints($table) {
+       function getConstraintsWithFields($table) {
                global $data;
 
                $data->clean($table);
index 08b4e024c5d5e4bb756122a367fc183c3762809e..0b87f0adaa02bfcfb01dbacfa56ea72aff145849 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres74.php,v 1.70 2007/12/12 04:11:10 xzilla Exp $
+ * $Id: Postgres74.php,v 1.71 2007/12/28 16:21:25 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -149,6 +149,48 @@ class Postgres74 extends Postgres73 {
 
        // Constraint functions
 
+       /**
+        * Returns a list of all constraints on a table
+        * @param $table The table to find rules for
+        * @return A recordset
+        */
+       function getConstraints($table) {
+               $this->clean($table);
+
+               // This SQL is greatly complicated by the need to retrieve
+               // index clustering information for primary and unique constraints
+               $sql = "SELECT
+                               pc.conname,
+                               pg_catalog.pg_get_constraintdef(pc.oid, true) AS consrc,
+                               pc.contype,
+                               CASE WHEN pc.contype='u' OR pc.contype='p' THEN (
+                                       SELECT
+                                               indisclustered
+                                       FROM
+                                               pg_catalog.pg_depend pd,
+                                               pg_catalog.pg_class pl,
+                                               pg_catalog.pg_index pi
+                                       WHERE
+                                               pd.refclassid=pc.tableoid
+                                               AND pd.refobjid=pc.oid
+                                               AND pd.objid=pl.oid
+                                               AND pl.oid=pi.indexrelid
+                               ) ELSE
+                                       NULL
+                               END AS indisclustered
+                       FROM
+                               pg_catalog.pg_constraint pc
+                       WHERE
+                               pc.conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
+                                       AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
+                                       WHERE nspname='{$this->_schema}'))
+                       ORDER BY
+                               1
+               ";
+
+               return $this->selectSet($sql);
+       }
+
        /**
         * Returns a list of all constraints on a table,
         * including constraint name, definition, related col and referenced namespace,
@@ -156,7 +198,7 @@ class Postgres74 extends Postgres73 {
         * @param $table the table where we are looking for fk
         * @return a recordset
         */
-       function getConstraints($table) {
+       function getConstraintsWithFields($table) {
                global $data;
 
                $data->clean($table);
@@ -602,7 +644,7 @@ class Postgres74 extends Postgres73 {
                                return -5;
                        }
 
-                        $funcname = $newname; 
+                        $funcname = $newname;
                }
 
                // Alter the owner, if necessary
@@ -615,7 +657,7 @@ class Postgres74 extends Postgres73 {
                                        $this->rollbackTransaction();
                                        return -6;
                                }
-                   }   
+                   }
 
                }
 
@@ -665,9 +707,9 @@ class Postgres74 extends Postgres73 {
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue);
                if ($status != 0)
                        return $status;
-               
+
                /* $schema not supported in pg74 */
-               
+
                // if name != seqname, sequence has been renamed in parent
                $sequence = ($seqrs->fields['seqname'] == $name) ? $seqrs->fields['seqname'] : $name;
                $this->clean($increment);
index 66a0be38cafc65ef1c7487291f2d8ddb15ccb24d..5615a663ac7ac4c4eb76acae9788e77f2a43dd3a 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.2 support
  *
- * $Id: Postgres82.php,v 1.9 2007/10/30 19:23:19 xzilla Exp $
+ * $Id: Postgres82.php,v 1.10 2007/12/28 16:21:25 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres81.php');
@@ -11,7 +11,7 @@ include_once('./classes/database/Postgres81.php');
 class Postgres82 extends Postgres81 {
 
        var $major_version = 8.2;
-       
+
        // Array of allowed index types
        var $typIndexes = array('BTREE', 'RTREE', 'GIST', 'GIN', 'HASH');
 
@@ -30,7 +30,7 @@ class Postgres82 extends Postgres81 {
                'schema' => array('CREATE', 'USAGE', 'ALL PRIVILEGES'),
                'tablespace' => array('CREATE', 'ALL PRIVILEGES')
        );
-  
+
        // List of characters in acl lists and the privileges they
        // refer to.
        var $privmap = array(
@@ -46,7 +46,7 @@ class Postgres82 extends Postgres81 {
                'C' => 'CREATE',
                'T' => 'TEMPORARY',
                'c' => 'CONNECT'
-       );      
+       );
 
        /**
         * Constructor
@@ -57,7 +57,7 @@ class Postgres82 extends Postgres81 {
        }
 
        // Help functions
-       
+
        function getHelpPages() {
                include_once('./help/PostgresDoc82.php');
                return $this->help_page;
@@ -70,9 +70,9 @@ class Postgres82 extends Postgres81 {
         */
        function getDatabases($currentdatabase = NULL) {
                global $conf, $misc;
-               
+
                $server_info = $misc->getServerInfo();
-               
+
                if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser($server_info['username'])) {
                        $username = $server_info['username'];
                        $this->clean($username);
@@ -93,9 +93,9 @@ class Postgres82 extends Postgres81 {
                $sql = "SELECT pdb.datname AS datname, pr.rolname AS datowner, pg_encoding_to_char(encoding) AS datencoding,
                                (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pdb.oid=pd.objoid) AS datcomment,
                                (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace,
-                                                          pg_catalog.pg_database_size(pdb.oid) as dbsize 
-                        FROM pg_catalog.pg_database pdb LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid)  
-                                               WHERE true 
+                                                          pg_catalog.pg_database_size(pdb.oid) as dbsize
+                        FROM pg_catalog.pg_database pdb LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid)
+                                               WHERE true
                        {$where}
                        {$clause}
                        {$orderby}";
@@ -121,13 +121,13 @@ class Postgres82 extends Postgres81 {
                $this->clean($newName);
                $this->clean($newOwner);
                $this->clean($comment);
-               
+
                $status = $this->beginTransaction();
                if ($status != 0) {
                        $this->rollbackTransaction();
                        return -1;
                }
-               
+
                if ($dbName != $newName) {
                        $status = $this->alterDatabaseRename($dbName, $newName);
                        if ($status != 0) {
@@ -142,7 +142,7 @@ class Postgres82 extends Postgres81 {
                        return -2;
                }
 
-               if (trim($comment) != '' ) {    
+               if (trim($comment) != '' ) {
                        $status = $this->setComment('DATABASE', $dbName, '', $comment);
                        if ($status != 0) {
                                $this->rollbackTransaction();
@@ -164,7 +164,7 @@ class Postgres82 extends Postgres81 {
        }
 
        // Tablespace functions
-       
+
        /**
         * Retrieves information for all tablespaces
         * @param $all Include all tablespaces (necessary when moving objects back to the default space)
@@ -172,17 +172,17 @@ class Postgres82 extends Postgres81 {
         */
        function getTablespaces($all = false) {
                global $conf;
-               
+
                $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation,
                     (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid) AS spccomment
                                        FROM pg_catalog.pg_tablespace";
-                                       
+
                if (!$conf['show_system'] && !$all) {
                        $sql .= " WHERE spcname NOT LIKE 'pg\\\\_%'";
                }
-               
+
                $sql .= " ORDER BY spcname";
-                                       
+
                return $this->selectSet($sql);
        }
 
@@ -192,16 +192,16 @@ class Postgres82 extends Postgres81 {
         */
        function getTablespace($spcname) {
                $this->clean($spcname);
-               
+
                $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation,
                     (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid) AS spccomment
                                        FROM pg_catalog.pg_tablespace WHERE spcname='{$spcname}'";
-                                       
+
                return $this->selectSet($sql);
        }
-       
+
        // Constraints methods
-       
+
        /**
         * Returns a list of all constraints on a table,
         * including constraint name, definition, related col and referenced namespace,
@@ -209,11 +209,11 @@ class Postgres82 extends Postgres81 {
         * @param $table the table where we are looking for fk
         * @return a recordset
         */
-       function getConstraints($table) {
+       function getConstraintsWithFields($table) {
                global $data;
 
                $data->clean($table);
-       
+
                // get the max number of col used in a constraint for the table
                $sql = "SELECT DISTINCT
                                max(SUBSTRING(array_dims(c.conkey) FROM  E'^\\\[.*:(.*)\\\]$')) as nb
@@ -227,11 +227,11 @@ class Postgres82 extends Postgres81 {
                $rs = $this->selectSet($sql);
 
                if ($rs->EOF) $max_col = 0;
-                       else $max_col = $rs->fields['nb'];
+               else $max_col = $rs->fields['nb'];
 
                $sql = '
                        SELECT
-                               c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid, true) AS consrc, 
+                               c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid, true) AS consrc,
                                ns1.nspname as p_schema, r1.relname as p_table, ns2.nspname as f_schema,
                                r2.relname as f_table, f1.attname as p_field, f2.attname as f_field,
                                pg_catalog.obj_description(c.oid, \'pg_constraint\') AS constcomment
@@ -247,19 +247,19 @@ class Postgres82 extends Postgres81 {
                                LEFT JOIN (
                                        pg_catalog.pg_class AS r2 JOIN pg_catalog.pg_namespace AS ns2 ON (r2.relnamespace=ns2.oid)
                                ) ON (c.confrelid=r2.oid)
-                               LEFT JOIN pg_catalog.pg_attribute AS f2 ON 
+                               LEFT JOIN pg_catalog.pg_attribute AS f2 ON
                                        (f2.attrelid=r2.oid AND ((c.confkey[1]=f2.attnum AND c.conkey[1]=f1.attnum)';
                for ($i = 2; $i <= $rs->fields['nb']; $i++)
                        $sql.= "OR (c.confkey[$i]=f2.attnum AND c.conkey[$i]=f1.attnum)";
 
                $sql .= sprintf("))
-                       WHERE 
+                       WHERE
                                r1.relname = '%s' AND ns1.nspname='%s'
                        ORDER BY 1", $table, $this->_schema);
 
                return $this->selectSet($sql);
        }
-       
+
        // Capabilities
        function hasSharedComments() {return true;}
        function hasCreateTableLikeWithConstraints() {return true;}
index 20ecc813adfcf35f6f51bd4c5a876bcad61c9723..d56430094bc60423643e5d4627a30b6cc9b610f5 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List tables in a database
         *
-        * $Id: tblproperties.php,v 1.90 2007/12/28 15:28:57 ioguix Exp $
+        * $Id: tblproperties.php,v 1.91 2007/12/28 16:21:25 ioguix Exp $
         */
 
        // Include application functions
                // Get columns
                $attrs = $data->getTableAttributes($_REQUEST['table']);
                // Get Pk & Constraints
-               $ck = $data->getConstraints($_REQUEST['table']);
+               $ck = $data->getConstraintsWithFields($_REQUEST['table']);
 
                // Show comment if any
                if ($tdata->fields['relcomment'] !== null)