MDL-15671 support for table sequence resetting - code by Andrei Bautu (with minor modifications)

This commit is contained in:
skodak 2008-08-25 12:52:49 +00:00
parent a3668b25c0
commit e16e38818e
6 changed files with 98 additions and 3 deletions

View file

@ -455,6 +455,13 @@ abstract class moodle_database {
$this->columns = array(); $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. * Returns sql generator used for db manipulation.
* Used mostly in upgrade.php scripts. * 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. * 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 * the resulting SQL, so when using this method, and both arguments contain
* placeholders, you should use named placeholders. * placeholders, you should use named placeholders.
* @param string $needle the SQL expression that will be searched for. * @param string $needle the SQL expression that will be searched for.

View file

@ -348,4 +348,21 @@ class mssql_adodb_moodle_database extends adodb_moodle_database {
return ($returnid ? $id : true); 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)");
}
} }

View file

@ -274,4 +274,19 @@ class mysqli_adodb_moodle_database extends adodb_moodle_database {
public function sql_regex($positivematch=true) { public function sql_regex($positivematch=true) {
return $positivematch ? 'REGEXP' : 'NOT REGEXP'; 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");
}
}

View file

@ -604,4 +604,31 @@ class oci8po_adodb_moodle_database extends adodb_moodle_database {
/// Fail safe to original value /// Fail safe to original value
return $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");
}
} }

View file

@ -461,4 +461,19 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
public function sql_regex($positivematch=true) { public function sql_regex($positivematch=true) {
return $positivematch ? '~*' : '!~*'; 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");
}
}

View file

@ -323,4 +323,18 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
} }
return implode('||', $elements); 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'");
}
} }