]> git.mjollnir.org Git - moodle.git/commitdiff
Make generators reentrant. MDL-14897
authorstronk7 <stronk7>
Mon, 19 May 2008 03:04:41 +0000 (03:04 +0000)
committerstronk7 <stronk7>
Mon, 19 May 2008 03:04:41 +0000 (03:04 +0000)
lib/ddl/mssql_sql_generator.php
lib/ddl/mysql_sql_generator.php
lib/ddl/oracle_sql_generator.php
lib/ddl/postgres_sql_generator.php
lib/ddl/sql_generator.php

index 9d889693914a9257dc2d12fd6c5b517336a2eea2..402157f2e6efd208390b1b7070d05ab772339d0d 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
+// Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
 //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
@@ -361,8 +361,7 @@ class mssql_sql_generator extends sql_generator {
         }
 
     /// Just prevent default clauses in this type of sentences for mssql and launch the parent one
-        $this->alter_column_skip_default = true;
-        $results = array_merge($results, parent::getAlterFieldSQL($xmldb_table, $xmldb_field)); // Call parent
+        $results = array_merge($results, parent::getAlterFieldSQL($xmldb_table, $xmldb_field, NULL, true, NULL)); // Call parent
 
     /// Finally, process the default clause to add it back if necessary
         if ($typechanged || $lengthchanged) {
@@ -478,7 +477,7 @@ class mssql_sql_generator extends sql_generator {
 
     /// Get the quoted name of the table and field
         $tablename = $this->getTableName($xmldb_table);
-        $fieldname = $this->getEncQuoted($xmldb_field->getName());
+        $fieldname = $xmldb_field->getName();
 
     /// Look for any default constraint in this field and drop it
         if ($default = get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint
index d9de79fad1caf327e52d96d94ccaa95f7b3863a6..a12721c7d251dec2c8e235d22a0a03eb4df47d27 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
+// Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
 //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
index 387b4f61dca6852be96cf198eda91fc08df98c44..27a253652887dbc2cbb6a7fdb4e6266c924a5b8f 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
+// Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
 //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
@@ -333,7 +333,7 @@ class oracle_sql_generator extends sql_generator {
 
     /// Get the quoted name of the table and field
         $tablename = $this->getTableName($xmldb_table);
-        $fieldname = $this->getEncQuoted($xmldb_field->getName());
+        $fieldname = $xmldb_field->getName();
 
     /// Take a look to field metadata
         $meta = $this->mdb->get_columns($tablename);
index f231be4c4b0b66ab2861fe5e63e71e61ebf880f8..1b75a8bbf9b67a0da3972a0890521a19675fa59b 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
+// Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
 //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
@@ -183,7 +183,11 @@ class postgres_sql_generator extends sql_generator {
      *
      * This function can be safely removed once min req. for PG will be 8.0
      */
-    public function getAddFieldSQL($xmldb_table, $xmldb_field) {
+    public function getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
+
+        $skip_type_clause = is_null($skip_type_clause) ? $this->alter_column_skip_type : $skip_type_clause;
+        $skip_default_clause = is_null($skip_default_clause) ? $this->alter_column_skip_default : $skip_default_clause;
+        $skip_notnull_clause = is_null($skip_notnull_clause) ? $this->alter_column_skip_notnull : $skip_notnull_clause;
 
         $results = array();
 
@@ -192,21 +196,10 @@ class postgres_sql_generator extends sql_generator {
 
         $defaultvalue = $xmldb_field->getDefault();
 
-    /// Save old flags
-        $old_skip_default = $this->alter_column_skip_default;
-        $old_skip_notnull = $this->alter_column_skip_notnull;
-
-    /// Prevent default clause and launch parent getAddField()
-        $this->alter_column_skip_default = true;
-        $this->alter_column_skip_notnull = true;
-        $results = parent::getAddFieldSQL($xmldb_table, $xmldb_field);
-
-    /// Re-set old flags
-        $this->alter_column_skip_default = $old_skip_default;
-        $this->alter_column_skip_notnull = $old_skip_notnull;
+        $results = parent::getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause, $skip_default_clause, $skip_notnull_clause);
 
     /// Add default (only if not skip_default)
-        if (!$this->alter_column_skip_default) {
+        if (!$skip_default_clause) {
             $default_clause = $this->getDefaultClause($xmldb_field);
             if ($default_clause) {
                 $sql = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $fieldname . ' SET' . $default_clause; /// Add default clause
@@ -224,7 +217,7 @@ class postgres_sql_generator extends sql_generator {
         }
 
     /// Add not null (only if no skip_notnull)
-        if (!$this->alter_column_skip_notnull) {
+        if (!$skip_notnull_clause) {
             if ($xmldb_field->getNotnull()) {
                 $results[] = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $fieldname . ' SET NOT NULL'; /// Add not null
             }
@@ -325,12 +318,10 @@ class postgres_sql_generator extends sql_generator {
     ///     - rename the temp column to the original name
         if ($typechanged || $precisionchanged || $decimalchanged) {
             $tempcolname = $xmldb_field->getName() . '_alter_column_tmp';
-        /// Prevent temp field to have both NULL/NOT NULL and DEFAULT constraints
-            $this->alter_column_skip_notnull = true;
-            $this->alter_column_skip_default = true;
             $xmldb_field->setName($tempcolname);
         /// Create the temporal column
-            $results = array_merge($results, $this->getAddFieldSQL($xmldb_table, $xmldb_field));
+        /// Prevent temp field to have both NULL/NOT NULL and DEFAULT constraints
+            $results = array_merge($results, $this->getAddFieldSQL($xmldb_table, $xmldb_field, NULL, true, true));
         /// Detect some basic casting options
             if ((substr($oldmetatype, 0, 1) == 'C' && $xmldb_field->getType() == XMLDB_TYPE_NUMBER) ||
                 (substr($oldmetatype, 0, 1) == 'C' && $xmldb_field->getType() == XMLDB_TYPE_FLOAT)) {
index 4242c657222711446b5e90e7adc5fba1ec9d0136..669f5f2215804596f3295840860848c9df53f8e9 100644 (file)
@@ -361,7 +361,12 @@ abstract class sql_generator {
     /**
      * Given one correct XMLDBField, returns the complete SQL line to create it
      */
-    public function getFieldSQL($xmldb_field, $skip_type_clause = false, $skip_default_clause = false, $skip_notnull_clause = false)  {
+    public function getFieldSQL($xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL, $specify_nulls_clause = NULL)  {
+
+        $skip_type_clause = is_null($skip_type_clause) ? $this->alter_column_skip_type : $skip_type_clause;
+        $skip_default_clause = is_null($skip_default_clause) ? $this->alter_column_skip_default : $skip_default_clause;
+        $skip_notnull_clause = is_null($skip_notnull_clause) ? $this->alter_column_skip_notnull : $skip_notnull_clause;
+        $specify_nulls_clause = is_null($specify_nulls_clause) ? $this->specify_nulls : $specify_nulls_clause;
 
     /// First of all, convert integers to numbers if defined
         if ($this->integer_to_number) {
@@ -403,7 +408,7 @@ abstract class sql_generator {
             if ($xmldb_field->getNotNull()) {
                 $notnull = ' NOT NULL';
             } else {
-                if ($this->specify_nulls) {
+                if ($specify_nulls_clause) {
                     $notnull = ' NULL';
                 }
             }
@@ -567,7 +572,11 @@ abstract class sql_generator {
     /**
      * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add the field to the table
      */
-    public function getAddFieldSQL($xmldb_table, $xmldb_field) {
+    public function getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
+
+        $skip_type_clause = is_null($skip_type_clause) ? $this->alter_column_skip_type : $skip_type_clause;
+        $skip_default_clause = is_null($skip_default_clause) ? $this->alter_column_skip_default : $skip_default_clause;
+        $skip_notnull_clause = is_null($skip_notnull_clause) ? $this->alter_column_skip_notnull : $skip_notnull_clause;
 
         $results = array();
 
@@ -575,9 +584,9 @@ abstract class sql_generator {
         $tablename = $this->getTableName($xmldb_table);
 
     /// Build the standard alter table add
-        $sql = $this->getFieldSQL($xmldb_field, $this->alter_column_skip_type,
-                                  $this->alter_column_skip_default,
-                                  $this->alter_column_skip_notnull);
+        $sql = $this->getFieldSQL($xmldb_field, $skip_type_clause,
+                                  $skip_default_clause,
+                                  $skip_notnull_clause);
         $altertable = 'ALTER TABLE ' . $tablename . ' ADD ' . $sql;
     /// Add the after clause if necesary
         if ($this->add_after_clause && $xmldb_field->getPrevious()) {
@@ -616,12 +625,13 @@ abstract class sql_generator {
     /**
      * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table
      */
-    public function getAlterFieldSQL($xmldb_table, $xmldb_field) {
+    public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
 
-        $results = array();
+        $skip_type_clause = is_null($skip_type_clause) ? $this->alter_column_skip_type : $skip_type_clause;
+        $skip_default_clause = is_null($skip_default_clause) ? $this->alter_column_skip_default : $skip_default_clause;
+        $skip_notnull_clause = is_null($skip_notnull_clause) ? $this->alter_column_skip_notnull : $skip_notnull_clause;
 
-    /// Always specify NULLs in alter fields because we can change not nulls to nulls
-        $this->specify_nulls = true;
+        $results = array();
 
     /// Get the quoted name of the table and field
         $tablename = $this->getTableName($xmldb_table);
@@ -629,9 +639,10 @@ abstract class sql_generator {
 
     /// Build de alter sentence using the alter_column_sql template
         $alter = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->alter_column_sql);
-        $colspec = $this->getFieldSQL($xmldb_field, $this->alter_column_skip_type,
-                                      $this->alter_column_skip_default,
-                                      $this->alter_column_skip_notnull);
+        $colspec = $this->getFieldSQL($xmldb_field, $skip_type_clause,
+                                      $skip_default_clause,
+                                      $skip_notnull_clause,
+                                      true);
         $alter = str_replace('COLUMNSPECS', $colspec, $alter);
 
     /// Add the after clause if necesary