Skip to content

Commit c2a8802

Browse files
authored
增强 whereBrackets,支持查询条件收集器 (#580)
* 增强 whereBrackets,支持查询条件收集器 * 更新文档 * 更新文档 * 优化代码,完善测试用例 * 修复测试
1 parent 3e765a6 commit c2a8802

File tree

15 files changed

+718
-390
lines changed

15 files changed

+718
-390
lines changed

doc/base/version/2.0-2.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ v2.0 是一个非常成功的 LTS 版本,进行了底层重构,增加了强
1818

1919
## 新功能
2020

21+
### v2.1.53
22+
23+
**发布日期:** `2023-09-01`
24+
25+
* 增强 `whereBrackets`,支持查询条件收集器 ([#580](https://github.com/imiphp/imi/pull/580)) ([文档](https://doc.imiphp.com/v2.1/components/db/index.html#whereBrackets))
26+
2127
### v2.1.52
2228

2329
**发布日期:** `2023-08-18`

doc/components/db/index.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,26 +453,37 @@ Db::query()->orWhereRaw('id >= :value', [':value' => 1]);
453453
#### whereBrackets
454454

455455
```php
456+
// 查询条件收集器:where (age < 14 or age > 60)
457+
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query, \Imi\Db\Query\Interfaces\IWhereCollector $where) {
458+
// 注意:使用第 2 个参数 $where,而不是 $query
459+
$where->where('age', '<', 14)->orWhere('age', '>', 60);
460+
// 不要有返回值
461+
}, 'or');
462+
456463
// where id = 1 or (age < 14)
457464
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
458-
// 直接返回字符串
465+
// 返回条件字符串
459466
return 'age < 14';
460467
}, 'or');
468+
461469
// 支持使用 sql 语句: where id = 1 or (age > 10 and age < 14)
462470
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
463-
// 直接返回字符串
471+
// 返回 Where 系列数组
464472
return [
465473
\Imi\Db\Query\Where\Where::raw('age > 10'),
466474
new \Imi\Db\Query\Where\Where('age', '<', 14),
467475
];
468476
}, 'or');
477+
469478
// where id = 1 or (age < 14)
470479
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
471-
// 直接返回字符串
480+
// 返回 Where 系列对象
472481
return new \Imi\Db\Query\Where\Where('age', '<', 14);
473482
}, 'or');
483+
484+
// OR 条件
474485
Db::query()->where('id', '=', 1)->orWhereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
475-
// 直接返回字符串
486+
// 返回 Where 系列对象
476487
return new \Imi\Db\Query\Where\Where('age', '<', 14);
477488
});
478489
```

src/Components/pgsql/tests/Unit/Db/Pdo/QueryCurdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class QueryCurdTest extends QueryCurdBaseTest
2626
*
2727
* @var string
2828
*/
29-
protected $expectedTestWhereExSql = 'select * from "tb_article" where ("id" = :p1 and ("id" in (:p2) ) )';
29+
protected $expectedTestWhereExSql = 'select * from "tb_article" where ("id" = :p1 and ("id" in (:p2)))';
3030

3131
/**
3232
* 测试 JSON 查询的 SQL.

src/Components/pgsql/tests/Unit/Db/Swoole/QueryCurdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class QueryCurdTest extends QueryCurdBaseTest
2626
*
2727
* @var string
2828
*/
29-
protected $expectedTestWhereExSql = 'select * from "tb_article" where ("id" = :p1 and ("id" in (:p2) ) )';
29+
protected $expectedTestWhereExSql = 'select * from "tb_article" where ("id" = :p1 and ("id" in (:p2)))';
3030

3131
/**
3232
* 测试 JSON 查询的 SQL.

src/Components/swoole/tests/unit/Component/Tests/Db/Swoole/QueryCurdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class QueryCurdTest extends QueryCurdBaseTest
2323
*
2424
* @var string
2525
*/
26-
protected $expectedTestWhereExSql = 'select * from `tb_article` where (`id` = :p1 and (`id` in (:p2) ) )';
26+
protected $expectedTestWhereExSql = 'select * from `tb_article` where (`id` = :p1 and (`id` in (:p2)))';
2727

2828
/**
2929
* 测试 JSON 查询的 SQL.
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Imi\Db\Query\Interfaces;
6+
7+
interface IBaseWhereCollector
8+
{
9+
/**
10+
* 设置 where 条件,一般用于 =、>、<、like等.
11+
*
12+
* @param mixed $value
13+
*
14+
* @return static
15+
*/
16+
public function where(string $fieldName, string $operation, $value, string $logicalOperator = 'and'): self;
17+
18+
/**
19+
* 设置 where 条件,用原生语句.
20+
*
21+
* @return static
22+
*/
23+
public function whereRaw(string $raw, string $logicalOperator = 'and', array $binds = []): self;
24+
25+
/**
26+
* 设置 where 条件,传入回调,回调中的条件加括号.
27+
*
28+
* @return static
29+
*/
30+
public function whereBrackets(callable $callback, string $logicalOperator = 'and'): self;
31+
32+
/**
33+
* 设置 where 条件,使用 IBaseWhere 结构.
34+
*
35+
* @return static
36+
*/
37+
public function whereStruct(IBaseWhere $where, string $logicalOperator = 'and'): self;
38+
39+
/**
40+
* 设置 where 条件,支持语法如下:.
41+
*
42+
* [
43+
* 'id' => 1,
44+
* 'or' => [
45+
* 'id' => 2,
46+
* ],
47+
* 'title' => ['like', '%test%'],
48+
* 'age' => ['>', 18],
49+
* 'age' => ['between', 19, 29]
50+
* ]
51+
*
52+
* SQL: id = 1 or (id = 2) and title like '%test%' and age > 18 and age between 19 and 29
53+
*
54+
* @return static
55+
*/
56+
public function whereEx(array $condition, string $logicalOperator = 'and'): self;
57+
58+
/**
59+
* where between $begin end $end.
60+
*
61+
* @param mixed $begin
62+
* @param mixed $end
63+
*
64+
* @return static
65+
*/
66+
public function whereBetween(string $fieldName, $begin, $end, string $logicalOperator = 'and'): self;
67+
68+
/**
69+
* or where between $begin end $end.
70+
*
71+
* @param mixed $begin
72+
* @param mixed $end
73+
*
74+
* @return static
75+
*/
76+
public function orWhereBetween(string $fieldName, $begin, $end): self;
77+
78+
/**
79+
* where not between $begin end $end.
80+
*
81+
* @param mixed $begin
82+
* @param mixed $end
83+
*
84+
* @return static
85+
*/
86+
public function whereNotBetween(string $fieldName, $begin, $end, string $logicalOperator = 'and'): self;
87+
88+
/**
89+
* or where not between $begin end $end.
90+
*
91+
* @param mixed $begin
92+
* @param mixed $end
93+
*
94+
* @return static
95+
*/
96+
public function orWhereNotBetween(string $fieldName, $begin, $end): self;
97+
98+
/**
99+
* 设置 where or 条件.
100+
*
101+
* @param mixed $value
102+
*
103+
* @return static
104+
*/
105+
public function orWhere(string $fieldName, string $operation, $value): self;
106+
107+
/**
108+
* 设置 where or 条件,用原生语句.
109+
*
110+
* @return static
111+
*/
112+
public function orWhereRaw(string $where, array $binds = []): self;
113+
114+
/**
115+
* 设置 where or 条件,传入回调,回调中的条件加括号.
116+
*
117+
* @return static
118+
*/
119+
public function orWhereBrackets(callable $callback): self;
120+
121+
/**
122+
* 设置 where or 条件,使用 IBaseWhere 结构.
123+
*
124+
* @return static
125+
*/
126+
public function orWhereStruct(IBaseWhere $where): self;
127+
128+
/**
129+
* 设置 where or 条件,支持语法参考 whereEx 方法.
130+
*
131+
* @return static
132+
*/
133+
public function orWhereEx(array $condition): self;
134+
135+
/**
136+
* where field in (list).
137+
*
138+
* @return static
139+
*/
140+
public function whereIn(string $fieldName, array $list, string $logicalOperator = 'and'): self;
141+
142+
/**
143+
* or where field in (list).
144+
*
145+
* @return static
146+
*/
147+
public function orWhereIn(string $fieldName, array $list): self;
148+
149+
/**
150+
* where field not in (list).
151+
*
152+
* @return static
153+
*/
154+
public function whereNotIn(string $fieldName, array $list, string $logicalOperator = 'and'): self;
155+
156+
/**
157+
* or where field not in (list).
158+
*
159+
* @return static
160+
*/
161+
public function orWhereNotIn(string $fieldName, array $list): self;
162+
163+
/**
164+
* where field is null.
165+
*
166+
* @return static
167+
*/
168+
public function whereIsNull(string $fieldName, string $logicalOperator = 'and'): self;
169+
170+
/**
171+
* or where field is null.
172+
*
173+
* @return static
174+
*/
175+
public function orWhereIsNull(string $fieldName): self;
176+
177+
/**
178+
* where field is not null.
179+
*
180+
* @return static
181+
*/
182+
public function whereIsNotNull(string $fieldName, string $logicalOperator = 'and'): self;
183+
184+
/**
185+
* or where field is not null.
186+
*
187+
* @return static
188+
*/
189+
public function orWhereIsNotNull(string $fieldName): self;
190+
}

0 commit comments

Comments
 (0)