From 49bf7f95973b8c2da59904294a6e0a816a9c7214 Mon Sep 17 00:00:00 2001 From: Instrye Date: Thu, 26 Mar 2020 17:34:15 +0800 Subject: [PATCH 1/2] fix. MYSQLI::DBDebug can't woker --- system/Database/MySQLi/Connection.php | 14 +++++++++++-- tests/system/Database/Live/DEBugTest.php | 26 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/system/Database/Live/DEBugTest.php diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index 26b213ce9a20..579c04fd3a6e 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -326,8 +326,18 @@ 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) + { + 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..a8c744a053f5 --- /dev/null +++ b/tests/system/Database/Live/DEBugTest.php @@ -0,0 +1,26 @@ +setPrivateProperty($this->db, 'DBDebug', true); + $this->expectException('mysqli_sql_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); + } +} From d4ce8ffda15f9785dcadc665a43f937db34340e1 Mon Sep 17 00:00:00 2001 From: Instrye Date: Fri, 27 Mar 2020 08:49:56 +0800 Subject: [PATCH 2/2] fix. Postgre and SQLite3 --- system/Database/MySQLi/Connection.php | 3 ++- system/Database/Postgre/Connection.php | 16 ++++++++++++++-- system/Database/SQLite3/Connection.php | 18 +++++++++++++++--- tests/system/Database/Live/DEBugTest.php | 8 +++++++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index 579c04fd3a6e..471f8e466d52 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -332,12 +332,13 @@ public function execute(string $sql) } catch (\mysqli_sql_exception $e) { + log_message('error', $e); if ($this->DBDebug) { throw $e; } - return false; } + 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 index a8c744a053f5..737f4e956e7b 100644 --- a/tests/system/Database/Live/DEBugTest.php +++ b/tests/system/Database/Live/DEBugTest.php @@ -13,7 +13,7 @@ class DEBugTest extends CIDatabaseTestCase public function testDBDebugTrue() { $this->setPrivateProperty($this->db, 'DBDebug', true); - $this->expectException('mysqli_sql_exception'); + $this->expectException('Exception'); $result = $this->db->simpleQuery('SELECT * FROM db_error'); } @@ -23,4 +23,10 @@ public function testDBDebugFalse() $result = $this->db->simpleQuery('SELECT * FROM db_error'); $this->assertEquals(false, $result); } + + public function tearDown(): void + { + $this->setPrivateProperty($this->db, 'DBDebug', true); + parent::tearDown(); + } }