Skip to content

Commit

Permalink
fix: SQLite3 may not throw DatabaseException
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 27, 2024
1 parent 9a31656 commit cd5ab99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions system/Database/BasePreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Events\Events;
use ErrorException;
use Exception;

/**
* @template TConnection
Expand Down Expand Up @@ -125,8 +126,13 @@ public function execute(...$data)
try {
$exception = null;
$result = $this->_execute($data);
} catch (BadMethodCallException $exception) {
throw $exception;
} catch (ArgumentCountError|ErrorException $exception) {
$result = false;
} catch (Exception $exception) {
// SQLite3 throws `Exception`.
$result = false;
}

// Update our query object
Expand Down
9 changes: 6 additions & 3 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Exceptions\DatabaseException;
use ErrorException;
use Exception;
use SQLite3;
use SQLite3Result;
Expand Down Expand Up @@ -87,9 +86,13 @@ public function connect(bool $persistent = false)
$this->database = WRITEPATH . $this->database;
}

return (! $this->password)
$sqlite = (! $this->password)
? new SQLite3($this->database)
: new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);

$sqlite->enableExceptions(true);

return $sqlite;
} catch (Exception $e) {
throw new DatabaseException('SQLite3 error: ' . $e->getMessage());
}
Expand Down Expand Up @@ -146,7 +149,7 @@ protected function execute(string $sql)
return $this->isWriteType($sql)
? $this->connID->exec($sql)
: $this->connID->query($sql);
} catch (ErrorException $e) {
} catch (Exception $e) {
log_message('error', (string) $e);

if ($this->DBDebug) {
Expand Down

0 comments on commit cd5ab99

Please sign in to comment.