Skip to content
Permalink
Browse files

MDL-18577 drop enums support - step3: Drop enums from generator class…

…es. Only code used to drop enums remains until Moodle 2.1
  • Loading branch information...
stronk7
stronk7 committed May 1, 2009
1 parent ffaf9c5 commit 2d2d79ef492a8dd8568d5c1553e9b72d919eb634
@@ -214,6 +214,8 @@ public function index_exists(xmldb_table $xmldb_table, xmldb_index $xmldb_index)
* 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
@@ -192,17 +192,6 @@ public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null
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 @@ public function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) {
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 @@ public function getRenameTableExtraSQL($xmldb_table, $newname) {
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 @@ public function getModifyDefaultSQL($xmldb_table, $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
@@ -498,6 +447,8 @@ public function getDefaultConstraintName($xmldb_table, $xmldb_field) {
* 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) {
@@ -264,10 +264,17 @@ public function getCreateEnumSQL($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) {
/// 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 @@ public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
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 @@ function getCommentSQL ($xmldb_table) {
* 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();
}
@@ -79,7 +79,7 @@ public function reset_sequence($table) {
// 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 @@ public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null
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 @@ public function getCreateTriggerSQL($xmldb_table, $xmldb_field) {
*/
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 @@ function getCommentSQL ($xmldb_table) {
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 @@ public function getRenameTableExtraSQL($xmldb_table, $newname) {
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 @@ public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause =
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 @@ public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
* 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) {
@@ -133,17 +133,6 @@ public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null
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 @@ public function getRenameTableExtraSQL($xmldb_table, $newname) {
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 @@ public function getAlterFieldSQL($xmldb_table, $xmldb_field) {
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 @@ public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
* 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) {
@@ -1179,13 +1179,17 @@ public function getDropTableExtraSQL($xmldb_table) {
/**
* 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);
/**
* 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);

0 comments on commit 2d2d79e

Please sign in to comment.
You can’t perform that action at this time.