]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17491 oci native driver: alter column from TEXT to integer/number now working
authorstronk7 <stronk7>
Mon, 28 Sep 2009 23:16:55 +0000 (23:16 +0000)
committerstronk7 <stronk7>
Mon, 28 Sep 2009 23:16:55 +0000 (23:16 +0000)
lib/ddl/oracle_sql_generator.php

index 13c6e0650667121281e46e98023850c50a536a79..87f908800fa2e85b239e90d6b10521a8df223cfb 100644 (file)
@@ -332,7 +332,7 @@ class oracle_sql_generator extends sql_generator {
         $fieldname = $xmldb_field->getName();
 
     /// Take a look to field metadata
-        $meta = $this->mdb->get_columns($xmldb_table->getName(), false);
+        $meta = $this->mdb->get_columns($xmldb_table->getName());
         $metac = $meta[$fieldname];
         $oldmetatype = $metac->meta_type;
 
@@ -410,10 +410,25 @@ class oracle_sql_generator extends sql_generator {
             $skip_notnull_clause = true;
             $skip_default_clause = true;
             $xmldb_field->setName($tempcolname);
+            // Drop the temp column, in case it exists (due to one previous failure in conversion)
+            // really ugly but we cannot enclose DDL into transaction :-(
+            if (isset($meta[$tempcolname])) {
+                $results = array_merge($results, $this->getDropFieldSQL($xmldb_table, $xmldb_field));
+            }
         /// Create the temporal column
             $results = array_merge($results, $this->getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause, $skip_type_clause, $skip_notnull_clause));
         /// Copy contents from original col to the temporal one
-            $results[] = 'UPDATE ' . $tablename . ' SET ' . $tempcolname . ' = ' . $fieldname;
+
+            // From TEXT to integer/number we need explicit conversion
+            if ($oldmetatype == 'X' && $xmldb_field->GetType() == XMLDB_TYPE_INTEGER) {
+                $results[] = 'UPDATE ' . $tablename . ' SET ' . $tempcolname . ' = CAST(' . $this->mdb->sql_compare_text($fieldname) . ' AS INT)';
+            } else if ($oldmetatype == 'X' && $xmldb_field->GetType() == XMLDB_TYPE_NUMBER) {
+                $results[] = 'UPDATE ' . $tablename . ' SET ' . $tempcolname . ' = CAST(' . $this->mdb->sql_compare_text($fieldname) . ' AS NUMBER)';
+
+            // Normal cases, implicit conversion
+            } else {
+                $results[] = 'UPDATE ' . $tablename . ' SET ' . $tempcolname . ' = ' . $fieldname;
+            }
         /// Drop the old column
             $xmldb_field->setName($fieldname); //Set back the original field name
             $results = array_merge($results, $this->getDropFieldSQL($xmldb_table, $xmldb_field));