From bccee89c131b6b1c65273b0e54f397f977a8750a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 16 Sep 2023 13:12:46 +0200 Subject: [PATCH] lazy create mysql connecion --- src/Database/Mysql/Connection.php | 69 ++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/src/Database/Mysql/Connection.php b/src/Database/Mysql/Connection.php index ed2b967..4eb622f 100644 --- a/src/Database/Mysql/Connection.php +++ b/src/Database/Mysql/Connection.php @@ -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'); } } -- 2.45.2