]> git.mjollnir.org Git - moodle.git/commitdiff
Now all the XMLDBxxx->geSQL functions return an array of statements to be
authorstronk7 <stronk7>
Thu, 17 Aug 2006 19:20:45 +0000 (19:20 +0000)
committerstronk7 <stronk7>
Thu, 17 Aug 2006 19:20:45 +0000 (19:20 +0000)
executed (easily to handle them in the installation/upgrade process
individually. Also, it's possible to specify one statement end.

lib/xmldb/classes/XMLDBStructure.class.php
lib/xmldb/classes/XMLDBTable.class.php
lib/xmldb/classes/generators/XMLDBGenerator.class.php
lib/xmldb/classes/generators/mssql/mssql.class.php
lib/xmldb/classes/generators/mysql/mysql.class.php
lib/xmldb/classes/generators/oci8po/oci8po.class.php
lib/xmldb/classes/generators/postgres7/postgres7.class.php

index 985c3cbc83769b2116cc8ea9946a1d07294fa5da..f9086e782ecdb70a2b5c1fab811411f189724d0f 100644 (file)
@@ -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;
     }
 }
 
index fd917bab210f9b884630f498d8d62105cc87b98c..3cd412668cba43a10cfc54268850b3087526db93 100644 (file)
@@ -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;
     }
 }
 
index 73cdbcae59cebd162729d02c7906c03350ea808f..e360afce0c37c57411fa9aa0c4e790772ffb84aa 100644 (file)
@@ -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;
     }
index 57e00b0b92ac8c5c998e3844c3c73c8c766b5e00..7d99cf130f0bd91cf595f4f526b3d02559baee15 100644 (file)
@@ -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
index af2da9a2c615b997130e97c6e123933bbcc7a29b..31bd4c65c69e8e9477fdca3c0bd12308ceed08a0 100644 (file)
@@ -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);
     }
 
     /**
index 769add5c1ab93410e27eb32fea78697a457d386a..106b55db360a019bf275965909d3594b3a415dbc 100644 (file)
@@ -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);
      }
 
     /**
index 36c3a546e10f1a236586a698359e4dc93c0e6dea..61f22b8a8039a28928d9744588f7d1703fc35805 100644 (file)
@@ -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);
      }
 
     /**