]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17020 dml: native pgsql driver - implementation and unittests of transactions
authorskodak <skodak>
Mon, 27 Oct 2008 20:21:04 +0000 (20:21 +0000)
committerskodak <skodak>
Mon, 27 Oct 2008 20:21:04 +0000 (20:21 +0000)
lib/dml/moodle_database.php
lib/dml/pgsql_native_moodle_database.php
lib/dml/simpletest/testdml.php

index 2ac780951053106f01ce9456fcddb6a33ea59bd3..cedb15eacefd0a2a78acad1fda434183dbae9db8 100644 (file)
@@ -1543,21 +1543,21 @@ abstract class moodle_database {
      * this is _very_ useful for massive updates
      */
     public function begin_sql() {
-        return true;
+        return false;
     }
 
     /**
      * on DBs that support it, commit the transaction
      */
     public function commit_sql() {
-        return true;
+        return false;
     }
 
     /**
      * on DBs that support it, rollback the transaction
      */
     public function rollback_sql() {
-        return true;
+        return false;
     }
 
 /// performance and logging
index c76e17ce4874df0b4929efbd489f3274c52fdd94..ca78d839cb5e391810ffb6eb9ccd57d133012f22 100644 (file)
@@ -1036,4 +1036,47 @@ class pgsql_native_moodle_database extends moodle_database {
         return $positivematch ? '~*' : '!~*';
     }
 
+/// transactions
+    /**
+     * on DBs that support it, switch to transaction mode and begin a transaction
+     * you'll need to ensure you call commit_sql() or your changes *will* be lost.
+     *
+     * this is _very_ useful for massive updates
+     */
+    public function begin_sql() {
+        $sql = "BEGIN ISOLATION LEVEL READ COMMITTED";
+        $result = pg_query($this->pgsql, $sql);
+        if ($result === false) {
+            return false;
+        }
+        pg_free_result($result);
+        return true;
+    }
+
+    /**
+     * on DBs that support it, commit the transaction
+     */
+    public function commit_sql() {
+        $sql = "COMMIT";
+        $result = pg_query($this->pgsql, $sql);
+        if ($result === false) {
+            return false;
+        }
+        pg_free_result($result);
+        return true;
+    }
+
+    /**
+     * on DBs that support it, rollback the transaction
+     */
+    public function rollback_sql() {
+        $sql = "ROLLBACK";
+        $result = pg_query($this->pgsql, $sql);
+        if ($result === false) {
+            return false;
+        }
+        pg_free_result($result);
+        return true;
+    }
+
 }
index 01a19420bdaed87ceaf061ca497b3015609846a0..3be5bad67c92864c3fbea30042ce59ad80335e4a 100755 (executable)
@@ -1394,6 +1394,69 @@ class dml_test extends UnitTestCase {
         $this->assertEqual($DB->get_field_sql(
                 "SELECT " . $DB->sql_position("'Oracle'", "'Moodle'") . $DB->sql_null_from_clause()), 0);
     }
+
+    function test_begin_sql() {
+        $DB = $this->tdb;
+        $dbman = $DB->get_manager();
+
+        $table = $this->get_test_table($dbman, "testtable");
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+        $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+        $dbman->create_table($table);
+        $this->tables[$table->getName()] = $table;
+
+        $active = $DB->begin_sql();
+        if ($active) {
+            // test only if driver supports transactions
+            $data = (object)array('course'=>3);
+            $DB->insert_record('testtable', $data);
+            $this->assertEqual(1, $DB->count_records('testtable'));
+            $DB->commit_sql();
+        }
+    }
+
+    function test_commit_sql() {
+        $DB = $this->tdb;
+        $dbman = $DB->get_manager();
+
+        $table = $this->get_test_table($dbman, "testtable");
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+        $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+        $dbman->create_table($table);
+        $this->tables[$table->getName()] = $table;
+
+        $active = $DB->begin_sql();
+        if ($active) {
+            // test only if driver supports transactions
+            $data = (object)array('course'=>3);
+            $DB->insert_record('testtable', $data);
+            $DB->commit_sql();
+            $this->assertEqual(1, $DB->count_records('testtable'));
+        }
+    }
+
+    function test_rollback_sql() {
+        $DB = $this->tdb;
+        $dbman = $DB->get_manager();
+
+        $table = $this->get_test_table($dbman, "testtable");
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+        $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+        $dbman->create_table($table);
+        $this->tables[$table->getName()] = $table;
+
+        $active = $DB->begin_sql();
+        if ($active) {
+            // test only if driver supports transactions
+            $data = (object)array('course'=>3);
+            $DB->insert_record('testtable', $data);
+            $DB->rollback_sql();
+            $this->assertEqual(0, $DB->count_records('testtable'));
+        }
+    }
 }
 
 /**