#23480 [Ver->Csd]: Sequences for DB:MySQL

From: Date: Sun, 15 Jun 2003 04:46:32 +0000
Subject: #23480 [Ver->Csd]: Sequences for DB:MySQL
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-17477@lists.php.net to get a copy of this message
ID: 23480 Updated by: cox@php.net Reported By: blair at squiz dot net -Status: Verified +Status: Closed Bug Type: PEAR related Operating System: Redhat Linux PHP Version: 4.3.1 Assigned To: ssb New Comment: This bug has been fixed in CVS. In case this was a PHP problem, snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. In case this was a documentation problem, the fix will show up soon at http://www.php.net/manual/. In case this was a PHP.net website problem, the change will show up on the PHP.net site and on the mirror sites in short time. Thank you for the report, and for helping us make PHP better. Previous Comments: ------------------------------------------------------------------------ [2003-05-05 04:21:26] lsmith@php.net the patch is good imho as I have stated in the linked thread. It does not add overhead to common use of nextId. Also it makes the behavior finally consistent with other backends. BC issues should be minimal since the purpose of nextId is to return unique indentifiers and not a specific number. Therefore only people aware of this issues with their own BC hacks will be affected. ------------------------------------------------------------------------ [2003-05-04 18:50:17] blair at squiz dot net Just to confirm that there is no BC issue for people using nextId() with it's ondemand feature. The potential BC issue will be for those that are running createSequence() themselves (eg in an install script). ------------------------------------------------------------------------ [2003-05-04 18:27:06] nicos@php.net I'm not sure if we should apply this if we dont want to break BC. Stig ? ------------------------------------------------------------------------ [2003-05-04 18:27:05] nicos@php.net I'm not sure if we should apply this if we dont want to break BC. Stig ? ------------------------------------------------------------------------ [2003-05-04 18:12:54] blair at squiz dot net After sounding this out on the pear-dev mailing list here, http://marc.theaimsgroup.com/?l=pear-dev&m=105183392425465&w=2, I am submitting this as a bug, this is a copy of the post : ... I have noticed an inconsistency between MySQL and PostgreSQL DB objects when you are creating sequences. When you call createSequence() with MySQL it set's the value to 1, so when you make your first call with nextId() a 2 is returned. When you call createSequence() with Postgre and then call nextId() a 1 is returned, which is what I would expect. I can't test what the other databases do, but by looking at nextId() fn in the other DB classes that are using tables to emulate sequences (ie mssql, fbsql and odbc) createSequence() appears to initialise the value to zero because these fns repeat in the do..while loop after creating the sequence. I have attached a patch to the DB/mysql.php that fixes this issue in createSequence() and alters nextId() to perform the same repeat that the other classes do - please tell me what you think. BTW - if you are wondering why I need to call createSequence() rather than just using the on demand feature of nextId() it has to do with Postgre, transactions and a failed NEXTVAL() call - see the points raised in this bug report, http://bugs.php.net/bug.php?id=22761 --- /home/brobertson/pear/DB/mysql.php Wed Apr 23 09:50:42 2003 +++ mysql.php Fri May 2 09:49:36 2003 @@ -553,13 +553,11 @@ $result->getCode() == DB_ERROR_NOSUCHTABLE) { $result = $this->createSequence($seq_name); - // Since createSequence initializes the ID to be 1, - // we do not need to retrieve the ID again (or we will get 2) if (DB::isError($result)) { return $this->raiseError($result); } else { - // First ID of a newly created sequence is 1 - return 1; + // now just repeat and we will get our first value + $repeat = 1; } /** BACKWARDS COMPAT **/ @@ -590,8 +588,13 @@ if (DB::isError($res)) { return $res; } - // insert yields value 1, nextId call will generate ID 2 - return $this->query("INSERT INTO ${seqname} VALUES(0)"); + // insert yields value 1 + $res = $this->query("INSERT INTO ${seqname} VALUES(0)"); + if (DB::isError($res)) { + return $res; + } + // so reset to zero + return $this->query("UPDATE ${seqname} SET id = 0;"); } // }}} ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=23480&edit=1

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