* 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
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
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
*/
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;
}
- /**
- * 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
* 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) {
/**
* 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
+ }
}
/**
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
*/
* 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();
}
// 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) {
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
*/
*/
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;
}
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
*/
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;
}
- /**
- * 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
* 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) {
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
*/
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;
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
* 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) {
/**
* 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);
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
*/
* 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);