From f43121769bf2a59fabbbd698cec12519879a5cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 17 Apr 2023 20:42:22 +0200 Subject: [PATCH] turn on mysqli exceptions --- src/Database/Mysql/Connection.php | 40 ++++++++----------------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/src/Database/Mysql/Connection.php b/src/Database/Mysql/Connection.php index 0b8212c..ed2b967 100644 --- a/src/Database/Mysql/Connection.php +++ b/src/Database/Mysql/Connection.php @@ -19,15 +19,9 @@ class Connection implements IConnection $socket = (string) ini_get('mysqli.default_socket'); } + mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $this->connection = new mysqli($host, $user, $password, $db, $port, $socket); - - if ($this->connection->connect_error) { - throw new \Exception('Connection failed: ' . $this->connection->connect_error); - } - - if (!$this->connection->set_charset('utf8mb4')) { - throw new \Exception($this->connection->error); - } + $this->connection->set_charset('utf8mb4'); } public function __destruct() @@ -37,30 +31,24 @@ class Connection implements IConnection public function startTransaction(): void { - if (!$this->connection->autocommit(false)) { - throw new \Exception($this->connection->error); - } + $this->connection->autocommit(false); } public function commit(): void { - if (!$this->connection->commit() || !$this->connection->autocommit(true)) { - throw new \Exception($this->connection->error); - } + $this->connection->commit(); + $this->connection->autocommit(true); } public function rollback(): void { - if (!$this->connection->rollback() || !$this->connection->autocommit(true)) { - throw new \Exception($this->connection->error); - } + $this->connection->rollback(); + $this->connection->autocommit(true); } public function query(string $query): ?IResultSet { - if (!($result = $this->connection->query($query))) { - throw new \Exception($this->connection->error . '. Query: ' . $query); - } + $result = $this->connection->query($query); if ($result !== true) { return new ResultSet($result); @@ -71,9 +59,7 @@ class Connection implements IConnection public function multiQuery(string $query): array { - if (!$this->connection->multi_query($query)) { - throw new \Exception($this->connection->error . '. Query: ' . $query); - } + $this->connection->multi_query($query); $ret = []; do { @@ -86,18 +72,12 @@ class Connection implements IConnection $this->connection->more_results(); } while ($this->connection->next_result()); - if ($this->connection->error) { - throw new \Exception($this->connection->error . '. Query: ' . $query); - } - return $ret; } public function prepare(string $query): IStatement { - if (!($stmt = $this->connection->prepare($query))) { - throw new \Exception($this->connection->error . '. Query: ' . $query); - } + $stmt = $this->connection->prepare($query); return new Statement($stmt); } -- 2.45.2