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