mysql->nextID() problem and patch

From: Date: Tue, 02 Apr 2002 23:19:08 +0000
Subject: mysql->nextID() problem and patch
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-5175@lists.php.net to get a copy of this message
I've been noticing that if (for whatever reason) the sequence table exists but is empty, then the nextID() method for mysql doesn't work. It keeps returning "0". Here is a patch to fix it. A bit kludgy perhaps, but I don't know how else to handle it. - Colin cmv@vincent:/usr/local/sources/php4/pear/DB$ diff -u mysql.php NEW.mysql.php --- mysql.php Tue Apr 2 18:13:52 2002 +++ NEW.mysql.php Tue Apr 2 18:17:27 2002 @@ -555,6 +555,34 @@ // This should kill all rows except the highest, now we // can try again $repeat = 1; + } else if ($result==1 && mysql_affected_rows($this->connection)==0) { + // Sequence table must be empty for some reason, so fill it and return 1 + + // Obtain a user-level lock + $result = $this->getOne("SELECT GET_LOCK('${seqname}_lock',10)"); + if (DB::isError($result)) { + return $result; + } + if ($result == 0) { + // Failed to get the lock, bail with a DB_ERROR_NOT_LOCKED error + return $this->mysqlRaiseError(1100); + } + + // add the default value + $result = $this->query("INSERT INTO ${seqname} VALUES (0)"); + if (DB::isError($result)) { + return $result; + } + + // Release the lock + $result = $this->getOne("SELECT RELEASE_LOCK('${seqname}_lock')"); + if (DB::isError($result)) { + return $result; + } + + // We know what the result will be, so no need to try again + return 1; + } else { $repeat = 0; }

« previous php.pear.dev (#5175) next »