From 889f75c0ebb35b0b5502776dd295613bdcdc5b41 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 19 Nov 2023 14:17:10 +0100 Subject: [PATCH] Migrate SQLite to use exceptions instead of returning FALSE Includes forward-compatible checks for PHP 9.0, when this will be the default See https://wiki.php.net/rfc/sqlite3_exceptions Implements #52 --- .../php/rdbms/sqlite3/SQLite3Connection.class.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/php/rdbms/sqlite3/SQLite3Connection.class.php b/src/main/php/rdbms/sqlite3/SQLite3Connection.class.php index 3b7d8ec1..38605db0 100755 --- a/src/main/php/rdbms/sqlite3/SQLite3Connection.class.php +++ b/src/main/php/rdbms/sqlite3/SQLite3Connection.class.php @@ -103,7 +103,9 @@ public function connect($reconnect= false) { try { $this->handle= new SQLite3($database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE); + PHP_VERSION_ID >= 90000 || $this->handle->enableExceptions(true); } catch (Exception $e) { + $this->handle= false; throw new SQLConnectException($e->getMessage().': '.$database, $this->dsn); } @@ -160,16 +162,17 @@ public function identity($field= null) { protected function query0($sql, $buffered= true) { $this->handle instanceof SQLite3 || $this->connections->establish($this); - $result= $this->handle->query($sql); - if (false === $result) { - $e= new SQLStatementFailedException( + try { + $result= $this->handle->query($sql); + } catch (Exception $e) { + throw new SQLStatementFailedException( 'Statement failed: '.$this->handle->lastErrorMsg().' @ '.$this->dsn->getDatabase(), $sql, $this->handle->lastErrorCode() ); - \xp::gc(__FILE__); - throw $e; - } else if ($result->numColumns()) { + } + + if ($result->numColumns()) { return new SQLite3ResultSet($result); } else { return new QuerySucceeded($this->handle->changes());