forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysqlQuery.php
More file actions
180 lines (152 loc) · 4.76 KB
/
MysqlQuery.php
File metadata and controls
180 lines (152 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace App\ApiJson\Model;
use App\ApiJson\Entity\ConditionEntity;
use App\ApiJson\Interface\QueryInterface;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
class MysqlQuery implements QueryInterface
{
/** @var int 查询行为 */
const ACTION_QUERY = 1;
/** @var int 修改行为 */
const ACTION_UPDATE = 2;
/** @var int 插入行为 */
const ACTION_INSERT = 4;
/** @var string $primaryKey */
protected string $primaryKey = 'id';
/** @var bool $build 是否已经生成条件 */
protected bool $build = false;
/** @var Builder $db */
protected Builder $db;
public function __construct(protected string $tableName, protected ConditionEntity $conditionEntity)
{
$this->db = Db::table($tableName);
}
public function getDb(): Builder
{
return $this->db;
}
/**
* @param string $primaryKey
*/
public function setPrimaryKey(string $primaryKey): void
{
$this->primaryKey = $primaryKey;
}
/**
* @return string
*/
public function getPrimaryKey(): string
{
return $this->primaryKey;
}
public function all(): array
{
$this->buildQuery();
return $this->db->get()->all();
}
public function count($columns = '*'): int
{
$this->buildQuery();
return $this->db->count();
}
public function toSql(): string
{
$this->buildQuery();
return $this->db->toSql();
}
public function insert(array $values, $sequence = null): int
{
$this->build = true;
return $this->db->insertGetId($values, $sequence);
}
public function update(array $values): bool
{
$this->build = true;
$this->buildQuery(self::ACTION_UPDATE);
if (empty($this->db->getBindings()['where'])) return false; // 不允许空条件修改
return $this->db->update($values);
}
public function delete($id = null): bool
{
$this->build = true;
return $this->db->delete($id);
}
public function getBindings(): array
{
return $this->db->getBindings();
}
public function callProcedure(array $dataItem, bool $query = true): array
{
if (empty($this->conditionEntity->getProcedure())) {
return [];
}
$count = -1;
$list = []; //默认值
$procedure = $this->conditionEntity->getProcedure();
$rule = '/(?<functionName>.+)\((?<args>.+?)\)/';
preg_match($rule, $procedure, $match);
if (empty($match['functionName'])) {
return [];
}
$args = array_map('trim', explode(',', $match['args']));
$callArgs = [];
foreach ($args as $arg) {
if (in_array($arg, array_keys($dataItem))) {
$callArgs[] = $dataItem[$arg];
} else if ($arg == '@limit') {
$callArgs[] = $this->conditionEntity->getLimit();
} else if ($arg == '@offset') {
$callArgs[] = $this->conditionEntity->getOffset();
} else {
$callArgs[] = $arg;
}
}
$sql = sprintf("CALL %s(%s)", $match['functionName'], join(',', $callArgs));
if ($query) {
$list = Db::select($sql);
} else {
$count = Db::affectingStatement($sql);
}
return [
'count' => $count,
'update' => !$query,
'list' => $list,
];
}
protected function buildQuery(int $actionType = self::ACTION_QUERY)
{
if ($this->build) return;
$this->build = true;
$queryWhere = $this->conditionEntity->getQueryWhere();
foreach ($queryWhere as $itemWhere) {
$this->db->whereRaw($itemWhere['sql'], $itemWhere['bind']);
}
if ($actionType != self::ACTION_QUERY) return; //下面不再非查询操作
$this->db->select(Db::raw($this->conditionEntity->getColumn()));
$limit = $this->conditionEntity->getLimit();
if ($limit > 0) {
$this->db->limit($limit);
}
$offset = $this->conditionEntity->getOffset();
if ($offset > 0) {
$this->db->offset($offset);
}
$group = $this->conditionEntity->getGroup();
if (!empty($group)) {
$this->db->groupBy(...$group);
}
$orderArr = $this->conditionEntity->getOrder();
if (!empty($orderArr)) {
foreach ($orderArr as $orderItem) {
$this->db->orderBy($orderItem[0], $orderItem[1]);
}
}
$havingArr = $this->conditionEntity->getHaving();
if (!empty($havingArr)) {
foreach ($havingArr as $havingItem) {
$this->db->having($havingItem);
}
}
}
}