mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 16:15:34 +08:00
2
.github/workflows/integration-test.yml
vendored
2
.github/workflows/integration-test.yml
vendored
@@ -36,7 +36,7 @@ jobs:
|
|||||||
uses: "shivammathur/setup-php@v2"
|
uses: "shivammathur/setup-php@v2"
|
||||||
with:
|
with:
|
||||||
php-version: ${{ matrix.php-versions }}
|
php-version: ${{ matrix.php-versions }}
|
||||||
extensions: swoole, posix, json, mbstring
|
extensions: swoole, posix, json, mbstring, pdo, sqlite3, pdo_sqlite
|
||||||
env:
|
env:
|
||||||
SWOOLE_CONFIGURE_OPTS: --enable-openssl
|
SWOOLE_CONFIGURE_OPTS: --enable-openssl
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,17 @@ $ob_event_provider = new EventProvider();
|
|||||||
|
|
||||||
// 注册一个最低级别的 WorkerStart 事件,用于在框架的事件初始化之后开始运行 PHPUnit
|
// 注册一个最低级别的 WorkerStart 事件,用于在框架的事件初始化之后开始运行 PHPUnit
|
||||||
ob_event_provider()->addEventListener(WorkerStartEvent::getName(), function () {
|
ob_event_provider()->addEventListener(WorkerStartEvent::getName(), function () {
|
||||||
|
register_shutdown_function(function () {
|
||||||
|
$error = error_get_last();
|
||||||
|
// 下面这段代码的作用就是,不是错误引发的退出时照常退出即可
|
||||||
|
if (($error['type'] ?? 0) != 0) {
|
||||||
|
logger()->emergency(zm_internal_errcode('E00027') . 'Internal fatal error: ' . $error['message'] . ' at ' . $error['file'] . "({$error['line']})");
|
||||||
|
}
|
||||||
|
ob_dump($error);
|
||||||
|
ob_dump(func_get_args());
|
||||||
|
ob_dump(debug_backtrace());
|
||||||
|
Framework::getInstance()->stop();
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
// 不退出,而是返回 code
|
// 不退出,而是返回 code
|
||||||
$retcode = Command::main(false);
|
$retcode = Command::main(false);
|
||||||
@@ -43,7 +54,7 @@ ob_event_provider()->addEventListener(WorkerStartEvent::getName(), function () {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$options = ServerStartCommand::exportOptionArray();
|
$options = ServerStartCommand::exportOptionArray();
|
||||||
$options['driver'] = DIRECTORY_SEPARATOR === '/' ? 'swoole' : 'workerman';
|
$options['driver'] = 'workerman';
|
||||||
$options['worker-num'] = 1;
|
$options['worker-num'] = 1;
|
||||||
$options['private-mode'] = true;
|
$options['private-mode'] = true;
|
||||||
(new Framework($options))->init()->start();
|
(new Framework($options))->init()->start();
|
||||||
|
|||||||
@@ -26,8 +26,7 @@
|
|||||||
"symfony/polyfill-ctype": "^1.19",
|
"symfony/polyfill-ctype": "^1.19",
|
||||||
"symfony/polyfill-mbstring": "^1.19",
|
"symfony/polyfill-mbstring": "^1.19",
|
||||||
"symfony/polyfill-php80": "^1.16",
|
"symfony/polyfill-php80": "^1.16",
|
||||||
"symfony/routing": "~6.0 || ~5.0 || ~4.0",
|
"symfony/routing": "~6.0 || ~5.0 || ~4.0"
|
||||||
"zhamao/logger": "dev-master"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"brainmaestro/composer-git-hooks": "^2.8",
|
"brainmaestro/composer-git-hooks": "^2.8",
|
||||||
|
|||||||
@@ -80,11 +80,18 @@ $config['file_server'] = [
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
/* MySQL 数据库连接配置,框架将自动生成连接池,支持多个连接池 */
|
/* MySQL 和 SQLite3 数据库连接配置,框架将自动生成连接池,支持多个连接池 */
|
||||||
$config['mysql'] = [
|
$config['database'] = [
|
||||||
[
|
'sqlite_db1' => [
|
||||||
'pool_name' => '', // 默认只有一个空名称的连接池,如果需要多个连接池,请复制此段配置并修改参数和名称
|
'enable' => false,
|
||||||
'host' => '', // 填写数据库服务器地址后才会创建数据库连接
|
'type' => 'sqlite',
|
||||||
|
'dbname' => 'a.db',
|
||||||
|
'pool_size' => 10,
|
||||||
|
],
|
||||||
|
'zm' => [
|
||||||
|
'enable' => false,
|
||||||
|
'type' => 'mysql',
|
||||||
|
'host' => '127.0.0.1', // 填写数据库服务器地址后才会创建数据库连接
|
||||||
'port' => 3306,
|
'port' => 3306,
|
||||||
'username' => 'root',
|
'username' => 'root',
|
||||||
'password' => 'ZhamaoTEST',
|
'password' => 'ZhamaoTEST',
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ use ZM\Container\ContainerInterface;
|
|||||||
use ZM\Context\Context;
|
use ZM\Context\Context;
|
||||||
use ZM\Logger\ConsoleLogger;
|
use ZM\Logger\ConsoleLogger;
|
||||||
use ZM\Middleware\MiddlewareHandler;
|
use ZM\Middleware\MiddlewareHandler;
|
||||||
use ZM\Store\MySQL\MySQLException;
|
use ZM\Store\Database\DBException;
|
||||||
use ZM\Store\MySQL\MySQLWrapper;
|
use ZM\Store\Database\DBWrapper;
|
||||||
|
|
||||||
// 防止重复引用引发报错
|
// 防止重复引用引发报错
|
||||||
if (function_exists('zm_internal_errcode')) {
|
if (function_exists('zm_internal_errcode')) {
|
||||||
@@ -172,21 +172,21 @@ function app(string $abstract = null, array $parameters = [])
|
|||||||
/**
|
/**
|
||||||
* 获取 MySQL 调用的类
|
* 获取 MySQL 调用的类
|
||||||
*
|
*
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
function mysql(string $name = '')
|
function db(string $name = '')
|
||||||
{
|
{
|
||||||
return new MySQLWrapper($name);
|
return new DBWrapper($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取构建 MySQL 的类
|
* 获取构建 MySQL 的类
|
||||||
*
|
*
|
||||||
* @throws MySQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
function mysql_builder(string $name = '')
|
function sql_builder(string $name = '')
|
||||||
{
|
{
|
||||||
return (new MySQLWrapper($name))->createQueryBuilder();
|
return (new DBWrapper($name))->createQueryBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,12 +12,11 @@ use ZM\Annotation\AnnotationMap;
|
|||||||
use ZM\Annotation\AnnotationParser;
|
use ZM\Annotation\AnnotationParser;
|
||||||
use ZM\Annotation\Framework\Init;
|
use ZM\Annotation\Framework\Init;
|
||||||
use ZM\Container\ContainerServicesProvider;
|
use ZM\Container\ContainerServicesProvider;
|
||||||
use ZM\Exception\ConfigException;
|
|
||||||
use ZM\Exception\ZMKnownException;
|
use ZM\Exception\ZMKnownException;
|
||||||
use ZM\Framework;
|
use ZM\Framework;
|
||||||
use ZM\Process\ProcessStateManager;
|
use ZM\Process\ProcessStateManager;
|
||||||
use ZM\Store\MySQL\MySQLException;
|
use ZM\Store\Database\DBException;
|
||||||
use ZM\Store\MySQL\MySQLPool;
|
use ZM\Store\Database\DBPool;
|
||||||
use ZM\Utils\ZMUtil;
|
use ZM\Utils\ZMUtil;
|
||||||
|
|
||||||
class WorkerEventListener
|
class WorkerEventListener
|
||||||
@@ -99,6 +98,10 @@ class WorkerEventListener
|
|||||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||||
ProcessStateManager::removeProcessState(ZM_PROCESS_WORKER, ProcessManager::getProcessId());
|
ProcessStateManager::removeProcessState(ZM_PROCESS_WORKER, ProcessManager::getProcessId());
|
||||||
}
|
}
|
||||||
|
// 清空 MySQL 的连接池
|
||||||
|
foreach (DBPool::getAllPools() as $name => $pool) {
|
||||||
|
DBPool::destroyPool($name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,29 +152,21 @@ class WorkerEventListener
|
|||||||
*
|
*
|
||||||
* TODO:未来新增其他db的连接池
|
* TODO:未来新增其他db的连接池
|
||||||
*
|
*
|
||||||
* @throws ConfigException
|
* @throws DBException
|
||||||
* @throws MySQLException
|
|
||||||
*/
|
*/
|
||||||
private function initConnectionPool()
|
private function initConnectionPool()
|
||||||
{
|
{
|
||||||
// 清空 MySQL 的连接池
|
// 清空 MySQL 的连接池
|
||||||
foreach (MySQLPool::getAllPools() as $name => $pool) {
|
foreach (DBPool::getAllPools() as $name => $pool) {
|
||||||
MySQLPool::destroyPool($name);
|
DBPool::destroyPool($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取 MySQL 配置文件
|
// 读取 MySQL 配置文件
|
||||||
$conf = config('global.mysql');
|
$conf = config('global.database');
|
||||||
if (is_array($conf) && !is_assoc_array($conf)) {
|
// 如果有多个数据库连接,则遍历
|
||||||
// 如果有多个数据库连接,则遍历
|
foreach ($conf as $name => $conn_conf) {
|
||||||
foreach ($conf as $conn_conf) {
|
if (($conn_conf['enable'] ?? true) !== false) {
|
||||||
if ($conn_conf['host'] !== '') {
|
DBPool::create($name, $conn_conf);
|
||||||
MySQLPool::create($conn_conf['pool_name'], $conn_conf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif (is_assoc_array($conf)) {
|
|
||||||
// 这种情况也支持,但是不推荐
|
|
||||||
if ($conf['host'] !== '') {
|
|
||||||
MySQLPool::create($conf['pool_name'], $conf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ZM\Store\MySQL;
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
use Doctrine\DBAL\Driver as DoctrineDriver;
|
use Doctrine\DBAL\Driver as DoctrineDriver;
|
||||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||||
@@ -13,10 +13,10 @@ class MySQLDriver implements DoctrineDriver
|
|||||||
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
|
||||||
{
|
{
|
||||||
logger()->debug('Requiring new connection');
|
logger()->debug('Requiring new connection');
|
||||||
return new MySQLConnection($params);
|
return new DBConnection($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDatabasePlatform(): MySqlPlatform
|
public function getDatabasePlatform()
|
||||||
{
|
{
|
||||||
return new MySqlPlatform();
|
return new MySqlPlatform();
|
||||||
}
|
}
|
||||||
@@ -33,14 +33,8 @@ class MySQLDriver implements DoctrineDriver
|
|||||||
|
|
||||||
public function getDatabase($conn)
|
public function getDatabase($conn)
|
||||||
{
|
{
|
||||||
$conf = config('global.mysql');
|
if ($conn instanceof DBConnection) {
|
||||||
|
return config('database')[$conn->getPoolName()]['dbname'] ?? '';
|
||||||
if ($conn instanceof MySQLConnection) {
|
|
||||||
foreach ($conf as $v) {
|
|
||||||
if (($v['name'] ?? $v['dbname']) === $conn->getPoolName()) {
|
|
||||||
return $v['dbname'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
41
src/ZM/Store/Database/SQLiteDriver.php
Normal file
41
src/ZM/Store/Database/SQLiteDriver.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ZM\Store\Database;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Driver as DoctrineDriver;
|
||||||
|
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
||||||
|
use Doctrine\DBAL\Schema\SqliteSchemaManager;
|
||||||
|
|
||||||
|
class SQLiteDriver implements DoctrineDriver
|
||||||
|
{
|
||||||
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
|
||||||
|
{
|
||||||
|
logger()->debug('Requiring new connection');
|
||||||
|
return new DBConnection($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDatabasePlatform()
|
||||||
|
{
|
||||||
|
return new SqlitePlatform();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSchemaManager($conn)
|
||||||
|
{
|
||||||
|
return new SqliteSchemaManager($conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'pdo_sqlite_pool';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDatabase($conn)
|
||||||
|
{
|
||||||
|
if ($conn instanceof DBConnection) {
|
||||||
|
return config('database')[$conn->getPoolName()]['dbname'] ?? '';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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