From: stronk7 Date: Thu, 17 Aug 2006 19:20:45 +0000 (+0000) Subject: Now all the XMLDBxxx->geSQL functions return an array of statements to be X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9dcc6300a7b13b363f3625e6703b3aad81f63e2f;p=moodle.git Now all the XMLDBxxx->geSQL functions return an array of statements to be executed (easily to handle them in the installation/upgrade process individually. Also, it's possible to specify one statement end. --- diff --git a/lib/xmldb/classes/XMLDBStructure.class.php b/lib/xmldb/classes/XMLDBStructure.class.php index 985c3cbc83..f9086e782e 100644 --- a/lib/xmldb/classes/XMLDBStructure.class.php +++ b/lib/xmldb/classes/XMLDBStructure.class.php @@ -697,19 +697,16 @@ class XMLDBStructure extends XMLDBObject { * This function will return the SQL code needed to create the table for the specified DB and * prefix. Just one simple wrapper over generators. */ - function getCreateStructureSQL ($dbtype, $prefix) { + function getCreateStructureSQL ($dbtype, $prefix, $statement_end=true) { - $sqltext = ''; + $results = array(); - $classname = 'XMLDB' . $dbtype; - $generator = new $classname(); - $generator->setPrefix($prefix); if ($tables = $this->getTables()) { foreach ($tables as $table) { - $sqltext .= $generator->getCreateTableSQL($table) . "\n\n"; + $results = array_merge($results, $table->getCreateTableSQL($dbtype, $prefix, $statement_end)); } } - return $sqltext; + return $results; } } diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index fd917bab21..3cd412668c 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -718,11 +718,18 @@ class XMLDBTable extends XMLDBObject { * This function will return the SQL code needed to create the table for the specified DB and * prefix. Just one simple wrapper over generators. */ - function getCreateTableSQL ($dbtype, $prefix) { + function getCreateTableSQL ($dbtype, $prefix, $statement_end=true) { + + $results = array(); + $classname = 'XMLDB' . $dbtype; $generator = new $classname(); $generator->setPrefix($prefix); - return $generator->getCreateTableSQL($this); + $results = $generator->getCreateTableSQL($this); + 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 73cdbcae59..e360afce0c 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -27,8 +27,7 @@ /// This class represent the base generator class where all the /// needed functions to generate proper SQL are defined. -/// If fact, this class generate SQL code to be used against MySQL -/// so the rest of classes will inherit, by default, the same logic. +/// The rest of classes will inherit, by default, the same logic. /// Functions will be overriden as needed to generate correct SQL. class XMLDBgenerator { @@ -41,6 +40,8 @@ class XMLDBgenerator { var $quote_string = '"'; // String used to quote names var $quote_all = false; // To decide if we want to quote all the names or only the reserved ones + var $statement_end = ';'; // String to be automatically added at the end of each statement + var $integer_to_number = false; // To create all the integers as NUMBER(x) (also called DECIMAL, NUMERIC...) var $float_to_number = false; // To create all the floats as NUMBER(x) (also called DECIMAL, NUMERIC...) @@ -98,10 +99,13 @@ class XMLDBgenerator { } /** - * Given one correct XMLDBTable, returns the complete SQL lines to create it + * Given one correct XMLDBTable, returns the SQL statements + * to create it (inside one array) */ function getCreateTableSQL($xmldb_table) { + $results = array(); //Array where all the sentences will be stored + /// Table header $table = 'CREATE TABLE ' . $this->getEncQuoted($this->prefix . $xmldb_table->getName()) . ' ('; @@ -141,12 +145,16 @@ class XMLDBgenerator { $table = trim($table,','); $table .= "\n)"; + /// Add the CREATE TABLE to results + $results[] = $table; + /// Add comments if specified if ($this->add_table_comments) { - $table .= $this->getCommentSQL ($xmldb_table) . ";\n"; - } else { - $table .= ";\n"; + $comment = $this->getCommentSQL ($xmldb_table); + /// Add the COMMENT to results + $results = array_merge($results, $comment); } + /// Add the indexes (each one, one statement) $indexcombs = array(); //To store all the key combinations used if ($xmldb_indexes = $xmldb_table->getIndexes()) { @@ -157,13 +165,15 @@ class XMLDBgenerator { if ($indextext = $this->getCreateIndexSQL($xmldb_table, $xmldb_index)) { /// Only create the index if the combination hasn't been used before if (!in_array($currentcomb, $indexcombs)) { - $table .= "\n" . $indextext; + /// Add the INDEX to the array + $results = array_merge($results, $indextext); } } /// Add the index to the array of used combinations $indexcombs[] = $currentcomb; } } + /// Also, add the indexes needed from keys, based on configuration (each one, one statement) if ($xmldb_keys = $xmldb_table->getKeys()) { foreach ($xmldb_keys as $xmldb_key) { @@ -199,7 +209,8 @@ class XMLDBgenerator { $currentcomb = strtolower(implode('-', $fieldsarr)); /// Only create the index if the combination hasn't been used before if (!in_array($currentcomb, $indexcombs)) { - $table .= "\n" . $indextext; + /// Add the INDEX to the array + $results = array_merge($results, $indextext); } } /// Add the index to the array of used combinations @@ -213,16 +224,20 @@ class XMLDBgenerator { /// Iterate over fields looking for sequences foreach ($xmldb_fields as $xmldb_field) { if ($xmldb_field->getSequence()) { - $table .= "\n" . $this->getCreateSequenceSQL($xmldb_table, $xmldb_field); + /// returns an array of statements needed to create one sequence + $sequence_sentences = $this->getCreateSequenceSQL($xmldb_table, $xmldb_field); + /// Add the SEQUENCE to the array + $results = array_merge($results, $sequence_sentences); } } } - return $table; + return $results; } /** - * Given one correct XMLDBIndex, returns the complete SQL line to create it + * Given one correct XMLDBIndex, returns the SQL statements + * needed to create it (in array) */ function getCreateIndexSQL ($xmldb_table, $xmldb_index) { @@ -236,9 +251,9 @@ class XMLDBgenerator { $index = 'CREATE' . $unique . ' INDEX '; $index .= $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_index->getFields()), $suffix); $index .= ' ON ' . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); - $index .= ' (' . implode(', ', $this->getEncQuoted($xmldb_index->getFields())) . ');'; + $index .= ' (' . implode(', ', $this->getEncQuoted($xmldb_index->getFields())) . ')'; - return $index; + return array($index); } /** @@ -444,6 +459,22 @@ class XMLDBgenerator { } } + /** + * Given one string (or one array), ends it with statement_end + */ + function getEndedStatements ($input) { + + if (is_array($input)) { + foreach ($input as $key=>$content) { + $input[$key] = $this->getEndedStatements($content); + } + return $input; + } else { + $input = trim($input) . $this->statement_end; + return $input; + } + } + /// ALL THESE FUNCTION MUST BE CUSTOMISED BY ALL THE XMLDGenerator classes /** @@ -468,14 +499,15 @@ class XMLDBgenerator { } /** - * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes + * Returns the code (array of statements) needed + * to create one sequence for the xmldb_table and xmldb_field passes */ function getCreateSequenceSQL ($xmldb_table, $xmldb_field) { return 'Code for extra sequence SQL goes to getCreateSequenceSQL(). Can be disabled with sequence_extra_code=false'; } /** - * Returns the code needed to add one comment to the table + * Returns the code (array of statements) needed to add one comment to the table */ function getCommentSQL ($xmldb_table) { return 'Code for table comment goes to getCommentSQL(). Can be disabled with add_table_comments=false;'; @@ -488,7 +520,7 @@ class XMLDBgenerator { function getReservedWords() { /// Some wel-know reserved words $reserved_words = array ( - 'user', 'scale', 'type', 'comment' + 'user', 'scale', 'type', 'comment', 'view', 'value', 'table', 'index', 'key', 'sequence', 'trigger' ); return $reserved_words; } diff --git a/lib/xmldb/classes/generators/mssql/mssql.class.php b/lib/xmldb/classes/generators/mssql/mssql.class.php index 57e00b0b92..7d99cf130f 100644 --- a/lib/xmldb/classes/generators/mssql/mssql.class.php +++ b/lib/xmldb/classes/generators/mssql/mssql.class.php @@ -32,6 +32,8 @@ class XMLDBmssql extends XMLDBgenerator { /// Only set values that are different from the defaults present in XMLDBgenerator + var $statement_end = "\ngo"; // String to be automatically added at the end of each statement + var $number_type = 'DECIMAL'; // Proper type for NUMBER(x) in this DB var $unsigned_allowed = false; // To define in the generator must handle unsigned information diff --git a/lib/xmldb/classes/generators/mysql/mysql.class.php b/lib/xmldb/classes/generators/mysql/mysql.class.php index af2da9a2c6..31bd4c65c6 100644 --- a/lib/xmldb/classes/generators/mysql/mysql.class.php +++ b/lib/xmldb/classes/generators/mysql/mysql.class.php @@ -146,16 +146,17 @@ class XMLDBmysql extends XMLDBGenerator { } /** - * Returns the code needed to add one comment to the table + * Returns the code (in array) needed to add one comment to the table */ function getCommentSQL ($xmldb_table) { $comment = ''; if ($xmldb_table->getComment()) { + $comment .= 'ALTER TABLE ' . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); $comment .= " COMMENT='" . substr($xmldb_table->getComment(), 0, 250) . "'"; } - return $comment; + return array($comment); } /** diff --git a/lib/xmldb/classes/generators/oci8po/oci8po.class.php b/lib/xmldb/classes/generators/oci8po/oci8po.class.php index 769add5c1a..106b55db36 100644 --- a/lib/xmldb/classes/generators/oci8po/oci8po.class.php +++ b/lib/xmldb/classes/generators/oci8po/oci8po.class.php @@ -120,33 +120,33 @@ class XMLDBoci8po extends XMLDBgenerator { */ function getCreateSequenceSQL ($xmldb_table, $xmldb_field) { - $sequence = "\nCREATE SEQUENCE "; + $sequence = "CREATE SEQUENCE "; $sequence.= $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq'); $sequence.= "\n START WITH 1"; $sequence.= "\n INCREMENT BY 1"; - $sequence.= "\n NOMAXVALUE;"; + $sequence.= "\n NOMAXVALUE"; $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg'); - $trigger = "\nCREATE OR REPLACE TRIGGER " . $trigger_name; + $trigger = "CREATE OR REPLACE TRIGGER " . $trigger_name; $trigger.= "\n BEFORE INSERT"; $trigger.= "\nON " . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); $trigger.= "\n FOR EACH ROW"; $trigger.= "\nBEGIN"; $trigger.= "\n SELECT " . $trigger_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;"; - $trigger.= "\nEND;"; - return $sequence . "\n" . $trigger; + $trigger.= "\nEND"; + return array($sequence, $trigger); } /** - * Returns the code needed to add one comment to the table + * Returns the code (in array) needed to add one comment to the table */ function getCommentSQL ($xmldb_table) { - $comment = ";\n\nCOMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); + $comment = "COMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'"; - return $comment; + return array($comment); } /** diff --git a/lib/xmldb/classes/generators/postgres7/postgres7.class.php b/lib/xmldb/classes/generators/postgres7/postgres7.class.php index 36c3a546e1..61f22b8a80 100644 --- a/lib/xmldb/classes/generators/postgres7/postgres7.class.php +++ b/lib/xmldb/classes/generators/postgres7/postgres7.class.php @@ -128,14 +128,14 @@ class XMLDBpostgres7 extends XMLDBgenerator { } /** - * Returns the code needed to add one comment to the table + * Returns the code (in array) needed to add one comment to the table */ function getCommentSQL ($xmldb_table) { - $comment = ";\n\nCOMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); + $comment = "COMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName()); $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'"; - return $comment; + return array($comment); } /**