Commit 2d2d79ef authored by stronk7's avatar stronk7
Browse files

MDL-18577 drop enums support - step3: Drop enums from generator classes. Only...

MDL-18577 drop enums support - step3: Drop enums from generator classes. Only code used to drop enums remains until Moodle 2.1
parent ffaf9c59
......@@ -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
......
......@@ -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) {
......
......@@ -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();
}
......
......@@ -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) {
......
......@@ -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) {
......
......@@ -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);
......
......@@ -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);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment