Merge pull request 'turn on mysqli exceptions' (#8) from feature/enable-mysqli-exceptions into master
All checks were successful
soko-web/pipeline/head This commit looks good

Reviewed-on: #8
This commit is contained in:
Bence Pőcze 2023-04-17 20:45:53 +02:00
commit 644197fae8
Signed by: Gitea
GPG Key ID: 7B89B83EED9AD2C6

View File

@ -19,15 +19,9 @@ class Connection implements IConnection
$socket = (string) ini_get('mysqli.default_socket'); $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); $this->connection = new mysqli($host, $user, $password, $db, $port, $socket);
$this->connection->set_charset('utf8mb4');
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);
}
} }
public function __destruct() public function __destruct()
@ -37,30 +31,24 @@ class Connection implements IConnection
public function startTransaction(): void public function startTransaction(): void
{ {
if (!$this->connection->autocommit(false)) { $this->connection->autocommit(false);
throw new \Exception($this->connection->error);
}
} }
public function commit(): void public function commit(): void
{ {
if (!$this->connection->commit() || !$this->connection->autocommit(true)) { $this->connection->commit();
throw new \Exception($this->connection->error); $this->connection->autocommit(true);
}
} }
public function rollback(): void public function rollback(): void
{ {
if (!$this->connection->rollback() || !$this->connection->autocommit(true)) { $this->connection->rollback();
throw new \Exception($this->connection->error); $this->connection->autocommit(true);
}
} }
public function query(string $query): ?IResultSet public function query(string $query): ?IResultSet
{ {
if (!($result = $this->connection->query($query))) { $result = $this->connection->query($query);
throw new \Exception($this->connection->error . '. Query: ' . $query);
}
if ($result !== true) { if ($result !== true) {
return new ResultSet($result); return new ResultSet($result);
@ -71,9 +59,7 @@ class Connection implements IConnection
public function multiQuery(string $query): array public function multiQuery(string $query): array
{ {
if (!$this->connection->multi_query($query)) { $this->connection->multi_query($query);
throw new \Exception($this->connection->error . '. Query: ' . $query);
}
$ret = []; $ret = [];
do { do {
@ -86,18 +72,12 @@ class Connection implements IConnection
$this->connection->more_results(); $this->connection->more_results();
} while ($this->connection->next_result()); } while ($this->connection->next_result());
if ($this->connection->error) {
throw new \Exception($this->connection->error . '. Query: ' . $query);
}
return $ret; return $ret;
} }
public function prepare(string $query): IStatement public function prepare(string $query): IStatement
{ {
if (!($stmt = $this->connection->prepare($query))) { $stmt = $this->connection->prepare($query);
throw new \Exception($this->connection->error . '. Query: ' . $query);
}
return new Statement($stmt); return new Statement($stmt);
} }