Skip to content

Commit 9e447af

Browse files
committed
update 语句拼装时使用原生SQL写法再进行拼装 对后续的合并查询和子查询提供基础
1 parent ba3a01a commit 9e447af

25 files changed

+243
-129
lines changed

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ class ConditionEntity
66
{
77
protected array $changeLog = [];
88

9+
/**
10+
* @var array
11+
*/
12+
protected array $where = [];
13+
14+
protected int $limit = 0;
15+
protected int $offset = 0;
16+
protected array $column = ['*'];
17+
protected array $group = [];
18+
protected array $order = [];
19+
protected array $having = [];
20+
921
/**
1022
* @param array $condition 条件
1123
*/
@@ -29,6 +41,120 @@ public function getCondition(): array
2941
return $this->condition;
3042
}
3143

44+
public function getQueryWhere(): array
45+
{
46+
return $this->where;
47+
}
48+
49+
public function setQueryWhere(array $where)
50+
{
51+
$this->where = $where;
52+
}
53+
54+
public function addQueryWhere(string $key, string $sql, array $bindArgs = [])
55+
{
56+
$this->where[$key] = [
57+
'sql' => $sql,
58+
'bind' => $bindArgs
59+
];
60+
}
61+
62+
/**
63+
* @param array|string[] $column
64+
*/
65+
public function setColumn(array $column): void
66+
{
67+
$this->column = $column;
68+
}
69+
70+
/**
71+
* @param array $group
72+
*/
73+
public function setGroup(array $group): void
74+
{
75+
$this->group = $group;
76+
}
77+
78+
/**
79+
* @param array $having
80+
*/
81+
public function setHaving(array $having): void
82+
{
83+
$this->having = $having;
84+
}
85+
86+
/**
87+
* @param int $limit
88+
*/
89+
public function setLimit(int $limit): void
90+
{
91+
$this->limit = $limit;
92+
}
93+
94+
/**
95+
* @param int $offset
96+
*/
97+
public function setOffset(int $offset): void
98+
{
99+
$this->offset = $offset;
100+
}
101+
102+
/**
103+
* @param array $order
104+
*/
105+
public function setOrder(array $order): void
106+
{
107+
$this->order = $order;
108+
}
109+
110+
/**
111+
* @return array
112+
*/
113+
public function getColumn(): array
114+
{
115+
return $this->column;
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
public function getGroup(): array
122+
{
123+
return $this->group;
124+
}
125+
126+
/**
127+
* @return array
128+
*/
129+
public function getHaving(): array
130+
{
131+
return $this->having;
132+
}
133+
134+
/**
135+
* @return int
136+
*/
137+
public function getLimit(): int
138+
{
139+
return $this->limit;
140+
}
141+
142+
/**
143+
* @return int
144+
*/
145+
public function getOffset(): int
146+
{
147+
return $this->offset;
148+
}
149+
150+
/**
151+
* @return array
152+
*/
153+
public function getOrder(): array
154+
{
155+
return $this->order;
156+
}
157+
32158
protected function log(array $condition)
33159
{
34160
$this->changeLog[] = [

app/ApiJson/Entity/TableEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TableEntity
1717
* @param string $tableName 表名
1818
* @param array $jsonContent json源数据
1919
*/
20-
public function __construct(protected string $tableName, protected array $jsonContent, protected array $globalArgs, protected array $extendData = [])
20+
public function __construct(protected string $tableName, protected array $jsonContent, protected array $globalArgs = [], protected array $extendData = [])
2121
{
2222
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
2323
$this->realTableName = $sanitizeTableName;

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
abstract class AbstractHandle
99
{
10-
/** @var string 清洗后的查询key */
11-
protected string $sanitizeKey;
12-
1310
/** @var string 关键字 */
1411
protected string $keyWord;
1512

1613
protected array $unsetKey = [];
1714

18-
public function __construct(protected QueryInterface $query, protected ConditionEntity $condition)
15+
public function __construct(protected ConditionEntity $condition)
1916
{
2017
}
2118

app/ApiJson/Handle/FunctionColumnHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function buildModel()
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
1818
$value = str_replace([';',':'], [',', ' AS '], $value);
19-
$this->query->select(explode(',', $value));
19+
$this->condition->setColumn(explode(',', $value));
2020
$this->unsetKey[] = $this->keyWord;
2121
}
2222
}

app/ApiJson/Handle/FunctionGroupHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function buildModel()
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
1818
$groupArr = explode(',', $value);
19-
$this->query->groupBy($groupArr);
19+
$this->condition->setGroup($groupArr);
2020
$this->unsetKey[] = $this->keyWord;
2121
}
2222
}

app/ApiJson/Handle/FunctionHavingHandle.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public function buildModel()
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
1818
$havingArr = explode(';', $value);
19+
$sql = [];
1920
foreach ($havingArr as $having) {
20-
$this->query->having($having);
21+
$sql[] = sprintf("`%s`%s", $this->sanitizeKey($key), trim($having));
2122
}
22-
$this->unsetKey[] = $this->keyWord;
23+
$this->condition->setHaving($sql); //解析时为AND
24+
$this->unsetKey[] = $key;
2325
}
2426
}
2527
}

app/ApiJson/Handle/FunctionLimitHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function buildModel()
1515
return $key == $this->keyWord;
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
18-
$this->query->limit((int)$value);
18+
$this->condition->setLimit((int)$value);
1919
$this->unsetKey[] = $this->keyWord;
2020
}
2121
}

app/ApiJson/Handle/FunctionOffsetHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function buildModel()
1515
return $key == $this->keyWord;
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
18-
$this->query->offset((int)$value);
18+
$this->condition->setOffset((int)$value);
1919
$this->unsetKey[] = $this->keyWord;
2020
}
2121
}

app/ApiJson/Handle/FunctionOrderHandle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public function buildModel()
1616
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1717
{
1818
$orderArr = explode(',', $value);
19+
$orderCondition = [];
1920
foreach ($orderArr as $order) {
20-
$this->query->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
21+
$orderCondition[] = [str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc'];
2122
}
23+
$this->condition->setOrder($orderCondition);
2224
$this->unsetKey[] = $this->keyWord;
2325
}
2426
}

app/ApiJson/Handle/WhereBetweenHandle.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ protected function buildModel()
1212
{
1313
$value = !is_array($value) ? [$value] : $value;
1414
$sql = [];
15+
$bind = [];
1516
foreach ($value as $item) {
1617
$itemArr = explode(',', $item);
17-
$sql[] = sprintf("%s BETWEEN %s AND %s", $this->sanitizeKey($key), trim($itemArr[0]), trim($itemArr[1]));
18+
$sql[] = sprintf("`%s` BETWEEN ? AND ?", $this->sanitizeKey($key));
19+
$bind = array_merge($bind, [trim($itemArr[0]), trim($itemArr[1])]);
1820
}
19-
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
21+
$this->condition->addQueryWhere($key, join(' OR ', $sql), $bind);
2022
$this->unsetKey[] = $key;
2123
}
2224
}

0 commit comments

Comments
 (0)