Now generators support to add/drop column defaults
This commit is contained in:
@@ -51,6 +51,9 @@ class XMLDBgenerator {
|
||||
var $unsigned_allowed = true; // To define in the generator must handle unsigned information
|
||||
var $default_for_char = null; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
|
||||
|
||||
var $drop_default_clause_required = false; //To specify if the generator must use some DEFAULT clause to drop defaults
|
||||
var $drop_default_clause = ''; //The DEFAULT clause required to drop defaults
|
||||
|
||||
var $default_after_null = true; //To decide if the default clause of each field must go after the null clause
|
||||
|
||||
var $specify_nulls = false; //To force the generator if NULL clauses must be specified. It shouldn't be necessary
|
||||
@@ -418,7 +421,7 @@ class XMLDBgenerator {
|
||||
$default = '';
|
||||
|
||||
if ($xmldb_field->getDefault() != NULL) {
|
||||
$default = ' default ';
|
||||
$default = ' DEFAULT ';
|
||||
if ($xmldb_field->getType() == XMLDB_TYPE_CHAR ||
|
||||
$xmldb_field->getType() == XMLDB_TYPE_TEXT) {
|
||||
$default .= "'" . addslashes($xmldb_field->getDefault()) . "'";
|
||||
@@ -431,7 +434,15 @@ class XMLDBgenerator {
|
||||
if ($this->default_for_char !== NULL &&
|
||||
$xmldb_field->getType() == XMLDB_TYPE_CHAR &&
|
||||
$xmldb_field->getNotNull()) {
|
||||
$default .= ' default ' . "'" . $this->default_for_char . "'";
|
||||
$default = ' DEFAULT ' . "'" . $this->default_for_char . "'";
|
||||
} else {
|
||||
/// If the DB requires to explicity define some clause to drop one default, do it here
|
||||
/// never applying defaults to TEXT and BINARY fields
|
||||
if ($this->drop_default_clause_required &&
|
||||
$xmldb_field->getType() != XMLDB_TYPE_TEXT &&
|
||||
$xmldb_field->getType() != XMLDB_TYPE_BINARY) {
|
||||
$default = ' DEFAULT ' . $this->drop_default_clause;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $default;
|
||||
@@ -593,6 +604,7 @@ class XMLDBgenerator {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given three strings (table name, list of fields (comma separated) and suffix), create the proper object name
|
||||
* quoting it if necessary
|
||||
@@ -824,6 +836,22 @@ class XMLDBgenerator {
|
||||
return array('Code for table drop goes to getDropTableExtraSQL(). Can be disabled with drop_table_extra_code=false;');
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getDropDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
return array('Code to drop one default goes to getDropDefaultSQL()');
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
return array('Code to create one default goes to getCreate()');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of reserved words (lowercase) for this DB
|
||||
* You MUST provide the real list for each DB inside every XMLDB class
|
||||
|
||||
@@ -157,11 +157,8 @@ class XMLDBmssql extends XMLDBgenerator {
|
||||
$checkconsname = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
|
||||
|
||||
/// Look for any default constraint in this field and drop it
|
||||
if ($default = get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint
|
||||
FROM syscolumns
|
||||
WHERE id = object_id('{$tablename}') AND
|
||||
name = '{$fieldname}'")) {
|
||||
$results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $default->defaultconstraint;
|
||||
if ($defaultname = $this->getDefaultConstraintName($xmldb_table, $xmldb_field)) {
|
||||
$results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $defaultname;
|
||||
}
|
||||
|
||||
/// Look for any check constraint in this field and drop it
|
||||
@@ -186,6 +183,66 @@ class XMLDBmssql extends XMLDBgenerator {
|
||||
return parent::getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// This method does exactly the same than getDropDefaultSQL(), first trying to
|
||||
/// drop the default if it exists and then, regenerating it, so we simply wrap over it
|
||||
return $this->getDropDefaultSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getDropDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// MSSQL is a bit special and it requires the corresponding DEFAULT CONSTRAINT to be dropped
|
||||
|
||||
$results = array();
|
||||
|
||||
/// Get the quoted name of the table and field
|
||||
$tablename = $this->getEncQuoted($this->prefix . $xmldb_table->getName());
|
||||
$fieldname = $this->getEncQuoted($xmldb_field->getName());
|
||||
|
||||
/// Look for the default contraint and, if found, drop it
|
||||
if ($defaultname = $this->getDefaultConstraintName($xmldb_table, $xmldb_field)) {
|
||||
$results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $defaultname;
|
||||
}
|
||||
|
||||
/// Now, check if, with the current field attributes, we have to build one default
|
||||
if ($default_clause = $this->getDefaultClause($xmldb_field)) {
|
||||
/// We need to build the default (Moodle) default, so do it
|
||||
$results[] = 'ALTER TABLE ' . $tablename . ' ADD' . $default_clause . ' FOR ' . $fieldname;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, returns the name of its default constraint in DB
|
||||
* or false if not found
|
||||
* This function should be considered internal and never used outside from generator
|
||||
*/
|
||||
function getDefaultConstraintName($xmldb_table, $xmldb_field) {
|
||||
|
||||
global $db;
|
||||
|
||||
/// Get the quoted name of the table and field
|
||||
$tablename = $this->getEncQuoted($this->prefix . $xmldb_table->getName());
|
||||
$fieldname = $this->getEncQuoted($xmldb_field->getName());
|
||||
|
||||
/// Look for any default constraint in this field and drop it
|
||||
if ($default = get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint
|
||||
FROM syscolumns
|
||||
WHERE id = object_id('{$tablename}')
|
||||
AND name = '{$fieldname}'")) {
|
||||
return $default->defaultconstraint;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of reserved words (lowercase) for this DB
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,9 @@ class XMLDBmysql extends XMLDBGenerator {
|
||||
|
||||
var $default_for_char = ''; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
|
||||
|
||||
var $drop_default_clause_required = true; //To specify if the generator must use some DEFAULT clause to drop defaults
|
||||
var $drop_default_clause = 'NULL'; //The DEFAULT clause required to drop defaults
|
||||
|
||||
var $primary_key_name = ''; //To force primary key names to one string (null=no force)
|
||||
|
||||
var $unique_keys = false; // Does the generator build unique key
|
||||
@@ -152,6 +155,26 @@ class XMLDBmysql extends XMLDBGenerator {
|
||||
return $dbtype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for MySQL that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getDropDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for MySQL that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDB Field, return its enum SQL
|
||||
*/
|
||||
|
||||
@@ -41,6 +41,9 @@ class XMLDBoci8po extends XMLDBgenerator {
|
||||
var $default_for_char = ' '; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
|
||||
// Using this whitespace here because Oracle doesn't distinguish empty and null! :-(
|
||||
|
||||
var $drop_default_clause_required = true; //To specify if the generator must use some DEFAULT clause to drop defaults
|
||||
var $drop_default_clause = 'NULL'; //The DEFAULT clause required to drop defaults
|
||||
|
||||
var $default_after_null = false; //To decide if the default clause of each field must go after the null clause
|
||||
|
||||
var $unique_keys = false; // Does the generator build unique keys
|
||||
@@ -232,7 +235,7 @@ class XMLDBoci8po extends XMLDBgenerator {
|
||||
}
|
||||
$olddecimals = empty($metac->scale) ? null : $metac->scale;
|
||||
$oldnotnull = empty($metac->not_null) ? false : $metac->not_null;
|
||||
$olddefault = empty($metac->default_value) ? null : $metac->default_value;
|
||||
$olddefault = empty($metac->default_value) || strtoupper($metac->default_value) == 'NULL' ? null : $metac->default_value;
|
||||
|
||||
$typechanged = true; //By default, assume that the column type has changed
|
||||
$precisionchanged = true; //By default, assume that the column precision has changed
|
||||
@@ -274,6 +277,7 @@ class XMLDBoci8po extends XMLDBgenerator {
|
||||
("'" . $xmldb_field->getDefault() . "'" === $olddefault)) { //Equality with quotes because ADOdb returns the default with quotes
|
||||
$defaultchanged = false;
|
||||
}
|
||||
|
||||
/// Detect if we are changing the nullability
|
||||
if (($xmldb_field->getNotnull() === $oldnotnull)) {
|
||||
$notnullchanged = false;
|
||||
@@ -304,7 +308,7 @@ class XMLDBoci8po extends XMLDBgenerator {
|
||||
/// Re-enable the notnull and default sections so the general AlterFieldSQL can use it
|
||||
$this->alter_column_skip_notnull = false;
|
||||
$this->alter_column_skip_default = false;
|
||||
/// Dissavle the type section because we have done it with the temp field
|
||||
/// Dissable the type section because we have done it with the temp field
|
||||
$this->alter_column_skip_type = true;
|
||||
}
|
||||
|
||||
@@ -342,6 +346,26 @@ class XMLDBoci8po extends XMLDBgenerator {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for Oracle that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getDropDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for Oracle that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of reserved words (lowercase) for this DB
|
||||
*/
|
||||
|
||||
@@ -266,6 +266,26 @@ class XMLDBpostgres7 extends XMLDBgenerator {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for PostgreSQL that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default
|
||||
* (usually invoked from getModifyDefaultSQL()
|
||||
*/
|
||||
function getDropDefaultSQL($xmldb_table, $xmldb_field) {
|
||||
/// Just a wrapper over the getAlterFieldSQL() function for PostgreSQL that
|
||||
/// is capable of handling defaults
|
||||
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of reserved words (lowercase) for this DB
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user