zhamao-framework/src/ZM/MySQL/MySQLConnection.php

96 lines
2.8 KiB
PHP
Raw Normal View History

2021-07-09 01:38:30 +08:00
<?php /** @noinspection PhpComposerExtensionStubsInspection */
namespace ZM\MySQL;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\ParameterType;
use PDO;
use PDOException;
use PDOStatement;
use Swoole\Database\PDOProxy;
use Swoole\Database\PDOStatementProxy;
use ZM\Console\Console;
use ZM\Exception\DbException;
use ZM\Store\MySQL\SqlPoolStorage;
class MySQLConnection implements Connection
{
/** @var PDO|PDOProxy */
private $conn;
public function __construct() {
2021-09-01 14:14:00 +08:00
Console::debug("Constructing...");
2021-07-09 01:38:30 +08:00
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
}
public function prepare($sql, $options = []) {
try {
2021-09-01 14:14:00 +08:00
Console::debug("Running SQL prepare: ".$sql);
2021-07-09 01:38:30 +08:00
$statement = $this->conn->prepare($sql, $options);
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
return new MySQLStatement($statement);
}
public function query(...$args) {
try {
$statement = $this->conn->query(...$args);
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
return new MySQLStatement($statement);
}
public function quote($value, $type = ParameterType::STRING) {
return $this->conn->quote($value, $type);
}
public function exec($sql) {
try {
2021-09-01 14:14:00 +08:00
Console::debug("Running SQL exec: ".$sql);
2021-07-09 01:38:30 +08:00
$statement = $this->conn->exec($sql);
assert($statement !== false);
return $statement;
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
}
public function lastInsertId($name = null) {
try {
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
}
public function beginTransaction() {
return $this->conn->beginTransaction();
}
public function commit() {
return $this->conn->commit();
}
public function rollBack() {
return $this->conn->rollBack();
}
public function errorCode() {
return $this->conn->errorCode();
}
public function errorInfo() {
return $this->conn->errorInfo();
}
public function __destruct() {
2021-09-01 14:14:00 +08:00
Console::debug("Destructing");
2021-07-09 01:38:30 +08:00
SqlPoolStorage::$sql_pool->putConnection($this->conn);
}
}