Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
imi
imi
提交
09a69fb2
imi
项目概览
imi
/
imi
9 个月 前同步成功
通知
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 搜索 >>
提交
09a69fb2
编写于
2月 15, 2022
作者:
weixin_47267244
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Query->where() 条件的值支持传原始 SQL
上级
1946e8b9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
8 deletion
+50
-8
doc/base/version/2.0-2.1.md
doc/base/version/2.0-2.1.md
+2
-0
doc/components/db/index.md
doc/components/db/index.md
+3
-0
src/Db/Query/Where/Where.php
src/Db/Query/Where/Where.php
+31
-8
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
+14
-0
未找到文件。
doc/base/version/2.0-2.1.md
浏览文件 @
09a69fb2
...
@@ -16,6 +16,8 @@ v2.1.0 版本相比 v2.0.0 版本不会出现非常大的不兼容更改,可
...
@@ -16,6 +16,8 @@ v2.1.0 版本相比 v2.0.0 版本不会出现非常大的不兼容更改,可
*
[
枚举类增加 validate() 和 assert()
](
/components/struct/enum.html
)
*
[
枚举类增加 validate() 和 assert()
](
/components/struct/enum.html
)
*
[
Query->where() 条件的值支持传原始 SQL
](
/components/db/index.html
)
### v2.1.1
### v2.1.1
*
[
定时任务的最小、最大延迟执行秒数
](
/components/task/cron.html
)
*
[
定时任务的最小、最大延迟执行秒数
](
/components/task/cron.html
)
...
...
doc/components/db/index.md
浏览文件 @
09a69fb2
...
@@ -563,6 +563,9 @@ Db::query()->where('id', '=', 1);
...
@@ -563,6 +563,9 @@ Db::query()->where('id', '=', 1);
// id > 1
// id > 1
Db
::
query
()
->
where
(
'id'
,
'>'
,
1
);
Db
::
query
()
->
where
(
'id'
,
'>'
,
1
);
// 使用 Raw 原样代入值,例:value = 1 + 2
Db
::
query
()
->
where
(
'value'
,
'='
,
new
\
Imi\Db\Query\Raw
(
'1 + 2'
));
// title like '%test%'
// title like '%test%'
Db
::
query
()
->
where
(
'title'
,
'like'
,
'%test%'
);
Db
::
query
()
->
where
(
'title'
,
'like'
,
'%test%'
);
...
...
src/Db/Query/Where/Where.php
浏览文件 @
09a69fb2
...
@@ -7,6 +7,7 @@ namespace Imi\Db\Query\Where;
...
@@ -7,6 +7,7 @@ namespace Imi\Db\Query\Where;
use
Imi\Db\Mysql\Consts\LogicalOperator
;
use
Imi\Db\Mysql\Consts\LogicalOperator
;
use
Imi\Db\Query\Interfaces\IQuery
;
use
Imi\Db\Query\Interfaces\IQuery
;
use
Imi\Db\Query\Interfaces\IWhere
;
use
Imi\Db\Query\Interfaces\IWhere
;
use
Imi\Db\Query\Raw
;
use
Imi\Db\Query\Traits\TRaw
;
use
Imi\Db\Query\Traits\TRaw
;
class
Where
extends
BaseWhere
implements
IWhere
class
Where
extends
BaseWhere
implements
IWhere
...
@@ -138,6 +139,10 @@ class Where extends BaseWhere implements IWhere
...
@@ -138,6 +139,10 @@ class Where extends BaseWhere implements IWhere
{
{
case
'between'
:
case
'between'
:
case
'not between'
:
case
'not between'
:
if
(
!
\
is_array
(
$thisValues
)
||
!
isset
(
$thisValues
[
0
],
$thisValues
[
1
]))
{
throw
new
\
RuntimeException
(
sprintf
(
'where %s value must be [beginValue, endValue]'
,
$operation
));
}
$begin
=
$query
->
getAutoParamName
();
$begin
=
$query
->
getAutoParamName
();
$end
=
$query
->
getAutoParamName
();
$end
=
$query
->
getAutoParamName
();
$result
.
=
"
{
$begin
}
and
{
$end
}
"
;
$result
.
=
"
{
$begin
}
and
{
$end
}
"
;
...
@@ -148,18 +153,36 @@ class Where extends BaseWhere implements IWhere
...
@@ -148,18 +153,36 @@ class Where extends BaseWhere implements IWhere
case
'not in'
:
case
'not in'
:
$result
.
=
'('
;
$result
.
=
'('
;
$valueNames
=
[];
$valueNames
=
[];
foreach
(
$thisValues
as
$value
)
if
(
\
is_array
(
$thisValues
))
{
foreach
(
$thisValues
as
$value
)
{
$paramName
=
$query
->
getAutoParamName
();
$valueNames
[]
=
$paramName
;
$binds
[
$paramName
]
=
$value
;
}
$result
.
=
implode
(
','
,
$valueNames
)
.
')'
;
}
elseif
(
$thisValues
instanceof
Raw
)
{
$result
.
=
$thisValues
->
toString
(
$query
)
.
')'
;
}
else
{
{
$paramName
=
$query
->
getAutoParamName
();
throw
new
\
RuntimeException
(
sprintf
(
'Invalid value type %s of where %s'
,
\
gettype
(
$thisValues
),
$operation
));
$valueNames
[]
=
$paramName
;
$binds
[
$paramName
]
=
$value
;
}
}
$result
.
=
implode
(
','
,
$valueNames
)
.
')'
;
break
;
break
;
default
:
default
:
$value
=
$query
->
getAutoParamName
();
if
(
$thisValues
instanceof
Raw
)
$result
.
=
$value
;
{
$binds
[
$value
]
=
$thisValues
;
$result
.
=
$thisValues
->
toString
(
$query
);
}
else
{
$value
=
$query
->
getAutoParamName
();
$result
.
=
$value
;
$binds
[
$value
]
=
$thisValues
;
}
break
;
break
;
}
}
...
...
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
浏览文件 @
09a69fb2
...
@@ -6,6 +6,7 @@ namespace Imi\Test\Component\Tests\Db;
...
@@ -6,6 +6,7 @@ namespace Imi\Test\Component\Tests\Db;
use
Imi\Db\Db
;
use
Imi\Db\Db
;
use
Imi\Db\Mysql\Query\Lock\MysqlLock
;
use
Imi\Db\Mysql\Query\Lock\MysqlLock
;
use
Imi\Db\Query\Raw
;
use
Imi\Test\BaseTest
;
use
Imi\Test\BaseTest
;
use
PHPUnit\Framework\Assert
;
use
PHPUnit\Framework\Assert
;
...
@@ -421,4 +422,17 @@ abstract class QueryCurdBaseTest extends BaseTest
...
@@ -421,4 +422,17 @@ abstract class QueryCurdBaseTest extends BaseTest
'json_data'
=>
'{"a": "1", "uid": "'
.
$uid
.
'", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3]}'
,
'json_data'
=>
'{"a": "1", "uid": "'
.
$uid
.
'", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3]}'
,
],
$result
->
get
());
],
$result
->
get
());
}
}
/**
* @depends testInsert
*/
public
function
testRaw
(
array
$args
):
void
{
[
'ids'
=>
$ids
]
=
$args
;
$id
=
$ids
[
0
];
$query
=
Db
::
query
(
$this
->
poolName
);
$record
=
$query
->
from
(
$this
->
tableArticle
)
->
where
(
'id'
,
'='
,
new
Raw
((
string
)
$id
))
->
select
()
->
get
();
Assert
::
assertEquals
(
$id
,
$record
[
'id'
]);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录