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

index 8f83996f3e05b68e20cc58ba05b1c2a8f7ede43f..e513a32eab5c0f17ba9c90c7cc1258dc6b2e697c 100644 (file)
@@ -230,10 +230,29 @@ class XMLDBoci8po extends XMLDBgenerator {
     /// Drop old trigger
         $results[] = "DROP TRIGGER " . $oldtriggername;
 
-        $new_xmldb_table = new XMLDBTable($newname); /// Temp table for trigger code generation
+        $newt = new XMLDBTable($newname); /// Temp table for trigger code generation
 
     /// Create new trigger
-        $results = array_merge($results, $this->getCreateTriggerSQL($new_xmldb_table, $xmldb_field));
+        $results = array_merge($results, $this->getCreateTriggerSQL($newt, $xmldb_field));
+
+    /// 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;
     }
@@ -434,6 +453,31 @@ class XMLDBoci8po 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 = strtoupper($this->getTableName($xmldb_table));
+
+        if ($constraints = get_records_sql("SELECT lower(c.constraint_name) AS name, c.search_condition AS description
+                                              FROM user_constraints c
+                                             WHERE c.table_name = '{$tablename}'
+                                               AND c.constraint_type = 'C'
+                                               AND c.constraint_name not like 'SYS%'")) {
+            foreach ($constraints as $constraint) {
+                $results[$constraint->name] = $constraint;
+            }
+        }
+
+        return $results;
+    }
+
     /**
      * Returns an array of reserved words (lowercase) for this DB
      */