]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17354 moved reset_sequence into dbmanager
authorskodak <skodak>
Fri, 21 Nov 2008 21:40:50 +0000 (21:40 +0000)
committerskodak <skodak>
Fri, 21 Nov 2008 21:40:50 +0000 (21:40 +0000)
18 files changed:
lib/ddl/database_manager.php
lib/ddl/mssql_sql_generator.php
lib/ddl/mysql_sql_generator.php
lib/ddl/oracle_sql_generator.php
lib/ddl/postgres_sql_generator.php
lib/ddl/simpletest/testddl.php
lib/ddl/sql_generator.php
lib/ddl/sqlite_sql_generator.php
lib/dml/moodle_database.php
lib/dml/mssql_adodb_moodle_database.php
lib/dml/mysqli_adodb_moodle_database.php
lib/dml/mysqli_native_moodle_database.php
lib/dml/oci8po_adodb_moodle_database.php
lib/dml/pgsql_native_moodle_database.php
lib/dml/postgres7_adodb_moodle_database.php
lib/dml/simpletest/testdml.php
lib/dml/sqlite3_pdo_moodle_database.php
lib/dtl/database_importer.php

index 56a42157d33f91e0d28446b2cb7fb4f309445a85..96121272557a48152891c5e033b5325b8bb844a6 100644 (file)
@@ -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)
      *
index c892ad96af945a946b3cd869b5d1afe34221dabf..72492722d413ea3517f4debc22ae18d470a500b1 100644 (file)
@@ -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)
index d7a3e36dbfcf2861a5decf14f751a775b1fbe4e4..cf99b7b5bfa45297a81239416f582ac04255a87c 100644 (file)
@@ -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)
      *
index da67f8c8096205056783a09853cdbae59d4d657d..f7976121b838633aab61874ad8a92f214d7abbe2 100644 (file)
@@ -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)
index d15b589d1ad0dcfcb93c8851d5a992d866778e9c..39f6666a46de70a2d66bf7a48d815d1475e5e599 100644 (file)
@@ -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
      */
index db5dac4cd3e0b5996052cb047f337478f2753dfa..3bcf4bd2d3e247a13e15d7943967eb43cb484ddd 100755 (executable)
@@ -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() {
index ce8b339554affe1920a8e1aa83256bb33bb867e2..44f977ce297ba3432dbc012c5d9646e7973bc4a2 100644 (file)
@@ -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
      */
index 78286888406d63a8da3d4d5c46cb13996a438407..4018476c8c8432201ba14168b01851346b142473 100644 (file)
@@ -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
      */
index 7ef862844f7c2fa690ab92e82b78677716e30aee..405a82655e4028a345665caf56d62b613a9735d1 100644 (file)
@@ -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.
index 49be8e1a12bc485f8ac5ca71cdea1615e73c50cc..53fcac745ab6bb7c4cdeeff7d59898b6ecaf9aa2 100644 (file)
@@ -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.
index 5d4a3f1014e667dd1b3897a95df76484db6298e1..bb95379e0367fff7b25c88df82f9984c7d63edea 100644 (file)
@@ -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.
index 758e85e6ba1d612800456ffd62c90df7698c0474..28e39d263cd17b9edd342503dfc331ab86fda5f6 100644 (file)
@@ -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
index 00b37d1e12f15fe6fbc955049492102b0e096829..c641a928e9adcceedc36136ce51862501d02a33f 100644 (file)
@@ -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.
index df78830208cf40413313d67058de454face8cefc..b433b1899764dfe5a239c12a42038b1406740091 100644 (file)
@@ -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
index 2b983948c1e8679829bd5dff68a4f535dbffedaa..cc2c5ffe1f95dce9d351162fc52ac9ead1e3dd67 100644 (file)
@@ -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.
index fd181e008afb29460db1034c570142852a71383f..8a50af326c5547c6a034ab2485ec6c10bb397bc8 100755 (executable)
@@ -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){}
index f926cbe073a8197751df7adf94ee3a94eca052ec..3633898f167a265477d9a3826df132448e09a1f6 100644 (file)
@@ -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'");
-    }
 }
index e88f66c12c96ab56da7b95da0385886275286ee1..45a9dcb5029abd38e6e438664863bd786b237103 100644 (file)
@@ -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;
             }
         }