]> git.mjollnir.org Git - moodle.git/commitdiff
MSSQL needs to drop the default constraint before being able to drop the column
authorstronk7 <stronk7>
Tue, 12 Sep 2006 21:24:03 +0000 (21:24 +0000)
committerstronk7 <stronk7>
Tue, 12 Sep 2006 21:24:03 +0000 (21:24 +0000)
lib/xmldb/classes/generators/mssql/mssql.class.php

index 5d5e4d7a7cba2c861faf6b831114874dc3af86d8..5a44d6dc68d51fd5de0e8106a369132dcb632f37 100644 (file)
@@ -136,6 +136,36 @@ class XMLDBmssql extends XMLDBgenerator {
         return $sql;
     }
 
+    /**
+     * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop the field from the table
+     * MSSQL overwrites the standard sentence because it needs to do some extra work dropping the default constraints
+     */
+    function getDropFieldSQL($xmldb_table, $xmldb_field) {
+
+        global $db;
+
+        $results = array();
+
+    /// Get the quoted name of the table and field
+        $tablename = $this->getEncQuoted($this->prefix . $xmldb_table->getName());
+        $fieldname = $this->getEncQuoted($xmldb_field->getName());
+    
+    /// Look for any default constraint in this field and drop it
+        if ($default = get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint
+                                       FROM syscolumns
+                                       WHERE id = object_id('{$tablename}') AND
+                                             name = '$fieldname'")) {
+            echo "DEFAULT FOUND";
+            $results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $default->defaultconstraint;
+        }
+            echo "DEFAULT NOT FOUND";
+
+    /// Build the standard alter table drop
+        $results[] = 'ALTER TABLE ' . $tablename . ' DROP COLUMN ' . $fieldname;
+
+        return $results;
+    }
+
     /**
      * Returns an array of reserved words (lowercase) for this DB
      */