Re: mysql->nextID() problem and patch

From: Date: Fri, 05 Apr 2002 02:01:40 +0000
Subject: Re: mysql->nextID() problem and patch
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-5194@lists.php.net to get a copy of this message
Colin Viebrock wrote: > > 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. Hi Colin, I was looking to it and found this other solution: @@ -565,6 +565,11 @@ // This should kill all rows except the highest, now we // can try again $repeat = 1; + + // If the seq table is empty, force it to have 1 + } elseif (mysql_affected_rows($this->connection) == 0) { + $result = $this->query("REPLACE INTO ${seqname} VALUES (1)"); + I'll commit my patch if there are no objections with it. Tomas V.V.Cox > > 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; > } > > -- > PEAR Development Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

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