Skip to content

Commit b68a357

Browse files
committed
update 分离语句拼装
1 parent e67bc10 commit b68a357

20 files changed

+348
-18
lines changed

app/ApiJson/ApiJson.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99

1010
class ApiJson
1111
{
12-
/** @var array $request */
13-
protected $request;
14-
15-
/** @var string $method */
16-
protected $method;
17-
18-
public function __construct(RequestInterface $request, string $method)
12+
public function __construct(protected RequestInterface $request, protected string $method)
1913
{
20-
$this->request = $request;
21-
$this->method = $method;
2214
}
2315

2416
public function Query(): array
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\ApiJson\Entity;
4+
5+
class ConditionEntity
6+
{
7+
protected array $methodRules = [
8+
9+
];
10+
11+
/**
12+
* @param array $condition 条件
13+
*/
14+
public function __construct(protected array $condition)
15+
{
16+
$this->formatSql();
17+
}
18+
19+
/**
20+
* 整理语句
21+
*/
22+
protected function formatSql()
23+
{
24+
25+
}
26+
}

app/ApiJson/Entity/TableEntity.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\ApiJson\Entity;
4+
5+
class TableEntity
6+
{
7+
/** @var ConditionEntity $ConditionEntity */
8+
protected ConditionEntity $conditionEntity;
9+
10+
/** @var string $realTableName 真实表名 */
11+
protected string $realTableName;
12+
13+
/**
14+
* @param string $tableName 表名
15+
* @param array $jsonContent json数据
16+
*/
17+
public function __construct(protected string $tableName, protected array $jsonContent)
18+
{
19+
$this->realTableName = $tableName;
20+
}
21+
22+
protected function parseConditionEntity()
23+
{
24+
$entity = new ConditionEntity(
25+
$this->getConditionByContent()
26+
);
27+
$this->conditionEntity = $entity;
28+
}
29+
30+
protected function getConditionByContent(): array
31+
{
32+
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
33+
if (isset($this->jsonContent[$sanitizeTableName])) {
34+
$this->realTableName = $sanitizeTableName;
35+
return $this->jsonContent[$sanitizeTableName];
36+
}
37+
return $this->jsonContent[$this->tableName];
38+
}
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
class FunctionColumnHandle extends MethodHandleInterface
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@column';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->value = str_replace([';',':'], [',', ' AS '], $this->value);
15+
$this->builder->select(explode(',', $this->value));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
class FunctionGroupHandle extends MethodHandleInterface
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@group';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$groupArr = explode(',', $this->value);
15+
$this->builder->groupBy($groupArr);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
class FunctionHavingHandle extends MethodHandleInterface
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@having';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$havingArr = explode(';', $this->value);
15+
foreach ($havingArr as $having) {
16+
$this->builder->havingRaw($having);
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
class FunctionOrderHandle extends MethodHandleInterface
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@order';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$orderArr = explode(',', $this->value);
15+
foreach ($orderArr as $order) {
16+
$this->builder->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
17+
}
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
use Hyperf\Database\Query\Builder;
6+
7+
abstract class MethodHandleInterface
8+
{
9+
/** @var string 清洗后的查询key */
10+
protected string $sanitizeKey;
11+
12+
public function __construct(protected Builder $builder, 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(): string
19+
{
20+
if (!$this->validateCondition()) return "";
21+
return $this->buildModel();
22+
}
23+
24+
abstract protected function validateCondition(): bool;
25+
26+
abstract protected function buildModel();
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
class WhereBetweenHandle extends MethodHandleInterface
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return str_ends_with($this->key, '$');
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$value = !is_array($this->value) ? [$this->value] : $this->value;
15+
$sql = [];
16+
foreach ($value as $item) {
17+
$itemArr = explode(',', $item);
18+
$sql[] = sprintf("%s BETWEEN %s AND %s", $this->sanitizeKey, trim($itemArr[0]), trim($itemArr[1]));
19+
}
20+
$this->builder->whereRaw(join(' OR ', $sql)); //3.2.3
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
use Hyperf\Database\Query\Builder;
6+
7+
class WhereExistsHandle extends MethodHandleInterface
8+
{
9+
protected function validateCondition(): bool
10+
{
11+
return str_ends_with($this->key, '}{@');
12+
}
13+
14+
protected function buildModel()
15+
{
16+
$this->builder->whereExists(function(Builder $query) {
17+
$query = $query->from($this->value['from']);
18+
19+
//这里应该再接入处理列表
20+
foreach ($this->value[$this->value['from']] as $k => $v) {
21+
$query->where($k, $v);
22+
}
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)