Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
imi
imi
提交
f63b049f
imi
项目概览
imi
/
imi
8 个月 前同步成功
通知
53
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
imi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f63b049f
编写于
6月 12, 2021
作者:
weixin_47267244
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增支持数据库连接后,执行初始化的 SQL
上级
11c9c658
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
75 addition
and
25 deletion
+75
-25
doc/components/db/index.md
doc/components/db/index.md
+35
-0
src/Db/Drivers/Base.php
src/Db/Drivers/Base.php
+29
-0
src/Db/Drivers/Mysqli/Driver.php
src/Db/Drivers/Mysqli/Driver.php
+2
-8
src/Db/Drivers/PdoMysql/Driver.php
src/Db/Drivers/PdoMysql/Driver.php
+2
-8
src/Db/Drivers/Swoole/Driver.php
src/Db/Drivers/Swoole/Driver.php
+7
-9
未找到文件。
doc/components/db/index.md
浏览文件 @
f63b049f
...
...
@@ -66,6 +66,11 @@ return [
// 'dbClass' => \Imi\Db\Drivers\Mysqli\Driver::class,
// 使用 Swoole MySQL 驱动
// 'dbClass' => \Imi\Db\Drivers\Swoole\Driver::class,
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
// uri 写法
// 'resource' => [
...
...
@@ -125,6 +130,11 @@ return [
// 'dbClass' => \Imi\Db\Drivers\Mysqli\Driver::class,
// 使用 Swoole MySQL 驱动
// 'dbClass' => \Imi\Db\Drivers\Swoole\Driver::class,
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
// uri 写法
// 'resource' => [
...
...
@@ -197,6 +207,11 @@ return [
// 'dbClass' => \Imi\Db\Drivers\Mysqli\Driver::class,
// 使用 Swoole MySQL 驱动
// 'dbClass' => \Imi\Db\Drivers\Swoole\Driver::class,
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
],
// 异步池子,worker进程使用
...
...
@@ -223,6 +238,11 @@ return [
// 'dbClass' => \Imi\Db\Drivers\Mysqli\Driver::class,
// 使用 Swoole MySQL 驱动
// 'dbClass' => \Imi\Db\Drivers\Swoole\Driver::class,
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
[
'host'
=>
'127.0.0.2'
,
...
...
@@ -238,6 +258,11 @@ return [
// 'dbClass' => \Imi\Db\Drivers\Mysqli\Driver::class,
// 使用 Swoole MySQL 驱动
// 'dbClass' => \Imi\Db\Drivers\Swoole\Driver::class,
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
]
],
],
...
...
@@ -273,6 +298,11 @@ return [
// 'timeout' => '建立连接超时时间',
// 'charset' => '',
// 'strict_type' => false, //开启严格模式,返回的字段将自动转为数字类型
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
],
// 异步池子,worker进程使用
...
...
@@ -291,6 +321,11 @@ return [
// 'timeout' => '建立连接超时时间',
// 'charset' => '',
// 'options' => [], // PDO连接选项
// 数据库连接后,执行初始化的 SQL
// 'sqls' => [
// 'select 1',
// 'select 2',
// ],
],
// uri 写法
// 'resource' => [
...
...
src/Db/Drivers/Base.php
浏览文件 @
f63b049f
...
...
@@ -8,4 +8,33 @@ use Imi\Util\Traits\THashCode;
abstract
class
Base
implements
IDb
{
use
THashCode
;
/**
* 连接配置.
*
* @var array
*/
protected
$option
;
public
function
__construct
(
array
$option
=
[])
{
$this
->
option
=
$option
;
}
/**
* 数据库连接后,执行初始化的 SQL.
*
* @return void
*/
protected
function
execInitSqls
():
void
{
$sqls
=
$this
->
option
[
'initSqls'
]
??
[];
if
(
$sqls
)
{
foreach
(
$sqls
as
$sql
)
{
$this
->
exec
(
$sql
);
}
}
}
}
src/Db/Drivers/Mysqli/Driver.php
浏览文件 @
f63b049f
...
...
@@ -24,13 +24,6 @@ class Driver extends Base implements IDb
*/
protected
$instance
;
/**
* 连接配置.
*
* @var array
*/
protected
$option
;
/**
* 最后执行过的SQL语句.
*
...
...
@@ -89,7 +82,7 @@ class Driver extends Base implements IDb
{
$option
[
'password'
]
=
''
;
}
$this
->
option
=
$option
;
parent
::
__construct
(
$option
)
;
$this
->
isCacheStatement
=
Config
::
get
(
'@app.db.statement.cache'
,
true
);
$this
->
transaction
=
new
Transaction
();
}
...
...
@@ -121,6 +114,7 @@ class Driver extends Base implements IDb
$option
=
$this
->
option
;
$this
->
instance
=
$instance
=
new
\
mysqli
(
$option
[
'host'
]
??
'127.0.0.1'
,
$option
[
'username'
],
$option
[
'password'
],
$option
[
'database'
],
$option
[
'port'
]
??
3306
);
$instance
->
set_charset
(
$option
[
'charset'
]
??
'utf8'
);
$this
->
execInitSqls
();
return
true
;
}
...
...
src/Db/Drivers/PdoMysql/Driver.php
浏览文件 @
f63b049f
...
...
@@ -23,13 +23,6 @@ class Driver extends Base implements IDb
*/
protected
$instance
;
/**
* 连接配置.
*
* @var array
*/
protected
$option
;
/**
* 最后执行过的SQL语句.
*
...
...
@@ -86,7 +79,7 @@ class Driver extends Base implements IDb
{
$option
[
'options'
]
=
[];
}
$this
->
option
=
$option
;
parent
::
__construct
(
$option
)
;
$this
->
isCacheStatement
=
Config
::
get
(
'@app.db.statement.cache'
,
true
);
$this
->
transaction
=
new
Transaction
();
}
...
...
@@ -147,6 +140,7 @@ class Driver extends Base implements IDb
{
$option
=
$this
->
option
;
$this
->
instance
=
new
\
PDO
(
$this
->
buildDSN
(),
$option
[
'username'
],
$option
[
'password'
],
$option
[
'options'
]);
$this
->
execInitSqls
();
return
true
;
}
...
...
src/Db/Drivers/Swoole/Driver.php
浏览文件 @
f63b049f
...
...
@@ -25,13 +25,6 @@ class Driver extends Base implements IDb
*/
protected
$instance
;
/**
* 连接配置.
*
* @var array
*/
protected
$option
;
/**
* 最后执行过的SQL语句.
*
...
...
@@ -79,7 +72,7 @@ class Driver extends Base implements IDb
*/
public
function
__construct
(
$option
=
[])
{
$this
->
option
=
$option
;
parent
::
__construct
(
$option
)
;
$this
->
isCacheStatement
=
Config
::
get
(
'@app.db.statement.cache'
,
true
);
$this
->
transaction
=
new
Transaction
();
}
...
...
@@ -125,8 +118,13 @@ class Driver extends Base implements IDb
{
$serverConfig
=
array_merge
(
$serverConfig
,
$option
[
'options'
]);
}
$result
=
$instance
->
connect
(
$serverConfig
);
if
(
$result
)
{
$this
->
execInitSqls
();
}
return
$
instance
->
connect
(
$serverConfig
)
;
return
$
result
;
}
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录