Skip to content

Commit

Permalink
pgsql: avoid Insert_ID() failing when lastval() is not set
Browse files Browse the repository at this point in the history
If Insert_ID() is executed before nextval() has been called in the
current session, SELECT lastval() will fail with
`ERROR:  lastval is not yet defined in this session`.

When using Exceptions, this causes one to be thrown but this is not the
expected behavior, as the function is expected to just return false in
this case.

Fixes #978

Signed-off-by: Damien Regad <dregad@mantisbt.org>

Rewritten commit message
  • Loading branch information
NathanGibbs3 committed Jun 10, 2023
1 parent 943ac8a commit b819440
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions drivers/adodb-postgres8.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ class ADODB_postgres8 extends ADODB_postgres7
* @return int last inserted ID for given table/column, or the most recently
* returned one if $table or $column are empty
*/
protected function _insertID($table = '', $column = '')
{
return empty($table) || empty($column)
? $this->GetOne("SELECT lastval()")
: $this->GetOne("SELECT currval(pg_get_serial_sequence('$table', '$column'))");
protected function _insertID( $table = '', $column = '' ){
$sql = 'SELECT ';
$sql .= empty($table) || empty($column) ? 'lastval()' : "currval(pg_get_serial_sequence('$table', '$column'))";
$result = @$this->GetOne($sql);
if( $result === false || $result == $ADODB_GETONE_EOF ){
if( $this->debug ){
ADOConnection::outp(__FUNCTION__ . "() failed : " . $this->errorMsg());
}
return false;
}
return $result;
}
}

Expand Down

0 comments on commit b819440

Please sign in to comment.