diff options
| author | Nathan Gibbs <nathan@cmpublishers.com> | 2023-06-10 19:43:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-11 01:43:45 +0200 |
| commit | b819440c251b1d916b663a91e32ac9eae2eaf57f (patch) | |
| tree | f210c9e8c0a5494ce53f258c53b9991fd1b43e7e | |
| parent | 943ac8a7de20117d85860a9b67f9eb65c5dfe4d7 (diff) | |
| download | adodb-b819440c251b1d916b663a91e32ac9eae2eaf57f.tar.gz adodb-b819440c251b1d916b663a91e32ac9eae2eaf57f.tar.bz2 adodb-b819440c251b1d916b663a91e32ac9eae2eaf57f.zip | |
pgsql: avoid Insert_ID() failing when lastval() is not set
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
| -rw-r--r-- | drivers/adodb-postgres8.inc.php | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/drivers/adodb-postgres8.inc.php b/drivers/adodb-postgres8.inc.php index 37c2aae1..911ec178 100644 --- a/drivers/adodb-postgres8.inc.php +++ b/drivers/adodb-postgres8.inc.php @@ -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; } } |
