mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-15671 support for table sequence resetting - code by Andrei Bautu (with minor modifications)
This commit is contained in:
parent
a3668b25c0
commit
e16e38818e
6 changed files with 98 additions and 3 deletions
|
@ -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.
|
||||
|
|
|
@ -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)");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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'");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue