]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15671 support for table sequence resetting - code by Andrei Bautu (with minor...
authorskodak <skodak>
Mon, 25 Aug 2008 12:52:49 +0000 (12:52 +0000)
committerskodak <skodak>
Mon, 25 Aug 2008 12:52:49 +0000 (12:52 +0000)
lib/dml/moodle_database.php
lib/dml/mssql_adodb_moodle_database.php
lib/dml/mysqli_adodb_moodle_database.php
lib/dml/oci8po_adodb_moodle_database.php
lib/dml/postgres7_adodb_moodle_database.php
lib/dml/sqlite3_pdo_moodle_database.php

index 5367966375c0b5eda2341d95ce75ac0a362036c2..605b939796a9864203ecebe39aa0af3e09da557f 100644 (file)
@@ -455,6 +455,13 @@ abstract class moodle_database {
         $this->columns = array();
     }
 
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return success
+     */
+    public abstract function reset_sequence($table);
+
     /**
      * Returns sql generator used for db manipulation.
      * Used mostly in upgrade.php scripts.
@@ -1346,7 +1353,7 @@ abstract class moodle_database {
 
     /**
      * Returns the SQL for returning searching one string for the location of another.
-     * Note, there is no guarantee which order $needle, $haystack will be in 
+     * Note, there is no guarantee which order $needle, $haystack will be in
      * the resulting SQL, so when using this method, and both arguments contain
      * placeholders, you should use named placeholders.
      * @param string $needle the SQL expression that will be searched for.
index a617edc55fe7743eab0adcd080f38f194461a647..f3500446420026936467dfb9f5e9af64b8f6a273 100644 (file)
@@ -348,4 +348,21 @@ class mssql_adodb_moodle_database extends adodb_moodle_database {
 
         return ($returnid ? $id : true);
     }
+
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return bool success
+     */
+    public function reset_sequence($table) {
+        // From http://msdn.microsoft.com/en-us/library/ms176057.aspx
+        if (!$this->get_manager()->table_exists($table)) {
+            return false;
+        }
+        $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
+        if ($value == 0) {
+            $value = 1;
+        }
+        return $this->change_database_structure("DBCC CHECKIDENT ('$this->prefix$table', RESEED, $value)");
+    }
 }
index 360a92397da28d28be433750218c0229c38a7f92..a3f2cb2ca9d1922b76f76a9bdd0b163b1588e9fc 100644 (file)
@@ -274,4 +274,19 @@ class mysqli_adodb_moodle_database extends adodb_moodle_database {
     public function sql_regex($positivematch=true) {
         return $positivematch ? 'REGEXP' : 'NOT REGEXP';
     }
-}
+
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return bool success
+     */
+    public function reset_sequence($table) {
+        // From http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
+        if (!$this->get_manager()->table_exists($table)) {
+            return false;
+        }
+        $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
+        $value++;
+        return $this->change_database_structure("ALTER TABLE $this->prefix$table AUTO_INCREMENT = $value");
+    }
+}
\ No newline at end of file
index eff21664dfef98e3824d8cc1ebf3f35af75aa322..1b754713be15edd6cea57173242f212f39a239b6 100644 (file)
@@ -604,4 +604,31 @@ class oci8po_adodb_moodle_database extends adodb_moodle_database {
     /// Fail safe to original value
         return $value;
     }
+
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return bool success
+     */
+    public function reset_sequence($table) {
+        // From http://www.acs.ilstu.edu/docs/oracle/server.101/b10759/statements_2011.htm
+        $dbman = $this->get_manager();
+        if (!$dbman->table_exists($table)) {
+            return false;
+        }
+        $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
+        $value++;
+        $xmldb_table = new xmldb_table($table);
+        $this->reads++;
+        $seqname = $dbman->find_sequence_name($xmldb_table);
+        if (!$seqname) {
+        /// Fallback, seqname not found, something is wrong. Inform and use the alternative getNameForObject() method
+            $generator = $dbman->generator;
+            $generator->setPrefix($this->getPrefix());
+            $seqname = $generator->getNameForObject($table, 'id', 'seq');
+        }
+
+        $this->change_database_structure("DROP SEQUENCE $seqname");
+        return $this->change_database_structure("CREATE SEQUENCE $seqname START WITH $value INCREMENT BY 1 NOMAXVALUE");
+    }
 }
index 0ab6728a5c699a0f65d9d4e65f087fe77aef034e..9170b5c1797632a17a2902b4a840d7f45a8310e8 100644 (file)
@@ -461,4 +461,19 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
     public function sql_regex($positivematch=true) {
         return $positivematch ? '~*' : '!~*';
     }
-}
+
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return bool success
+     */
+    public function reset_sequence($table) {
+        // From http://www.postgresql.org/docs/7.4/static/sql-altersequence.html
+        if (!$this->get_manager()->table_exists($table)) {
+            return false;
+        }
+        $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
+        $value++;
+        return $this->change_database_structure("ALTER SEQUENCE $this->prefix{$table}_id_seq RESTART WITH $value");
+    }
+}
\ No newline at end of file
index b461a1d4c447916f280530640adc67b0a9ad78a7..b1d212ad81b0f7f6dde0eb2a38daa9a9f4ca607b 100644 (file)
@@ -323,4 +323,18 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
         }
         return implode('||', $elements);
     }
+
+    /**
+     * Reset a sequence to the id field of a table.
+     * @param string $table name of table
+     * @return bool success
+     */
+    public function reset_sequence($table) {
+        // From http://sqlite.org/autoinc.html
+        if (!$this->get_manager()->table_exists($table)) {
+            return false;
+        }
+        $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
+        return $this->change_database_structure("UPDATE sqlite_sequence SET seq=$value WHERE name='$this->prefix$table'");
+    }
 }
\ No newline at end of file