diff options
author | Dries Buytaert <dries@buytaert.net> | 2002-08-20 19:29:16 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2002-08-20 19:29:16 +0000 |
commit | bfe5b85dbdab7b0c9f868dcb3beb71848ffbd295 (patch) | |
tree | 222ac3236359bdc76cfea99c09911d016fe23488 | |
parent | afd87425d98ee62864911a2ccd29beb0eea843cb (diff) | |
download | brdo-bfe5b85dbdab7b0c9f868dcb3beb71848ffbd295.tar.gz brdo-bfe5b85dbdab7b0c9f868dcb3beb71848ffbd295.tar.bz2 |
- Applied a (modified) version of Marco's SQL sequence patch.
-rw-r--r-- | database/database.mysql | 25 | ||||
-rw-r--r-- | includes/database.mysql.inc | 15 | ||||
-rw-r--r-- | includes/database.pear.inc | 6 | ||||
-rw-r--r-- | modules/node.module | 7 | ||||
-rw-r--r-- | modules/node/node.module | 7 | ||||
-rw-r--r-- | modules/taxonomy.module | 6 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.module | 6 | ||||
-rw-r--r-- | update.php | 26 |
8 files changed, 58 insertions, 40 deletions
diff --git a/database/database.mysql b/database/database.mysql index fa6eba04e..8618128fc 100644 --- a/database/database.mysql +++ b/database/database.mysql @@ -94,18 +94,6 @@ CREATE TABLE cache ( ) TYPE=MyISAM; # -# Table structure for table 'collection' -# - -CREATE TABLE collection ( - cid int(10) unsigned NOT NULL auto_increment, - name varchar(32) NOT NULL default '', - types varchar(128) NOT NULL default '', - PRIMARY KEY (cid), - UNIQUE KEY name (name) -) TYPE=MyISAM; - -# # Table structure for table 'comments' # @@ -385,19 +373,14 @@ CREATE TABLE system ( description varchar(255) NOT NULL default '', status int(2) NOT NULL default '0', PRIMARY KEY (filename) -) TYPE=MyISAM; # -# Table structure for table 'tag' +# Table structure for table 'sequences' # -CREATE TABLE tag ( - tid int(10) unsigned NOT NULL auto_increment, - name varchar(32) NOT NULL default '', - attributes varchar(255) NOT NULL default '', - collections varchar(32) NOT NULL default '', - PRIMARY KEY (tid), - UNIQUE KEY name (name,collections) +CREATE TABLE sequences ( + name VARCHAR(255) NOT NULL PRIMARY KEY, + id INT UNSIGNED NOT NULL ) TYPE=MyISAM; # diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 65372f539..df0d74e32 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -93,4 +93,19 @@ function db_error() { return mysql_errno(); } +function db_next_id($name) { + + /* + ** Note that REPLACE query below correctly creates a new sequence + ** when needed + */ + + db_query("LOCK TABLES sequences WRITE"); + $id = db_result(db_query("SELECT id FROM sequences WHERE name = '%s'", $name)) + 1; + db_query("REPLACE INTO sequences VALUES ('%s', '%d')", $name, $id); + db_query("UNLOCK TABLES"); + + return $id; +} + ?>
\ No newline at end of file diff --git a/includes/database.pear.inc b/includes/database.pear.inc index 8f9e21bff..e7fcf0f1a 100644 --- a/includes/database.pear.inc +++ b/includes/database.pear.inc @@ -100,4 +100,10 @@ function db_error($result) { return DB::isError($db_handle); } +function db_next_id($name) { + global $db_handle; + + return $db_handle->nextID($name); +} + ?>
\ No newline at end of file diff --git a/modules/node.module b/modules/node.module index 3b5d913e7..e9bdb3d17 100644 --- a/modules/node.module +++ b/modules/node.module @@ -149,8 +149,7 @@ function node_save($node, $filter) { // Set some required fields: $node->created = time(); $node->changed = time(); - $node->nid = db_result(db_query("SELECT MAX(nid) + 1 FROM node")); - $node->nid = empty($node->nid) ? 1 : $node->nid; + $node->nid = db_next_id("node"); // Prepare the query: foreach ($node as $key => $value) { @@ -1073,7 +1072,7 @@ function node_submit($node) { else { $fields = array("nid", "uid" => ($user->uid ? $user->uid : 0), "body", "teaser", "title", "type" => $node->type); } - + $nid = node_save($node, array_merge($fields, module_invoke($node->type, "save", "update", $node))); /* @@ -1120,7 +1119,7 @@ function node_submit($node) { else { $fields = array("uid" => ($user->uid ? $user->uid : 0), "body", "comment" => 2, "teaser", "title", "type" => $node->type); } - + $nid = node_save($node, array_merge($fields, module_invoke($node->type, "save", "create", $node))); /* diff --git a/modules/node/node.module b/modules/node/node.module index 3b5d913e7..e9bdb3d17 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -149,8 +149,7 @@ function node_save($node, $filter) { // Set some required fields: $node->created = time(); $node->changed = time(); - $node->nid = db_result(db_query("SELECT MAX(nid) + 1 FROM node")); - $node->nid = empty($node->nid) ? 1 : $node->nid; + $node->nid = db_next_id("node"); // Prepare the query: foreach ($node as $key => $value) { @@ -1073,7 +1072,7 @@ function node_submit($node) { else { $fields = array("nid", "uid" => ($user->uid ? $user->uid : 0), "body", "teaser", "title", "type" => $node->type); } - + $nid = node_save($node, array_merge($fields, module_invoke($node->type, "save", "update", $node))); /* @@ -1120,7 +1119,7 @@ function node_submit($node) { else { $fields = array("uid" => ($user->uid ? $user->uid : 0), "body", "comment" => 2, "teaser", "title", "type" => $node->type); } - + $nid = node_save($node, array_merge($fields, module_invoke($node->type, "save", "create", $node))); /* diff --git a/modules/taxonomy.module b/modules/taxonomy.module index e3b475c7c..d3c1a0826 100644 --- a/modules/taxonomy.module +++ b/modules/taxonomy.module @@ -153,11 +153,7 @@ function taxonomy_save_term($edit) { taxonomy_del_term($edit["tid"]); } else { - $edit["tid"] = db_result(db_query("SELECT MAX(tid) + 1 FROM term_data")); - if (!$edit["tid"]) { - // first term - $edit["tid"] = 1; - } + $edit["tid"] = db_next_id("term_data"); $data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]); db_query("INSERT INTO term_data ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2)); } diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index e3b475c7c..d3c1a0826 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -153,11 +153,7 @@ function taxonomy_save_term($edit) { taxonomy_del_term($edit["tid"]); } else { - $edit["tid"] = db_result(db_query("SELECT MAX(tid) + 1 FROM term_data")); - if (!$edit["tid"]) { - // first term - $edit["tid"] = 1; - } + $edit["tid"] = db_next_id("term_data"); $data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]); db_query("INSERT INTO term_data ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2)); } diff --git a/update.php b/update.php index 302ab3fab..9c74537d3 100644 --- a/update.php +++ b/update.php @@ -48,7 +48,8 @@ $mysql_updates = array( "2002-07-07" => "update_33", "2002-07-31" => "update_34", "2002-08-10" => "update_35", - "2002-08-16" => "update_36" + "2002-08-16" => "update_36", + "2002-08-19" => "update_37" ); // Update functions @@ -508,6 +509,29 @@ function update_36() { update_sql("ALTER TABLE rating CHANGE new current int(6) NOT NULL default '0';"); } +function update_37() { + + update_sql("DROP TABLE IF EXISTS sequences;"); + + update_sql("CREATE TABLE sequences ( + name VARCHAR(255) NOT NULL PRIMARY KEY, + id INT UNSIGNED NOT NULL + ) TYPE=MyISAM;"); + + if ($max = db_result(db_query("SELECT MAX(nid) FROM node;"))) { + update_sql("REPLACE INTO sequences VALUES ('node', $max);"); + } + + if ($max = db_result(db_query("SELECT MAX(cid) FROM comments;"))) { + update_sql("REPLACE INTO sequences VALUES ('comments', $max);"); + } + // NOTE: move the comments bit down as soon as we switched to use the new comment module! + + if ($max = db_result(db_query("SELECT MAX(tid) FROM term_data;"))) { + update_sql("REPLACE INTO sequences VALUES ('term_data', $max);"); + } +} + function update_upgrade3() { update_sql("INSERT INTO system VALUES ('archive.module','archive','module','',1);"); update_sql("INSERT INTO system VALUES ('block.module','block','module','',1);"); |