Skip to content

Commit

Permalink
MDL-29514 drop support for enum data types
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Mar 10, 2012
1 parent 23778a4 commit 05aae0a
Show file tree
Hide file tree
Showing 20 changed files with 2 additions and 776 deletions.
34 changes: 0 additions & 34 deletions admin/tool/xmldb/actions/main_view/main_view.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,40 +277,6 @@ function invoke() {
}
}
}
// 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 .= '<tr class="highlight"><td class="error cell" colspan="10">';
} else {
$o .= '<tr class="r' . $row . '"><td class="error cell" colspan="10">';
}
$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' .
' <a href="http://docs.moodle.org/dev/DB_layer_2.0_migration_docs#The_changes">' .
' Moodle Docs</a> 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 .= '</td></tr>';
}
}
}
}
}
}
}
// 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') &&
Expand Down
59 changes: 0 additions & 59 deletions admin/tool/xmldb/actions/view_table_php/view_table_php.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ function invoke() {
$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',
Expand Down Expand Up @@ -224,13 +223,6 @@ function invoke() {
$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));
Expand Down Expand Up @@ -593,57 +585,6 @@ function change_field_notnull_php($structure, $table, $field) {
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
Expand Down
82 changes: 0 additions & 82 deletions lib/ddl/database_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,57 +221,6 @@ public function index_exists(xmldb_table $xmldb_table, xmldb_index $xmldb_index)
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"
Expand Down Expand Up @@ -707,37 +656,6 @@ public function change_field_default(xmldb_table $xmldb_table, xmldb_field $xmld
$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
Expand Down
91 changes: 0 additions & 91 deletions lib/ddl/mssql_sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -214,11 +212,6 @@ public function getDropFieldSQL($xmldb_table, $xmldb_field) {
$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;

Expand Down Expand Up @@ -259,22 +252,6 @@ public function getRenameTableExtraSQL($xmldb_table, $newname) {

$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;
}

Expand Down Expand Up @@ -435,25 +412,6 @@ public function getModifyDefaultSQL($xmldb_table, $xmldb_field) {
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()
Expand Down Expand Up @@ -521,55 +479,6 @@ public function getDefaultConstraintName($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 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.
Expand Down
Loading

0 comments on commit 05aae0a

Please sign in to comment.