return $this->generator->table_exists($table, $temptable);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return success
+ */
+ public function reset_sequence($table) {
+ /// Calculate the name of the table
+ if (is_string($table)) {
+ $tablename = $table;
+ } else {
+ $tablename = $table->getName();
+ }
+
+ /// Check the table exists
+ if (!$this->table_exists($table)) {
+ throw new ddl_table_missing_exception($tablename);
+ }
+
+ return $this->generator->reset_sequence($table);
+ }
+
/**
* Given one xmldb_field, check if it exists in DB (true/false)
*
parent::__construct($mdb);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return bool true
+ * @throws dml_exception if error
+ */
+ public function reset_sequence($table) {
+ if (is_string($table)) {
+ $tablename = $table;
+ } else {
+ $tablename = $table->getName();
+ }
+ // From http://msdn.microsoft.com/en-us/library/ms176057.aspx
+ $value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
+ if ($value == 0) {
+ $value = 1;
+ }
+ return $this->mdb->change_database_structure("DBCC CHECKIDENT ('$this->prefix$tablename', RESEED, $value)");
+ }
+
+
/**
* Given one correct xmldb_table, returns the SQL statements
* to create temporary table (inside one array)
parent::__construct($mdb);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return bool success
+ */
+ public function reset_sequence($table) {
+ if (is_string($table)) {
+ $tablename = $table;
+ } else {
+ $tablename = $table->getName();
+ }
+ // 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");
+ }
+
+
/**
* Given one xmldb_table, check if it exists in DB (true/false)
*
parent::__construct($mdb);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return bool true
+ * @throws dml_exception if error
+ */
+ public function reset_sequence($table) {
+ if (is_string($table)) {
+ $tablename = $table;
+ $xmldb_table = new xmldb_table($tablename);
+ } else {
+ $tablename = $table->getName();
+ $xmldb_table = $table;
+ }
+ // From http://www.acs.ilstu.edu/docs/oracle/server.101/b10759/statements_2011.htm
+ $value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
+ $value++;
+
+ $seqname = $this->mdb->get_manager()->find_sequence_name($xmldb_table);
+
+ if (!$seqname) {
+ /// Fallback, seqname not found, something is wrong. Inform and use the alternative getNameForObject() method
+ $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");
+ }
+
+
/**
* Given one correct xmldb_table, returns the SQL statements
* to create temporary table (inside one array)
parent::__construct($mdb);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return bool true
+ * @throws dml_exception if error
+ */
+ public function reset_sequence($table) {
+ if (is_string($table)) {
+ $tablename = $table;
+ } else {
+ $tablename = $table->getName();
+ }
+ // 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");
+ }
+
/**
* Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type
*/
}
+ public function test_reset_sequence() {
+ $DB = $this->tdb;
+ $dbman = $DB->get_manager();
+
+ $table = new xmldb_table('testtable');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $dbman->create_table($table);
+ $this->tables[$table->getName()] = $table;
+
+ $record = (object)array('id'=>666, 'course'=>10);
+ $DB->import_record('testtable', $record);
+ $DB->delete_records('testtable');
+
+ $this->assertTrue($dbman->reset_sequence('testtable'));
+ $this->assertEqual(1, $DB->insert_record('testtable', (object)array('course'=>13)));
+
+ $DB->import_record('testtable', $record);
+ $this->assertTrue($dbman->reset_sequence('testtable'));
+ $this->assertEqual(667, $DB->insert_record('testtable', (object)array('course'=>13)));
+
+ $dbman->drop_table($table);
+ }
+
+
// Following methods are not supported == Do not test
/*
public function testRenameIndex() {
return $exists;
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return success
+ */
+ public abstract function reset_sequence($tablename);
+
/**
* This function will return the SQL code needed to create db tables and statements
*/
parent::__construct($mdb);
}
+ /**
+ * Reset a sequence to the id field of a table.
+ * @param string $table name of table
+ * @return bool success
+ */
+ public function reset_sequence($table) {
+ if (is_string($table)) {
+ $tablename = $table;
+ } else {
+ $tablename = $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'");
+ }
+
/**
* Given one correct xmldb_key, returns its specs
*/
$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.
return ($returnid ? $id : true);
}
- /**
- * Reset a sequence to the id field of a table.
- * @param string $table name of table
- * @return bool true
- * @throws dml_exception if error
- */
- 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)");
- }
-
-
/**
* Import a record into a table, id field is required.
* Basic safety checks only. Lobs are supported.
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");
- }
-
/**
* Import a record into a table, id field is required.
* Basic safety checks only. Lobs are supported.
return $this->columns[$table];
}
- /**
- * Reset a sequence to the id field of a table.
- * @param string $table name of table
- * @return bool true
- * @throws dml_exception if error
- */
- 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");
- }
-
/**
* Is db in unicode mode?
* @return bool
return $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
- */
- 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->query_start('--find_sequence_name', null, SQL_QUERY_AUX);
- $seqname = $dbman->find_sequence_name($xmldb_table);
- $this->query_end(true);
-
- 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");
- }
-
/**
* Import a record into a table, id field is required.
* Basic safety checks only. Lobs are supported.
return $this->columns[$table];
}
- /**
- * Reset a sequence to the id field of a table.
- * @param string $table name of table
- * @return bool true
- * @throws dml_exception if error
- */
- public function reset_sequence($table) {
- 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");
- }
-
/**
* Is db in unicode mode?
* @return bool
return $positivematch ? '~*' : '!~*';
}
- /**
- * Reset a sequence to the id field of a table.
- * @param string $table name of table
- * @return bool true
- * @throws dml_exception if error
- */
- 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");
- }
-
/**
* Import a record into a table, id field is required.
* Basic safety checks only. Lobs are supported.
$this->assertEqual(2, $records[13]->course);
}
- public function test_reset_sequence() {
- $DB = $this->tdb;
- $dbman = $DB->get_manager();
-
- $table = $this->get_test_table($dbman, "testtable");
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
- $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $dbman->create_table($table);
- $this->tables[$table->getName()] = $table;
-
- $record = (object)array('id'=>666, 'course'=>10);
- $DB->import_record('testtable', $record);
- $DB->delete_records('testtable');
-
- $this->assertTrue($DB->reset_sequence('testtable'));
- $this->assertEqual(1, $DB->insert_record('testtable', (object)array('course'=>13)));
-
- $DB->import_record('testtable', $record);
- $this->assertTrue($DB->reset_sequence('testtable'));
- $this->assertEqual(667, $DB->insert_record('testtable', (object)array('course'=>13)));
- }
-
-
public function test_insert_record_clob() {
global $CFG;
public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false){}
public function insert_record($table, $dataobject, $returnid=true, $bulk=false){}
public function import_record($table, $dataobject){}
- public function reset_sequence($table){}
public function update_record_raw($table, $params, $bulk=false){}
public function update_record($table, $dataobject, $bulk=false){}
public function set_field_select($table, $newfield, $newvalue, $select, array $params=null){}
}
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'");
- }
}
$fields = $table->getFields();
foreach ($fields as $field) {
if ($field->getSequence()) {
- $this->mdb->reset_sequence($tablename);
+ $this->manager->reset_sequence($tablename);
return;
}
}