Skip to content

Feat/测试中修改 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/ApiJson/Entity/ConditionEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class ConditionEntity
*/
protected array $where = [];

protected int $limit = 0;
protected int $limit = 10;
protected int $offset = 0;
protected array $column = ['*'];
protected string $column = '*';
protected array $group = [];
protected array $order = [];
protected array $having = [];
Expand Down Expand Up @@ -60,9 +60,9 @@ public function addQueryWhere(string $key, string $sql, array $bindArgs = [])
}

/**
* @param array|string[] $column
* @param string $column
*/
public function setColumn(array $column): void
public function setColumn(string $column): void
{
$this->column = $column;
}
Expand Down Expand Up @@ -108,9 +108,9 @@ public function setOrder(array $order): void
}

/**
* @return array
* @return string
*/
public function getColumn(): array
public function getColumn(): string
{
return $this->column;
}
Expand Down
4 changes: 3 additions & 1 deletion app/ApiJson/Handle/AbstractHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ protected function subTableQuery(array $data): QueryInterface
{
$tableName = $data['from'];
$tableEntity = new TableEntity($tableName, $data);
$handle = new Handle($tableEntity->getConditionEntity(), $tableEntity);
$conditionEntity = $tableEntity->getConditionEntity();
$conditionEntity->setLimit(0);
$handle = new Handle($conditionEntity, $tableEntity);
$handle->build();
/** @var QueryInterface $query */
return new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName(), $tableEntity->getConditionEntity());
Expand Down
2 changes: 1 addition & 1 deletion app/ApiJson/Handle/FunctionColumnHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function buildModel()
}, ARRAY_FILTER_USE_KEY) as $key => $value)
{
$value = str_replace([';',':'], [',', ' AS '], $value);
$this->condition->setColumn(explode(',', $value));
$this->condition->setColumn($value);
$this->unsetKey[] = $this->keyWord;
}
}
Expand Down
12 changes: 7 additions & 5 deletions app/ApiJson/Handle/WhereSubQueryHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ protected function buildModel()
{
$query = $this->subTableQuery($value);

$op = '=';
$op = ' = ';
if(str_ends_with($key, '>=@')) {
$op = '>=';
$op = ' >= ';
} else if(str_ends_with($key, '<=@')) {
$op = '<=';
$op = ' <= ';
} else if(str_ends_with($key, '>@')) {
$op = '>';
$op = ' > ';
} else if(str_ends_with($key, '<@')) {
$op = '<';
$op = ' < ';
} else if(str_ends_with($key, '{}@')) {
$op = ' IN ';
}
$sql = sprintf('`%s`%s(%s)', $this->sanitizeKey($key), $op, $query->toSql());
$this->condition->addQueryWhere($key, $sql, $query->getBindings());
Expand Down
2 changes: 1 addition & 1 deletion app/ApiJson/Model/MysqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function buildQuery(bool $query = true)
}
if (!$query) return; //下面不再非查询操作

$this->db->select($this->conditionEntity->getColumn());
$this->db->select(Db::raw($this->conditionEntity->getColumn()));
$limit = $this->conditionEntity->getLimit();
if ($limit > 0) {
$this->db->limit($limit);
Expand Down
14 changes: 11 additions & 3 deletions app/ApiJson/Parse/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public function __construct(protected array $json, protected string $method = 'G

public function handle(bool $isQueryMany = false, array $extendData = []): array
{
if (empty($extendData)) {
$extendData = $this->json; //引入原json
}
$result = [];
foreach ($this->json as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
if (is_null($condition)) continue;
if (in_array($tableName, $this->filterKey())) {
$this->tagColumn[$tableName] = $condition;
continue;
Expand All @@ -56,9 +60,9 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array
if (str_ends_with($tableName, '[]')) {
$isQueryMany = true;
}
// if (!preg_match("/^[A-Za-z]+$/", $tableName) || !is_array($condition)) {
// continue; //不满足表名规范 跳出不往下执行
// }
if (!preg_match("/^[A-Z].+/", $tableName) || !is_array($condition)) {
continue; //不满足表名规范 跳出不往下执行
}
$this->tableEntities[$tableName] = new TableEntity($tableName, $this->json, $this->getGlobalArgs(), array_merge($result, $extendData));
foreach ($this->supMethod as $methodClass) {
/** @var AbstractMethod $method */
Expand Down Expand Up @@ -93,6 +97,10 @@ protected function handleArray(array $jsonData, array $extendData = []): array
{
$result = [[]];
foreach ($jsonData as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
if (is_null($condition)) continue;
if (!preg_match("/^[A-Z].+/", $tableName) || !is_array($condition)) {
continue; //不满足表名规范 跳出不往下执行
}
foreach ($result as $key => $item) {
if (in_array($tableName, $this->filterKey())) {
$this->tagColumn[$tableName] = $condition;
Expand Down
19 changes: 15 additions & 4 deletions app/ApiJson/Replace/QuoteReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ protected function process()
$condition = $this->condition->getCondition();

foreach (array_filter($condition, function($key){
return str_ends_with($key, '@') && !str_ends_with($key, '}{@');
return str_ends_with($key, '@');
}, ARRAY_FILTER_USE_KEY) as $key => $value)
{
if (!is_string($value)) continue;
$path = str_replace(['/', '[]'], ['.', 'currentItem'], $value);
$newKey = substr($key, 0, strlen($key) - 1);
$condition[$newKey] = data_get($this->condition->getExtendData(), $path);
unset($condition[$key]);
if (str_starts_with($path, '.')) {
$path = 'currentItem' . $path;
}
$value = data_get($this->condition->getExtendData(), $path);
if (!is_null($value)) { //常规情况下的引用
$newKey = substr($key, 0, strlen($key) - 1);
$condition[$newKey] = $value;
unset($condition[$key]);
} else { //非常规情况下引入 比如引入子查询等
$path .= '@';
$value = data_get($this->condition->getExtendData(), $path);
$condition[$key] = $value;
}
$this->condition->setCondition($condition);
}
}
Expand Down
Loading