From: stronk7 Date: Sat, 23 Sep 2006 21:24:51 +0000 (+0000) Subject: adding support for alter columns X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=0e9e0b3bb3e0aef2010f810fa688a1644c315cc9;p=moodle.git adding support for alter columns (not finished yet) --- diff --git a/lib/ddllib.php b/lib/ddllib.php index 30b60c8714..345952bf81 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -390,4 +390,97 @@ function drop_field($table, $field, $continue=true, $feedback=true) { return execute_sql_arr($sqlarr, $continue, $feedback); } +/** + * This function will change the precision of the field in the table passed as arguments + * + * @uses $CFG, $db + * @param XMLDBTable table object (just the name is mandatory) + * @param XMLDBField field object (full specs are required) + * @param boolean continue to specify if must continue on error (true) or stop (false) + * @param boolean feedback to specify to show status info (true) or not (false) + * @return boolean true on success, false on error + */ +function change_field_precision($table, $field, $continue=true, $feedback=true) { + + global $CFG, $db; + + $status = true; + + if (strtolower(get_class($table)) != 'xmldbtable') { + return false; + } + if (strtolower(get_class($field)) != 'xmldbfield') { + return false; + } + + if(!$sqlarr = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, false)) { + return false; + } + + return execute_sql_arr($sqlarr, $continue, $feedback); +} + +/** + * This function will change the unsigned/signed of the field in the table passed as arguments + * + * @uses $CFG, $db + * @param XMLDBTable table object (just the name is mandatory) + * @param XMLDBField field object (full specs are required) + * @param boolean continue to specify if must continue on error (true) or stop (false) + * @param boolean feedback to specify to show status info (true) or not (false) + * @return boolean true on success, false on error + */ +function change_field_unsigned($table, $field, $continue=true, $feedback=true) { + +/// Just a wrapper over change_field_precision. Does exactly the same processing + return change_field_precision($table, $field, $continue, $feedback); +} + +/** + * This function will change the nullability of the field in the table passed as arguments + * + * @uses $CFG, $db + * @param XMLDBTable table object (just the name is mandatory) + * @param XMLDBField field object (full specs are required) + * @param boolean continue to specify if must continue on error (true) or stop (false) + * @param boolean feedback to specify to show status info (true) or not (false) + * @return boolean true on success, false on error + */ +function change_field_notnull($table, $field, $continue=true, $feedback=true) { + +/// Just a wrapper over change_field_precision. Does exactly the same processing + return change_field_precision($table, $field, $continue, $feedback); +} + +/** + * This function will change the default of the field in the table passed as arguments + * One null value in the default field means delete the default + * + * @uses $CFG, $db + * @param XMLDBTable table object (just the name is mandatory) + * @param XMLDBField field object (full specs are required) + * @param boolean continue to specify if must continue on error (true) or stop (false) + * @param boolean feedback to specify to show status info (true) or not (false) + * @return boolean true on success, false on error + */ +function change_field_default($table, $field, $continue=true, $feedback=true) { + + global $CFG, $db; + + $status = true; + + if (strtolower(get_class($table)) != 'xmldbtable') { + return false; + } + if (strtolower(get_class($field)) != 'xmldbfield') { + return false; + } + + if(!$sqlarr = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, false)) { + return false; + } + + return execute_sql_arr($sqlarr, $continue, $feedback); +} + ?> diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index 83fadb9c02..14be973803 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -873,6 +873,42 @@ class XMLDBTable extends XMLDBObject { return $results; } + /** + * This function will return the SQL code needed to alter one field in the table for the specified DB and + * prefix. Just one simple wrapper over generators. + */ + function getAlterFieldSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) { + + $results = array(); + + $classname = 'XMLDB' . $dbtype; + $generator = new $classname(); + $generator->setPrefix($prefix); + $results = $generator->getAlterFieldSQL($this, $xmldb_field); + if ($statement_end) { + $results = $generator->getEndedStatements($results); + } + return $results; + } + + /** + * This function will return the SQL code needed to modify the default of one field in the table for the specified DB and + * prefix. Just one simple wrapper over generators. + */ + function getModifyDefaultSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) { + + $results = array(); + + $classname = 'XMLDB' . $dbtype; + $generator = new $classname(); + $generator->setPrefix($prefix); + $results = $generator->getModifyDefaultSQL($this, $xmldb_field); + if ($statement_end) { + $results = $generator->getEndedStatements($results); + } + return $results; + } + } ?> diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 36d59b390a..9a8c7f1c17 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -93,6 +93,10 @@ class XMLDBgenerator { var $drop_table_extra_code = false; //Does the generatos need to add code after table drop + var $alter_column_sql = 'ALTER TABLE TABLENAME ALTER COLUMN COLUMNSPECS'; //The SQL template to alter columns + + var $alter_column_skip_default = false; //The generator will skip the default clause on alter columns + var $prefix; // Prefix to be used for all the DB objects var $reserved_words; // List of reserved words (in order to quote them properly) @@ -293,7 +297,7 @@ class XMLDBgenerator { /** * Given one correct XMLDBField, returns the complete SQL line to create it */ - function getFieldSQL($xmldb_field) { + function getFieldSQL($xmldb_field, $skip_default_clause = false) { /// First of all, convert integers to numbers if defined if ($this->integer_to_number) { @@ -335,7 +339,11 @@ class XMLDBgenerator { } } /// Calculate the default clause - $default = $this->getDefaultClause($xmldb_field); + if (!$skip_default_clause) { //Only if we don't want to skip it + $default = $this->getDefaultClause($xmldb_field); + } else { + $default = ''; + } /// Based on default_after_null, set both clauses properly if ($this->default_after_null) { $field .= $notnull . $default; @@ -522,6 +530,51 @@ class XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table + */ + function getAlterFieldSQL($xmldb_table, $xmldb_field) { + + $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()); + + /// Build de alter sentence using the alter_column_sql template + $alter = str_replace('TABLENAME', $this->getEncQuoted($this->prefix . $xmldb_table->getName()), $this->alter_column_sql); + $alter = str_replace('COLUMNSPECS', $this->getFieldSQL($xmldb_field, $this->alter_column_skip_default), $alter); + + /// Build the standard alter table modify + $results[] = $alter; + + /// Add the after clause if necesary + if ($this->add_after_clause && $xmldb_field->getPrevious()) { + $altertable .= ' after ' . $this->getEncQuoted($xmldb_field->getPrevious()); + } + + return $results; + } + + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to modify the default of the field in the table + */ + function getModifyDefaultSQL($xmldb_table, $xmldb_field) { + + $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()); + + /// Decide if we are going to create/modify or to drop the default + if ($xmldb_field->getDefault() === null) { + return $this->getDropDefaultSQL($xmldb_table, $xmldb_field); //Drop + } else { + return $this->getCreateDefaultSQL($xmldb_table, $xmldb_field); //Create/modify + } + } + /** * Given three strings (table name, list of fields (comma separated) and suffix), create the proper object name * quoting it if necessary