DB::nextId() produces undesirable/unexpected results
| From: | Brian Abent | Date: | Thu, 24 Jan 2002 18:12:37 +0000 |
| Subject: | DB::nextId() produces undesirable/unexpected results | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-4144@lists.php.net to get a copy of this message | ||
Hello,
I am using pgsql. When I call nextid() with the name of a sequence as the
first parameter, and false as the second parameter, an error occurs. I
checked the source in DB/pgsql.php and found that nextId() appends '_seq' to
$seq_name without my permission.
At first I thought this was a bug, because there is no mention of renaming
$seq_name in the comments above nextId() : * @param string $seq_name the
name of the sequence
However, I checked oci8.php and found the same behavior so it must be
intentional. This seems a bit presumptuous on the part of pear db. I
explicitly passed a valid sequence name to a method that should (according
to its comments) accept a valid sequence name. I suggest fixing(?) the
behavior of nextId() (although many people probably rely on this
functionality by now) or creating a new method that will be compatible with
existing schemas. I would be happy to contribute to whatever solution the
pear db team thinks is the best.
I suspect this behavior has something to do with allowing nextId() to create
sequences by default -- therefore attempting to ensure consistent naming of
sequences. This 'feature' seems unnecessary because any developer or DBA
that has a clue about their database will have already created their
sequences -- more than likely at the time they created their tables, defined
their relationships, and wrote their triggers. While this behavior might
make PHP/PEAR 'easier' for beginning developers - it alienates developers
with existing schemas who would like to port applications to PEAR.
One last observation: the first line in nextId is:
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
Again, I don't think renaming explicit input is a good idea here. I do
think validating input and returning a PEAR_Error is an appropriate
solution. quick example:
< $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
---
> if (preg_match('/[^a-z0-9_]/i',$seq_name)) {
> return new PEAR_Error('Invalid characters in nextId()');
> }
> $sqn = $seq_name;
Again, I am happy to contribute code if necessary.
Thanks,
Brian Abent