Skip to content

Commit cd5ab99

Browse files
committed
fix: SQLite3 may not throw DatabaseException
Use enableExceptions(). See https://www.php.net/manual/en/sqlite3.enableexceptions.php
1 parent 9a31656 commit cd5ab99

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

system/Database/BasePreparedQuery.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Database\Exceptions\DatabaseException;
1919
use CodeIgniter\Events\Events;
2020
use ErrorException;
21+
use Exception;
2122

2223
/**
2324
* @template TConnection
@@ -125,8 +126,13 @@ public function execute(...$data)
125126
try {
126127
$exception = null;
127128
$result = $this->_execute($data);
129+
} catch (BadMethodCallException $exception) {
130+
throw $exception;
128131
} catch (ArgumentCountError|ErrorException $exception) {
129132
$result = false;
133+
} catch (Exception $exception) {
134+
// SQLite3 throws `Exception`.
135+
$result = false;
130136
}
131137

132138
// Update our query object

system/Database/SQLite3/Connection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use ErrorException;
1918
use Exception;
2019
use SQLite3;
2120
use SQLite3Result;
@@ -87,9 +86,13 @@ public function connect(bool $persistent = false)
8786
$this->database = WRITEPATH . $this->database;
8887
}
8988

90-
return (! $this->password)
89+
$sqlite = (! $this->password)
9190
? new SQLite3($this->database)
9291
: new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
92+
93+
$sqlite->enableExceptions(true);
94+
95+
return $sqlite;
9396
} catch (Exception $e) {
9497
throw new DatabaseException('SQLite3 error: ' . $e->getMessage());
9598
}
@@ -146,7 +149,7 @@ protected function execute(string $sql)
146149
return $this->isWriteType($sql)
147150
? $this->connID->exec($sql)
148151
: $this->connID->query($sql);
149-
} catch (ErrorException $e) {
152+
} catch (Exception $e) {
150153
log_message('error', (string) $e);
151154

152155
if ($this->DBDebug) {

0 commit comments

Comments
 (0)