]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-18577 drop enums support - step3: Drop enums from generator classes. Only code...
authorstronk7 <stronk7>
Fri, 1 May 2009 23:49:31 +0000 (23:49 +0000)
committerstronk7 <stronk7>
Fri, 1 May 2009 23:49:31 +0000 (23:49 +0000)
lib/ddl/database_manager.php
lib/ddl/mssql_sql_generator.php
lib/ddl/mysql_sql_generator.php
lib/ddl/oracle_sql_generator.php
lib/ddl/postgres_sql_generator.php
lib/ddl/sql_generator.php
lib/ddl/sqlite_sql_generator.php

index 4125d90351d082a5367f254a45312fad35ecdfc1..bf7055d52e3e541d14188caf94c1dbb269a3ee81 100644 (file)
@@ -214,6 +214,8 @@ class database_manager {
      * to 1 "enum-like" constraint. So, if more than one is returned, only the first one will be
      * retrieved by this funcion.
      *
+     * TODO: Moodle 2.1 - Drop find_check_constraint_name()
+     *
      * @param xmldb_table the table to be searched
      * @param xmldb_field the field to be searched
      * @return string check constraint name or false
index 72492722d413ea3517f4debc22ae18d470a500b1..bf0a116cf96f1b01b3ed670694715c8689b85a96 100644 (file)
@@ -192,17 +192,6 @@ class mssql_sql_generator extends sql_generator {
         return $dbtype;
     }
 
-    /**
-     * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
-     */
-    public function getEnumExtraSQL($xmldb_table, $xmldb_field) {
-
-        $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
-        $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . '))';
-
-        return $sql;
-    }
-
     /**
      * Given one xmldb_table and one xmldb_field, 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 and
@@ -252,39 +241,12 @@ class mssql_sql_generator extends sql_generator {
             return array();
         }
 
-    /// Drop the check constraint if exists
-        if ($xmldb_field->getEnum()) {
-            $results = array_merge($results, $this->getDropEnumSQL($xmldb_table, $xmldb_field));
-        }
-
     /// Call to standard (parent) getRenameFieldSQL() function
         $results = array_merge($results, parent::getRenameFieldSQL($xmldb_table, $xmldb_field, $newname));
 
         return $results;
     }
 
-    /**
-     * Returns the code (array of statements) needed to execute extra statements on field rename
-     */
-    public function getRenameFieldExtraSQL($xmldb_table, $xmldb_field, $newname) {
-
-        $results = array();
-
-    /// If the field is enum, drop and re-create the check constraint
-        if ($xmldb_field->getEnum()) {
-        /// Drop the current enum (not needed, it has been dropped before for msqql (in getRenameFieldSQL)
-            //$results = array_merge($results, $this->getDropEnumSQL($xmldb_table, $xmldb_field));
-        /// Change field name (over a clone to avoid some potential problems later)
-            $new_xmldb_field = clone($xmldb_field);
-            $new_xmldb_field->setName($newname);
-
-        /// Recreate the enum
-            $results = array_merge($results, $this->getCreateEnumSQL($xmldb_table, $new_xmldb_field));
-        }
-
-        return $results;
-    }
-
     /**
      * Returns the code (array of statements) needed to execute extra statements on table rename
      */
@@ -305,11 +267,6 @@ class mssql_sql_generator extends sql_generator {
             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;
             }
         }
 
@@ -397,19 +354,11 @@ class mssql_sql_generator extends sql_generator {
         return $results;
     }
 
-    /**
-     * Given one xmldb_table and one xmldb_field, return the SQL statements needded to create its enum
-     * (usually invoked from getModifyEnumSQL()
-     */
-    public function getCreateEnumSQL($xmldb_table, $xmldb_field) {
-    /// All we have to do is to create the check constraint
-        return array('ALTER TABLE ' . $this->getTableName($xmldb_table) .
-                     ' ADD ' . $this->getEnumExtraSQL($xmldb_table, $xmldb_field));
-    }
-
     /**
      * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop its enum
      * (usually invoked from getModifyEnumSQL()
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getDropEnumSQL($xmldb_table, $xmldb_field) {
     /// Let's introspect to know the real name of the check constraint
@@ -498,6 +447,8 @@ class mssql_sql_generator extends sql_generator {
      * order to return only the check constraints belonging to one field.
      * Each element contains the name of the constraint and its description
      * If no check constraints are found, returns an empty array
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
         
index 272961f83bb8e82e58342cbf77996b10417a423f..a2a012b538a3cc09eac3c8474ad51972aa740bf1 100644 (file)
@@ -264,10 +264,17 @@ class mysql_sql_generator extends sql_generator {
     /**
      * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop its enum
      * (usually invoked from getModifyEnumSQL()
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getDropEnumSQL($xmldb_table, $xmldb_field) {
-    /// For MySQL, just alter the field
-        return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
+    /// Let's introspect to know if there is one enum
+        if ($check_constraints = $this->getCheckConstraintsFromDB($xmldb_table, $xmldb_field)) {
+        /// For MySQL, just alter the field
+            return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
+        } else {
+            return array(); /// Enum not found. Nothing to do
+        }
     }
 
     /**
@@ -310,13 +317,6 @@ class mysql_sql_generator extends sql_generator {
         return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
     }
 
-    /**
-     * Given one XMLDB Field, return its enum SQL
-     */
-    public function getEnumSQL($xmldb_field) {
-        return 'enum(' . implode(', ', $xmldb_field->getEnumValues()) . ')';
-    }
-
     /**
      * Returns the code (in array) needed to add one comment to the table
      */
@@ -339,13 +339,15 @@ class mysql_sql_generator extends sql_generator {
      * If no check constraints are found, returns an empty array
      * MySQL doesn't have check constraints in this implementation, but
      * we return them based on the enum fields in the table
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
 
         $tablename = $xmldb_table->getName($xmldb_table);
 
     /// Fetch all the columns in the table
-        if (!$columns = $this->mdb->get_columns($tablename)) {
+        if (!$columns = $this->mdb->get_columns($tablename, false)) {
             return array();
         }
 
index f7976121b838633aab61874ad8a92f214d7abbe2..f54ee6f9205f0e4a1275a4850274a9df18b3632a 100644 (file)
@@ -79,7 +79,7 @@ class oracle_sql_generator extends sql_generator {
         // From http://www.acs.ilstu.edu/docs/oracle/server.101/b10759/statements_2011.htm
         $value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
         $value++;
-        
+
         $seqname = $this->mdb->get_manager()->find_sequence_name($xmldb_table);
 
         if (!$seqname) {
@@ -162,17 +162,6 @@ class oracle_sql_generator extends sql_generator {
         return $dbtype;
     }
 
-    /**
-     * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
-     */
-    public function getEnumExtraSQL($xmldb_table, $xmldb_field) {
-
-        $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
-        $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . '))';
-
-        return $sql;
-    }
-
     /**
      * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes
      */
@@ -221,19 +210,16 @@ class oracle_sql_generator extends sql_generator {
      */
     public function getDropSequenceSQL($xmldb_table, $xmldb_field, $include_trigger=false) {
 
-        $sequence_name = $this->getSequenceFromDB($xmldb_table);
+        $result = array();
 
-        $sequence = "DROP SEQUENCE " . $sequence_name;
-
-        $trigger_name = $this->getTriggerFromDB($xmldb_table);
-
-        $trigger = "DROP TRIGGER " . $trigger_name;
+        if ($sequence_name = $this->getSequenceFromDB($xmldb_table)) {
+            $result[] = "DROP SEQUENCE " . $sequence_name;
+        }
 
-        if ($include_trigger) {
-            $result =  array($sequence, $trigger);
-        } else {
-            $result = array($sequence);
+        if ($trigger_name = $this->getTriggerFromDB($xmldb_table) && $include_trigger) {
+            $result[] = "DROP TRIGGER " . $trigger_name;
         }
+
         return $result;
     }
 
@@ -248,27 +234,6 @@ class oracle_sql_generator extends sql_generator {
         return array($comment);
     }
 
-    /**
-     * Returns the code (array of statements) needed to execute extra statements on field rename
-     */
-    public function getRenameFieldExtraSQL($xmldb_table, $xmldb_field, $newname) {
-
-        $results = array();
-
-    /// If the field is enum, drop and re-create the check constraint
-        if ($xmldb_field->getEnum()) {
-        /// Drop the current enum
-            $results = array_merge($results, $this->getDropEnumSQL($xmldb_table, $xmldb_field));
-        /// Change field name (over a clone to avoid some potential problems later)
-            $new_xmldb_field = clone($xmldb_field);
-            $new_xmldb_field->setName($newname);
-        /// Recreate the enum
-            $results = array_merge($results, $this->getCreateEnumSQL($xmldb_table, $new_xmldb_field));
-        }
-
-        return $results;
-    }
-
     /**
      * Returns the code (array of statements) needed to execute extra statements on table drop
      */
@@ -314,11 +279,6 @@ class oracle_sql_generator extends sql_generator {
             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 . ')';
             }
         }
 
@@ -486,19 +446,11 @@ class oracle_sql_generator extends sql_generator {
         return $results;
     }
 
-    /**
-     * Given one xmldb_table and one xmldb_field, return the SQL statements needded to create its enum
-     * (usually invoked from getModifyEnumSQL()
-     */
-    public function getCreateEnumSQL($xmldb_table, $xmldb_field) {
-    /// All we have to do is to create the check constraint
-        return array('ALTER TABLE ' . $this->getTableName($xmldb_table) .
-                     ' ADD ' . $this->getEnumExtraSQL($xmldb_table, $xmldb_field));
-    }
-
     /**
      * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop its enum
      * (usually invoked from getModifyEnumSQL()
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getDropEnumSQL($xmldb_table, $xmldb_field) {
     /// Let's introspect to know the real name of the check constraint
@@ -540,6 +492,8 @@ class oracle_sql_generator extends sql_generator {
      * order to return only the check constraints belonging to one field.
      * Each element contains the name of the constraint and its description
      * If no check constraints are found, returns an empty array
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
 
index c1c0e9ad1de061a6d7e484b938b60417f62c3b6b..963256fff46e3cfa26cfbfd4b8ba2fc753eb4dd4 100644 (file)
@@ -133,17 +133,6 @@ class postgres_sql_generator extends sql_generator {
         return $dbtype;
     }
 
-    /**
-     * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
-     */
-    public function getEnumExtraSQL($xmldb_table, $xmldb_field) {
-
-        $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
-        $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . '))';
-
-        return $sql;
-    }
-
     /**
      * Returns the code (in array) needed to add one comment to the table
      */
@@ -183,12 +172,7 @@ class postgres_sql_generator extends sql_generator {
             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;
@@ -381,40 +365,11 @@ class postgres_sql_generator extends sql_generator {
         return $results;
     }
 
-    /**
-     * Returns the code (array of statements) needed to execute extra statements on field rename
-     */
-    public function getRenameFieldExtraSQL($xmldb_table, $xmldb_field, $newname) {
-
-        $results = array();
-
-    /// If the field is enum, drop and re-create the check constraint
-        if ($xmldb_field->getEnum()) {
-        /// Drop the current enum
-            $results = array_merge($results, $this->getDropEnumSQL($xmldb_table, $xmldb_field));
-        /// Change field name (over a clone to avoid some potential problems later)
-            $new_xmldb_field = clone($xmldb_field);
-            $new_xmldb_field->setName($newname);
-        /// Recreate the enum
-            $results = array_merge($results, $this->getCreateEnumSQL($xmldb_table, $new_xmldb_field));
-        }
-
-        return $results;
-    }
-
-    /**
-     * Given one xmldb_table and one xmldb_field, return the SQL statements needded to create its enum
-     * (usually invoked from getModifyEnumSQL()
-     */
-    public function getCreateEnumSQL($xmldb_table, $xmldb_field) {
-    /// All we have to do is to create the check constraint
-        return array('ALTER TABLE ' . $this->getTableName($xmldb_table) .
-                     ' ADD ' . $this->getEnumExtraSQL($xmldb_table, $xmldb_field));
-    }
-
     /**
      * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop its enum
      * (usually invoked from getModifyEnumSQL()
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getDropEnumSQL($xmldb_table, $xmldb_field) {
     /// Let's introspect to know the real name of the check constraint
@@ -456,6 +411,8 @@ class postgres_sql_generator extends sql_generator {
      * order to return only the check constraints belonging to one field.
      * Each element contains the name of the constraint and its description
      * If no check constraints are found, returns an empty array
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
 
index b09f4f8fef382ae36a9163f3671b2f3db2031616..fe922c9eeb6200a60eace9b958d6534b68c7db08 100644 (file)
@@ -1179,6 +1179,8 @@ abstract class sql_generator {
     /**
      * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop its default
      * (usually invoked from getModifyDefaultSQL()
+     *
+     * TODO: Moodle 2.1 - Drop getDropDefaultSQL()
      */
     public abstract function getDropDefaultSQL($xmldb_table, $xmldb_field);
 
@@ -1186,6 +1188,8 @@ abstract class sql_generator {
      * Given one xmldb_table and one optional xmldb_field, return one array with all the check
      * constrainst found for that table (or field). Must exist for each DB supported.
      * (usually invoked from find_check_constraint_name)
+     *
+     * TODO: Moodle 2.1 - Drop getCheckConstraintsFromDB
      */
     public abstract function getCheckConstraintsFromDB($xmldb_table, $xmldb_field=null);
 
index c7bd41427f147b655751c28dd19d6866bc72fb99..307c399b4bc152689d235950f4e359c2d8f756e2 100644 (file)
@@ -368,15 +368,6 @@ class sqlite_sql_generator extends sql_generator {
         return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
     }
 
-    /**
-     * Given one XMLDB Field, return its enum SQL
-     */
-    public function getEnumSQL($xmldb_field) {
-        // Enum values are between /*LISTSTART*/ and /*LISTEND*/ so that
-        // get_columns can easily find them
-        return 'enum CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (/*LISTSTART*/' . implode(',', $xmldb_field->getEnumValues()) . '/*LISTEND*/))';
-    }
-
     /**
      * Returns the code (in array) needed to add one comment to the table
      */
@@ -391,6 +382,8 @@ class sqlite_sql_generator extends sql_generator {
      * order to return only the check constraints belonging to one field.
      * Each element contains the name of the constraint and its description
      * If no check constraints are found, returns an empty array.
+     *
+     * TODO: Moodle 2.1 - drop in Moodle 2.1
      */
     public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
         $tablename = $xmldb_table->getName($xmldb_table);