Skip to content

Commit

Permalink
Migrate SQLite to use exceptions instead of returning FALSE
Browse files Browse the repository at this point in the history
Includes forward-compatible checks for PHP 9.0, when this will be the default
See https://wiki.php.net/rfc/sqlite3_exceptions
Implements #52
  • Loading branch information
thekid committed Nov 19, 2023
1 parent fa2bbef commit 889f75c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/php/rdbms/sqlite3/SQLite3Connection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 889f75c

Please sign in to comment.