]> git.mjollnir.org Git - moodle.git/commitdiff
Adding getSequenceFromDB() for Oracle in order to detect the correct
authorstronk7 <stronk7>
Sun, 8 Oct 2006 09:36:33 +0000 (09:36 +0000)
committerstronk7 <stronk7>
Sun, 8 Oct 2006 09:36:33 +0000 (09:36 +0000)
sequence name in the insert_record() function.

lib/xmldb/classes/XMLDBTable.class.php
lib/xmldb/classes/generators/XMLDBGenerator.class.php
lib/xmldb/classes/generators/oci8po/oci8po.class.php

index a19197e446a3c6408b795b5cde2976d8ec34bec5..943564f46862024b60f680d69ab7928c5bce1aa3 100644 (file)
@@ -1035,6 +1035,19 @@ class XMLDBTable extends XMLDBObject {
         }
         return $results;
     }
+
+    /**
+     * This function will return the name of the sequence created for the pk of the table specified
+     * Just one simple wrapper over generators. Returns false if not found
+     * Note that not all DB use sequences (only Oracle and PostgreSQL)
+     */
+    function getSequenceFromDB($dbtype, $prefix) {
+
+        $classname = 'XMLDB' . $dbtype;
+        $generator = new $classname();
+        $generator->setPrefix($prefix);
+        return $generator->getSequenceFromDB($this);
+    }
 }
 
 ?>
index 839af46e8d6b74acfa3583b99c8daff504e95915..26d3fe24318b1b8cd88964ce53f04d17f6da2364 100644 (file)
@@ -1066,6 +1066,15 @@ class XMLDBgenerator {
         }
     }
 
+    /**
+     * Returns the name (string) of the sequence used in the table for the autonumeric pk
+     * Only some DB have this implemented
+     */
+    function getSequenceFromDB($xmldb_table) {
+        return false;
+    }
+
+
 /// ALL THESE FUNCTION MUST BE CUSTOMISED BY ALL THE XMLDGenerator classes
 
     /**
index 3fe24ebc452df13092046393b2ae5dc26304c676..97d462e49e3ca0425149b2767ac923cc75aa2944 100644 (file)
@@ -500,6 +500,32 @@ class XMLDBoci8po extends XMLDBgenerator {
         return $results;
     }
 
+    /**
+     * Given one XMLDBTable returns one string with the sequence of the table
+     * in the table (fetched from DB)
+     * The sequence name for oracle is calculated by looking the corresponding
+     * trigger and retrieving the sequence name from it (because sequences are
+     * independent elements)
+     * If no sequence is found, returns false
+     */
+    function getSequenceFromDB($xmldb_table) {
+
+         $tablename = strtoupper($this->getTableName($xmldb_table));
+         $sequencename = false;
+
+        if ($trigger = get_record_sql("SELECT trigger_name, trigger_body
+                                         FROM user_triggers
+                                        WHERE table_name = '{$tablename}'")) {
+        /// If trigger found, regexp it looking for the sequence name
+            preg_match('/.*SELECT (.*)\.nextval/i', $trigger->trigger_body, $matches);
+            if (isset($matches[1])) {
+                $sequencename = $matches[1];
+            }
+        }
+
+        return $sequencename;
+    }
+
     /**
      * Returns an array of reserved words (lowercase) for this DB
      */