Skip to content

Commit

Permalink
增强 whereBrackets,支持查询条件收集器
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Aug 31, 2023
1 parent 4ede7df commit e45a034
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 382 deletions.
19 changes: 15 additions & 4 deletions doc/components/db/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,26 +453,37 @@ Db::query()->orWhereRaw('id >= :value', [':value' => 1]);
#### whereBrackets

```php
// 查询条件收集器:where (age < 14 or age > 60)
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query, \Imi\Db\Query\Interfaces\IWhereCollector $where) {
// 注意:使用第 2 个参数 $where,而不是 $query
$where->where('age', '<', 14)->orWhere('age', '>', 60);
// 不要有返回值
}, 'or');

// where id = 1 or (age < 14)
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
// 直接返回字符串
// 返回条件字符串
return 'age < 14';
}, 'or');

// 支持使用 sql 语句: where id = 1 or (age > 10 and age < 14)
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
// 直接返回字符串
// 返回 Where 系列数组
return [
\Imi\Db\Query\Where\Where::raw('age > 10'),
new \Imi\Db\Query\Where\Where('age', '<', 14),
];
}, 'or');

// where id = 1 or (age < 14)
Db::query()->where('id', '=', 1)->whereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
// 直接返回字符串
// 返回 Where 系列对象
return new \Imi\Db\Query\Where\Where('age', '<', 14);
}, 'or');

// OR 条件
Db::query()->where('id', '=', 1)->orWhereBrackets(function(\Imi\Db\Query\Interfaces\IQuery $query) {
// 直接返回字符串
// 返回 Where 系列对象
return new \Imi\Db\Query\Where\Where('age', '<', 14);
});
```
Expand Down
190 changes: 190 additions & 0 deletions src/Db/Query/Interfaces/IBaseWhereCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

declare(strict_types=1);

namespace Imi\Db\Query\Interfaces;

interface IBaseWhereCollector
{
/**
* 设置 where 条件一般用于 =><like等.
*
* @param mixed $value
*
* @return static
*/
public function where(string $fieldName, string $operation, $value, string $logicalOperator = 'and'): self;

/**
* 设置 where 条件用原生语句.
*
* @return static
*/
public function whereRaw(string $raw, string $logicalOperator = 'and', array $binds = []): self;

/**
* 设置 where 条件传入回调回调中的条件加括号.
*
* @return static
*/
public function whereBrackets(callable $callback, string $logicalOperator = 'and'): self;

/**
* 设置 where 条件使用 IBaseWhere 结构.
*
* @return static
*/
public function whereStruct(IBaseWhere $where, string $logicalOperator = 'and'): self;

/**
* 设置 where 条件支持语法如下:.
*
* [
* 'id' => 1,
* 'or' => [
* 'id' => 2,
* ],
* 'title' => ['like', '%test%'],
* 'age' => ['>', 18],
* 'age' => ['between', 19, 29]
* ]
*
* SQL: id = 1 or (id = 2) and title like '%test%' and age > 18 and age between 19 and 29
*
* @return static
*/
public function whereEx(array $condition, string $logicalOperator = 'and'): self;

/**
* where between $begin end $end.
*
* @param mixed $begin
* @param mixed $end
*
* @return static
*/
public function whereBetween(string $fieldName, $begin, $end, string $logicalOperator = 'and'): self;

/**
* or where between $begin end $end.
*
* @param mixed $begin
* @param mixed $end
*
* @return static
*/
public function orWhereBetween(string $fieldName, $begin, $end): self;

/**
* where not between $begin end $end.
*
* @param mixed $begin
* @param mixed $end
*
* @return static
*/
public function whereNotBetween(string $fieldName, $begin, $end, string $logicalOperator = 'and'): self;

/**
* or where not between $begin end $end.
*
* @param mixed $begin
* @param mixed $end
*
* @return static
*/
public function orWhereNotBetween(string $fieldName, $begin, $end): self;

/**
* 设置 where or 条件.
*
* @param mixed $value
*
* @return static
*/
public function orWhere(string $fieldName, string $operation, $value): self;

/**
* 设置 where or 条件用原生语句.
*
* @return static
*/
public function orWhereRaw(string $where, array $binds = []): self;

/**
* 设置 where or 条件传入回调回调中的条件加括号.
*
* @return static
*/
public function orWhereBrackets(callable $callback): self;

/**
* 设置 where or 条件使用 IBaseWhere 结构.
*
* @return static
*/
public function orWhereStruct(IBaseWhere $where): self;

/**
* 设置 where or 条件支持语法参考 whereEx 方法.
*
* @return static
*/
public function orWhereEx(array $condition): self;

/**
* where field in (list).
*
* @return static
*/
public function whereIn(string $fieldName, array $list, string $logicalOperator = 'and'): self;

/**
* or where field in (list).
*
* @return static
*/
public function orWhereIn(string $fieldName, array $list): self;

/**
* where field not in (list).
*
* @return static
*/
public function whereNotIn(string $fieldName, array $list, string $logicalOperator = 'and'): self;

/**
* or where field not in (list).
*
* @return static
*/
public function orWhereNotIn(string $fieldName, array $list): self;

/**
* where field is null.
*
* @return static
*/
public function whereIsNull(string $fieldName, string $logicalOperator = 'and'): self;

/**
* or where field is null.
*
* @return static
*/
public function orWhereIsNull(string $fieldName): self;

/**
* where field is not null.
*
* @return static
*/
public function whereIsNotNull(string $fieldName, string $logicalOperator = 'and'): self;

/**
* or where field is not null.
*
* @return static
*/
public function orWhereIsNotNull(string $fieldName): self;
}
Loading

0 comments on commit e45a034

Please sign in to comment.