orm refactoring

This commit is contained in:
Yuri Kuznetsov
2021-07-24 15:16:57 +03:00
parent 9dacaf7ddf
commit dadcee8fe2
9 changed files with 53 additions and 97 deletions
@@ -29,7 +29,7 @@
namespace Espo\ORM\Query\Part;
class SelectExpression implements SelectItem
class SelectExpression
{
private $expression;
@@ -1,37 +0,0 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\ORM\Query\Part;
interface SelectItem
{
public function getExpression(): Expression;
public function getAlias(): ?string;
}
+9 -6
View File
@@ -31,7 +31,7 @@ namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\WhereClause;
use Espo\ORM\Query\Part\SelectExpression as SelectExpr;
use Espo\ORM\Query\Part\SelectItem;
use Espo\ORM\Query\Part\SelectExpression;
use RuntimeException;
@@ -58,9 +58,9 @@ class Select implements SelectingQuery
}
/**
* Get select items.
* Get SELECT items.
*
* @return SelectItem[]
* @return SelectExpression[]
*/
public function getSelect(): array
{
@@ -82,7 +82,7 @@ class Select implements SelectingQuery
}
/**
* Get order.
* Get ORDER items.
*/
public function getOrder(): array
{
@@ -90,7 +90,7 @@ class Select implements SelectingQuery
}
/**
* Whether is distinct.
* Whether DISTINCT is applied.
*/
public function isDistinct(): bool
{
@@ -98,13 +98,16 @@ class Select implements SelectingQuery
}
/**
* Get group by.
* Get GROUP BY items.
*/
public function getGroupBy(): array
{
return $this->params['orderBy'] ?? [];
}
/**
* Get WHERE clause.
*/
public function getWhere(): ?WhereClause
{
$whereClause = $this->params['whereClause'] ?? null;
+11 -13
View File
@@ -30,7 +30,7 @@
namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\Expression;
use Espo\ORM\Query\Part\SelectItem;
use Espo\ORM\Query\Part\SelectExpression;
use InvalidArgumentException;
use RuntimeException;
@@ -127,23 +127,21 @@ class SelectBuilder implements Builder
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a SelectExpression|Expression|string will append the item.
*
* Usage options:
* * `select(SelectExpression $expression)`
* * `select([$expr1, $expr2, ...])`
* * `select([[$expr1, $alias1], [$expr2, $alias2], ...])`
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(string $expression, string $alias)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
* @param SelectExpression|SelectExpression[]|Expression|string $select
* An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is not an array.
*/
public function select($select, ?string $alias = null): self
{
if (is_array($select)) {
$this->params['select'] = $this->normilizeSelectItemArray($select);
$this->params['select'] = $this->normilizeSelectExpressionArray($select);
return $this;
}
@@ -151,7 +149,7 @@ class SelectBuilder implements Builder
if ($select instanceof Expression) {
$select = $select->getValue();
}
else if ($select instanceof SelectItem) {
else if ($select instanceof SelectExpression) {
$alias = $alias ?? $select->getAlias();
$select = $select->getExpression()->getValue();
}
@@ -271,7 +269,7 @@ class SelectBuilder implements Builder
return $this;
}
private function normilizeSelectItemArray(array $itemList): array
private function normilizeSelectExpressionArray(array $itemList): array
{
$resultList = [];
@@ -282,7 +280,7 @@ class SelectBuilder implements Builder
continue;
}
if ($item instanceof SelectItem) {
if ($item instanceof SelectExpression) {
$resultList[] = $item->getAlias() ?
[$item->getExpression()->getValue(), $item->getAlias()] :
[$item->getExpression()->getValue()];
+7 -9
View File
@@ -38,7 +38,7 @@ use Espo\ORM\{
Query\Query,
Query\Builder,
Query\Part\Expression,
Query\Part\SelectItem,
Query\Part\SelectExpression,
};
use ReflectionClass;
@@ -52,18 +52,16 @@ class QueryBuilder
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a SelectExpression|Expression|string will append the item.
*
* Usage options:
* * `select(SelectExpression $expression)`
* * `select([$expr1, $expr2, ...])`
* * `select([[$expr1, $alias1], [$expr2, $alias2], ...])`
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(string $expression, string $alias)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
* @param SelectExpression|SelectExpression[]|Expression|string $select
* An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is not an array.
*/
public function select($select = null, ?string $alias = null): SelectBuilder
{
@@ -35,7 +35,7 @@ use Espo\ORM\{
EntityManager,
Query\Select,
Query\Part\WhereItem,
Query\Part\SelectItem,
Query\Part\SelectExpression,
Mapper\RDBMapper,
Repository\RDBRelationSelectBuilder as Builder,
};
@@ -286,18 +286,16 @@ class RDBRelation
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a SelectExpression|Expression|string will append the item.
*
* Usage options:
* * `select(SelectExpression $expression)`
* * `select([$expr1, $expr2, ...])`
* * `select([[$expr1, $alias1], [$expr2, $alias2], ...])`
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(string $expression, string $alias)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
* @param SelectExpression|SelectExpression[]|Expression|string $select
* An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is not an array.
*/
public function select($select = [], ?string $alias = null): Builder
{
@@ -37,7 +37,7 @@ use Espo\ORM\{
Query\Select,
Query\SelectBuilder,
Query\Part\WhereItem,
Query\Part\SelectItem,
Query\Part\SelectExpression,
Mapper\Mapper,
};
@@ -364,18 +364,16 @@ class RDBRelationSelectBuilder
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a SelectExpression|Expression|string will append the item.
*
* Usage options:
* * `select(SelectExpression $expression)`
* * `select([$expr1, $expr2, ...])`
* * `select([[$expr1, $alias1], [$expr2, $alias2], ...])`
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(string $expression, string $alias)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
* @param SelectExpression|SelectExpression[]|Expression|string $select
* An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is not an array.
*/
public function select($select, ?string $alias = null): self
{
@@ -38,7 +38,7 @@ use Espo\ORM\{
Mapper\RDBMapper,
Query\Select,
Query\Part\WhereItem,
Query\Part\SelectItem,
Query\Part\SelectExpression,
};
use StdClass;
@@ -782,18 +782,16 @@ class RDBRepository extends Repository
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a SelectExpression|Expression|string will append the item.
*
* Usage options:
* * `select(SelectExpression $expression)`
* * `select([$expr1, $expr2, ...])`
* * `select([[$expr1, $alias1], [$expr2, $alias2], ...])`
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(string $expression, string $alias)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
* @param SelectExpression|SelectExpression[]|Expression|string $select
* An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is not an array.
*/
public function select($select = [], ?string $alias = null): RDBSelectBuilder
{
@@ -37,7 +37,7 @@ use Espo\ORM\{
Query\Select,
Query\SelectBuilder,
Query\Part\WhereItem,
Query\Part\SelectItem,
Query\Part\SelectExpression,
Mapper\Mapper,
};
@@ -304,7 +304,7 @@ class RDBSelectBuilder
/**
* Specify SELECT. Columns and expressions to be selected. If not called, then
* all entity attributes will be selected. Passing an array will reset
* previously set items. Passing a string|Expression|SelectItem will append the item.
* previously set items. Passing a string|Expression|SelectExpression will append the item.
*
* Usage options:
* * `select([$expr1, $expr2, ...])`
@@ -312,9 +312,9 @@ class RDBSelectBuilder
* * `select([$selectItem1, $selectItem2, ...])`
* * `select(string|Expression $expression)`
* * `select(string|Expression $expression, string $alias)`
* * `select(SelectItem $selectItem)`
* * `select(SelectExpression $selectItem)`
*
* @param array|string|Expression|SelectItem $select An array of expressions or one expression.
* @param array|string|Expression|SelectExpression $select An array of expressions or one expression.
* @param string|null $alias An alias. Actual if the first parameter is a string.
*/
public function select($select, ?string $alias = null): self