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