refactor MySQL component to support SQLite at the same time

This commit is contained in:
crazywhalecc
2022-08-27 19:45:23 +08:00
parent e9b6965678
commit 085472a12c
8 changed files with 199 additions and 160 deletions

View File

@@ -4,14 +4,14 @@
declare(strict_types=1);
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\ParameterType;
use PDO;
use PDOException;
class MySQLConnection implements Connection
class DBConnection implements Connection
{
/** @var PDO */
private $conn;
@@ -21,20 +21,20 @@ class MySQLConnection implements Connection
public function __construct($params)
{
logger()->debug('Constructing...');
$this->conn = MySQLPool::pool($params['dbName'])->get();
$this->conn = DBPool::pool($params['dbName'])->get();
$this->pool_name = $params['dbName'];
}
public function __destruct()
{
logger()->debug('Destructing');
MySQLPool::pool($this->pool_name)->put($this->conn);
DBPool::pool($this->pool_name)->put($this->conn);
}
/**
* @param mixed $sql
* @param mixed $options
* @throws MySQLException
* @param mixed $sql
* @param mixed $options
* @throws DBException
*/
public function prepare($sql, $options = [])
{
@@ -43,13 +43,13 @@ class MySQLConnection implements Connection
$statement = $this->conn->prepare($sql, $options);
assert($statement !== false);
} 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)
{
@@ -57,9 +57,9 @@ class MySQLConnection implements Connection
$statement = $this->conn->query(...$args);
assert($statement !== false);
} 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)
@@ -68,8 +68,8 @@ class MySQLConnection implements Connection
}
/**
* @param mixed $sql
* @throws MySQLException
* @param mixed $sql
* @throws DBException
*/
public function exec($sql)
{
@@ -79,20 +79,20 @@ class MySQLConnection implements Connection
assert($statement !== false);
return $statement;
} catch (PDOException $exception) {
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
throw new DBException($exception->getMessage(), 0, $exception);
}
}
/**
* @param null|mixed $name
* @throws MySQLException
* @param null|mixed $name
* @throws DBException
*/
public function lastInsertId($name = null)
{
try {
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
} catch (PDOException $exception) {
throw new MySQLException($exception->getMessage(), $exception->getCode(), $exception);
throw new DBException($exception->getMessage(), 0, $exception);
}
}

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace ZM\Store\Database;
use ZM\Exception\ZMException;
class DBException extends ZMException
{
}

View File

@@ -4,7 +4,7 @@
declare(strict_types=1);
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
use OneBot\Driver\Driver;
use OneBot\Driver\Interfaces\PoolInterface;
@@ -14,8 +14,9 @@ use OneBot\Driver\Workerman\ObjectPool as WorkermanObjectPool;
use OneBot\Driver\Workerman\WorkermanDriver;
use PDO;
use RuntimeException;
use ZM\Store\FileSystem;
class MySQLPool
class DBPool
{
/**
* @var array<string, SwooleObjectPool|WorkermanObjectPool> 连接池列表
@@ -25,26 +26,44 @@ class MySQLPool
/**
* 通过配置文件创建一个 MySQL 连接池
*
* @throws MySQLException
* @throws DBException
*/
public static function create(string $name, array $config)
{
$size = $config['pool_size'] ?? 128;
$connect_str = 'mysql:host={host};port={port};dbname={dbname};charset={charset}';
$table = [
'{host}' => $config['host'],
'{port}' => $config['port'],
'{dbname}' => $config['dbname'],
'{charset}' => $config['charset'] ?? 'utf8mb4',
];
$connect_str = str_replace(array_keys($table), array_values($table), $connect_str);
self::checkExtension();
$size = $config['pool_size'] ?? 64;
switch ($config['type']) {
case 'mysql':
$connect_str = 'mysql:host={host};port={port};dbname={dbname};charset={charset}';
$table = [
'{host}' => $config['host'],
'{port}' => $config['port'],
'{dbname}' => $config['dbname'],
'{charset}' => $config['charset'] ?? 'utf8mb4',
];
$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()) {
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;
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)
{
if (!isset(self::$pools[$name])) {
if (!isset(self::$pools[$name]) && count(self::$pools) !== 1) {
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();
phpinfo(); // 这个phpinfo是有用的不能删除
@@ -102,7 +121,7 @@ class MySQLPool
continue;
}
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扩展。');
}
}
}

View File

@@ -2,16 +2,16 @@
declare(strict_types=1);
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
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;
public function __construct(MySQLWrapper $wrapper)
public function __construct(DBWrapper $wrapper)
{
parent::__construct($wrapper->getConnection());
$this->wrapper = $wrapper;
@@ -19,7 +19,7 @@ class MySQLQueryBuilder extends QueryBuilder
/**
* @throws DbException
* @return int|MySQLStatementWrapper
* @return DBStatementWrapper|int
*/
public function execute()
{

View File

@@ -6,7 +6,7 @@
declare(strict_types=1);
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType;
@@ -15,7 +15,7 @@ use PDO;
use PDOStatement;
use Traversable;
class MySQLStatement implements IteratorAggregate, Statement
class DBStatement implements IteratorAggregate, Statement
{
/** @var PDOStatement */
private $statement;

View File

@@ -7,16 +7,16 @@
declare(strict_types=1);
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\ForwardCompatibility\Result;
use Throwable;
use Traversable;
class MySQLStatementWrapper
class DBStatementWrapper
{
public $stmt;
public ?Result $stmt;
public function __construct(?Result $stmt)
{
@@ -45,7 +45,7 @@ class MySQLStatementWrapper
/**
* wrapper method
* @throws MySQLException
*@throws DBException
* @return array|false|mixed
*/
public function fetchNumeric()
@@ -53,13 +53,13 @@ class MySQLStatementWrapper
try {
return $this->stmt->fetchNumeric();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
*@throws DBException
* @return array|false|mixed
*/
public function fetchAssociative()
@@ -67,13 +67,13 @@ class MySQLStatementWrapper
try {
return $this->stmt->fetchAssociative();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
*@throws DBException
* @return false|mixed
*/
public function fetchOne()
@@ -81,143 +81,143 @@ class MySQLStatementWrapper
try {
return $this->stmt->fetchOne();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function fetchAllNumeric(): array
{
try {
return $this->stmt->fetchAllNumeric();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function fetchAllAssociative(): array
{
try {
return $this->stmt->fetchAllAssociative();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function fetchAllKeyValue(): array
{
try {
return $this->stmt->fetchAllKeyValue();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function fetchAllAssociativeIndexed(): array
{
try {
return $this->stmt->fetchAllAssociativeIndexed();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function fetchFirstColumn(): array
{
try {
return $this->stmt->fetchFirstColumn();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function iterateNumeric(): Traversable
{
try {
return $this->stmt->iterateNumeric();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function iterateAssociative(): Traversable
{
try {
return $this->stmt->iterateAssociative();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function iterateKeyValue(): Traversable
{
try {
return $this->stmt->iterateKeyValue();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function iterateAssociativeIndexed(): Traversable
{
try {
return $this->stmt->iterateAssociativeIndexed();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
*/
public function iterateColumn(): Traversable
{
try {
return $this->stmt->iterateColumn();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws MySQLException
* @throws DBException
* @return int
*/
public function rowCount()
@@ -225,7 +225,7 @@ class MySQLStatementWrapper
try {
return $this->stmt->rowCount();
} catch (Throwable $e) {
throw new MySQLException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}

View File

@@ -2,11 +2,7 @@
declare(strict_types=1);
/**
* @noinspection PhpUnused
*/
namespace ZM\Store\MySQL;
namespace ZM\Store\Database;
use Closure;
use Doctrine\DBAL\Cache\QueryCacheProfile;
@@ -16,23 +12,29 @@ use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use Throwable;
use Traversable;
use ZM\Store\MySQL\MySQLException as DbException;
class MySQLWrapper
class DBWrapper
{
/** @var Connection */
private $connection;
private Connection $connection;
/**
* MySQLWrapper constructor.
* @throws DbException
* DBWrapper constructor.
* @throws DBException
*/
public function __construct(string $name)
{
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) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
@@ -67,7 +69,7 @@ class MySQLWrapper
/**
* wrapper method
* @throws DbException
* @throws DBException
* @return array|false
*/
public function fetchAssociative(string $query, array $params = [], array $types = [])
@@ -75,13 +77,13 @@ class MySQLWrapper
try {
return $this->connection->fetchAssociative($query, $params, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), intval($e->getCode()), $e);
}
}
/**
* wrapper method
* @throws DbException
* @throws DBException
* @return array|false
*/
public function fetchNumeric(string $query, array $params = [], array $types = [])
@@ -89,12 +91,12 @@ class MySQLWrapper
try {
return $this->connection->fetchNumeric($query, $params, $types);
} 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
*/
public function fetchOne(string $query, array $params = [], array $types = [])
@@ -102,7 +104,7 @@ class MySQLWrapper
try {
return $this->connection->fetchOne($query, $params, $types);
} 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
* @throws DbException
* @throws DBException
*/
public function delete(string $table, array $criteria, array $types = []): int
{
try {
return $this->connection->delete($table, $criteria, $types);
} 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
* @param string $table 表名
* @throws DbException
* @throws DBException
*/
public function update(string $table, array $data, array $criteria, array $types = []): int
{
try {
return $this->connection->update($table, $data, $criteria, $types);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $table 表名
* @throws DbException
* @throws DBException
*/
public function insert(string $table, array $data, array $types = []): int
{
try {
return $this->connection->insert($table, $data, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return array<int,array<int,mixed>>
*/
public function fetchAllNumeric(string $query, array $params = [], array $types = []): array
@@ -205,7 +207,7 @@ class MySQLWrapper
try {
return $this->connection->fetchAllNumeric($query, $params, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return array<int,array<string,mixed>>
*/
public function fetchAllAssociative(string $query, array $params = [], array $types = []): array
@@ -223,7 +225,7 @@ class MySQLWrapper
try {
return $this->connection->fetchAllAssociative($query, $params, $types);
} 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, int|string>|array<string, int|string> $types Parameter types
*
* @throws DbException
* @throws DBException
*/
public function fetchAllKeyValue(string $query, array $params = [], array $types = []): array
{
try {
return $this->connection->fetchAllKeyValue($query, $params, $types);
} 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, int|string>|array<string, int|string> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return array<mixed,array<string,mixed>>
*/
public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array
@@ -258,7 +260,7 @@ class MySQLWrapper
try {
return $this->connection->fetchAllAssociativeIndexed($query, $params, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return array<int,mixed>
*/
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array
@@ -276,7 +278,7 @@ class MySQLWrapper
try {
return $this->connection->fetchFirstColumn($query, $params, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return Traversable<int,array<int,mixed>>
*/
public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable
@@ -294,7 +296,7 @@ class MySQLWrapper
try {
return $this->connection->iterateNumeric($query, $params, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return Traversable<int,array<string,mixed>>
*/
public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable
@@ -312,7 +314,7 @@ class MySQLWrapper
try {
return $this->connection->iterateAssociative($query, $params, $types);
} 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, int|string>|array<string, int|string> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return Traversable<mixed,mixed>
*/
public function iterateKeyValue(string $query, array $params = [], array $types = []): Traversable
@@ -330,7 +332,7 @@ class MySQLWrapper
try {
return $this->connection->iterateKeyValue($query, $params, $types);
} 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, int|string>|array<string, int|string> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return Traversable<mixed,array<string,mixed>>
*/
public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable
@@ -348,7 +350,7 @@ class MySQLWrapper
try {
return $this->connection->iterateAssociativeIndexed($query, $params, $types);
} 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, null|int|string|Type>|array<string, null|int|string|Type> $types Parameter types
*
* @throws DbException
* @throws DBException
* @return Traversable<int,mixed>
*/
public function iterateColumn(string $query, array $params = [], array $types = []): Traversable
@@ -366,7 +368,7 @@ class MySQLWrapper
try {
return $this->connection->iterateColumn($query, $params, $types);
} 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, 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 {
$query = $this->connection->executeQuery($sql, $params, $types, $qcp);
return new MySQLStatementWrapper($query);
return new DBStatementWrapper($query);
} 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 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
* @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 {
$query = $this->connection->executeCacheQuery($sql, $params, $types, $qcp);
return new MySQLStatementWrapper($query);
return new DBStatementWrapper($query);
} 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, 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
*/
public function executeStatement(string $sql, array $params = [], array $types = [])
@@ -419,7 +422,7 @@ class MySQLWrapper
try {
return $this->connection->executeStatement($sql, $params, $types);
} 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()
* @throws DbException
* @throws DBException
* @return mixed
*/
public function transactional(Closure $func)
@@ -455,20 +458,20 @@ class MySQLWrapper
return $res;
} catch (Throwable $e) {
$this->rollBack();
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws DbException
* @throws DBException
*/
public function setNestTransactionsWithSavepoints(bool $nest_transactions_with_savepoints)
{
try {
$this->connection->setNestTransactionsWithSavepoints($nest_transactions_with_savepoints);
} 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
* @throws DbException
* @throws DBException
*/
public function commit(): bool
{
try {
return $this->connection->commit();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws DbException
* @throws DBException
*/
public function rollBack(): bool
{
try {
return $this->connection->rollBack();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $savepoint the name of the savepoint to create
* @throws DbException
* @throws DBException
*/
public function createSavepoint(string $savepoint)
{
try {
$this->connection->createSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $savepoint the name of the savepoint to release
* @throws DbException
* @throws DBException
*/
public function releaseSavepoint(string $savepoint)
{
try {
$this->connection->releaseSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @param string $savepoint the name of the savepoint to rollback to
* @throws DbException
* @throws DBException
*/
public function rollbackSavepoint(string $savepoint)
{
try {
$this->connection->rollbackSavepoint($savepoint);
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws DbException
* @throws DBException
*/
public function setRollbackOnly()
{
try {
$this->connection->setRollbackOnly();
} catch (Throwable $e) {
throw new DbException($e->getMessage(), $e->getCode(), $e);
throw new DBException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* wrapper method
* @throws DbException
* @throws DBException
*/
public function isRollbackOnly(): bool
{
try {
return $this->connection->isRollbackOnly();
} 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
*/
public function createQueryBuilder(): MySQLQueryBuilder
public function createQueryBuilder(): DBQueryBuilder
{
return new MySQLQueryBuilder($this);
return new DBQueryBuilder($this);
}
public function getConnection(): 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);
}
}
}

View File

@@ -1,9 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Store\MySQL;
class MySQLException extends \ZM\Exception\ZMException
{
}