From: skodak Date: Mon, 25 Aug 2008 12:52:49 +0000 (+0000) Subject: MDL-15671 support for table sequence resetting - code by Andrei Bautu (with minor... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e16e38818ec846715ba4020125ece90a367a52dd;p=moodle.git MDL-15671 support for table sequence resetting - code by Andrei Bautu (with minor modifications) --- diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 5367966375..605b939796 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -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. diff --git a/lib/dml/mssql_adodb_moodle_database.php b/lib/dml/mssql_adodb_moodle_database.php index a617edc55f..f350044642 100644 --- a/lib/dml/mssql_adodb_moodle_database.php +++ b/lib/dml/mssql_adodb_moodle_database.php @@ -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)"); + } } diff --git a/lib/dml/mysqli_adodb_moodle_database.php b/lib/dml/mysqli_adodb_moodle_database.php index 360a92397d..a3f2cb2ca9 100644 --- a/lib/dml/mysqli_adodb_moodle_database.php +++ b/lib/dml/mysqli_adodb_moodle_database.php @@ -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 diff --git a/lib/dml/oci8po_adodb_moodle_database.php b/lib/dml/oci8po_adodb_moodle_database.php index eff21664df..1b754713be 100644 --- a/lib/dml/oci8po_adodb_moodle_database.php +++ b/lib/dml/oci8po_adodb_moodle_database.php @@ -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"); + } } diff --git a/lib/dml/postgres7_adodb_moodle_database.php b/lib/dml/postgres7_adodb_moodle_database.php index 0ab6728a5c..9170b5c179 100644 --- a/lib/dml/postgres7_adodb_moodle_database.php +++ b/lib/dml/postgres7_adodb_moodle_database.php @@ -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 diff --git a/lib/dml/sqlite3_pdo_moodle_database.php b/lib/dml/sqlite3_pdo_moodle_database.php index b461a1d4c4..b1d212ad81 100644 --- a/lib/dml/sqlite3_pdo_moodle_database.php +++ b/lib/dml/sqlite3_pdo_moodle_database.php @@ -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