SQL generators must not execute changes in DB. Just provide the needed SQL

in order to make database_manager to execute it. reset_sequence reimplemented.
This commit is contained in:
stronk7
2009-08-31 14:23:40 +00:00
parent 576ace3245
commit b1ca138716
8 changed files with 57 additions and 48 deletions
+7 -6
View File
@@ -109,11 +109,8 @@ class database_manager {
* @return success
*/
public function reset_sequence($table) {
/// Calculate the name of the table
if (is_string($table)) {
$tablename = $table;
} else {
$tablename = $table->getName();
if (!is_string($table) and !($table instanceof xmldb_table)) {
throw new ddl_exception('ddlunknownerror', NULL, 'incorrect table parameter!');
}
/// Check the table exists
@@ -121,7 +118,11 @@ class database_manager {
throw new ddl_table_missing_exception($tablename);
}
return $this->generator->reset_sequence($table);
if (!$sqlarr = $this->generator->getResetSequenceSQL($table)) {
throw new ddl_exception('ddlunknownerror', null, 'table reset sequence sql not generated');
}
$this->execute_sql_arr($sqlarr);
}
/**
+9 -10
View File
@@ -84,22 +84,21 @@ class mssql_sql_generator extends sql_generator {
/**
* Reset a sequence to the id field of a table.
* @param string $table name of table
* @return bool true
* @throws dml_exception if error
* @param string $table name of table or xmldb_table object
* @return array sql commands to execute
*/
public function reset_sequence($table) {
public function getResetSequenceSQL($table) {
if (is_string($table)) {
$tablename = $table;
} else {
$tablename = $table->getName();
$table = new xmldb_table($table);
}
// From http://msdn.microsoft.com/en-us/library/ms176057.aspx
$value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
$value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'. $table->getName() . '}');
if ($value == 0) {
$value = 1;
}
return $this->mdb->change_database_structure("DBCC CHECKIDENT ('$this->prefix$tablename', RESEED, $value)");
return array("DBCC CHECKIDENT ('" . $this->getTableName($table) . "', RESEED, $value)");
}
/**
@@ -517,7 +516,7 @@ class mssql_sql_generator extends sql_generator {
* TODO: Moodle 2.1 - drop in Moodle 2.1
*/
public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
$results = array();
+9 -7
View File
@@ -84,19 +84,21 @@ class mysql_sql_generator extends sql_generator {
/**
* Reset a sequence to the id field of a table.
* @param string $table name of table
* @return bool success
* @param string $table name of table or xmldb_table object
* @return array sql commands to execute
*/
public function reset_sequence($table) {
if (is_string($table)) {
$tablename = $table;
} else {
public function getResetSequenceSQL($table) {
if ($table instanceof xmldb_table) {
$tablename = $table->getName();
} else {
$tablename = $table;
}
// From http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
$value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
$value++;
return $this->mdb->change_database_structure("ALTER TABLE $this->prefix$tablename AUTO_INCREMENT = $value");
return array("ALTER TABLE $this->prefix$tablename AUTO_INCREMENT = $value");
}
+6 -6
View File
@@ -66,11 +66,11 @@ class oracle_sql_generator extends sql_generator {
/**
* Reset a sequence to the id field of a table.
* @param string $table name of table
* @return bool true
* @throws dml_exception if error
* @param string $table name of table or xmldb_table object
* @return array sql commands to execute
*/
public function reset_sequence($table) {
public function getResetSequenceSQL($table) {
if (is_string($table)) {
$tablename = $table;
$xmldb_table = new xmldb_table($tablename);
@@ -89,8 +89,8 @@ class oracle_sql_generator extends sql_generator {
$seqname = $this->getNameForObject($table, 'id', 'seq');
}
$this->mdb->change_database_structure("DROP SEQUENCE $seqname");
return $this->mdb->change_database_structure("CREATE SEQUENCE $seqname START WITH $value INCREMENT BY 1 NOMAXVALUE");
return array ("DROP SEQUENCE $seqname",
"CREATE SEQUENCE $seqname START WITH $value INCREMENT BY 1 NOMAXVALUE");
}
+9 -8
View File
@@ -63,20 +63,21 @@ class postgres_sql_generator extends sql_generator {
/**
* Reset a sequence to the id field of a table.
* @param string $table name of table
* @return bool true
* @throws dml_exception if error
* @param string $table name of table or xmldb_table object
* @return array sql commands to execute
*/
public function reset_sequence($table) {
if (is_string($table)) {
$tablename = $table;
} else {
public function getResetSequenceSQL($table) {
if ($table instanceof xmldb_table) {
$tablename = $table->getName();
} else {
$tablename = $table;
}
// From http://www.postgresql.org/docs/7.4/static/sql-altersequence.html
$value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
$value++;
return $this->mdb->change_database_structure("ALTER SEQUENCE $this->prefix{$tablename}_id_seq RESTART WITH $value");
return array("ALTER SEQUENCE $this->prefix{$tablename}_id_seq RESTART WITH $value");
}
/**
+9 -3
View File
@@ -1299,18 +1299,24 @@ class ddl_test extends UnitTestCase {
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
// Drop if exists
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$dbman->create_table($table);
$this->tables[$table->getName()] = $table;
$tablename = $table->getName();
$this->tables[$tablename] = $table;
$record = (object)array('id'=>666, 'course'=>10);
$DB->import_record('testtable', $record);
$DB->delete_records('testtable');
$this->assertTrue($dbman->reset_sequence('testtable'));
$dbman->reset_sequence($table); // using xmldb object
$this->assertEqual(1, $DB->insert_record('testtable', (object)array('course'=>13)));
$DB->import_record('testtable', $record);
$this->assertTrue($dbman->reset_sequence('testtable'));
$dbman->reset_sequence($tablename); // using string
$this->assertEqual(667, $DB->insert_record('testtable', (object)array('course'=>13)));
$dbman->drop_table($table);
+1 -1
View File
@@ -185,7 +185,7 @@ abstract class sql_generator {
* @param string $table name of table
* @return success
*/
public abstract function reset_sequence($tablename);
public abstract function getResetSequenceSQL($tablename);
/**
* This function will return the SQL code needed to create db tables and statements
+7 -7
View File
@@ -74,18 +74,18 @@ class sqlite_sql_generator extends sql_generator {
/**
* Reset a sequence to the id field of a table.
* @param string $table name of table
* @param string $table name of table or xmldb_object
* @return bool success
*/
public function reset_sequence($table) {
if (is_string($table)) {
$tablename = $table;
} else {
$tablename = $table->getName();
public function getResetSequenceSQL($table) {
if ($table instanceof xmldb_table) {
$table = $table->getName();
}
// From http://sqlite.org/autoinc.html
$value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
return $this->mdb->change_database_structure("UPDATE sqlite_sequence SET seq=$value WHERE name='$this->prefix$tablename'");
return array("UPDATE sqlite_sequence SET seq=$value WHERE name='$this->prefix$tablename'");
}
/**