Fix getConstraints with pg73 & pg74
authorioguix <ioguix>
Sat, 29 Sep 2007 09:00:33 +0000 (09:00 +0000)
committerioguix <ioguix>
Sat, 29 Sep 2007 09:00:33 +0000 (09:00 +0000)
classes/database/Postgres73.php
classes/database/Postgres74.php

index c0a6c868e53117305d49f14f6d22d4880c95f4dc..753f5cf54516069f420b4e95d7945be0f62374c5 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.170 2007/09/25 16:08:05 ioguix Exp $
+ * $Id: Postgres73.php,v 1.171 2007/09/29 09:00:33 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1235,7 +1235,7 @@ class Postgres73 extends Postgres72 {
 
        $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) 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
                        FROM
index 7c8718e627b7e02285d5d431b9a3fa657ba7fc21..fe5d245cc69045a20c2670df4c81a7955476bed4 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.61 2007/09/19 14:42:12 ioguix Exp $
+ * $Id: Postgres74.php,v 1.62 2007/09/29 09:00:33 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -147,6 +147,65 @@ class Postgres74 extends Postgres73 {
                }
        }
 
+       // Constraint functions
+       
+       /**
+        * Returns a list of all constraints on a table,
+        * including constraint name, definition, related col and referenced namespace,
+        * table and col if needed
+        * @param $table the table where we are looking for fk
+        * @return a recordset
+        */
+       function getConstraints($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  '^\\\[.*:(.*)\\\]$')) as nb
+               FROM
+                     pg_catalog.pg_constraint AS c
+                 JOIN pg_catalog.pg_class AS r ON (c.conrelid = r.oid)
+                     JOIN pg_catalog.pg_namespace AS ns ON r.relnamespace=ns.oid
+               WHERE
+                       r.relname = '$table' AND ns.nspname='". $this->_schema ."'";
+
+               $rs = $this->selectSet($sql);
+
+               if ($rs->EOF) $max_col = 0;
+               else $max_col = $rs->fields['nb'];
+
+               $sql = '
+                       SELECT
+                               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
+                       FROM
+                               pg_catalog.pg_constraint AS c
+                               JOIN pg_catalog.pg_class AS r1 ON (c.conrelid=r1.oid)
+                               JOIN pg_catalog.pg_attribute AS f1 ON (f1.attrelid=r1.oid AND (f1.attnum=c.conkey[1]';
+               for ($i = 2; $i <= $rs->fields['nb']; $i++) {
+                       $sql.= " OR f1.attnum=c.conkey[$i]";
+               }
+               $sql.= '))
+                               JOIN pg_catalog.pg_namespace AS ns1 ON r1.relnamespace=ns1.oid
+                               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 
+                                       (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 
+                               r1.relname = '%s' AND ns1.nspname='%s'
+                       ORDER BY 1", $table, $this->_schema);
+
+               return $this->selectSet($sql);
+       }
+       
        /**
         * Creates a new table in the database copying attribs and other properties from another table
         * @param $name The name of the table