in order to make database_manager to execute it. reset_sequence reimplemented.
* @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
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);
}
/**
/**
* 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)");
}
/**
* TODO: Moodle 2.1 - drop in Moodle 2.1
*/
public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
-
+
$results = array();
/**
* 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");
}
/**
* 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);
$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");
}
/**
* 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");
}
/**
$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);
* @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
/**
* 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'");
}
/**