refactoring

This commit is contained in:
Yuri Kuznetsov
2022-04-01 11:12:21 +03:00
parent 23d78e1e46
commit 30b4b8d501
4 changed files with 186 additions and 30 deletions
+10 -10
View File
@@ -34,9 +34,9 @@ use RuntimeException;
class Item
{
public const TYPE_AND = 'and';
public const TYPE_AND = Item\Type::AND;
public const TYPE_OR = 'or';
public const TYPE_OR = Item\Type::OR;
private string $type;
@@ -55,11 +55,11 @@ class Item
* @var string[]
*/
private $noAttributeTypeList = [
self::TYPE_OR,
self::TYPE_AND,
'not',
'subQueryNotIn',
'subQueryIn',
Item\Type::AND,
Item\Type::OR,
Item\Type::NOT,
Item\Type::SUBQUERY_IN,
Item\Type::SUBQUERY_NOT_IN,
'having', // @todo Check usage. Maybe to be removed.
];
@@ -67,8 +67,8 @@ class Item
* @var string[]
*/
private $withNestedItemsTypeList = [
self::TYPE_OR,
self::TYPE_AND,
Item\Type::AND,
Item\Type::OR,
];
private function __construct(string $type)
@@ -129,7 +129,7 @@ class Item
public static function fromRawAndGroup(array $paramList): self
{
return self::fromRaw([
'type' => self::TYPE_AND,
'type' => Item\Type::AND,
'value' => $paramList,
]);
}
@@ -0,0 +1,149 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 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\Core\Select\Where\Item;
class Type
{
public const AND = 'and';
public const OR = 'or';
public const NOT = 'not';
public const SUBQUERY_NOT_IN = 'subQueryNotIn';
public const SUBQUERY_IN = 'subQueryIn';
public const EXPRESSION = 'expression';
public const IN = 'in';
public const NOT_IN = 'notIn';
public const EQUALS = 'equals';
public const NOT_EQUALS = 'notEquals';
public const ON = 'on';
public const NOT_ON = 'notOn';
public const LIKE = 'like';
public const NOT_LIKE = 'notLike';
public const STARTS_WITH = 'startsWith';
public const ENDS_WITH = 'endsWith';
public const CONTAINS = 'contains';
public const GREATER_THAN = 'greaterThan';
public const LESS_THAN = 'lessThan';
public const GREATER_THAN_OR_EQUALS = 'greaterThanOrEquals';
public const LESS_THAN_OR_EQUALS = 'lessThanOrEquals';
public const AFTER = 'after';
public const BEFORE = 'before';
public const BETWEEN = 'between';
public const ANY = 'any';
public const NONE = 'none';
public const IS_NULL = 'isNull';
public const IS_NOT_NULL = 'isNotNull';
public const IS_TRUE = 'isTrue';
public const IS_FALSE = 'isFalse';
public const TODAY = 'today';
public const PAST = 'past';
public const FUTURE = 'future';
public const LAST_X_DAYS = 'lastXDays';
public const NEXT_X_DAYS = 'nextXDays';
public const OLDER_THAN_X_DAYS = 'olderThanXDays';
public const AFTER_X_DAYS = 'afterXDays';
public const CURRENT_MONTH = 'currentMonth';
public const NEXT_MONTH = 'nextMonth';
public const LAST_MONTH = 'lastMonth';
public const CURRENT_QUARTER = 'currentQuarter';
public const LAST_QUARTER = 'lastQuarter';
public const CURRENT_YEAR = 'currentYear';
public const LAST_YEAR = 'lastYear';
public const CURRENT_FISCAL_YEAR = 'currentFiscalYear';
public const LAST_FISCAL_YEAR = 'lastFiscalYear';
public const CURRENT_FISCAL_QUARTER = 'currentFiscalQuarter';
public const LAST_FISCAL_QUARTER = 'lastFiscalQuarter';
public const ARRAY_ANY_OF = 'arrayAnyOf';
public const ARRAY_NONE_OF = 'arrayNoneOf';
public const ARRAY_ALL_OF = 'arrayAllOf';
public const ARRAY_IS_EMPTY = 'arrayIsEmpty';
public const ARRAY_IS_NOT_EMPTY = 'arrayIsNotEmpty';
public const IS_LINKED_WITH = 'linkedWith';
public const IS_NOT_LINKED_WITH = 'notLinkedWith';
public const IS_LINKED_WITH_ALL = 'linkedWithAll';
public const IS_LINKED_WITH_ANY = 'isLinked';
public const IS_LINKED_WITH_NONE = 'isNotLinked';
}
@@ -44,6 +44,11 @@ class ItemBuilder
private ?string $timeZone = null;
public static function create(): self
{
return new self();
}
public function setType(string $type): self
{
$this->type = $type;
@@ -29,6 +29,8 @@
namespace Espo\Core\Select\Where;
use Espo\Core\Select\Where\Item\Type;
use Espo\{
Core\Exceptions\Error,
ORM\Query\SelectBuilder as QueryBuilder,
@@ -115,7 +117,10 @@ class ItemGeneralConverter implements ItemConverter
if ($attribute) {
if ($this->itemConverterFactory->has($this->entityType, $attribute, $type)) {
$converter = $this->itemConverterFactory->create(
$this->entityType, $attribute, $type, $this->user
$this->entityType,
$attribute,
$type,
$this->user
);
return $converter->convert($queryBuilder, $item);
@@ -129,17 +134,16 @@ class ItemGeneralConverter implements ItemConverter
}
switch ($type) {
case 'or':
case 'and':
case Type::OR:
case Type::AND:
return WhereClause::fromRaw(
$this->groupProcessAndOr($queryBuilder, $type, $attribute, $value)
);
case 'not':
case 'subQueryNotIn':
case 'subQueryIn':
case Type::NOT:
case Type::SUBQUERY_NOT_IN:
case Type::SUBQUERY_IN:
return WhereClause::fromRaw(
$this->groupProcessSubQuery($queryBuilder, $type, $attribute, $value)
@@ -163,11 +167,11 @@ class ItemGeneralConverter implements ItemConverter
$this->groupProcessColumn($queryBuilder, $type, $attribute, $value)
);
case 'arrayAnyOf':
case 'arrayNoneOf':
case 'arrayIsEmpty':
case 'arrayIsNotEmpty':
case 'arrayAllOf':
case Type::ARRAY_ANY_OF:
case Type::ARRAY_NONE_OF:
case Type::ARRAY_IS_EMPTY:
case Type::ARRAY_IS_EMPTY:
case Type::ARRAY_ALL_OF:
return WhereClause::fromRaw(
$this->groupProcessArray($queryBuilder, $type, $attribute, $value)
@@ -246,7 +250,7 @@ class ItemGeneralConverter implements ItemConverter
->from($this->entityType);
$whereItem = Item::fromRaw([
'type' => 'and',
'type' => Type::AND,
'value' => $value,
]);
@@ -286,7 +290,6 @@ class ItemGeneralConverter implements ItemConverter
$alias = $link . 'ColumnFilter' . $this->randomStringGenerator->generate();
$queryBuilder->distinct();
$queryBuilder->leftJoin($link, $alias);
$columnKey = $alias . 'Middle.' . $column;
@@ -393,7 +396,7 @@ class ItemGeneralConverter implements ItemConverter
}
}
if ($type === 'arrayAnyOf') {
if ($type === Type::ARRAY_ANY_OF) {
if (is_null($value) || !$value && !is_array($value)) {
throw new Error("Bad where item. No value.");
}
@@ -415,7 +418,7 @@ class ItemGeneralConverter implements ItemConverter
];
}
if ($type === 'arrayNoneOf') {
if ($type === Type::ARRAY_NONE_OF) {
if (is_null($value) || !$value && !is_array($value)) {
throw new Error("Bad where item 'array'. No value.");
}
@@ -438,7 +441,7 @@ class ItemGeneralConverter implements ItemConverter
];
}
if ($type === 'arrayIsEmpty') {
if ($type === Type::ARRAY_IS_EMPTY) {
$queryBuilder->distinct();
$queryBuilder->leftJoin(
@@ -456,7 +459,7 @@ class ItemGeneralConverter implements ItemConverter
];
}
if ($type === 'arrayIsNotEmpty') {
if ($type === Type::ARRAY_IS_NOT_EMPTY) {
$queryBuilder->distinct();
$queryBuilder->leftJoin(
@@ -474,7 +477,7 @@ class ItemGeneralConverter implements ItemConverter
];
}
if ($type === 'arrayAllOf') {
if ($type === Type::ARRAY_ALL_OF) {
if (is_null($value) || !$value && !is_array($value)) {
throw new Error("Bad where item 'array'. No value.");
}
@@ -1223,7 +1226,6 @@ class ItemGeneralConverter implements ItemConverter
$alias = $link . 'IsLinkedFilter' . $this->randomStringGenerator->generate();
$queryBuilder->distinct();
$queryBuilder->leftJoin($link, $alias);
return [