2022-03-15 18:05:33 +08:00
|
|
|
|
<?php
|
2021-07-09 01:38:30 +08:00
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */
|
2021-07-09 01:38:30 +08:00
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
declare(strict_types=1);
|
2021-07-09 01:38:30 +08:00
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
namespace ZM\MySQL;
|
2021-07-09 01:38:30 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function __construct()
|
|
|
|
|
|
{
|
|
|
|
|
|
Console::debug('Constructing...');
|
2021-07-09 01:38:30 +08:00
|
|
|
|
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function __destruct()
|
|
|
|
|
|
{
|
|
|
|
|
|
Console::debug('Destructing!!!');
|
|
|
|
|
|
SqlPoolStorage::$sql_pool->putConnection($this->conn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function prepare($sql, $options = [])
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
try {
|
2022-03-15 18:05:33 +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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function query(...$args)
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function quote($value, $type = ParameterType::STRING)
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->quote($value, $type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function exec($sql)
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
try {
|
2022-03-15 18:05:33 +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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function lastInsertId($name = null)
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
try {
|
|
|
|
|
|
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
|
|
|
|
|
} catch (PDOException $exception) {
|
|
|
|
|
|
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function beginTransaction()
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->beginTransaction();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function commit()
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function rollBack()
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->rollBack();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function errorCode()
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->errorCode();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function errorInfo()
|
|
|
|
|
|
{
|
2021-07-09 01:38:30 +08:00
|
|
|
|
return $this->conn->errorInfo();
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|