Skip to content

Commit f673bb0

Browse files
authored
Merge pull request #8467 from kenjis/fix-sqlite3-exception-not-thrown
fix: SQLite3 may not throw DatabaseException
2 parents 50cc0df + 7e505b5 commit f673bb0

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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) {

system/Database/SQLite3/PreparedQuery.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BadMethodCallException;
1717
use CodeIgniter\Database\BasePreparedQuery;
1818
use CodeIgniter\Database\Exceptions\DatabaseException;
19+
use Exception;
1920
use SQLite3;
2021
use SQLite3Result;
2122
use SQLite3Stmt;
@@ -82,7 +83,15 @@ public function _execute(array $data): bool
8283
$this->statement->bindValue($key + 1, $item, $bindType);
8384
}
8485

85-
$this->result = $this->statement->execute();
86+
try {
87+
$this->result = $this->statement->execute();
88+
} catch (Exception $e) {
89+
if ($this->db->DBDebug) {
90+
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
91+
}
92+
93+
return false;
94+
}
8695

8796
return $this->result !== false;
8897
}

0 commit comments

Comments
 (0)