mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
add SQLiteDriver.php and change MySQLDriver.php
This commit is contained in:
@@ -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 $conn_conf) {
|
foreach ($conf as $name => $conn_conf) {
|
||||||
if ($conn_conf['host'] !== '') {
|
if (($conn_conf['enable'] ?? true) !== false) {
|
||||||
MySQLPool::create($conn_conf['pool_name'], $conn_conf);
|
DBPool::create($name, $conn_conf);
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif (is_assoc_array($conf)) {
|
|
||||||
// 这种情况也支持,但是不推荐
|
|
||||||
if ($conf['host'] !== '') {
|
|
||||||
MySQLPool::create($conf['pool_name'], $conf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 '';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user