Index: moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/moodle_database.php,v retrieving revision 1.42 diff -u -r1.42 moodle_database.php --- moodle_database.php 25 Jul 2008 23:31:08 -0000 1.42 +++ moodle_database.php 31 Jul 2008 08:03:24 -0000 @@ -456,6 +456,12 @@ } /** + * Reset a sequence to the id field of a table. + * @return void + */ + public abstract function reset_sequence($table); + + /** * Returns sql generator used for db manipulation. * Used mostly in upgrade.php scripts. * @return object database_manager instance Index: mssql_adodb_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/mssql_adodb_moodle_database.php,v retrieving revision 1.15 diff -u -r1.15 mssql_adodb_moodle_database.php --- mssql_adodb_moodle_database.php 28 Jun 2008 21:09:10 -0000 1.15 +++ mssql_adodb_moodle_database.php 31 Jul 2008 08:04:01 -0000 @@ -341,4 +341,21 @@ return ($returnid ? $id : true); } -} + + /** + * Reset a sequence to the id field of a table. + * @return void + */ + public function reset_sequence($table) { + // From http://msdn.microsoft.com/en-us/library/ms176057.aspx + $columns = $this->get_columns($table, false); + if(empty($columns['id'])) { + return; + } + $value = $this->get_field_sql('SELECT MAX(id) FROM {' . $table . '}'); + if(!is_numeric($value)) { + $value = 1; + } + $this->change_database_structure("DBCC CHECKIDENT ('$this->prefix$table', RESEED, $value)"); + } +} \ No newline at end of file Index: mysqli_adodb_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/mysqli_adodb_moodle_database.php,v retrieving revision 1.9 diff -u -r1.9 mysqli_adodb_moodle_database.php --- mysqli_adodb_moodle_database.php 16 Jun 2008 21:01:54 -0000 1.9 +++ mysqli_adodb_moodle_database.php 31 Jul 2008 08:04:23 -0000 @@ -274,4 +274,23 @@ public function sql_regex($positivematch=true) { return $positivematch ? 'REGEXP' : 'NOT REGEXP'; } -} + + /** + * Reset a sequence to the id field of a table. + * @return void + */ + public function reset_sequence($table) { + // From http://dev.mysql.com/doc/refman/5.0/en/alter-table.html + $columns = $this->get_columns($table, false); + if(empty($columns['id'])) { + return; + } + $value = $this->get_field_sql('SELECT MAX(id) FROM {' . $table . '}'); + if(is_numeric($value)) { + ++$value; + } else { + $value = 1; + } + $this->change_database_structure("ALTER TABLE $this->prefix$table AUTO_INCREMENT = $value"); + } +} \ No newline at end of file Index: oci8po_adodb_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/oci8po_adodb_moodle_database.php,v retrieving revision 1.16 diff -u -r1.16 oci8po_adodb_moodle_database.php --- oci8po_adodb_moodle_database.php 26 Jun 2008 18:27:22 -0000 1.16 +++ oci8po_adodb_moodle_database.php 31 Jul 2008 08:04:56 -0000 @@ -597,4 +597,35 @@ /// Fail safe to original value return $value; } -} + + /** + * Reset a sequence to the id field of a table. + * @return void + */ + public function reset_sequence($table) { + // From http://www.acs.ilstu.edu/docs/oracle/server.101/b10759/statements_2011.htm + $columns = $this->get_columns($table, false); + if(empty($columns['id'])) { + return; + } + $value = $this->get_field_sql('SELECT MAX(id) FROM {'. $table . '}'); + if(is_numeric($value)) { + ++$value; + } else { + $value = 1; + } + $dbman = $this->get_manager(); + $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"); + $this->change_database_structure("CREATE SEQUENCE $seqname START WITH $value INCREMENT BY 1 NOMAXVALUE"); + } +} \ No newline at end of file Index: postgres7_adodb_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/postgres7_adodb_moodle_database.php,v retrieving revision 1.15 diff -u -r1.15 postgres7_adodb_moodle_database.php --- postgres7_adodb_moodle_database.php 22 Jun 2008 22:53:41 -0000 1.15 +++ postgres7_adodb_moodle_database.php 31 Jul 2008 08:06:10 -0000 @@ -461,4 +461,23 @@ public function sql_regex($positivematch=true) { return $positivematch ? '~*' : '!~*'; } -} + + /** + * Reset a sequence to the id field of a table. + * @return void + */ + public function reset_sequence($table) { + // From http://www.postgresql.org/docs/7.4/static/sql-altersequence.html + $columns = $this->get_columns($table, false); + if(empty($columns['id'])) { + return; + } + $value = $this->get_field_sql('SELECT MAX(id) FROM {' . $table . '}'); + if(is_numeric($value)) { + ++$value; + } else { + $value = 1; + } + $this->change_database_structure("ALTER SEQUENCE $this->prefix{$table}_id_seq RESTART WITH $value"); + } +} \ No newline at end of file Index: sqlite3_pdo_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/sqlite3_pdo_moodle_database.php,v retrieving revision 1.3 diff -u -r1.3 sqlite3_pdo_moodle_database.php --- sqlite3_pdo_moodle_database.php 13 Jul 2008 10:12:21 -0000 1.3 +++ sqlite3_pdo_moodle_database.php 31 Jul 2008 08:06:42 -0000 @@ -323,4 +323,21 @@ } return implode('||', $elements); } + + /** + * Reset a sequence to the id field of a table. + * @return void + */ + public function reset_sequence($table) { + // From http://sqlite.org/autoinc.html + $columns = $this->get_columns($table, false); + if(empty($columns['id'])) { + return; + } + $value = $this->get_field_sql('SELECT MAX(id) FROM {' . $table . '}'); + if(!is_numeric($value)) { + $value = 0; + } + $this->change_database_structure("UPDATE sqlite_sequence SET seq=$value WHERE name='$this->prefix$table'"); + } } \ No newline at end of file