]> git.mjollnir.org Git - moodle.git/commitdiff
rename_table() under mssql completed! B-)
authorstronk7 <stronk7>
Mon, 2 Oct 2006 15:56:34 +0000 (15:56 +0000)
committerstronk7 <stronk7>
Mon, 2 Oct 2006 15:56:34 +0000 (15:56 +0000)
lib/xmldb/classes/generators/mssql/mssql.class.php

index 7fa815c157944c5ecda02173774bc20daef0533d..3b4144c0b4798b4fbc64065add919f879d12f777 100644 (file)
@@ -56,6 +56,8 @@ class XMLDBmssql extends XMLDBgenerator {
     var $rename_table_sql = "sp_rename 'OLDNAME', 'NEWNAME'"; //SQL sentence to rename one table, both
                                   //OLDNAME and NEWNAME are dinamically replaced
 
+    var $rename_table_extra_code = true; //Does the generator need to add code after table rename
+
     var $rename_column_sql = "sp_rename 'TABLENAME.OLDFIELDNAME', 'NEWFIELDNAME', 'COLUMN'";
                                       ///TABLENAME, OLDFIELDNAME and NEWFIELDNAME are dianmically replaced
 
@@ -182,6 +184,40 @@ class XMLDBmssql extends XMLDBgenerator {
         return $results;
     }
 
+    /**
+     * Returns the code (array of statements) needed to execute extra statements on table rename
+     */
+    function getRenameTableExtraSQL ($xmldb_table, $newname) {
+
+        $results = array();
+
+        $newt = new XMLDBTable($newname); //Temporal table for name calculations
+
+        $oldtablename = $this->getTableName($xmldb_table);
+        $newtablename = $this->getTableName($newt);
+
+    /// Rename all the check constraints in the table
+        $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;
+            }
+        }
+
+
+        print_object($this->getCheckConstraintsFromDB($xmldb_table));
+
+        return $results;
+    }
+
     /**
      * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table
      */
@@ -332,8 +368,6 @@ class XMLDBmssql extends XMLDBgenerator {
      */
     function getDefaultConstraintName($xmldb_table, $xmldb_field) {
 
-        global $db;
-
     /// Get the quoted name of the table and field
         $tablename = $this->getTableName($xmldb_table);
         $fieldname = $this->getEncQuoted($xmldb_field->getName());
@@ -349,6 +383,34 @@ class XMLDBmssql extends XMLDBgenerator {
         }
     }
 
+    /**
+     * 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 o.name, c.text AS description
+                                            FROM sysobjects o,
+                                                 sysobjects p,
+                                                 syscomments c
+                                           WHERE p.id = o.parent_obj
+                                             AND o.id = c.id
+                                             AND o.xtype = 'C'
+                                             AND p.name = '{$tablename}'")) {
+            foreach ($constraints as $constraint) {
+                $results[$constraint->name] = $constraint;
+            }
+        }
+
+        return $results;
+    }
+
     /**
      * Returns an array of reserved words (lowercase) for this DB
      */