From: stronk7 Date: Tue, 12 Sep 2006 21:24:03 +0000 (+0000) Subject: MSSQL needs to drop the default constraint before being able to drop the column X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=2a0284176284d86a2b6584deb4f455be15ad28e1;p=moodle.git MSSQL needs to drop the default constraint before being able to drop the column --- diff --git a/lib/xmldb/classes/generators/mssql/mssql.class.php b/lib/xmldb/classes/generators/mssql/mssql.class.php index 5d5e4d7a7c..5a44d6dc68 100644 --- a/lib/xmldb/classes/generators/mssql/mssql.class.php +++ b/lib/xmldb/classes/generators/mssql/mssql.class.php @@ -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 */