From be415e95be3060f65bba826377f2b3ac8540c21b Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 21 Nov 2008 21:40:50 +0000 Subject: [PATCH] MDL-17354 moved reset_sequence into dbmanager --- lib/ddl/database_manager.php | 21 +++++++++++++++ lib/ddl/mssql_sql_generator.php | 21 +++++++++++++++ lib/ddl/mysql_sql_generator.php | 18 +++++++++++++ lib/ddl/oracle_sql_generator.php | 30 +++++++++++++++++++++ lib/ddl/postgres_sql_generator.php | 18 +++++++++++++ lib/ddl/simpletest/testddl.php | 26 ++++++++++++++++++ lib/ddl/sql_generator.php | 7 +++++ lib/ddl/sqlite_sql_generator.php | 16 +++++++++++ lib/dml/moodle_database.php | 7 ----- lib/dml/mssql_adodb_moodle_database.php | 19 ------------- lib/dml/mysqli_adodb_moodle_database.php | 15 ----------- lib/dml/mysqli_native_moodle_database.php | 16 ----------- lib/dml/oci8po_adodb_moodle_database.php | 30 --------------------- lib/dml/pgsql_native_moodle_database.php | 15 ----------- lib/dml/postgres7_adodb_moodle_database.php | 16 ----------- lib/dml/simpletest/testdml.php | 25 ----------------- lib/dml/sqlite3_pdo_moodle_database.php | 14 ---------- lib/dtl/database_importer.php | 2 +- 18 files changed, 158 insertions(+), 158 deletions(-) diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index 56a42157d3..9612127255 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -100,6 +100,27 @@ class database_manager { 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) * diff --git a/lib/ddl/mssql_sql_generator.php b/lib/ddl/mssql_sql_generator.php index c892ad96af..72492722d4 100644 --- a/lib/ddl/mssql_sql_generator.php +++ b/lib/ddl/mssql_sql_generator.php @@ -77,6 +77,27 @@ class mssql_sql_generator extends sql_generator { 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) diff --git a/lib/ddl/mysql_sql_generator.php b/lib/ddl/mysql_sql_generator.php index d7a3e36dbf..cf99b7b5bf 100644 --- a/lib/ddl/mysql_sql_generator.php +++ b/lib/ddl/mysql_sql_generator.php @@ -80,6 +80,24 @@ class mysql_sql_generator extends sql_generator { 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) * diff --git a/lib/ddl/oracle_sql_generator.php b/lib/ddl/oracle_sql_generator.php index da67f8c809..f7976121b8 100644 --- a/lib/ddl/oracle_sql_generator.php +++ b/lib/ddl/oracle_sql_generator.php @@ -62,6 +62,36 @@ class oracle_sql_generator extends sql_generator { 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) diff --git a/lib/ddl/postgres_sql_generator.php b/lib/ddl/postgres_sql_generator.php index d15b589d1a..39f6666a46 100644 --- a/lib/ddl/postgres_sql_generator.php +++ b/lib/ddl/postgres_sql_generator.php @@ -59,6 +59,24 @@ class postgres_sql_generator extends sql_generator { 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 */ diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index db5dac4cd3..3bcf4bd2d3 100755 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -839,6 +839,32 @@ class ddl_test extends UnitTestCase { } + 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() { diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index ce8b339554..44f977ce29 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -187,6 +187,13 @@ abstract class sql_generator { 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 */ diff --git a/lib/ddl/sqlite_sql_generator.php b/lib/ddl/sqlite_sql_generator.php index 7828688840..4018476c8c 100644 --- a/lib/ddl/sqlite_sql_generator.php +++ b/lib/ddl/sqlite_sql_generator.php @@ -71,6 +71,22 @@ class sqlite_sql_generator extends sql_generator { 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 */ diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 7ef862844f..405a82655e 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -571,13 +571,6 @@ 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. diff --git a/lib/dml/mssql_adodb_moodle_database.php b/lib/dml/mssql_adodb_moodle_database.php index 49be8e1a12..53fcac745a 100644 --- a/lib/dml/mssql_adodb_moodle_database.php +++ b/lib/dml/mssql_adodb_moodle_database.php @@ -356,25 +356,6 @@ 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 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. diff --git a/lib/dml/mysqli_adodb_moodle_database.php b/lib/dml/mysqli_adodb_moodle_database.php index 5d4a3f1014..bb95379e03 100644 --- a/lib/dml/mysqli_adodb_moodle_database.php +++ b/lib/dml/mysqli_adodb_moodle_database.php @@ -282,21 +282,6 @@ class mysqli_adodb_moodle_database extends adodb_moodle_database { 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. diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 758e85e6ba..28e39d263c 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -326,22 +326,6 @@ class mysqli_native_moodle_database extends moodle_database { 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 diff --git a/lib/dml/oci8po_adodb_moodle_database.php b/lib/dml/oci8po_adodb_moodle_database.php index 00b37d1e12..c641a928e9 100644 --- a/lib/dml/oci8po_adodb_moodle_database.php +++ b/lib/dml/oci8po_adodb_moodle_database.php @@ -614,36 +614,6 @@ class oci8po_adodb_moodle_database extends adodb_moodle_database { 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. diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index df78830208..b433b18997 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -450,21 +450,6 @@ class pgsql_native_moodle_database extends moodle_database { 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 diff --git a/lib/dml/postgres7_adodb_moodle_database.php b/lib/dml/postgres7_adodb_moodle_database.php index 2b983948c1..cc2c5ffe1f 100644 --- a/lib/dml/postgres7_adodb_moodle_database.php +++ b/lib/dml/postgres7_adodb_moodle_database.php @@ -466,22 +466,6 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database { 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. diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index fd181e008a..8a50af326c 100755 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -1019,30 +1019,6 @@ class dml_test extends UnitTestCase { $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; @@ -1528,7 +1504,6 @@ class moodle_database_for_testing extends moodle_database { 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){} diff --git a/lib/dml/sqlite3_pdo_moodle_database.php b/lib/dml/sqlite3_pdo_moodle_database.php index f926cbe073..3633898f16 100644 --- a/lib/dml/sqlite3_pdo_moodle_database.php +++ b/lib/dml/sqlite3_pdo_moodle_database.php @@ -331,18 +331,4 @@ 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'"); - } } diff --git a/lib/dtl/database_importer.php b/lib/dtl/database_importer.php index e88f66c12c..45a9dcb502 100644 --- a/lib/dtl/database_importer.php +++ b/lib/dtl/database_importer.php @@ -128,7 +128,7 @@ class database_importer { $fields = $table->getFields(); foreach ($fields as $field) { if ($field->getSequence()) { - $this->mdb->reset_sequence($tablename); + $this->manager->reset_sequence($tablename); return; } } -- 2.39.5