mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
refactor MySQL component to support SQLite at the same time
This commit is contained in:
@@ -4,14 +4,14 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use Doctrine\DBAL\Driver\Connection;
|
use Doctrine\DBAL\Driver\Connection;
|
||||||
use Doctrine\DBAL\ParameterType;
|
use Doctrine\DBAL\ParameterType;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
|
||||||
class MySQLConnection implements Connection
|
class DBConnection implements Connection
|
||||||
{
|
{
|
||||||
/** @var PDO */
|
/** @var PDO */
|
||||||
private $conn;
|
private $conn;
|
||||||
@@ -21,20 +21,20 @@ class MySQLConnection implements Connection
|
|||||||
public function __construct($params)
|
public function __construct($params)
|
||||||
{
|
{
|
||||||
logger()->debug('Constructing...');
|
logger()->debug('Constructing...');
|
||||||
$this->conn = MySQLPool::pool($params['dbName'])->get();
|
$this->conn = DBPool::pool($params['dbName'])->get();
|
||||||
$this->pool_name = $params['dbName'];
|
$this->pool_name = $params['dbName'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
logger()->debug('Destructing!!!');
|
logger()->debug('Destructing!!!');
|
||||||
MySQLPool::pool($this->pool_name)->put($this->conn);
|
DBPool::pool($this->pool_name)->put($this->conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $sql
|
* @param mixed $sql
|
||||||
* @param mixed $options
|
* @param mixed $options
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function prepare($sql, $options = [])
|
public function prepare($sql, $options = [])
|
||||||
{
|
{
|
||||||
@@ -43,13 +43,13 @@ class MySQLConnection implements Connection
|
|||||||
$statement = $this->conn->prepare($sql, $options);
|
$statement = $this->conn->prepare($sql, $options);
|
||||||
assert($statement !== false);
|
assert($statement !== false);
|
||||||
} catch (PDOException $exception) {
|
} catch (PDOException $exception) {
|
||||||
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
|
throw new DBException($exception->getMessage(), 0, $exception);
|
||||||
}
|
}
|
||||||
return new MySQLStatement($statement);
|
return new DBStatement($statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function query(...$args)
|
public function query(...$args)
|
||||||
{
|
{
|
||||||
@@ -57,9 +57,9 @@ class MySQLConnection implements Connection
|
|||||||
$statement = $this->conn->query(...$args);
|
$statement = $this->conn->query(...$args);
|
||||||
assert($statement !== false);
|
assert($statement !== false);
|
||||||
} catch (PDOException $exception) {
|
} catch (PDOException $exception) {
|
||||||
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
|
throw new DBException($exception->getMessage(), 0, $exception);
|
||||||
}
|
}
|
||||||
return new MySQLStatement($statement);
|
return new DBStatement($statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function quote($value, $type = ParameterType::STRING)
|
public function quote($value, $type = ParameterType::STRING)
|
||||||
@@ -68,8 +68,8 @@ class MySQLConnection implements Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $sql
|
* @param mixed $sql
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function exec($sql)
|
public function exec($sql)
|
||||||
{
|
{
|
||||||
@@ -79,20 +79,20 @@ class MySQLConnection implements Connection
|
|||||||
assert($statement !== false);
|
assert($statement !== false);
|
||||||
return $statement;
|
return $statement;
|
||||||
} catch (PDOException $exception) {
|
} catch (PDOException $exception) {
|
||||||
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
|
throw new DBException($exception->getMessage(), 0, $exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null|mixed $name
|
* @param null|mixed $name
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function lastInsertId($name = null)
|
public function lastInsertId($name = null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
||||||
} catch (PDOException $exception) {
|
} catch (PDOException $exception) {
|
||||||
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
|
throw new DBException($exception->getMessage(), 0, $exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
11
src/ZM/Store/Database/DBException.php
Normal file
11
src/ZM/Store/Database/DBException.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
|
use ZM\Exception\ZMException;
|
||||||
|
|
||||||
|
class DBException extends ZMException
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use OneBot\Driver\Driver;
|
use OneBot\Driver\Driver;
|
||||||
use OneBot\Driver\Interfaces\PoolInterface;
|
use OneBot\Driver\Interfaces\PoolInterface;
|
||||||
@@ -14,8 +14,9 @@ use OneBot\Driver\Workerman\ObjectPool as WorkermanObjectPool;
|
|||||||
use OneBot\Driver\Workerman\WorkermanDriver;
|
use OneBot\Driver\Workerman\WorkermanDriver;
|
||||||
use PDO;
|
use PDO;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
use ZM\Store\FileSystem;
|
||||||
|
|
||||||
class MySQLPool
|
class DBPool
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array<string, SwooleObjectPool|WorkermanObjectPool> 连接池列表
|
* @var array<string, SwooleObjectPool|WorkermanObjectPool> 连接池列表
|
||||||
@@ -25,26 +26,44 @@ class MySQLPool
|
|||||||
/**
|
/**
|
||||||
* 通过配置文件创建一个 MySQL 连接池
|
* 通过配置文件创建一个 MySQL 连接池
|
||||||
*
|
*
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public static function create(string $name, array $config)
|
public static function create(string $name, array $config)
|
||||||
{
|
{
|
||||||
$size = $config['pool_size'] ?? 128;
|
$size = $config['pool_size'] ?? 64;
|
||||||
$connect_str = 'mysql:host={host};port={port};dbname={dbname};charset={charset}';
|
switch ($config['type']) {
|
||||||
$table = [
|
case 'mysql':
|
||||||
'{host}' => $config['host'],
|
$connect_str = 'mysql:host={host};port={port};dbname={dbname};charset={charset}';
|
||||||
'{port}' => $config['port'],
|
$table = [
|
||||||
'{dbname}' => $config['dbname'],
|
'{host}' => $config['host'],
|
||||||
'{charset}' => $config['charset'] ?? 'utf8mb4',
|
'{port}' => $config['port'],
|
||||||
];
|
'{dbname}' => $config['dbname'],
|
||||||
$connect_str = str_replace(array_keys($table), array_values($table), $connect_str);
|
'{charset}' => $config['charset'] ?? 'utf8mb4',
|
||||||
self::checkExtension();
|
];
|
||||||
|
$connect_str = str_replace(array_keys($table), array_values($table), $connect_str);
|
||||||
|
$args = [$config['username'], $config['password'], $config['options'] ?? []];
|
||||||
|
self::checkMysqlExtension();
|
||||||
|
break;
|
||||||
|
case 'sqlite':
|
||||||
|
$connect_str = 'sqlite:{dbname}';
|
||||||
|
if (FileSystem::isRelativePath($config['dbname'])) {
|
||||||
|
$config['dbname'] = zm_dir(SOURCE_ROOT_DIR . '/' . $config['dbname']);
|
||||||
|
}
|
||||||
|
$table = [
|
||||||
|
'{dbname}' => $config['dbname'],
|
||||||
|
];
|
||||||
|
$args = [];
|
||||||
|
$connect_str = str_replace(array_keys($table), array_values($table), $connect_str);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new DBException('type ' . $config['type'] . ' not supported yet');
|
||||||
|
}
|
||||||
switch (Driver::getActiveDriverClass()) {
|
switch (Driver::getActiveDriverClass()) {
|
||||||
case WorkermanDriver::class:
|
case WorkermanDriver::class:
|
||||||
self::$pools[$name] = new WorkermanObjectPool($size, PDO::class, $connect_str, $config['username'], $config['password'], $config['options'] ?? []);
|
self::$pools[$name] = new WorkermanObjectPool($size, PDO::class, $connect_str, ...$args);
|
||||||
break;
|
break;
|
||||||
case SwooleDriver::class:
|
case SwooleDriver::class:
|
||||||
self::$pools[$name] = new SwooleObjectPool($size, PDO::class, $connect_str, $config['username'], $config['password'], $config['options'] ?? []);
|
self::$pools[$name] = new SwooleObjectPool($size, PDO::class, $connect_str, ...$args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +75,10 @@ class MySQLPool
|
|||||||
*/
|
*/
|
||||||
public static function pool(string $name)
|
public static function pool(string $name)
|
||||||
{
|
{
|
||||||
if (!isset(self::$pools[$name])) {
|
if (!isset(self::$pools[$name]) && count(self::$pools) !== 1) {
|
||||||
throw new RuntimeException("Pool {$name} not found");
|
throw new RuntimeException("Pool {$name} not found");
|
||||||
}
|
}
|
||||||
return self::$pools[$name];
|
return self::$pools[$name] ?? self::$pools[array_key_first(self::$pools)];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,9 +104,9 @@ class MySQLPool
|
|||||||
/**
|
/**
|
||||||
* 检查数据库启动必要的依赖扩展,如果不符合要求则抛出异常
|
* 检查数据库启动必要的依赖扩展,如果不符合要求则抛出异常
|
||||||
*
|
*
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public static function checkExtension()
|
public static function checkMysqlExtension()
|
||||||
{
|
{
|
||||||
ob_start();
|
ob_start();
|
||||||
phpinfo(); // 这个phpinfo是有用的,不能删除
|
phpinfo(); // 这个phpinfo是有用的,不能删除
|
||||||
@@ -102,7 +121,7 @@ class MySQLPool
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (mb_strpos($v, 'pdo_mysql') === false) {
|
if (mb_strpos($v, 'pdo_mysql') === false) {
|
||||||
throw new MySQLException(zm_internal_errcode('E00028') . '未安装 mysqlnd php-mysql扩展。');
|
throw new DBException(zm_internal_errcode('E00028') . '未安装 mysqlnd php-mysql扩展。');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use Doctrine\DBAL\Query\QueryBuilder;
|
use Doctrine\DBAL\Query\QueryBuilder;
|
||||||
use ZM\Store\MySQL\MySQLException as DbException;
|
use ZM\Store\Database\DBException as DbException;
|
||||||
|
|
||||||
class MySQLQueryBuilder extends QueryBuilder
|
class DBQueryBuilder extends QueryBuilder
|
||||||
{
|
{
|
||||||
private $wrapper;
|
private $wrapper;
|
||||||
|
|
||||||
public function __construct(MySQLWrapper $wrapper)
|
public function __construct(DBWrapper $wrapper)
|
||||||
{
|
{
|
||||||
parent::__construct($wrapper->getConnection());
|
parent::__construct($wrapper->getConnection());
|
||||||
$this->wrapper = $wrapper;
|
$this->wrapper = $wrapper;
|
||||||
@@ -19,7 +19,7 @@ class MySQLQueryBuilder extends QueryBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws DbException
|
* @throws DbException
|
||||||
* @return int|MySQLStatementWrapper
|
* @return DBStatementWrapper|int
|
||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use Doctrine\DBAL\Driver\Statement;
|
use Doctrine\DBAL\Driver\Statement;
|
||||||
use Doctrine\DBAL\ParameterType;
|
use Doctrine\DBAL\ParameterType;
|
||||||
@@ -15,7 +15,7 @@ use PDO;
|
|||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
|
|
||||||
class MySQLStatement implements IteratorAggregate, Statement
|
class DBStatement implements IteratorAggregate, Statement
|
||||||
{
|
{
|
||||||
/** @var PDOStatement */
|
/** @var PDOStatement */
|
||||||
private $statement;
|
private $statement;
|
||||||
@@ -7,16 +7,16 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use Doctrine\DBAL\Driver\ResultStatement;
|
use Doctrine\DBAL\Driver\ResultStatement;
|
||||||
use Doctrine\DBAL\ForwardCompatibility\Result;
|
use Doctrine\DBAL\ForwardCompatibility\Result;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
|
|
||||||
class MySQLStatementWrapper
|
class DBStatementWrapper
|
||||||
{
|
{
|
||||||
public $stmt;
|
public ?Result $stmt;
|
||||||
|
|
||||||
public function __construct(?Result $stmt)
|
public function __construct(?Result $stmt)
|
||||||
{
|
{
|
||||||
@@ -45,7 +45,7 @@ class MySQLStatementWrapper
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
*@throws DBException
|
||||||
* @return array|false|mixed
|
* @return array|false|mixed
|
||||||
*/
|
*/
|
||||||
public function fetchNumeric()
|
public function fetchNumeric()
|
||||||
@@ -53,13 +53,13 @@ class MySQLStatementWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->stmt->fetchNumeric();
|
return $this->stmt->fetchNumeric();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
*@throws DBException
|
||||||
* @return array|false|mixed
|
* @return array|false|mixed
|
||||||
*/
|
*/
|
||||||
public function fetchAssociative()
|
public function fetchAssociative()
|
||||||
@@ -67,13 +67,13 @@ class MySQLStatementWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->stmt->fetchAssociative();
|
return $this->stmt->fetchAssociative();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
*@throws DBException
|
||||||
* @return false|mixed
|
* @return false|mixed
|
||||||
*/
|
*/
|
||||||
public function fetchOne()
|
public function fetchOne()
|
||||||
@@ -81,143 +81,143 @@ class MySQLStatementWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->stmt->fetchOne();
|
return $this->stmt->fetchOne();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchAllNumeric(): array
|
public function fetchAllNumeric(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->fetchAllNumeric();
|
return $this->stmt->fetchAllNumeric();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchAllAssociative(): array
|
public function fetchAllAssociative(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->fetchAllAssociative();
|
return $this->stmt->fetchAllAssociative();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchAllKeyValue(): array
|
public function fetchAllKeyValue(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->fetchAllKeyValue();
|
return $this->stmt->fetchAllKeyValue();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchAllAssociativeIndexed(): array
|
public function fetchAllAssociativeIndexed(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->fetchAllAssociativeIndexed();
|
return $this->stmt->fetchAllAssociativeIndexed();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchFirstColumn(): array
|
public function fetchFirstColumn(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->fetchFirstColumn();
|
return $this->stmt->fetchFirstColumn();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function iterateNumeric(): Traversable
|
public function iterateNumeric(): Traversable
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->iterateNumeric();
|
return $this->stmt->iterateNumeric();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function iterateAssociative(): Traversable
|
public function iterateAssociative(): Traversable
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->iterateAssociative();
|
return $this->stmt->iterateAssociative();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function iterateKeyValue(): Traversable
|
public function iterateKeyValue(): Traversable
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->iterateKeyValue();
|
return $this->stmt->iterateKeyValue();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function iterateAssociativeIndexed(): Traversable
|
public function iterateAssociativeIndexed(): Traversable
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->iterateAssociativeIndexed();
|
return $this->stmt->iterateAssociativeIndexed();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function iterateColumn(): Traversable
|
public function iterateColumn(): Traversable
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->stmt->iterateColumn();
|
return $this->stmt->iterateColumn();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function rowCount()
|
public function rowCount()
|
||||||
@@ -225,7 +225,7 @@ class MySQLStatementWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->stmt->rowCount();
|
return $this->stmt->rowCount();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2,11 +2,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
/**
|
namespace ZM\Store\Database;
|
||||||
* @noinspection PhpUnused
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Doctrine\DBAL\Cache\QueryCacheProfile;
|
use Doctrine\DBAL\Cache\QueryCacheProfile;
|
||||||
@@ -16,23 +12,29 @@ use Doctrine\DBAL\ParameterType;
|
|||||||
use Doctrine\DBAL\Types\Type;
|
use Doctrine\DBAL\Types\Type;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
use ZM\Store\MySQL\MySQLException as DbException;
|
|
||||||
|
|
||||||
class MySQLWrapper
|
class DBWrapper
|
||||||
{
|
{
|
||||||
/** @var Connection */
|
private Connection $connection;
|
||||||
private $connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQLWrapper constructor.
|
* DBWrapper constructor.
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function __construct(string $name)
|
public function __construct(string $name)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection = DriverManager::getConnection(['driverClass' => MySQLDriver::class, 'dbName' => $name]);
|
$db_list = config()->get('global.database');
|
||||||
|
if (isset($db_list[$name]) || count($db_list) === 1) {
|
||||||
|
if ($name === '') {
|
||||||
|
$name = array_key_first($db_list);
|
||||||
|
}
|
||||||
|
$this->connection = DriverManager::getConnection(['driverClass' => $this->getConnectionClass($db_list[$name]['type']), 'dbName' => $name]);
|
||||||
|
} else {
|
||||||
|
throw new DBException('Cannot find database config named "' . $name . '" !');
|
||||||
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +69,7 @@ class MySQLWrapper
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array|false
|
* @return array|false
|
||||||
*/
|
*/
|
||||||
public function fetchAssociative(string $query, array $params = [], array $types = [])
|
public function fetchAssociative(string $query, array $params = [], array $types = [])
|
||||||
@@ -75,13 +77,13 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchAssociative($query, $params, $types);
|
return $this->connection->fetchAssociative($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), intval($e->getCode()), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array|false
|
* @return array|false
|
||||||
*/
|
*/
|
||||||
public function fetchNumeric(string $query, array $params = [], array $types = [])
|
public function fetchNumeric(string $query, array $params = [], array $types = [])
|
||||||
@@ -89,12 +91,12 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchNumeric($query, $params, $types);
|
return $this->connection->fetchNumeric($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return false|mixed
|
* @return false|mixed
|
||||||
*/
|
*/
|
||||||
public function fetchOne(string $query, array $params = [], array $types = [])
|
public function fetchOne(string $query, array $params = [], array $types = [])
|
||||||
@@ -102,7 +104,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchOne($query, $params, $types);
|
return $this->connection->fetchOne($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,14 +118,14 @@ class MySQLWrapper
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table 表
|
* @param string $table 表
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function delete(string $table, array $criteria, array $types = []): int
|
public function delete(string $table, array $criteria, array $types = []): int
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->delete($table, $criteria, $types);
|
return $this->connection->delete($table, $criteria, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,28 +149,28 @@ class MySQLWrapper
|
|||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @param string $table 表名
|
* @param string $table 表名
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function update(string $table, array $data, array $criteria, array $types = []): int
|
public function update(string $table, array $data, array $criteria, array $types = []): int
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->update($table, $data, $criteria, $types);
|
return $this->connection->update($table, $data, $criteria, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @param string $table 表名
|
* @param string $table 表名
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function insert(string $table, array $data, array $types = []): int
|
public function insert(string $table, array $data, array $types = []): int
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->insert($table, $data, $types);
|
return $this->connection->insert($table, $data, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +199,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array<int,array<int,mixed>>
|
* @return array<int,array<int,mixed>>
|
||||||
*/
|
*/
|
||||||
public function fetchAllNumeric(string $query, array $params = [], array $types = []): array
|
public function fetchAllNumeric(string $query, array $params = [], array $types = []): array
|
||||||
@@ -205,7 +207,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchAllNumeric($query, $params, $types);
|
return $this->connection->fetchAllNumeric($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +217,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array<int,array<string,mixed>>
|
* @return array<int,array<string,mixed>>
|
||||||
*/
|
*/
|
||||||
public function fetchAllAssociative(string $query, array $params = [], array $types = []): array
|
public function fetchAllAssociative(string $query, array $params = [], array $types = []): array
|
||||||
@@ -223,7 +225,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchAllAssociative($query, $params, $types);
|
return $this->connection->fetchAllAssociative($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,14 +235,14 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function fetchAllKeyValue(string $query, array $params = [], array $types = []): array
|
public function fetchAllKeyValue(string $query, array $params = [], array $types = []): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->fetchAllKeyValue($query, $params, $types);
|
return $this->connection->fetchAllKeyValue($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +252,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array<mixed,array<string,mixed>>
|
* @return array<mixed,array<string,mixed>>
|
||||||
*/
|
*/
|
||||||
public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array
|
public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array
|
||||||
@@ -258,7 +260,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchAllAssociativeIndexed($query, $params, $types);
|
return $this->connection->fetchAllAssociativeIndexed($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +270,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return array<int,mixed>
|
* @return array<int,mixed>
|
||||||
*/
|
*/
|
||||||
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array
|
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array
|
||||||
@@ -276,7 +278,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->fetchFirstColumn($query, $params, $types);
|
return $this->connection->fetchFirstColumn($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +288,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return Traversable<int,array<int,mixed>>
|
* @return Traversable<int,array<int,mixed>>
|
||||||
*/
|
*/
|
||||||
public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable
|
public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable
|
||||||
@@ -294,7 +296,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->iterateNumeric($query, $params, $types);
|
return $this->connection->iterateNumeric($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +306,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return Traversable<int,array<string,mixed>>
|
* @return Traversable<int,array<string,mixed>>
|
||||||
*/
|
*/
|
||||||
public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable
|
public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable
|
||||||
@@ -312,7 +314,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->iterateAssociative($query, $params, $types);
|
return $this->connection->iterateAssociative($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,7 +324,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return Traversable<mixed,mixed>
|
* @return Traversable<mixed,mixed>
|
||||||
*/
|
*/
|
||||||
public function iterateKeyValue(string $query, array $params = [], array $types = []): Traversable
|
public function iterateKeyValue(string $query, array $params = [], array $types = []): Traversable
|
||||||
@@ -330,7 +332,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->iterateKeyValue($query, $params, $types);
|
return $this->connection->iterateKeyValue($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +342,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
* @param array<int, int|string>|array<string, int|string> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return Traversable<mixed,array<string,mixed>>
|
* @return Traversable<mixed,array<string,mixed>>
|
||||||
*/
|
*/
|
||||||
public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable
|
public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable
|
||||||
@@ -348,7 +350,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->iterateAssociativeIndexed($query, $params, $types);
|
return $this->connection->iterateAssociativeIndexed($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,7 +360,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return Traversable<int,mixed>
|
* @return Traversable<int,mixed>
|
||||||
*/
|
*/
|
||||||
public function iterateColumn(string $query, array $params = [], array $types = []): Traversable
|
public function iterateColumn(string $query, array $params = [], array $types = []): Traversable
|
||||||
@@ -366,7 +368,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->iterateColumn($query, $params, $types);
|
return $this->connection->iterateColumn($query, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,15 +378,16 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function executeQuery(string $sql, array $params = [], array $types = [], ?QueryCacheProfile $qcp = null): MySQLStatementWrapper
|
public function executeQuery(string $sql, array $params = [], array $types = [], ?QueryCacheProfile $qcp = null): DBStatementWrapper
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$query = $this->connection->executeQuery($sql, $params, $types, $qcp);
|
$query = $this->connection->executeQuery($sql, $params, $types, $qcp);
|
||||||
return new MySQLStatementWrapper($query);
|
return new DBStatementWrapper($query);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw $e;
|
||||||
|
// throw new DBException($e->getMessage(), intval($e->getCode()), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,15 +396,15 @@ class MySQLWrapper
|
|||||||
* @param string $sql SQL query
|
* @param string $sql SQL query
|
||||||
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
* @param array<int, mixed>|array<string, mixed> $params Query parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function executeCacheQuery(string $sql, array $params, array $types, QueryCacheProfile $qcp): MySQLStatementWrapper
|
public function executeCacheQuery(string $sql, array $params, array $types, QueryCacheProfile $qcp): DBStatementWrapper
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$query = $this->connection->executeCacheQuery($sql, $params, $types, $qcp);
|
$query = $this->connection->executeCacheQuery($sql, $params, $types, $qcp);
|
||||||
return new MySQLStatementWrapper($query);
|
return new DBStatementWrapper($query);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,7 +414,7 @@ class MySQLWrapper
|
|||||||
* @param array<int, mixed>|array<string, mixed> $params Statement parameters
|
* @param array<int, mixed>|array<string, mixed> $params Statement parameters
|
||||||
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
* @param array<int, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
|
||||||
*
|
*
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return int|string the number of affected rows
|
* @return int|string the number of affected rows
|
||||||
*/
|
*/
|
||||||
public function executeStatement(string $sql, array $params = [], array $types = [])
|
public function executeStatement(string $sql, array $params = [], array $types = [])
|
||||||
@@ -419,7 +422,7 @@ class MySQLWrapper
|
|||||||
try {
|
try {
|
||||||
return $this->connection->executeStatement($sql, $params, $types);
|
return $this->connection->executeStatement($sql, $params, $types);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,7 +446,7 @@ class MySQLWrapper
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* overwrite method to $this->connection->transactional()
|
* overwrite method to $this->connection->transactional()
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function transactional(Closure $func)
|
public function transactional(Closure $func)
|
||||||
@@ -455,20 +458,20 @@ class MySQLWrapper
|
|||||||
return $res;
|
return $res;
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$this->rollBack();
|
$this->rollBack();
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function setNestTransactionsWithSavepoints(bool $nest_transactions_with_savepoints)
|
public function setNestTransactionsWithSavepoints(bool $nest_transactions_with_savepoints)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection->setNestTransactionsWithSavepoints($nest_transactions_with_savepoints);
|
$this->connection->setNestTransactionsWithSavepoints($nest_transactions_with_savepoints);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,108 +493,123 @@ class MySQLWrapper
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function commit(): bool
|
public function commit(): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->commit();
|
return $this->connection->commit();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function rollBack(): bool
|
public function rollBack(): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->rollBack();
|
return $this->connection->rollBack();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @param string $savepoint the name of the savepoint to create
|
* @param string $savepoint the name of the savepoint to create
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function createSavepoint(string $savepoint)
|
public function createSavepoint(string $savepoint)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection->createSavepoint($savepoint);
|
$this->connection->createSavepoint($savepoint);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @param string $savepoint the name of the savepoint to release
|
* @param string $savepoint the name of the savepoint to release
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function releaseSavepoint(string $savepoint)
|
public function releaseSavepoint(string $savepoint)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection->releaseSavepoint($savepoint);
|
$this->connection->releaseSavepoint($savepoint);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @param string $savepoint the name of the savepoint to rollback to
|
* @param string $savepoint the name of the savepoint to rollback to
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function rollbackSavepoint(string $savepoint)
|
public function rollbackSavepoint(string $savepoint)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection->rollbackSavepoint($savepoint);
|
$this->connection->rollbackSavepoint($savepoint);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function setRollbackOnly()
|
public function setRollbackOnly()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->connection->setRollbackOnly();
|
$this->connection->setRollbackOnly();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrapper method
|
* wrapper method
|
||||||
* @throws DbException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public function isRollbackOnly(): bool
|
public function isRollbackOnly(): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->connection->isRollbackOnly();
|
return $this->connection->isRollbackOnly();
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new DbException($e->getMessage(), $e->getCode(), $e);
|
throw new DBException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* overwrite method to $this->connection->createQueryBuilder
|
* overwrite method to $this->connection->createQueryBuilder
|
||||||
*/
|
*/
|
||||||
public function createQueryBuilder(): MySQLQueryBuilder
|
public function createQueryBuilder(): DBQueryBuilder
|
||||||
{
|
{
|
||||||
return new MySQLQueryBuilder($this);
|
return new DBQueryBuilder($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConnection(): Connection
|
public function getConnection(): Connection
|
||||||
{
|
{
|
||||||
return $this->connection;
|
return $this->connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws DBException
|
||||||
|
*/
|
||||||
|
private function getConnectionClass(string $type): string
|
||||||
|
{
|
||||||
|
switch ($type) {
|
||||||
|
case 'mysql':
|
||||||
|
return MySQLDriver::class;
|
||||||
|
case 'sqlite':
|
||||||
|
return SQLiteDriver::class;
|
||||||
|
default:
|
||||||
|
throw new DBException('Unknown database type: ' . $type);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
|
||||||
|
|
||||||
class MySQLException extends \ZM\Exception\ZMException
|
|
||||||
{
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user