mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-18 13:14:52 +08:00
add SQLiteDriver.php and change MySQLDriver.php
This commit is contained in:
parent
085472a12c
commit
dd01653da9
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 '';
|
||||
}
|
||||
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 '';
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user