PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » MDB2_Driver_pgsql » Bug #9989

lastInsertID with no table / field specified

Details

Request #9989lastInsertID with no table / field specified
Submitted2007-01-31 15:25 UTC
Frommario dot adam at schaeffler dot com
Assignedquipo
StatusClosed
PackageMDB2_Driver_pgsql
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2007-01-31 15:25 UTC] mario dot adam at schaeffler dot com

Description:
------------
The function lastInsertID() returns the result vom CURRVAL for a specified sequence. The function expects two optional parameters $table and $field which both are set to null per default. Calling this function with no parameters (and therefor use null for both $table and $field) would result in CURRVAL('_seq'), if '_seq' was the suffix for sequences. As long you do not have a sequence called like that, an error will be returned.

I think, it has to be dicussed, if it wasn't better to return the last used ID at all. LASTVAL could be used for that. A possible solution is pasted in "Test script" below...

Test script:
---------------
function lastInsertID($table = null, $field = null)
{
if (empty($table) && empty($field)) {
return $this->queryOne("SELECT lastval()", 'integer');
} else {
$seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->getSequenceName($seq);
return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
}
}