From dd01653da9f89bc5042b347e6f352b49a89de13c Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 27 Aug 2022 19:47:45 +0800 Subject: [PATCH] add SQLiteDriver.php and change MySQLDriver.php --- src/Globals/global_functions.php | 16 ++++---- src/ZM/Event/Listener/WorkerEventListener.php | 33 +++++++-------- .../Store/{MySQL => Database}/MySQLDriver.php | 16 +++----- src/ZM/Store/Database/SQLiteDriver.php | 41 +++++++++++++++++++ 4 files changed, 68 insertions(+), 38 deletions(-) rename src/ZM/Store/{MySQL => Database}/MySQLDriver.php (63%) create mode 100644 src/ZM/Store/Database/SQLiteDriver.php diff --git a/src/Globals/global_functions.php b/src/Globals/global_functions.php index 862741d1..a7288823 100644 --- a/src/Globals/global_functions.php +++ b/src/Globals/global_functions.php @@ -13,8 +13,8 @@ use ZM\Container\ContainerInterface; use ZM\Context\Context; use ZM\Logger\ConsoleLogger; use ZM\Middleware\MiddlewareHandler; -use ZM\Store\MySQL\MySQLException; -use ZM\Store\MySQL\MySQLWrapper; +use ZM\Store\Database\DBException; +use ZM\Store\Database\DBWrapper; // 防止重复引用引发报错 if (function_exists('zm_internal_errcode')) { @@ -172,21 +172,21 @@ function app(string $abstract = null, array $parameters = []) /** * 获取 MySQL 调用的类 * - * @throws MySQLException + * @throws DBException */ -function mysql(string $name = '') +function db(string $name = '') { - return new MySQLWrapper($name); + return new DBWrapper($name); } /** * 获取构建 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(); } /** diff --git a/src/ZM/Event/Listener/WorkerEventListener.php b/src/ZM/Event/Listener/WorkerEventListener.php index 2df8156b..e9e219c9 100644 --- a/src/ZM/Event/Listener/WorkerEventListener.php +++ b/src/ZM/Event/Listener/WorkerEventListener.php @@ -12,12 +12,11 @@ use ZM\Annotation\AnnotationMap; use ZM\Annotation\AnnotationParser; use ZM\Annotation\Framework\Init; use ZM\Container\ContainerServicesProvider; -use ZM\Exception\ConfigException; use ZM\Exception\ZMKnownException; use ZM\Framework; use ZM\Process\ProcessStateManager; -use ZM\Store\MySQL\MySQLException; -use ZM\Store\MySQL\MySQLPool; +use ZM\Store\Database\DBException; +use ZM\Store\Database\DBPool; use ZM\Utils\ZMUtil; class WorkerEventListener @@ -99,6 +98,10 @@ class WorkerEventListener if (DIRECTORY_SEPARATOR !== '\\') { 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的连接池 * - * @throws ConfigException - * @throws MySQLException + * @throws DBException */ private function initConnectionPool() { // 清空 MySQL 的连接池 - foreach (MySQLPool::getAllPools() as $name => $pool) { - MySQLPool::destroyPool($name); + foreach (DBPool::getAllPools() as $name => $pool) { + DBPool::destroyPool($name); } // 读取 MySQL 配置文件 - $conf = config('global.mysql'); - if (is_array($conf) && !is_assoc_array($conf)) { - // 如果有多个数据库连接,则遍历 - foreach ($conf as $conn_conf) { - if ($conn_conf['host'] !== '') { - MySQLPool::create($conn_conf['pool_name'], $conn_conf); - } - } - } elseif (is_assoc_array($conf)) { - // 这种情况也支持,但是不推荐 - if ($conf['host'] !== '') { - MySQLPool::create($conf['pool_name'], $conf); + $conf = config('global.database'); + // 如果有多个数据库连接,则遍历 + foreach ($conf as $name => $conn_conf) { + if (($conn_conf['enable'] ?? true) !== false) { + DBPool::create($name, $conn_conf); } } } diff --git a/src/ZM/Store/MySQL/MySQLDriver.php b/src/ZM/Store/Database/MySQLDriver.php similarity index 63% rename from src/ZM/Store/MySQL/MySQLDriver.php rename to src/ZM/Store/Database/MySQLDriver.php index 68815d0b..4c6e3c9c 100644 --- a/src/ZM/Store/MySQL/MySQLDriver.php +++ b/src/ZM/Store/Database/MySQLDriver.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace ZM\Store\MySQL; +namespace ZM\Store\Database; use Doctrine\DBAL\Driver as DoctrineDriver; use Doctrine\DBAL\Platforms\MySqlPlatform; @@ -13,10 +13,10 @@ class MySQLDriver implements DoctrineDriver public function connect(array $params, $username = null, $password = null, array $driverOptions = []) { logger()->debug('Requiring new connection'); - return new MySQLConnection($params); + return new DBConnection($params); } - public function getDatabasePlatform(): MySqlPlatform + public function getDatabasePlatform() { return new MySqlPlatform(); } @@ -33,14 +33,8 @@ class MySQLDriver implements DoctrineDriver public function getDatabase($conn) { - $conf = config('global.mysql'); - - if ($conn instanceof MySQLConnection) { - foreach ($conf as $v) { - if (($v['name'] ?? $v['dbname']) === $conn->getPoolName()) { - return $v['dbname']; - } - } + if ($conn instanceof DBConnection) { + return config('database')[$conn->getPoolName()]['dbname'] ?? ''; } return ''; } diff --git a/src/ZM/Store/Database/SQLiteDriver.php b/src/ZM/Store/Database/SQLiteDriver.php new file mode 100644 index 00000000..f2bfbab7 --- /dev/null +++ b/src/ZM/Store/Database/SQLiteDriver.php @@ -0,0 +1,41 @@ +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 ''; + } +}