From: stronk7 Date: Sun, 24 Sep 2006 00:02:34 +0000 (+0000) Subject: Now Oracle supports the first part of alter table alter column statements X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=19c8321e07ef478504f8f5ffc5154d4ae23c13c9;p=moodle.git Now Oracle supports the first part of alter table alter column statements --- diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 9a8c7f1c17..f7a1e2e2f8 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -97,6 +97,10 @@ class XMLDBgenerator { var $alter_column_skip_default = false; //The generator will skip the default clause on alter columns + var $alter_column_skip_type = false; //The generator will skip the type clause on alter columns + + var $alter_column_skip_notnull = false; //The generator will skip the null/notnull 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) @@ -297,7 +301,7 @@ class XMLDBgenerator { /** * Given one correct XMLDBField, returns the complete SQL line to create it */ - function getFieldSQL($xmldb_field, $skip_default_clause = false) { + function getFieldSQL($xmldb_field, $skip_type_clause = false, $skip_default_clause = false, $skip_notnull_clause = false) { /// First of all, convert integers to numbers if defined if ($this->integer_to_number) { @@ -314,12 +318,15 @@ class XMLDBgenerator { /// The name $field = $this->getEncQuoted($xmldb_field->getName()); - /// The type and length (if the field isn't enum) - if (!$xmldb_field->getEnum() || $this->enum_inline_code == false) { - $field .= ' ' . $this->getTypeSQL($xmldb_field->getType(), $xmldb_field->getLength(), $xmldb_field->getDecimals()); - } else { - /// call to custom function - $field .= ' ' . $this->getEnumSQL($xmldb_field); + /// The type and length only if we don't want to skip it + if (!$skip_type_clause) { + /// The type and length (if the field isn't enum) + if (!$xmldb_field->getEnum() || $this->enum_inline_code == false) { + $field .= ' ' . $this->getTypeSQL($xmldb_field->getType(), $xmldb_field->getLength(), $xmldb_field->getDecimals()); + } else { + /// call to custom function + $field .= ' ' . $this->getEnumSQL($xmldb_field); + } } /// The unsigned if supported if ($this->unsigned_allowed && ($xmldb_field->getType() == XMLDB_TYPE_INTEGER || @@ -331,11 +338,14 @@ class XMLDBgenerator { } /// Calculate the not null clause $notnull = ''; - if ($xmldb_field->getNotNull()) { - $notnull = ' NOT NULL'; - } else { - if ($this->specify_nulls) { - $notnull = ' NULL'; + /// Only if we don't want to skip it + if (!$skip_notnull_clause) { + if ($xmldb_field->getNotNull()) { + $notnull = ' NOT NULL'; + } else { + if ($this->specify_nulls) { + $notnull = ' NULL'; + } } } /// Calculate the default clause @@ -470,7 +480,7 @@ class XMLDBgenerator { $results[] = $drop; - /// call to getDropTableExtraSQL() if $rename_table_extra_code is enabled. It will add sequence/trigger drop code. + /// call to getDropTableExtraSQL() if $drop_table_extra_code is enabled. It will add sequence/trigger drop code. if ($this->drop_table_extra_code) { $extra_sentences = $this->getDropTableExtraSQL($xmldb_table); $results = array_merge($results, $extra_sentences); @@ -537,13 +547,18 @@ class XMLDBgenerator { $results = array(); + /// Always specify NULLs in alter fields because we can change not nulls to nulls + $this->specify_nulls = true; + /// 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); + $alter = str_replace('COLUMNSPECS', $this->getFieldSQL($xmldb_field, $this->alter_column_skip_type, + $this->alter_column_skip_default, + $this->alter_column_skip_notnull), $alter); /// Build the standard alter table modify $results[] = $alter; diff --git a/lib/xmldb/classes/generators/oci8po/oci8po.class.php b/lib/xmldb/classes/generators/oci8po/oci8po.class.php index 6e434adb3b..3de7a1777b 100644 --- a/lib/xmldb/classes/generators/oci8po/oci8po.class.php +++ b/lib/xmldb/classes/generators/oci8po/oci8po.class.php @@ -56,6 +56,8 @@ class XMLDBoci8po extends XMLDBgenerator { var $enum_inline_code = false; //Does the generator need to add inline code in the column definition + var $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY (COLUMNSPECS)'; //The SQL template to alter columns + /** * Creates one new XMLDBpostgres7 */ @@ -192,6 +194,52 @@ class XMLDBoci8po extends XMLDBgenerator { return $this->getDropSequenceSQL($xmldb_table, $xmldb_field, false); } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table + * Oracle has some severe limits: + * - clob and blob fields doesn't allow type to be specified + * - error is dropped if the null/not null clause is specified and hasn't changed + */ + function getAlterFieldSQL($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()); + + /// Take a look to field metadata + $meta = array_change_key_case($db->MetaColumns($tablename)); + $metac = $meta[$fieldname]; + $oldtype = strtolower($metac->type); + $oldlength = $metac->max_length; + $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; + + $islob = false; + + /// If field is CLOB and new one is also XMLDB_TYPE_TEXT or + /// if fiels is BLOB and new one is also XMLDB_TYPE_BINARY + /// prevent type to be specified, so only NULL and DEFAULT clauses are allowed + if (($oldtype = 'clob' && $xmldb_field->getType() == XMLDB_TYPE_TEXT) || + ($oldtype = 'blob' && $xmldb_field->getType() == XMLDB_TYPE_BINARY)) { + $this->alter_column_skip_type = true; + $islob = true; + } + + /// If field is NOT NULL and the new one too or + /// if field is NULL and the new one too + /// prevent null clause to be specified + if (($oldnotnull && $xmldb_field->getNotnull()) || + (!$oldnotnull && !$xmldb_field->getNotnull())) { + $this->alter_column_skip_notnull = true; + } + + /// In the rest of cases, use the general generator + return parent::getAlterFieldSQL($xmldb_table, $xmldb_field); + } + /** * Returns an array of reserved words (lowercase) for this DB */