From b899d9bf1d529b5310db0cb938f450014d80817b Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sat, 30 Sep 2006 17:03:17 +0000 Subject: [PATCH] Added support to change_field_enum() across all RDBMS --- lib/xmldb/classes/XMLDBTable.class.php | 18 ++++++++ .../generators/XMLDBGenerator.class.php | 41 ++++++++++++++++++- .../classes/generators/mssql/mssql.class.php | 20 +++++++++ .../classes/generators/mysql/mysql.class.php | 18 ++++++++ .../generators/oci8po/oci8po.class.php | 20 +++++++++ .../generators/postgres7/postgres7.class.php | 20 +++++++++ 6 files changed, 135 insertions(+), 2 deletions(-) diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index 79c5027b47..11c9c1e0e2 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -891,6 +891,24 @@ class XMLDBTable extends XMLDBObject { return $results; } + /** + * This function will return the SQL code needed to modify the enum of one field in the table for the specified DB and + * prefix. Just one simple wrapper over generators. + */ + function getModifyEnumSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) { + + $results = array(); + + $classname = 'XMLDB' . $dbtype; + $generator = new $classname(); + $generator->setPrefix($prefix); + $results = $generator->getModifyEnumSQL($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. diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 5a2fe8721d..feae0b6ba0 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -577,7 +577,28 @@ class XMLDBgenerator { /// Add the after clause if necesary if ($this->add_after_clause && $xmldb_field->getPrevious()) { - $altertable .= ' after ' . $this->getEncQuoted($xmldb_field->getPrevious()); + $alter .= ' after ' . $this->getEncQuoted($xmldb_field->getPrevious()); + } + + return $results; + } + + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to modify the enum of the field in the table + */ + function getModifyEnumSQL($xmldb_table, $xmldb_field) { + + $results = array(); + + /// Get the quoted name of the table and field + $tablename = $this->getTableName($xmldb_table); + $fieldname = $this->getEncQuoted($xmldb_field->getName()); + + /// Decide if we are going to create or to drop the enum (based exclusively in the values passed!) + if (!$xmldb_field->getEnum()) { + $results = $this->getDropEnumSQL($xmldb_table, $xmldb_field); //Drop + } else { + $results = $this->getCreateEnumSQL($xmldb_table, $xmldb_field); //Create/modify } return $results; @@ -971,6 +992,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 enum + * (usually invoked from getModifyEnumSQL() + */ + function getDropEnumSQL($xmldb_table, $xmldb_field) { + return array('Code to drop one enum goes to getDropEnumSQL()'); + } + + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add its enum + * (usually invoked from getModifyEnumSQL() + */ + function getCreateEnumSQL($xmldb_table, $xmldb_field) { + return array('Code to create one enum goes to getCreateEnumSQL()'); + } + /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default * (usually invoked from getModifyDefaultSQL() @@ -984,7 +1021,7 @@ class XMLDBgenerator { * (usually invoked from getModifyDefaultSQL() */ function getCreateDefaultSQL($xmldb_table, $xmldb_field) { - return array('Code to create one default goes to getCreate()'); + return array('Code to create one default goes to getCreateDefaultSQL()'); } /** diff --git a/lib/xmldb/classes/generators/mssql/mssql.class.php b/lib/xmldb/classes/generators/mssql/mssql.class.php index 912e622606..761556cdb7 100644 --- a/lib/xmldb/classes/generators/mssql/mssql.class.php +++ b/lib/xmldb/classes/generators/mssql/mssql.class.php @@ -250,6 +250,26 @@ class XMLDBmssql extends XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its enum + * (usually invoked from getModifyEnumSQL() + */ + 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 XMLDBTable and one XMLDBField, return the SQL statements needded to drop its enum + * (usually invoked from getModifyEnumSQL() + */ + function getDropEnumSQL($xmldb_table, $xmldb_field) { + /// All we have to do is to drop the check constraint + return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . + ' DROP CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck')); + } + /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default * (usually invoked from getModifyDefaultSQL() diff --git a/lib/xmldb/classes/generators/mysql/mysql.class.php b/lib/xmldb/classes/generators/mysql/mysql.class.php index fd052e0136..99c7b1cc2c 100644 --- a/lib/xmldb/classes/generators/mysql/mysql.class.php +++ b/lib/xmldb/classes/generators/mysql/mysql.class.php @@ -160,6 +160,24 @@ class XMLDBmysql extends XMLDBGenerator { return $dbtype; } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its enum + * (usually invoked from getModifyEnumSQL() + */ + function getCreateEnumSQL($xmldb_table, $xmldb_field) { + /// For MySQL, just alter the field + return $this->getAlterFieldSQL($xmldb_table, $xmldb_field); + } + + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its enum + * (usually invoked from getModifyEnumSQL() + */ + function getDropEnumSQL($xmldb_table, $xmldb_field) { + /// For MySQL, just alter the field + return $this->getAlterFieldSQL($xmldb_table, $xmldb_field); + } + /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default * (usually invoked from getModifyDefaultSQL() diff --git a/lib/xmldb/classes/generators/oci8po/oci8po.class.php b/lib/xmldb/classes/generators/oci8po/oci8po.class.php index 0e74f031b4..0a2ba5e96a 100644 --- a/lib/xmldb/classes/generators/oci8po/oci8po.class.php +++ b/lib/xmldb/classes/generators/oci8po/oci8po.class.php @@ -347,6 +347,26 @@ class XMLDBoci8po extends XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its enum + * (usually invoked from getModifyEnumSQL() + */ + 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 XMLDBTable and one XMLDBField, return the SQL statements needded to drop its enum + * (usually invoked from getModifyEnumSQL() + */ + function getDropEnumSQL($xmldb_table, $xmldb_field) { + /// All we have to do is to drop the check constraint + return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . + ' DROP CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck')); + } + /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default * (usually invoked from getModifyDefaultSQL() diff --git a/lib/xmldb/classes/generators/postgres7/postgres7.class.php b/lib/xmldb/classes/generators/postgres7/postgres7.class.php index 0e747b5240..7cd11c3a3d 100644 --- a/lib/xmldb/classes/generators/postgres7/postgres7.class.php +++ b/lib/xmldb/classes/generators/postgres7/postgres7.class.php @@ -259,6 +259,26 @@ class XMLDBpostgres7 extends XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its enum + * (usually invoked from getModifyEnumSQL() + */ + 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 XMLDBTable and one XMLDBField, return the SQL statements needded to drop its enum + * (usually invoked from getModifyEnumSQL() + */ + function getDropEnumSQL($xmldb_table, $xmldb_field) { + /// All we have to do is to drop the check constraint + return array('ALTER TABLE ' . $this->getTableName($xmldb_table) . + ' DROP CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck')); + } + /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to create its default * (usually invoked from getModifyDefaultSQL() -- 2.39.5