diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index 26b213ce9a20..471f8e466d52 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -326,8 +326,19 @@ public function execute(string $sql) $res->free(); } } - - return $this->connID->query($this->prepQuery($sql)); + try + { + return $this->connID->query($this->prepQuery($sql)); + } + catch (\mysqli_sql_exception $e) + { + log_message('error', $e); + if ($this->DBDebug) + { + throw $e; + } + } + return false; } //-------------------------------------------------------------------- diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index b239232b8c23..c3d6db6b4dda 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -187,11 +187,23 @@ public function getVersion(): string * * @param string $sql * - * @return resource + * @return mixed */ public function execute(string $sql) { - return pg_query($this->connID, $sql); + try + { + return pg_query($this->connID, $sql); + } + catch (\ErrorException $e) + { + log_message('error', $e); + if ($this->DBDebug) + { + throw $e; + } + } + return false; } //-------------------------------------------------------------------- diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index ae83cfc1c03f..570d68dce134 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -165,9 +165,21 @@ public function getVersion(): string */ public function execute(string $sql) { - return $this->isWriteType($sql) - ? $this->connID->exec($sql) - : $this->connID->query($sql); + try + { + return $this->isWriteType($sql) + ? $this->connID->exec($sql) + : $this->connID->query($sql); + } + catch (\ErrorException $e) + { + log_message('error', $e); + if ($this->DBDebug) + { + throw $e; + } + } + return false; } //-------------------------------------------------------------------- diff --git a/tests/system/Database/Live/DEBugTest.php b/tests/system/Database/Live/DEBugTest.php new file mode 100644 index 000000000000..737f4e956e7b --- /dev/null +++ b/tests/system/Database/Live/DEBugTest.php @@ -0,0 +1,32 @@ +setPrivateProperty($this->db, 'DBDebug', true); + $this->expectException('Exception'); + $result = $this->db->simpleQuery('SELECT * FROM db_error'); + } + + public function testDBDebugFalse() + { + $this->setPrivateProperty($this->db, 'DBDebug', false); + $result = $this->db->simpleQuery('SELECT * FROM db_error'); + $this->assertEquals(false, $result); + } + + public function tearDown(): void + { + $this->setPrivateProperty($this->db, 'DBDebug', true); + parent::tearDown(); + } +}