cvs: php4 /pear/DB mysql.php

From: Date: Sun, 22 Jul 2001 00:14:53 +0000
Subject: cvs: php4 /pear/DB mysql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-309@lists.php.net to get a copy of this message
ssb Sat Jul 21 20:14:53 2001 EDT Modified files: /php4/pear/DB mysql.php Log: (PEAR DB) mysql sequence emulation "terse table" patch from Oleg Rekutin Index: php4/pear/DB/mysql.php diff -u php4/pear/DB/mysql.php:1.63 php4/pear/DB/mysql.php:1.64 --- php4/pear/DB/mysql.php:1.63 Sat Jul 21 20:05:44 2001 +++ php4/pear/DB/mysql.php Sat Jul 21 20:14:53 2001 @@ -376,7 +376,8 @@ // XXX HACK: temporarily force error mode to "return" $tmp = $this->_default_error_mode; $this->_default_error_mode = PEAR_ERROR_RETURN; - $result = $this->query("INSERT INTO ${sqn}_seq VALUES(NULL)"); + $result = $this->query("UPDATE ${sqn}_seq ". + 'SET id=LAST_INSERT_ID(id+1)'); $this->_default_error_mode = $tmp; if ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { @@ -385,6 +386,25 @@ if (DB::isError($result)) { return $result; } + } else if (DB::isError($result) && + $result->getCode() == DB_ERROR_ALREADY_EXISTS) { + // Must be using old sequence emulation implementation, + // clean up the dupes + $highest_id = $this->getOne("SELECT id FROM ${sqn}_seq ". + 'ORDER BY id DESC LIMIT 1'); + if (DB::isError($highest_id)) { + return $highest_id; + } + // We should probably do something if $highest_id isn't + // numeric, but I'm at a loss as how to handle that... + $result = $this->query("DELETE FROM ${sqn}_seq ". + "WHERE id <> $highest_id"); + if (DB::isError($result)) { + return $result; + } + // This should kill all rows except the highest, now we + // can try again + $repeat = 1; } else { $repeat = 0; } @@ -401,9 +421,15 @@ function createSequence($seq_name) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); - return $this->query("CREATE TABLE ${sqn}_seq ". - '(id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'. + $res = $this->query("CREATE TABLE ${sqn}_seq ". + '(id INTEGER UNSIGNED AUTO_INCREMENT NOT +NULL,'. ' PRIMARY KEY(id))'); + if (DB::isError($res)) { + return $res; + } + // nextId call will generate ID 1 + return $this->query("INSERT INTO ${sqn}_seq VALUES(0)"); } // }}}

« previous php.pear.cvs (#309) next »