]> git.mjollnir.org Git - moodle.git/commitdiff
rename_table() under postgresql completed
authorstronk7 <stronk7>
Mon, 2 Oct 2006 16:27:18 +0000 (16:27 +0000)
committerstronk7 <stronk7>
Mon, 2 Oct 2006 16:27:18 +0000 (16:27 +0000)
lib/xmldb/classes/generators/postgres7/postgres7.class.php

index c8dd0e14e34b059db6eaa7427af5398b94723f65..5e2fe0206ee071a0cc2136982bfe06a57d37182f 100644 (file)
@@ -152,6 +152,25 @@ class XMLDBpostgres7 extends XMLDBgenerator {
     /// Rename de sequence
         $results[] = 'ALTER TABLE ' . $oldseqname . ' RENAME TO ' . $newseqname;
 
+    /// Rename all the check constraints in the table
+        $oldtablename = $this->getTableName($xmldb_table);
+        $newtablename = $this->getTableName($newt);
+
+        $oldconstraintprefix = $this->getNameForObject($xmldb_table->getName(), '');
+        $newconstraintprefix = $this->getNameForObject($newt->getName(), '', '');
+
+        if ($constraints = $this->getCheckConstraintsFromDB($xmldb_table)) {
+            foreach ($constraints as $constraint) {
+            /// Drop the old constraint
+                $results[] = 'ALTER TABLE ' . $newtablename . ' DROP CONSTRAINT ' . $constraint->name;
+            /// Calculate the new constraint name
+                $newconstraintname = str_replace($oldconstraintprefix, $newconstraintprefix, $constraint->name);
+            /// Add the new constraint
+                $results[] = 'ALTER TABLE ' . $newtablename . ' ADD CONSTRAINT ' . $newconstraintname .
+                             ' CHECK (' . $constraint->description . ')';
+             }
+         }
+
         return $results;
     }
 
@@ -375,6 +394,32 @@ class XMLDBpostgres7 extends XMLDBgenerator {
         return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
     }
 
+    /**
+     * Given one XMLDBTable returns one array with all the check constrainsts 
+     * in the table (fetched from DB)
+     * Each element contains the name of the constraint and its description
+     * If no check constraints are found, returns an empty array
+     */
+    function getCheckConstraintsFromDB($xmldb_table) {
+
+        $results = array();
+
+        $tablename = $this->getTableName($xmldb_table);
+
+        if ($constraints = get_records_sql("SELECT co.conname AS name, co.consrc AS description
+                                              FROM pg_constraint co,
+                                                   pg_class cl
+                                             WHERE co.conrelid = cl.oid
+                                               AND co.contype = 'c'
+                                               AND cl.relname = '{$tablename}'")) {
+            foreach ($constraints as $constraint) {
+                $results[$constraint->name] = $constraint;
+            }
+        }
+
+        return $results;
+    }
+
     /**
      * Returns an array of reserved words (lowercase) for this DB
      */