Skip to content

Commit b743d19

Browse files
committed
update 分离条件操作 & 抽象模型方法 & 修改语句条件流程
1 parent b68a357 commit b743d19

23 files changed

+346
-192
lines changed

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,59 @@
22

33
namespace App\ApiJson\Entity;
44

5+
use App\ApiJson\Interface\QueryInterface;
6+
use App\ApiJson\Handle\AbstractMethodHandle;
7+
use App\ApiJson\Handle\FunctionColumnHandle;
8+
use App\ApiJson\Handle\FunctionGroupHandle;
9+
use App\ApiJson\Handle\FunctionHavingHandle;
10+
use App\ApiJson\Handle\FunctionOrderHandle;
11+
use App\ApiJson\Handle\WhereBetweenHandle;
12+
use App\ApiJson\Handle\WhereExistsHandle;
13+
use App\ApiJson\Handle\WhereHandle;
14+
use App\ApiJson\Handle\WhereInHandle;
15+
use App\ApiJson\Handle\WhereJsonContainsHandle;
16+
use App\ApiJson\Handle\WhereLikeHandle;
17+
use App\ApiJson\Handle\WhereRawHandle;
18+
use App\ApiJson\Handle\WhereRegexpHandle;
19+
520
class ConditionEntity
621
{
22+
/**
23+
* 匹配规则 根据从上自下优先先匹先出
24+
* @var AbstractMethodHandle[]
25+
*/
726
protected array $methodRules = [
8-
27+
FunctionColumnHandle::class,
28+
FunctionHavingHandle::class,
29+
FunctionGroupHandle::class,
30+
FunctionOrderHandle::class,
31+
WhereJsonContainsHandle::class,
32+
WhereBetweenHandle::class,
33+
WhereExistsHandle::class,
34+
WhereRegexpHandle::class,
35+
WhereLikeHandle::class,
36+
WhereRawHandle::class,
37+
WhereInHandle::class,
38+
WhereHandle::class,
939
];
1040

1141
/**
1242
* @param array $condition 条件
1343
*/
1444
public function __construct(protected array $condition)
1545
{
16-
$this->formatSql();
1746
}
1847

1948
/**
2049
* 整理语句
2150
*/
22-
protected function formatSql()
51+
public function setQueryCondition(QueryInterface $query)
2352
{
24-
53+
foreach ($this->condition as $key => $value) {
54+
foreach ($this->methodRules as $rule) {
55+
$methodRule = new $rule($query, $key, $value);
56+
if ($methodRule->handle()) break;
57+
}
58+
}
2559
}
2660
}

app/ApiJson/Entity/TableEntity.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\ApiJson\Entity;
44

5+
use App\ApiJson\Interface\QueryInterface;
6+
57
class TableEntity
68
{
79
/** @var ConditionEntity $ConditionEntity */
@@ -10,20 +12,61 @@ class TableEntity
1012
/** @var string $realTableName 真实表名 */
1113
protected string $realTableName;
1214

15+
/** @var array $condition */
16+
protected array $condition;
17+
18+
/** @var QueryInterface $query */
19+
protected QueryInterface $query;
20+
1321
/**
1422
* @param string $tableName 表名
1523
* @param array $jsonContent json数据
1624
*/
1725
public function __construct(protected string $tableName, protected array $jsonContent)
1826
{
1927
$this->realTableName = $tableName;
28+
$this->condition = $this->getConditionByContent();
29+
}
30+
31+
public function getResult(): array
32+
{
33+
$this->buildQuery();
34+
$this->parseConditionEntity();
35+
return $this->formatResult($this->query->all());
36+
}
37+
38+
public function getCount(): int
39+
{
40+
$this->buildQuery();
41+
$this->parseConditionEntity();
42+
return $this->query->count();
43+
}
44+
45+
public function insert()
46+
{
47+
48+
}
49+
50+
public function update()
51+
{
52+
53+
}
54+
55+
protected function formatResult(array $result): array
56+
{
57+
return $result;
58+
}
59+
60+
protected function buildQuery()
61+
{
62+
$this->query = new (config(join('.', [
63+
'dependencies', QueryInterface::class
64+
])))($this->realTableName);
2065
}
2166

2267
protected function parseConditionEntity()
2368
{
24-
$entity = new ConditionEntity(
25-
$this->getConditionByContent()
26-
);
69+
$entity = new ConditionEntity($this->condition);
2770
$this->conditionEntity = $entity;
2871
}
2972

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
7+
abstract class AbstractMethodHandle
8+
{
9+
/** @var string 清洗后的查询key */
10+
protected string $sanitizeKey;
11+
12+
public function __construct(protected QueryInterface $query, protected string $key, protected $value)
13+
{
14+
preg_match('#(?<key>[a-zA-z0-9_]+)#', $this->key, $match);
15+
$this->sanitizeKey = $match['key'] ?? $this->key;
16+
}
17+
18+
public function handle(): bool
19+
{
20+
if (!$this->validateCondition()) return false;
21+
$this->buildModel();
22+
return true;
23+
}
24+
25+
abstract protected function validateCondition(): bool;
26+
27+
abstract protected function buildModel();
28+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class FunctionColumnHandle extends MethodHandleInterface
5+
class FunctionColumnHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -12,6 +12,6 @@ protected function validateCondition(): bool
1212
protected function buildModel()
1313
{
1414
$this->value = str_replace([';',':'], [',', ' AS '], $this->value);
15-
$this->builder->select(explode(',', $this->value));
15+
$this->query->select(explode(',', $this->value));
1616
}
1717
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class FunctionGroupHandle extends MethodHandleInterface
5+
class FunctionGroupHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -12,6 +12,6 @@ protected function validateCondition(): bool
1212
protected function buildModel()
1313
{
1414
$groupArr = explode(',', $this->value);
15-
$this->builder->groupBy($groupArr);
15+
$this->query->groupBy($groupArr);
1616
}
1717
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class FunctionHavingHandle extends MethodHandleInterface
5+
class FunctionHavingHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -13,7 +13,7 @@ protected function buildModel()
1313
{
1414
$havingArr = explode(';', $this->value);
1515
foreach ($havingArr as $having) {
16-
$this->builder->havingRaw($having);
16+
$this->query->having($having);
1717
}
1818
}
1919
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class FunctionOrderHandle extends MethodHandleInterface
5+
class FunctionOrderHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -13,7 +13,7 @@ protected function buildModel()
1313
{
1414
$orderArr = explode(',', $this->value);
1515
foreach ($orderArr as $order) {
16-
$this->builder->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
16+
$this->query->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
1717
}
1818
}
1919
}

app/ApiJson/Method/WhereBetweenHandle.php renamed to app/ApiJson/Handle/WhereBetweenHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class WhereBetweenHandle extends MethodHandleInterface
5+
class WhereBetweenHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -17,6 +17,6 @@ protected function buildModel()
1717
$itemArr = explode(',', $item);
1818
$sql[] = sprintf("%s BETWEEN %s AND %s", $this->sanitizeKey, trim($itemArr[0]), trim($itemArr[1]));
1919
}
20-
$this->builder->whereRaw(join(' OR ', $sql)); //3.2.3
20+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
2121
}
2222
}

app/ApiJson/Method/WhereExistsHandle.php renamed to app/ApiJson/Handle/WhereExistsHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

55
use Hyperf\Database\Query\Builder;
66

7-
class WhereExistsHandle extends MethodHandleInterface
7+
class WhereExistsHandle extends AbstractMethodHandle
88
{
99
protected function validateCondition(): bool
1010
{
@@ -13,7 +13,7 @@ protected function validateCondition(): bool
1313

1414
protected function buildModel()
1515
{
16-
$this->builder->whereExists(function(Builder $query) {
16+
$this->query->whereExists(function(Builder $query) {
1717
$query = $query->from($this->value['from']);
1818

1919
//这里应该再接入处理列表

app/ApiJson/Handle/WhereHandle.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereHandle extends AbstractMethodHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return true;
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->query->where($this->sanitizeKey, $this->value);
15+
}
16+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class WhereInHandle extends MethodHandleInterface
5+
class WhereInHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -11,6 +11,6 @@ protected function validateCondition(): bool
1111

1212
protected function buildModel()
1313
{
14-
$this->builder->whereIn($this->sanitizeKey, $this->value);
14+
$this->query->whereIn($this->sanitizeKey, $this->value);
1515
}
1616
}

app/ApiJson/Method/WhereJsonContainsHandle.php renamed to app/ApiJson/Handle/WhereJsonContainsHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class WhereJsonContainsHandle extends MethodHandleInterface
5+
class WhereJsonContainsHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -16,6 +16,6 @@ protected function buildModel()
1616
foreach ($value as $item) {
1717
$sql[] = sprintf("json_contains(%s,%s)", $this->sanitizeKey, trim($item));
1818
}
19-
$this->builder->whereRaw(join(' OR ', $sql)); //3.2.3
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
2020
}
2121
}

app/ApiJson/Method/WhereLikeHandle.php renamed to app/ApiJson/Handle/WhereLikeHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class WhereLikeHandle extends MethodHandleInterface
5+
class WhereLikeHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -16,6 +16,6 @@ protected function buildModel()
1616
foreach ($value as $item) {
1717
$sql[] = sprintf("%s LIKE %s", $this->sanitizeKey, trim($item));
1818
}
19-
$this->builder->whereRaw(join(' OR ', $sql)); //3.2.3
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
2020
}
2121
}

app/ApiJson/Method/WhereRawHandle.php renamed to app/ApiJson/Handle/WhereRawHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\ApiJson\Method;
3+
namespace App\ApiJson\Handle;
44

5-
class WhereRawHandle extends MethodHandleInterface
5+
class WhereRawHandle extends AbstractMethodHandle
66
{
77
protected function validateCondition(): bool
88
{
@@ -16,6 +16,6 @@ protected function buildModel()
1616
foreach ($conditionArr as $condition) {
1717
$sql[] = sprintf("`%s`%s", $this->sanitizeKey, trim($condition));
1818
}
19-
$this->builder->whereRaw(join(' OR ', $sql)); //3.2.3
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
2020
}
2121
}

0 commit comments

Comments
 (0)