2022-08-21 16:08:20 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
2022-08-27 19:45:23 +08:00
|
|
|
|
namespace ZM\Store\Database;
|
2022-08-21 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Driver\Connection;
|
|
|
|
|
|
use Doctrine\DBAL\ParameterType;
|
|
|
|
|
|
|
2022-08-27 19:45:23 +08:00
|
|
|
|
class DBConnection implements Connection
|
2022-08-21 16:08:20 +08:00
|
|
|
|
{
|
2022-11-03 10:18:17 +08:00
|
|
|
|
/** @var \PDO */
|
2022-08-21 16:08:20 +08:00
|
|
|
|
private $conn;
|
|
|
|
|
|
|
|
|
|
|
|
private $pool_name;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($params)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger()->debug('Constructing...');
|
2022-08-27 19:45:23 +08:00
|
|
|
|
$this->conn = DBPool::pool($params['dbName'])->get();
|
2022-08-21 16:08:20 +08:00
|
|
|
|
$this->pool_name = $params['dbName'];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
|
|
{
|
|
|
|
|
|
logger()->debug('Destructing!!!');
|
2022-08-27 19:45:23 +08:00
|
|
|
|
DBPool::pool($this->pool_name)->put($this->conn);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-27 19:45:23 +08:00
|
|
|
|
* @param mixed $sql
|
|
|
|
|
|
* @param mixed $options
|
|
|
|
|
|
* @throws DBException
|
2022-08-21 16:08:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function prepare($sql, $options = [])
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
logger()->debug('Running SQL prepare: ' . $sql);
|
|
|
|
|
|
$statement = $this->conn->prepare($sql, $options);
|
|
|
|
|
|
assert($statement !== false);
|
2022-11-03 10:18:17 +08:00
|
|
|
|
} catch (\PDOException $exception) {
|
2022-08-27 19:45:23 +08:00
|
|
|
|
throw new DBException($exception->getMessage(), 0, $exception);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
2022-08-27 19:45:23 +08:00
|
|
|
|
return new DBStatement($statement);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-27 19:45:23 +08:00
|
|
|
|
* @throws DBException
|
2022-08-21 16:08:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function query(...$args)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$statement = $this->conn->query(...$args);
|
|
|
|
|
|
assert($statement !== false);
|
2022-11-03 10:18:17 +08:00
|
|
|
|
} catch (\PDOException $exception) {
|
2022-08-27 19:45:23 +08:00
|
|
|
|
throw new DBException($exception->getMessage(), 0, $exception);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
2022-08-27 19:45:23 +08:00
|
|
|
|
return new DBStatement($statement);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function quote($value, $type = ParameterType::STRING)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->conn->quote($value, $type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-27 19:45:23 +08:00
|
|
|
|
* @param mixed $sql
|
|
|
|
|
|
* @throws DBException
|
2022-08-21 16:08:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function exec($sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
logger()->debug('Running SQL exec: ' . $sql);
|
|
|
|
|
|
$statement = $this->conn->exec($sql);
|
|
|
|
|
|
assert($statement !== false);
|
|
|
|
|
|
return $statement;
|
2022-11-03 10:18:17 +08:00
|
|
|
|
} catch (\PDOException $exception) {
|
2022-08-27 19:45:23 +08:00
|
|
|
|
throw new DBException($exception->getMessage(), 0, $exception);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-27 19:45:23 +08:00
|
|
|
|
* @param null|mixed $name
|
|
|
|
|
|
* @throws DBException
|
2022-08-21 16:08:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function lastInsertId($name = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
2022-11-03 10:18:17 +08:00
|
|
|
|
} catch (\PDOException $exception) {
|
2022-08-27 19:45:23 +08:00
|
|
|
|
throw new DBException($exception->getMessage(), 0, $exception);
|
2022-08-21 16:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getPoolName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->pool_name;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|