From 05aae0a571cb3d1bd4400962c1bbd2fa464be1a3 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 10 Mar 2012 17:46:09 +0100 Subject: [PATCH] MDL-29514 drop support for enum data types --- .../actions/main_view/main_view.class.php | 34 ------- .../view_table_php/view_table_php.class.php | 59 ------------ lib/ddl/database_manager.php | 82 ----------------- lib/ddl/mssql_sql_generator.php | 91 ------------------- lib/ddl/mysql_sql_generator.php | 80 ---------------- lib/ddl/oracle_sql_generator.php | 80 ---------------- lib/ddl/postgres_sql_generator.php | 84 ----------------- lib/ddl/simpletest/testddl.php | 67 -------------- lib/ddl/sql_generator.php | 27 ------ lib/ddl/sqlite_sql_generator.php | 50 ---------- lib/deprecatedlib.php | 37 -------- lib/dml/database_column_info.php | 11 --- lib/dml/mysqli_native_moodle_database.php | 36 +------- lib/dml/oci_native_moodle_database.php | 1 - lib/dml/pdo_moodle_database.php | 11 --- lib/dml/sqlite3_pdo_moodle_database.php | 7 -- lib/xmldb/xmldb.dtd | 3 - lib/xmldb/xmldb.xsd | 3 - lib/xmldb/xmldb_field.php | 12 --- lib/xmldb/xmldb_table.php | 3 - 20 files changed, 2 insertions(+), 776 deletions(-) diff --git a/admin/tool/xmldb/actions/main_view/main_view.class.php b/admin/tool/xmldb/actions/main_view/main_view.class.php index c8feb613396..30af5871962 100644 --- a/admin/tool/xmldb/actions/main_view/main_view.class.php +++ b/admin/tool/xmldb/actions/main_view/main_view.class.php @@ -277,40 +277,6 @@ class main_view extends XMLDBAction { } } } - // TODO: Drop this check in Moodle 2.1 - // Intercept loaded structure here and look for ENUM fields - if (isset($dbdir->xml_file)) { - if ($structure =& $dbdir->xml_file->getStructure()) { - if ($tables = $structure->getTables()) { - foreach ($tables as $table) { - if ($fields = $table->getFields()) { - foreach ($fields as $field) { - if (!empty($field->hasenums)) { - if ($hithis) { - $o .= ''; - } else { - $o .= ''; - } - $o .= 'Table ' . $table->getName() . ', field ' . $field->getName() . ' has ENUM info'; - if (!empty($field->hasenumsenabled)) { - $o .= ' that seems to be active (true). ENUMs support has been dropped in Moodle 2.0, ' . - ' the XMLDB Editor will delete any ENUM reference next time you save this file' . - ' and you MUST provide one upgrade block in your code to drop them from DB. See' . - ' ' . - ' Moodle Docs for more info and examples.'; - } else { - $o .= ' that seem to be inactive (false). ENUMs support has been dropped in Moodle 2.0,' . - ' the XMLDB Editor will, simply, delete any ENUM reference next time you save this file.' . - ' No further action is necessary.'; - } - $o .= ''; - } - } - } - } - } - } - } // If there are changes pending to be saved, but the file cannot be written... inform here if ($dbdir->path_exists && file_exists($key . '/install.xml') && diff --git a/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php b/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php index 57c0bd39e6d..7ce215793fe 100644 --- a/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php +++ b/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php @@ -127,7 +127,6 @@ class view_table_php extends XMLDBAction { $optionspacer . 'change_field_precision', $optionspacer . 'change_field_notnull', $optionspacer . 'change_field_default', - $optionspacer . 'drop_enum_from_field', // TODO: Moodle 2.1 - Drop drop_enum_from_field 'Keys', $optionspacer . 'add_key', $optionspacer . 'drop_key', @@ -224,13 +223,6 @@ class view_table_php extends XMLDBAction { $o.= $this->str['mustselectonefield']; } break; - case 'drop_enum_from_field': // TODO: Moodle 2.1 - Drop drop_enum_from_field - if ($fieldkeyindexinitial == 'f') { // Only if we have got one field - $o.= s($this->drop_enum_from_field_php($structure, $tableparam, $fieldkeyindexparam)); - } else { - $o.= $this->str['mustselectonefield']; - } - break; case 'change_field_default': if ($fieldkeyindexinitial == 'f') { // Only if we have got one field $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam)); @@ -593,57 +585,6 @@ class view_table_php extends XMLDBAction { return $result; } - /** - * This function will generate all the PHP code needed to - * drop the enum values (check constraint) of one field - * using XMLDB objects and functions - * - * Note this function is here as part of the process of - * dropping enums completely from Moodle 2.0: MDL-18577 - * and will be out in Moodle 2.1 - * TODO: Moodle 2.1 - Drop drop_enum_from_field_php - * - * @param xmldb_structure structure object containing all the info - * @param string table table name - * @param string field field name to change its enum - */ - function drop_enum_from_field_php($structure, $table, $field) { - - $result = ''; - // Validate if we can do it - if (!$table = $structure->getTable($table)) { - return false; - } - if (!$field = $table->getField($field)) { - return false; - } - if ($table->getAllErrors()) { - return false; - } - - // Add the standard PHP header - $result .= XMLDB_PHP_HEADER; - - // Add contents - $result .= XMLDB_LINEFEED; - $result .= ' // Drop list of values (enum) from field ' . $field->getName() . ' on table ' . $table->getName() . XMLDB_LINEFEED; - $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED; - $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED; - - // Launch the proper DDL - $result .= XMLDB_LINEFEED; - $result .= ' // Launch drop of list of values from field ' . $field->getName() . XMLDB_LINEFEED; - $result .= ' $dbman->drop_enum_from_field($table, $field);' . XMLDB_LINEFEED; - - // Add the proper upgrade_xxxx_savepoint call - $result .= $this->upgrade_savepoint_php ($structure); - - // Add standard PHP footer - $result .= XMLDB_PHP_FOOTER; - - return $result; - } - /** * This function will generate all the PHP code needed to * change the default of one field using XMLDB objects and functions diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index e1b9453403e..22d24e43b13 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -221,57 +221,6 @@ class database_manager { return ($this->find_index_name($xmldb_table, $xmldb_index) !== false); } - /** - * Given one xmldb_field, the function returns the name of the check constraint in DB (if exists) - * of false if it doesn't exist. Note that XMLDB limits the number of check constraints per field - * to 1 "enum-like" constraint. So, if more than one is returned, only the first one will be - * retrieved by this function. - * - * @todo MDL-31147 Moodle 2.1 - Drop find_check_constraint_name() - * - * @param xmldb_table $xmldb_table The table to be searched. - * @param xmldb_field $xmldb_field The field to be searched. - * @return string|bool check constraint name or false - */ - public function find_check_constraint_name(xmldb_table $xmldb_table, xmldb_field $xmldb_field) { - - /// Check the table exists - if (!$this->table_exists($xmldb_table)) { - throw new ddl_table_missing_exception($xmldb_table->getName()); - } - - /// Check the field exists - if (!$this->field_exists($xmldb_table, $xmldb_field)) { - throw new ddl_field_missing_exception($xmldb_field->getName(), $xmldb_table->getName()); - } - - /// Get list of check_constraints in table/field - $checks = false; - if ($objchecks = $this->generator->getCheckConstraintsFromDB($xmldb_table, $xmldb_field)) { - /// Get only the 1st element. Shouldn't be more than 1 under XMLDB - $objcheck = array_shift($objchecks); - if ($objcheck) { - $checks = strtolower($objcheck->name); - } - } - - /// Arriving here, check not found - return $checks; - } - - /** - * Given one xmldb_field, check if it has a check constraint in DB - * - * TODO: Moodle 2.1 - Drop check_constraint_exists() - * - * @param xmldb_table $xmldb_table The table. - * @param xmldb_field $xmldb_field The field to be searched for any existing constraint. - * @return boolean true/false - */ - public function check_constraint_exists(xmldb_table $xmldb_table, xmldb_field $xmldb_field) { - return ($this->find_check_constraint_name($xmldb_table, $xmldb_field) !== false); - } - /** * This function IS NOT IMPLEMENTED. ONCE WE'LL BE USING RELATIONAL * INTEGRITY IT WILL BECOME MORE USEFUL. FOR NOW, JUST CALCULATE "OFFICIAL" @@ -707,37 +656,6 @@ class database_manager { $this->execute_sql_arr($sqlarr); } - /** - * This function will drop the existing enum of the field in the table passed as arguments - * - * TODO: Moodle 2.1 - Drop drop_enum_from_field() - * - * @param xmldb_table $xmldb_table Table object (just the name is mandatory). - * @param xmldb_field $xmldb_field Index object (full specs are required). - * @return void - */ - public function drop_enum_from_field(xmldb_table $xmldb_table, xmldb_field $xmldb_field) { - if (!$this->table_exists($xmldb_table)) { - throw new ddl_table_missing_exception($xmldb_table->getName()); - } - /// Check the field exists - if (!$this->field_exists($xmldb_table, $xmldb_field)) { - throw new ddl_field_missing_exception($xmldb_field->getName(), $xmldb_table->getName()); - } - - if (!$this->check_constraint_exists($xmldb_table, $xmldb_field)) { - debugging('Enum for ' . $xmldb_table->getName() . '->' . $xmldb_field->getName() . - ' does not exist. Delete skipped', DEBUG_DEVELOPER); - return; //Enum does not exist, nothing to delete - } - - if (!$sqlarr = $this->generator->getDropEnumSQL($xmldb_table, $xmldb_field)) { - return; //Empty array = nothing to do = no error - } - - $this->execute_sql_arr($sqlarr); - } - /** * This function will rename the field in the table passed as arguments * Before renaming the field, the function will check it exists diff --git a/lib/ddl/mssql_sql_generator.php b/lib/ddl/mssql_sql_generator.php index e11176f2878..b24ab45db72 100644 --- a/lib/ddl/mssql_sql_generator.php +++ b/lib/ddl/mssql_sql_generator.php @@ -51,8 +51,6 @@ class mssql_sql_generator extends sql_generator { public $sequence_name = 'IDENTITY(1,1)'; //Particular name for inline sequences in this generator public $sequence_only = false; //To avoid to output the rest of the field specs, leaving only the name and the sequence_name variable - public $enum_inline_code = false; //Does the generator need to add inline code in the column definition - public $add_table_comments = false; // Does the generator need to add code for table comments public $concat_character = '+'; //Characters to be used as concatenation operator. If not defined @@ -214,11 +212,6 @@ class mssql_sql_generator extends sql_generator { $results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $defaultname; } - /// Look for any check constraint in this field and drop it - if ($drop_check = $this->getDropEnumSQL($xmldb_table, $xmldb_field)) { - $results = array_merge($results, $drop_check); - } - /// Build the standard alter table drop column $results[] = 'ALTER TABLE ' . $tablename . ' DROP COLUMN ' . $fieldname; @@ -259,22 +252,6 @@ class mssql_sql_generator extends sql_generator { $results = array(); - $newt = new xmldb_table($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; - } - } - return $results; } @@ -435,25 +412,6 @@ class mssql_sql_generator extends sql_generator { return $results; } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed 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 - if ($check_constraints = $this->getCheckConstraintsFromDB($xmldb_table, $xmldb_field)) { - $check_constraint = array_shift($check_constraints); /// Get the 1st (should be only one) - $constraint_name = strtolower($check_constraint->name); /// Extract the REAL name - /// All we have to do is to drop the check constraint - return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . - ' DROP CONSTRAINT ' . $constraint_name); - } else { /// Constraint not found. Nothing to do - return array(); - } - } - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its default * (usually invoked from getModifyDefaultSQL() @@ -521,55 +479,6 @@ class mssql_sql_generator extends sql_generator { } } - /** - * Given one xmldb_table returns one array with all the check constraints - * in the table (fetched from DB) - * Optionally the function allows one xmldb_field to be specified in - * 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) { - - - $results = array(); - - $tablename = $this->getTableName($xmldb_table); - - if ($constraints = $this->mdb->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 = ?", array($tablename))) { - foreach ($constraints as $constraint) { - $results[$constraint->name] = $constraint; - } - } - - /// Filter by the required field if specified - if ($xmldb_field) { - $filtered_results = array(); - $filter = $xmldb_field->getName(); - /// Lets clean a bit each constraint description, looking for the filtered field - foreach ($results as $key => $result) { - $description = trim(preg_replace('/[\(\)]/', '', $result->description)); // Parenthesis out & trim - /// description starts by [$filter] assume it's a constraint belonging to the field - if (preg_match("/^\[{$filter}\]/i", $description)) { - $filtered_results[$key] = $result; - } - } - /// Assign filtered results to the final results array - $results = $filtered_results; - } - - return $results; - } - /** * Given three strings (table name, list of fields (comma separated) and suffix), * create the proper object name quoting it if necessary. diff --git a/lib/ddl/mysql_sql_generator.php b/lib/ddl/mysql_sql_generator.php index 5f893633c3e..f09a8f6de88 100644 --- a/lib/ddl/mysql_sql_generator.php +++ b/lib/ddl/mysql_sql_generator.php @@ -59,8 +59,6 @@ class mysql_sql_generator extends sql_generator { public $sequence_extra_code = false; //Does the generator need to add extra code to generate the sequence fields public $sequence_name = 'auto_increment'; //Particular name for inline sequences in this generator - public $enum_extra_code = false; //Does the generator need to add extra code to generate code for the enums in the table - public $add_after_clause = true; // Does the generator need to add the after clause for fields public $concat_character = null; //Characters to be used as concatenation operator. If not defined @@ -233,31 +231,6 @@ class mysql_sql_generator extends sql_generator { return $dbtype; } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its enum - * (usually invoked from getModifyEnumSQL() - */ - public function getCreateEnumSQL($xmldb_table, $xmldb_field) { - /// For MySQL, just alter the field - return $this->getAlterFieldSQL($xmldb_table, $xmldb_field); - } - - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed 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 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 - } - } - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its default * (usually invoked from getModifyDefaultSQL() @@ -311,59 +284,6 @@ class mysql_sql_generator extends sql_generator { return array($comment); } - /** - * Given one xmldb_table returns one array with all the check constraints - * in the table (fetched from DB) - * Optionally the function allows one xmldb_field to be specified in - * 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 - * 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)) { - return array(); - } - - /// Filter by the required field if specified - if ($xmldb_field) { - $filter = $xmldb_field->getName(); - if (!isset($columns[$filter])) { - return array(); - } - $column = ($columns[$filter]); - if (!empty($column->enums)) { - $result = new stdClass(); - $result->name = $filter; - $result->description = implode(', ', $column->enums); - return array($result); - } else { - return array(); - } - - } else { - $results = array(); - /// Iterate over columns searching for enums - foreach ($columns as $key => $column) { - /// Enum found, let's add it to the constraints list - if (!empty($column->enums)) { - $result = new stdClass(); - $result->name = $key; - $result->description = implode(', ', $column->enums); - $results[$key] = $result; - } - } - return $results; - } - } - /** * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg) * return if such name is currently in use (true) or no (false) diff --git a/lib/ddl/oracle_sql_generator.php b/lib/ddl/oracle_sql_generator.php index 133b4ba8f86..a2cf1adc4ee 100644 --- a/lib/ddl/oracle_sql_generator.php +++ b/lib/ddl/oracle_sql_generator.php @@ -55,8 +55,6 @@ class oracle_sql_generator extends sql_generator { public $sequence_name = ''; //Particular name for inline sequences in this generator public $sequence_cache_size = 20; //Size of the sequences values cache (20 = Oracle Default) - public $enum_inline_code = false; //Does the generator need to add inline code in the column definition - public $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY (COLUMNSPECS)'; //The SQL template to alter columns /** @@ -288,20 +286,6 @@ class oracle_sql_generator extends sql_generator { $newt = new xmldb_table($newname); /// Temp table for trigger code generation $results = array_merge($results, $this->getCreateTriggerSQL($newt, $xmldb_field, $newseqname)); - /// 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; - } - } - return $results; } @@ -484,25 +468,6 @@ class oracle_sql_generator extends sql_generator { return $results; } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed 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 - if ($check_constraints = $this->getCheckConstraintsFromDB($xmldb_table, $xmldb_field)) { - $check_constraint = array_shift($check_constraints); /// Get the 1st (should be only one) - $constraint_name = strtolower($check_constraint->name); /// Extract the REAL name - /// All we have to do is to drop the check constraint - return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . - ' DROP CONSTRAINT ' . $constraint_name); - } else { /// Constraint not found. Nothing to do - return array(); - } - } - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its default * (usually invoked from getModifyDefaultSQL() @@ -523,51 +488,6 @@ class oracle_sql_generator extends sql_generator { return $this->getAlterFieldSQL($xmldb_table, $xmldb_field); } - /** - * Given one xmldb_table returns one array with all the check constraints - * in the table (fetched from DB) - * Optionally the function allows one xmldb_field to be specified in - * 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) { - - $results = array(); - - $tablename = strtoupper($this->getTableName($xmldb_table)); - - if ($constraints = $this->mdb->get_records_sql("SELECT lower(c.constraint_name) AS name, c.search_condition AS description - FROM user_constraints c - WHERE c.table_name = ? - AND c.constraint_type = 'C' - AND c.constraint_name not like 'SYS%'", - array($tablename))) { - foreach ($constraints as $constraint) { - $results[$constraint->name] = $constraint; - } - } - - /// Filter by the required field if specified - if ($xmldb_field) { - $filtered_results = array(); - $filter = $xmldb_field->getName(); - /// Lets clean a bit each constraint description, looking for the filtered field - foreach ($results as $key => $result) { - /// description starts by "$filter IN" assume it's a constraint belonging to the field - if (preg_match("/^{$filter} IN/i", $result->description)) { - $filtered_results[$key] = $result; - } - } - /// Assign filtered results to the final results array - $results = $filtered_results; - } - - return $results; - } - /** * Given one xmldb_table returns one string with the sequence of the table * in the table (fetched from DB) diff --git a/lib/ddl/postgres_sql_generator.php b/lib/ddl/postgres_sql_generator.php index 5dc170e1cd8..cf57825b77b 100644 --- a/lib/ddl/postgres_sql_generator.php +++ b/lib/ddl/postgres_sql_generator.php @@ -47,8 +47,6 @@ class postgres_sql_generator extends sql_generator { public $sequence_name_small = 'SERIAL'; //Particular name for inline sequences in this generator public $sequence_only = true; //To avoid to output the rest of the field specs, leaving only the name and the sequence_name variable - public $enum_inline_code = false; //Does the generator need to add inline code in the column definition - public $rename_index_sql = 'ALTER TABLE OLDINDEXNAME RENAME TO NEWINDEXNAME'; //SQL sentence to rename one index //TABLENAME, OLDINDEXNAME, NEWINDEXNAME are dynamically replaced @@ -181,20 +179,6 @@ class postgres_sql_generator extends sql_generator { /// Rename de sequence $results[] = 'ALTER TABLE ' . $oldseqname . ' RENAME TO ' . $newseqname; - /// 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; - } - } - return $results; } @@ -317,25 +301,6 @@ class postgres_sql_generator extends sql_generator { return $results; } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed 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 - if ($check_constraints = $this->getCheckConstraintsFromDB($xmldb_table, $xmldb_field)) { - $check_constraint = array_shift($check_constraints); /// Get the 1st (should be only one) - $constraint_name = strtolower($check_constraint->name); /// Extract the REAL name - /// All we have to do is to drop the check constraint - return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . - ' DROP CONSTRAINT ' . $constraint_name); - } else { /// Constraint not found. Nothing to do - return array(); - } - } - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its default * (usually invoked from getModifyDefaultSQL() @@ -356,55 +321,6 @@ class postgres_sql_generator extends sql_generator { return $this->getAlterFieldSQL($xmldb_table, $xmldb_field); } - /** - * Given one xmldb_table returns one array with all the check constraints - * in the table (fetched from DB) - * Optionally the function allows one xmldb_field to be specified in - * 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) { - - $results = array(); - - $tablename = $this->getTableName($xmldb_table); - - if ($constraints = $this->mdb->get_records_sql("SELECT co.conname AS name, co.consrc AS description - FROM pg_constraint co, pg_class cl - WHERE co.conrelid = cl.oid - AND co.contype = 'c' AND cl.relname = ?", - array($tablename))) { - foreach ($constraints as $constraint) { - $results[$constraint->name] = $constraint; - } - } - - /// Filter by the required field if specified - if ($xmldb_field) { - $filtered_results = array(); - $filter = $xmldb_field->getName(); - /// Lets clean a bit each constraint description, looking for the filtered field - foreach ($results as $key => $result) { - $description = preg_replace('/\("(.*?)"\)/', '($1)', $result->description);// Double quotes out - $description = preg_replace('/[\(\)]/', '', $description); // Parenthesis out - $description = preg_replace('/::[a-z]+/i', '', $description); // Casts out - $description = preg_replace("/({$filter})/i", '@$1@', $description); - $description = trim(preg_replace('/ or /i', ' OR ', $description)); // Uppercase or & trim - /// description starts by @$filter@ assume it's a constraint belonging to the field - if (preg_match("/^@{$filter}@/i", $description)) { - $filtered_results[$key] = $result; - } - } - /// Assign filtered results to the final results array - $results = $filtered_results; - } - - return $results; - } - public function addslashes($s) { // Postgres is gradually switching to ANSI quotes, we need to check what is expected if (!isset($this->std_strings)) { diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index 7f950868397..87d92ad9a80 100644 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -1132,73 +1132,6 @@ class ddl_test extends UnitTestCase { $dbman->drop_key($table, $key); } - /** - * Test behaviour of drop_enum_from_field() and related functions (find_check_constraint_name - * and check_constraint_exists). Needed to be able to drop existing "enum" fields in the upgrade - * from 1.9 to 2.0, will be completely deleted for Moodle 2.1 - * - * Because we already have dropped support for creation of enum fields in 2.0, we are going to - * create them here "manually" (hardcoded DB-dependent SQL). Just to be able to test the - * find and drop functions properly. - * - * TODO: Drop this tests completely from Moodle 2.1 - */ - public function test_drop_enum_from_field() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // Create normal table, no enums. - $table = new xmldb_table('test_table_cust0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $field = new xmldb_field('type'); - $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general'); - $table->addField($field); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertTrue($dbman->table_exists($table)); - $this->assertTrue($dbman->field_exists($table, $field)); - - // Check table hasn't enums at all - $this->assertFalse($dbman->check_constraint_exists($table, $field)); - $this->assertFalse($dbman->find_check_constraint_name($table, $field)); - ob_start(); - $this->assertFalse($dbman->drop_enum_from_field($table, $field)); // This just outputs debug warning if field hasn't enums - ob_end_clean(); - - // Insert some info - $record = new stdClass(); - $record->course = 666; - $record->type = 'qanda'; - $this->assertTrue($DB->insert_record('test_table_cust0', $record, false)); - - // Hackery starts here, depending of the db family we are testing... execute - // the needed SQL statements to get the "type" field defined as enum - $stmt = ''; - switch ($DB->get_dbfamily()) { - case 'mysql': // It's ENUM field for mysql - $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 MODIFY type ENUM ('general', 'qanda', 'moodle') NOT NULL DEFAULT 'general'"; - break; - default: // It's check constraint for "normal" DBs - $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 ADD CONSTRAINT ttcu0_ck CHECK (type IN ('general', 'qanda', 'moodle'))"; - } - $DB->change_database_structure($stmt); - - // Check table has enums now - $this->assertTrue($dbman->check_constraint_exists($table, $field)); - $this->assertTrue($dbman->find_check_constraint_name($table, $field)); - - // Removing an enum value - $dbman->drop_enum_from_field($table, $field); - - // Chech table hasn't enum anymore - $this->assertFalse($dbman->check_constraint_exists($table, $field)); - $this->assertFalse($dbman->find_check_constraint_name($table, $field)); - - $dbman->drop_table($table); - } - public function testRenameField() { $DB = $this->tdb; // do not use global $DB! $dbman = $this->tdb->get_manager(); diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index dfa8d1c4a63..6103931c3f2 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -1358,19 +1358,6 @@ abstract class sql_generator { return array(); } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its enum - * (usually invoked from getModifyEnumSQL() - * - * Note that this method may be dropped in future. - * - * @param xmldb_table $xmldb_table The xmldb_table object instance. - * @param xmldb_field $xmldb_field The xmldb_field object instance. - * - * @todo MDL-31147 Moodle 2.1 - Drop getDropEnumSQL() - */ - public abstract function getDropEnumSQL($xmldb_table, $xmldb_field); - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its default * (usually invoked from getModifyDefaultSQL() @@ -1384,20 +1371,6 @@ abstract class sql_generator { */ public abstract function getDropDefaultSQL($xmldb_table, $xmldb_field); - /** - * 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) - * - * Note that this method may be dropped in future. - * - * @param xmldb_table $xmldb_table The xmldb_table object instance. - * @param xmldb_field $xmldb_field The xmldb_field object instance. - * - * @todo MDL-31147 Moodle 2.1 - Drop getCheckConstraintsFromDB - */ - public abstract function getCheckConstraintsFromDB($xmldb_table, $xmldb_field=null); - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to add its default * (usually invoked from getModifyDefaultSQL() diff --git a/lib/ddl/sqlite_sql_generator.php b/lib/ddl/sqlite_sql_generator.php index 3977b95fe1f..e10b24a869b 100644 --- a/lib/ddl/sqlite_sql_generator.php +++ b/lib/ddl/sqlite_sql_generator.php @@ -54,9 +54,6 @@ class sqlite_sql_generator extends sql_generator { public $sequence_extra_code = false; //Does the generator need to add extra code to generate the sequence fields public $sequence_name = 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'; //Particular name for inline sequences in this generator - public $enum_inline_code = true; //Does the generator need to add inline code in the column definition - public $enum_extra_code = false; //Does the generator need to add extra code to generate code for the enums in the table - public $drop_index_sql = 'ALTER TABLE TABLENAME DROP INDEX INDEXNAME'; //SQL sentence to drop one index //TABLENAME, INDEXNAME are dynamically replaced @@ -281,22 +278,6 @@ class sqlite_sql_generator extends sql_generator { return $this->getAlterTableSchema($xmldb_table); } - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its enum - * (usually invoked from getModifyEnumSQL() - */ - public function getCreateEnumSQL($xmldb_table, $xmldb_field) { - return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field); - } - - /** - * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its enum - * (usually invoked from getModifyEnumSQL() - */ - public function getDropEnumSQL($xmldb_table, $xmldb_field) { - return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field); - } - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needed to create its default * (usually invoked from getModifyDefaultSQL() @@ -377,37 +358,6 @@ class sqlite_sql_generator extends sql_generator { return array(); } - /** - * Given one xmldb_table returns one array with all the check constraints - * in the table (fetched from DB) - * Optionally the function allows one xmldb_field to be specified in - * 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); - // Fetch all the columns in the table - if (!$columns = $this->mdb->get_columns($tablename, false)) { - return array(); - } - $results = array(); - $filter = $xmldb_field ? $xmldb_field->getName() : NULL; - // Iterate over columns searching for enums - foreach ($columns as $key => $column) { - // Enum found, let's add it to the constraints list - if (!empty($column->enums) && (!$filter || $column->name == $filter)) { - $result = new stdClass(); - $result->name = $key; - $result->description = implode(', ', $column->enums); - $results[$key] = $result; - } - } - return $results; - } - /** * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg) * return if such name is currently in use (true) or no (false) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 06448e165fd..eed77e964e5 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -884,29 +884,6 @@ function index_exists($table, $index) { return $DB->get_manager()->index_exists($table, $index); } -/** - * @deprecated - * @global object - * @param string $table - * @param string $field - * @return bool - */ -function find_check_constraint_name($table, $field) { - global $DB; - debugging('Deprecated ddllib function used!'); - return $DB->get_manager()->find_check_constraint_name($table, $field); -} - -/** - * @deprecated - * @global object - */ -function check_constraint_exists($table, $field) { - global $DB; - debugging('Deprecated ddllib function used!'); - return $DB->get_manager()->check_constraint_exists($table, $field); -} - /** * @deprecated * @global object @@ -1070,20 +1047,6 @@ function change_field_notnull($table, $field) { return true; } -/** - * @deprecated - * @global object - * @param string $table - * @param string $field - * @return bool - */ -function change_field_enum($table, $field) { - global $DB; - debugging('Deprecated ddllib function used! Only dropping of enums is allowed.'); - $DB->get_manager()->drop_enum_from_field($table, $field); - return true; -} - /** * @deprecated * @global object diff --git a/lib/dml/database_column_info.php b/lib/dml/database_column_info.php index 0e8a093625c..a31d3621181 100644 --- a/lib/dml/database_column_info.php +++ b/lib/dml/database_column_info.php @@ -60,7 +60,6 @@ class database_column_info { * integer - number of digits * float - digits left from floating point * boolean - 1 - * enums - null * @var int */ public $max_length; @@ -73,16 +72,6 @@ class database_column_info { */ public $scale; - /** - * Enumerated field options, - * null if not enum type - * - * For performance reasons this field is optional! - * You can use DDL sql_generator::getCheckConstraintsFromDB() if needed. - * @var string - */ - public $enums; - /** * True if not null, false otherwise * @var bool diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index f56c89c2ef0..41b6ae1ca76 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -455,10 +455,10 @@ class mysqli_native_moodle_database extends moodle_database { $info->name = $rawcolumn->field; $matches = null; - if (preg_match('/varchar\((\d+)\)/i', $rawcolumn->type, $matches)) { + if (preg_match('/(enum|varchar)\((\d+)\)/i', $rawcolumn->type, $matches)) { $info->type = 'varchar'; $info->meta_type = 'C'; - $info->max_length = $matches[1]; + $info->max_length = $matches[2]; $info->scale = null; $info->not_null = ($rawcolumn->null === 'NO'); $info->default_value = $rawcolumn->default; @@ -552,28 +552,6 @@ class mysqli_native_moodle_database extends moodle_database { $info->auto_increment= false; $info->unique = null; - } else if (preg_match('/enum\((.*)\)/i', $rawcolumn->type, $matches)) { - $info->type = 'enum'; - $info->meta_type = 'C'; - $info->enums = array(); - $info->max_length = 0; - $values = $matches[1]; - $values = explode(',', $values); - foreach ($values as $val) { - $val = trim($val, "'"); - $length = textlib::strlen($val); - $info->enums[] = $val; - $info->max_length = ($info->max_length < $length) ? $length : $info->max_length; - } - $info->scale = null; - $info->not_null = ($rawcolumn->null === 'NO'); - $info->default_value = $rawcolumn->default; - $info->has_default = is_null($info->default_value) ? false : true; - $info->primary_key = ($rawcolumn->key === 'PRI'); - $info->binary = false; - $info->unsigned = null; - $info->auto_increment= false; - $info->unique = null; } $this->columns[$table][$info->name] = new database_column_info($info); @@ -604,16 +582,6 @@ class mysqli_native_moodle_database extends moodle_database { } else if (is_float($value) and ($column->meta_type == 'C' or $column->meta_type == 'X')) { $value = "$value"; } - // workaround for problem with wrong enums in mysql - TODO: Out in Moodle 2.1 - if (!empty($column->enums)) { - if (is_null($value) and !$column->not_null) { - // ok - nulls allowed - } else { - if (!in_array((string)$value, $column->enums)) { - throw new dml_write_exception('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.'); - } - } - } return $value; } diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 3983dd10b41..b780bd42946 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -523,7 +523,6 @@ class oci_native_moodle_database extends moodle_database { or $rawcolumn->COLTYPE === 'NVARCHAR' or $rawcolumn->COLTYPE === 'CHAR' or $rawcolumn->COLTYPE === 'NCHAR') { - //TODO add some basic enum support here $info->type = $rawcolumn->COLTYPE; $info->meta_type = 'C'; $info->max_length = $rawcolumn->WIDTH; diff --git a/lib/dml/pdo_moodle_database.php b/lib/dml/pdo_moodle_database.php index eced736fcd3..8b090a49161 100644 --- a/lib/dml/pdo_moodle_database.php +++ b/lib/dml/pdo_moodle_database.php @@ -411,17 +411,6 @@ abstract class pdo_moodle_database extends moodle_database { if (is_bool($value)) { $value = (int)$value; // prevent "false" problems } - if (!empty($column->enums)) { - // workaround for problem with wrong enums - if (is_null($value) and !$column->not_null) { - // ok - nulls allowed - } else { - if (!in_array((string)$value, $column->enums)) { - debugging('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.'); - return false; - } - } - } $cleaned[$field] = $value; } diff --git a/lib/dml/sqlite3_pdo_moodle_database.php b/lib/dml/sqlite3_pdo_moodle_database.php index b28334c87cc..01731fc6c4a 100644 --- a/lib/dml/sqlite3_pdo_moodle_database.php +++ b/lib/dml/sqlite3_pdo_moodle_database.php @@ -260,13 +260,6 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database { $columninfo['meta_type'] = 'C'; break; case 'enu': // enums - if (preg_match('|'.$columninfo['name'].'\W+in\W+\(/\*liststart\*/(.*?)/\*listend\*/\)|im', $createsql, $tmp)) { - $tmp = explode(',', $tmp[1]); - foreach($tmp as $value) { - $columninfo['enums'][] = trim($value, '\'"'); - } - unset($tmp); - } $columninfo['meta_type'] = 'C'; break; case 'tex': // text diff --git a/lib/xmldb/xmldb.dtd b/lib/xmldb/xmldb.dtd index 0d20746a358..20bad535c7b 100644 --- a/lib/xmldb/xmldb.dtd +++ b/lib/xmldb/xmldb.dtd @@ -1,9 +1,6 @@ - - - diff --git a/lib/xmldb/xmldb.xsd b/lib/xmldb/xmldb.xsd index e7954237820..a2079c6dceb 100644 --- a/lib/xmldb/xmldb.xsd +++ b/lib/xmldb/xmldb.xsd @@ -64,9 +64,6 @@ - - - diff --git a/lib/xmldb/xmldb_field.php b/lib/xmldb/xmldb_field.php index bcede355d15..e0819fa0084 100644 --- a/lib/xmldb/xmldb_field.php +++ b/lib/xmldb/xmldb_field.php @@ -66,9 +66,6 @@ class xmldb_field extends xmldb_object { function setAttributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) { debugging('XMLDBField->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_field->set_attributes() instead.', DEBUG_DEVELOPER); - if ($enum) { - debugging('Also, ENUMs support has been dropped in Moodle 2.0. Your fields specs are incorrect because you are trying to introduce one new ENUM. Created DB estructures will ignore that.'); - } return $this->set_attributes($type, $precision, $unsigned, $notnull, $sequence, $default, $previous); } @@ -360,15 +357,6 @@ class xmldb_field extends xmldb_object { $this->next = trim($xmlarr['@']['NEXT']); } - /// TODO: Drop this check in Moodle 2.1 - /// Detect if there is old enum information in the XML file - if (isset($xmlarr['@']['ENUM']) && isset($xmlarr['@']['ENUMVALUES'])) { - $this->hasenums = true; - if ($xmlarr['@']['ENUM'] == 'true') { - $this->hasenumsenabled = true; - } - } - /// Set some attributes if ($result) { $this->loaded = true; diff --git a/lib/xmldb/xmldb_table.php b/lib/xmldb/xmldb_table.php index ed77de88323..a7011e4620b 100644 --- a/lib/xmldb/xmldb_table.php +++ b/lib/xmldb/xmldb_table.php @@ -709,9 +709,6 @@ class xmldb_table extends xmldb_object { function addFieldInfo($name, $type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) { debugging('XMLDBTable->addFieldInfo() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_table->add_field() instead', DEBUG_DEVELOPER); - if ($enum) { - debugging('Also, ENUMs support has been dropped in Moodle 2.0. Your fields specs are incorrect because you are trying to introduce one new ENUM. Created DB estructures will ignore that.'); - } return $this->add_field($name, $type, $precision, $unsigned, $notnull, $sequence, $default, $previous);