From ace9cea6b79f65bfdbd055c15e8dbf6f2c00b93e Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 13 May 2024 21:46:29 -0500 Subject: [PATCH] Throw exception when last insert ID can't be determined --- lib/DbConnection.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/DbConnection.php b/lib/DbConnection.php index da83f7ed..7021d549 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -400,21 +400,24 @@ class DbConnection{ } } - public function GetLastInsertedId(): ?int{ - $id = $this->_link->lastInsertId(); + /** + * Get the ID of the last row that was inserted during this database connection. + * @throws Exceptions\DatabaseQueryException When the last inserted ID can't be determined. + */ + public function GetLastInsertedId(): int{ + try{ + $id = $this->_link->lastInsertId(); + } + catch(\PDOException){ + $id = false; + } - if($id === false){ - return null; + if($id === false || $id == '0'){ + throw new Exceptions\DatabaseQueryException('Couldn\'t get last insert ID.'); } else{ - $id = (int)$id; + return intval($id); } - - if($id == 0){ - return null; - } - - return $id; } /**