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

125 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2021-07-09 01:38:30 +08:00
/** @noinspection PhpComposerExtensionStubsInspection */
2021-07-09 01:38:30 +08:00
declare(strict_types=1);
2021-07-09 01:38:30 +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 Swoole\Database\PDOProxy;
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()
{
Console::debug('Constructing...');
2021-07-09 01:38:30 +08:00
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
}
public function __destruct()
{
Console::debug('Destructing');
SqlPoolStorage::$sql_pool->putConnection($this->conn);
}
2022-04-03 01:47:38 +08:00
/**
* @param mixed $sql
* @param mixed $options
* @throws DbException
*/
public function prepare($sql, $options = [])
{
2021-07-09 01:38:30 +08:00
try {
Console::debug('Running SQL prepare: ' . $sql);
2021-07-09 01:38:30 +08:00
$statement = $this->conn->prepare($sql, $options);
2022-04-03 01:47:38 +08:00
assert($statement !== false);
2021-07-09 01:38:30 +08:00
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
return new MySQLStatement($statement);
}
2022-04-03 01:47:38 +08:00
/**
* @throws DbException
*/
public function query(...$args)
{
2021-07-09 01:38:30 +08:00
try {
$statement = $this->conn->query(...$args);
2022-04-03 01:47:38 +08:00
assert($statement !== false);
2021-07-09 01:38:30 +08:00
} catch (PDOException $exception) {
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
}
return new MySQLStatement($statement);
}
public function quote($value, $type = ParameterType::STRING)
{
2021-07-09 01:38:30 +08:00
return $this->conn->quote($value, $type);
}
2022-04-03 01:47:38 +08:00
/**
* @param mixed $sql
* @throws DbException
*/
public function exec($sql)
{
2021-07-09 01:38:30 +08:00
try {
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-04-03 01:47:38 +08:00
/**
* @param null|mixed $name
* @throws DbException
*/
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);
}
}
public function beginTransaction()
{
2021-07-09 01:38:30 +08:00
return $this->conn->beginTransaction();
}
public function commit()
{
2021-07-09 01:38:30 +08:00
return $this->conn->commit();
}
public function rollBack()
{
2021-07-09 01:38:30 +08:00
return $this->conn->rollBack();
}
public function errorCode()
{
2021-07-09 01:38:30 +08:00
return $this->conn->errorCode();
}
public function errorInfo()
{
2021-07-09 01:38:30 +08:00
return $this->conn->errorInfo();
}
}