lazy create mysql connecion
All checks were successful
soko-web/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-09-16 13:12:46 +02:00
parent 8bf495c89b
commit bccee89c13
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -7,48 +7,52 @@ use mysqli;
class Connection implements IConnection class Connection implements IConnection
{ {
private mysqli $connection; private string $host;
private string $user;
private string $password;
private string $db;
private int $port;
private string $socket;
private ?mysqli $connection = null;
public function __construct(string $host, string $user, string $password, string $db, int $port = -1, string $socket = null) public function __construct(string $host, string $user, string $password, string $db, int $port = -1, string $socket = null)
{ {
if ($port < 0) { $this->host = $host;
$port = (int) ini_get('mysqli.default_port'); $this->user = $user;
} $this->password = $password;
$this->db = $db;
if ($socket === null) { $this->port = $port < 0 ? (int) ini_get('mysqli.default_port') : $port;
$socket = (string) ini_get('mysqli.default_socket'); $this->socket = $socket === null ? (string) ini_get('mysqli.default_socket') : $socket;
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli($host, $user, $password, $db, $port, $socket);
$this->connection->set_charset('utf8mb4');
} }
public function __destruct() public function __destruct()
{ {
if ($this->connection === null) {
return;
}
$this->connection->close(); $this->connection->close();
} }
public function startTransaction(): void public function startTransaction(): void
{ {
$this->connection->autocommit(false); $this->getConnection()->autocommit(false);
} }
public function commit(): void public function commit(): void
{ {
$this->connection->commit(); $this->getConnection()->commit();
$this->connection->autocommit(true); $this->getConnection()->autocommit(true);
} }
public function rollback(): void public function rollback(): void
{ {
$this->connection->rollback(); $this->getConnection()->rollback();
$this->connection->autocommit(true); $this->getConnection()->autocommit(true);
} }
public function query(string $query): ?IResultSet public function query(string $query): ?IResultSet
{ {
$result = $this->connection->query($query); $result = $this->getConnection()->query($query);
if ($result !== true) { if ($result !== true) {
return new ResultSet($result); return new ResultSet($result);
@ -59,36 +63,51 @@ class Connection implements IConnection
public function multiQuery(string $query): array public function multiQuery(string $query): array
{ {
$this->connection->multi_query($query); $this->getConnection()->multi_query($query);
$ret = []; $ret = [];
do { do {
if ($result = $this->connection->store_result()) { if ($result = $this->getConnection()->store_result()) {
$ret[] = new ResultSet($result); $ret[] = new ResultSet($result);
} else { } else {
$ret[] = null; $ret[] = null;
} }
$this->connection->more_results(); $this->getConnection()->more_results();
} while ($this->connection->next_result()); } while ($this->getConnection()->next_result());
return $ret; return $ret;
} }
public function prepare(string $query): IStatement public function prepare(string $query): IStatement
{ {
$stmt = $this->connection->prepare($query); $stmt = $this->getConnection()->prepare($query);
return new Statement($stmt); return new Statement($stmt);
} }
public function lastId(): int public function lastId(): int
{ {
return $this->connection->insert_id; return $this->getConnection()->insert_id;
} }
public function getAffectedRows(): int public function getAffectedRows(): int
{ {
return $this->connection->affected_rows; return $this->getConnection()->affected_rows;
}
private function getConnection(): mysqli
{
if ($this->connection === null) {
$this->createConnection();
}
return $this->connection;
}
private function createConnection(): void
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->db, $this->port, $this->socket);
$this->connection->set_charset('utf8mb4');
} }
} }