Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd7df486

Browse files
authored
Merge pull requestkvnZero#21 from kvnZero/feat/存储过程
Feat/存储过程
2 parents11b09c6 +c4f6ab5 commitd7df486

File tree

13 files changed

+255
-63
lines changed

13 files changed

+255
-63
lines changed

‎README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@
3232

3333
https://github.com/Tencent/APIJSON
3434

35-
3635
###开发进度
3736

38-
1. ✅最基本CRUD 包括批量写入 批量更新 批量删除
37+
1. ✅最基本CRUD 包括批量写入 批量更新 批量删除 存储过程调用
3938
2. ✅支持@column@having@order@group@combine
4039
3. ✅支持运算符 {}, !{}, &{}, |{}, }{@, $, %, ~
4140
4. ✅支持多表查询、一对一查询、一对多查询、数组内多对多查询
4241
5. ✅支持数组内count/page
4342
6. ✅支持debug标签可返回语句执行
4443

45-
待完成:<b>复杂查询</b>, 存储过程调用,远程函数,权限,标签
46-
44+
待完成:<b>复杂查询</b>,Join查询,远程函数,权限,标签
4745

4846
###如何使用
4947

@@ -52,7 +50,7 @@ APIJSON通用文档(增删改查说明):https://github.com/Tencent/APIJSON
5250

5351
拉取该项目, 配置mysql数据库, 配置文件路径:`config\autoload\databases.php`
5452

55-
然后在项目目录执行:(需机器有:docker)
53+
然后在项目目录执行:(需宿主机有:docker)
5654

5755
```shell
5856
# 打包镜像
@@ -61,7 +59,7 @@ docker build -t hyperf-apijson:v1 .
6159
docker run -dit --name hyperf-apijson -p 9501:9501 hyperf-apijson:v1
6260
```
6361

64-
如果需要进行开发调试,使用Hyperf的Docker环境
62+
如果需要进行开发调试,使用Hyperf的Docker镜像 - <b>本项目可作为你的给Hyperf基础项目,可在该项目基础上进行业务开发,几乎没有互相冲突</b>
6563

6664
```shell
6765
docker run -dit --name hyperf-apijson -v {项目目录}:/opt/www -p 9501:9501 hyperf/hyperf:8.0-alpine-v3.12-swoole
@@ -70,6 +68,8 @@ docker run -dit --name hyperf-apijson -v {项目目录}:/opt/www -p 9501:9501 hy
7068
进入到docker环境执行 (如果composer下载不动 可以修改到阿里镜像源 Mac下建议本地开发):
7169
```shell
7270
cd /opt/www
71+
7372
composer update
73+
7474
php bin/hyperf.php start
7575
```

‎app/ApiJson/Entity/ConditionEntity.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ConditionEntity
1717
protectedarray$group = [];
1818
protectedarray$order = [];
1919
protectedarray$having = [];
20+
protectedstring$procedure ="";
2021

2122
/**
2223
* @param array $condition 条件
@@ -155,6 +156,22 @@ public function getOrder(): array
155156
return$this->order;
156157
}
157158

159+
/**
160+
* @param string $procedure
161+
*/
162+
publicfunctionsetProcedure(string$procedure):void
163+
{
164+
$this->procedure =$procedure;
165+
}
166+
167+
/**
168+
* @return string
169+
*/
170+
publicfunctiongetProcedure():string
171+
{
172+
return$this->procedure;
173+
}
174+
158175
protectedfunctionlog(array$condition)
159176
{
160177
$this->changeLog[] = [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespaceApp\ApiJson\Handle;
4+
5+
class FunctionProcedureHandleextends AbstractHandle
6+
{
7+
protectedstring$keyWord ='@procedure()';
8+
9+
publicfunctionbuildModel()
10+
{
11+
if (!in_array($this->keyWord,array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
15+
foreach (array_filter($this->condition->getCondition(),function($key){
16+
return$key ==$this->keyWord;
17+
},ARRAY_FILTER_USE_KEY)as$key =>$value)
18+
{
19+
$this->condition->setProcedure($value);
20+
$this->unsetKey[] =$this->keyWord;
21+
}
22+
}
23+
}

‎app/ApiJson/Method/GetMethod.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,32 @@ protected function validateCondition(): bool
1818

1919
protectedfunctionprocess()
2020
{
21-
$handle =newHandle($this->tableEntity->getConditionEntity(),$this->tableEntity);
22-
$handle->build();
23-
2421
$queryMany =$this->isQueryMany();
2522
if (!$queryMany) {
2623
$this->tableEntity->getConditionEntity()->setLimit(1);
2724
}
2825

26+
$handle =newHandle($this->tableEntity->getConditionEntity(),$this->tableEntity);
27+
$handle->build();
28+
2929
//该事件鼓励是做语句缓存或者事件触发 不赞成修改语句做法 修改语句应在更上层的QueryHandle事件
3030
$event =newQueryExecuteBefore($this->query->toSql(),$this->method);
3131
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
3232

3333
if(is_null($event->result)) {
3434
$result =$this->query->all();
3535

36-
$queryEvent =newQueryResult($result);
36+
$queryEvent =newQueryResult($result,$this->query->toSql());
3737
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($queryEvent);
3838

3939
$result =$queryEvent->result;
40+
41+
if (!empty($this->tableEntity->getConditionEntity()->getProcedure())) {
42+
foreach ($resultas$i =>$item) {
43+
$result[$i]['procedure'] =$this->query->callProcedure($item);
44+
}
45+
}
46+
4047
}else {
4148
$result =$event->result;
4249
}

‎app/ApiJson/Model/MysqlQuery.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,50 @@ public function getBindings(): array
8787
return$this->db->getBindings();
8888
}
8989

90+
publicfunctioncallProcedure(array$dataItem,bool$query =true):array
91+
{
92+
if (empty($this->conditionEntity->getProcedure())) {
93+
return [];
94+
}
95+
$count = -1;
96+
$list = [];//默认值
97+
98+
$procedure =$this->conditionEntity->getProcedure();
99+
$rule ='/(?<functionName>.+)\((?<args>.+?)\)/';
100+
preg_match($rule,$procedure,$match);
101+
if (empty($match['functionName'])) {
102+
return [];
103+
}
104+
$args =array_map('trim',explode(',',$match['args']));
105+
106+
$callArgs = [];
107+
foreach ($argsas$arg) {
108+
if (in_array($arg,array_keys($dataItem))) {
109+
$callArgs[] =$dataItem[$arg];
110+
}elseif ($arg =='@limit') {
111+
$callArgs[] =$this->conditionEntity->getLimit();
112+
}elseif ($arg =='@offset') {
113+
$callArgs[] =$this->conditionEntity->getOffset();
114+
}else {
115+
$callArgs[] =$arg;
116+
}
117+
}
118+
119+
$sql =sprintf("CALL %s(%s)",$match['functionName'],join(',',$callArgs));
120+
121+
if ($query) {
122+
$list = Db::select($sql);
123+
}else {
124+
$count = Db::affectingStatement($sql);
125+
}
126+
127+
return [
128+
'count' =>$count,
129+
'update' => !$query,
130+
'list' =>$list,
131+
];
132+
}
133+
90134
protectedfunctionbuildQuery(bool$query =true)
91135
{
92136
if ($this->build)return;

‎app/ApiJson/Parse/Handle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
useApp\ApiJson\Handle\FunctionLimitHandle;
2121
useApp\ApiJson\Handle\FunctionOffsetHandle;
2222
useApp\ApiJson\Handle\FunctionOrderHandle;
23+
useApp\ApiJson\Handle\FunctionProcedureHandle;
2324
useApp\ApiJson\Handle\WhereBetweenHandle;
2425
useApp\ApiJson\Handle\WhereExistsHandle;
2526
useApp\ApiJson\Handle\WhereHandle;
@@ -54,6 +55,7 @@ class Handle
5455
* @var AbstractHandle[]
5556
*/
5657
protectedarray$methodRules = [
58+
FunctionProcedureHandle::class,
5759
FunctionColumnHandle::class,
5860
FunctionHavingHandle::class,
5961
FunctionOffsetHandle::class,

‎app/ApiJson/Parse/Parse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Parse
1515
{
1616
protectedarray$tagColumn = [
1717
'tag' =>null,
18-
'debug' =>false
18+
'debug' =>false,
19+
'other' => []
1920
];
2021

2122
protectedarray$globalKey = [

‎app/Event/ApiJson/QueryResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class QueryResult
1111
{
12-
publicfunction__construct(publicarray$result)
12+
publicfunction__construct(publicarray$result,publicstring$sql)
1313
{
1414
}
1515
}

‎app/Event/StatementComplete.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class StatementComplete
1919
*/
2020
public$connection;
2121

22+
/**
23+
* The query result
24+
*
25+
* @var array|false
26+
*/
27+
public$result;
28+
2229
/**
2330
* The PDO statement.
2431
*
@@ -30,11 +37,13 @@ class StatementComplete
3037
* Create a new event instance.
3138
*
3239
* @param \Hyperf\Database\Connection $connection
40+
* @param array|false $result
3341
* @param \PDOStatement $statement
3442
*/
35-
publicfunction__construct($connection,$statement)
43+
publicfunction__construct($connection,$result,$statement)
3644
{
3745
$this->statement =$statement;
46+
$this->result =$result;
3847
$this->connection =$connection;
3948
}
4049
}

‎app/Listener/DbPreparedListener.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎app/Listener/QueryResultTryToJsonListener.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
namespaceApp\Listener;
55

6-
useApp\Constants\ConfigCode;
7-
useApp\Event\ApiJson\QueryResult;
6+
useApp\Event\StatementComplete;
87
useHyperf\Event\Annotation\Listener;
98
useHyperf\Event\Contract\ListenerInterface;
10-
useHyperf\Utils\Context;
119

1210
/**
1311
* @Listener
@@ -17,18 +15,15 @@ class QueryResultTryToJsonListener implements ListenerInterface
1715
publicfunctionlisten():array
1816
{
1917
return [
20-
QueryResult::class,
18+
StatementComplete::class,
2119
];
2220
}
2321

2422
publicfunctionprocess(object$event)
2523
{
26-
if (!$eventinstanceof QueryResult)return;
27-
28-
$statement = Context::get(ConfigCode::DB_QUERY_STATEMENT);
29-
if (empty($statement)) {
30-
return;
31-
}
24+
if (!$eventinstanceof StatementComplete)return;
25+
if (empty($event->result))return;
26+
$statement =$event->statement;
3227

3328
$columnCount =count(array_keys(current($event->result)));
3429
$columnMeta = [];

‎class_map/Hyperf/Database/Connection.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,9 @@ public function select(string $query, array $bindings = [], bool $useReadPdo = t
286286

287287
$statement->execute();
288288

289-
$response =$statement->fetchAll();
289+
$result =$statement->fetchAll();
290290

291-
$this->complete($statement);
292-
293-
return$response;
291+
return$this->complete($result,$statement);
294292
});
295293
}
296294

@@ -318,9 +316,9 @@ public function cursor(string $query, array $bindings = [], bool $useReadPdo = t
318316
// Next, we'll execute the query against the database and return the statement
319317
// so we can return the cursor. The cursor will use a PHP generator to give
320318
// back one row at a time without using a bunch of memory to render them.
321-
$statement->execute();
319+
$result =$statement->execute();
322320

323-
$this->complete($statement);
321+
$this->complete($result,$statement);
324322

325323
return$statement;
326324
});
@@ -990,12 +988,15 @@ protected function prepared(PDOStatement $statement)
990988
return$statement;
991989
}
992990

993-
protectedfunctioncomplete(PDOStatement$statement)
991+
protectedfunctioncomplete($result,PDOStatement$statement)
994992
{
995-
$this->event(newStatementComplete(
993+
$event =newStatementComplete(
996994
$this,
995+
$result,
997996
$statement
998-
));
997+
);
998+
$this->event($event);
999+
return$event->result;
9991000
}
10001001

10011002
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp