#23480 [Opn->Ver]: Sequences for DB:MySQL
| From: | nicos@php.net | Date: | Sun, 04 May 2003 23:27:05 +0000 |
| Subject: | #23480 [Opn->Ver]: Sequences for DB:MySQL | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-15851@lists.php.net to get a copy of this message | ||
ID: 23480
Updated by: nicos@php.net
Reported By: blair at squiz dot net
-Status: Open
+Status: Verified
Bug Type: PEAR related
Operating System: Redhat Linux
PHP Version: 4.3.1
New Comment:
I'm not sure if we should apply this if we dont want to break BC.
Stig ?
Previous Comments:
------------------------------------------------------------------------
[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