Compare commits

..

No commits in common. "ebe1fa2aa625c9035611bd995539e9d0f87d1e08" and "8bf495c89b4ce1456da1133adc285003e544dd48" have entirely different histories.

View File

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