Initial commit
This commit is contained in:
108
application/Espo/ORM/Query/Part/Where/AndGroup.php
Normal file
108
application/Espo/ORM/Query/Part/Where/AndGroup.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereClause;
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
|
||||
/**
|
||||
* AND-group. Immutable.
|
||||
*/
|
||||
class AndGroup implements WhereItem
|
||||
{
|
||||
/** @var array<string|int, mixed> */
|
||||
private $rawValue = [];
|
||||
|
||||
/**
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function getRaw(): array
|
||||
{
|
||||
return ['AND' => $this->getRawValue()];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return 'AND';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function getRawValue(): array
|
||||
{
|
||||
return $this->rawValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a number of items.
|
||||
*/
|
||||
public function getItemCount(): int
|
||||
{
|
||||
return count($this->rawValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string|int, mixed> $whereClause
|
||||
* @return self
|
||||
*/
|
||||
public static function fromRaw(array $whereClause): self
|
||||
{
|
||||
if (count($whereClause) === 1 && array_keys($whereClause)[0] === 0) {
|
||||
$whereClause = $whereClause[0];
|
||||
}
|
||||
|
||||
// Do not refactor.
|
||||
$obj = static::class === WhereClause::class ?
|
||||
new WhereClause() :
|
||||
new self();
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$obj->rawValue = $whereClause;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function create(WhereItem ...$itemList): self
|
||||
{
|
||||
$builder = self::createBuilder();
|
||||
|
||||
foreach ($itemList as $item) {
|
||||
$builder->add($item);
|
||||
}
|
||||
|
||||
return $builder->build();
|
||||
}
|
||||
|
||||
public static function createBuilder(): AndGroupBuilder
|
||||
{
|
||||
return new AndGroupBuilder();
|
||||
}
|
||||
}
|
||||
95
application/Espo/ORM/Query/Part/Where/AndGroupBuilder.php
Normal file
95
application/Espo/ORM/Query/Part/Where/AndGroupBuilder.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
|
||||
class AndGroupBuilder
|
||||
{
|
||||
/** @var array<string|int, mixed> */
|
||||
private array $raw = [];
|
||||
|
||||
public function build(): AndGroup
|
||||
{
|
||||
return AndGroup::fromRaw($this->raw);
|
||||
}
|
||||
|
||||
public function add(WhereItem $item): self
|
||||
{
|
||||
$key = $item->getRawKey();
|
||||
$value = $item->getRawValue();
|
||||
|
||||
if ($item instanceof AndGroup) {
|
||||
$this->raw = self::normalizeRaw($this->raw);
|
||||
|
||||
$this->raw[] = $item->getRawValue();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (count($this->raw) === 0) {
|
||||
$this->raw[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->raw = self::normalizeRaw($this->raw);
|
||||
|
||||
$this->raw[] = [$key => $value];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with another AndGroup.
|
||||
*/
|
||||
public function merge(AndGroup $andGroup): self
|
||||
{
|
||||
$this->raw = array_merge(
|
||||
self::normalizeRaw($this->raw),
|
||||
self::normalizeRaw($andGroup->getRawValue())
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string|int, mixed> $raw
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
private static function normalizeRaw(array $raw): array
|
||||
{
|
||||
if (count($raw) === 1 && array_keys($raw)[0] !== 0) {
|
||||
return [$raw];
|
||||
}
|
||||
|
||||
return $raw;
|
||||
}
|
||||
}
|
||||
433
application/Espo/ORM/Query/Part/Where/Comparison.php
Normal file
433
application/Espo/ORM/Query/Part/Where/Comparison.php
Normal file
@@ -0,0 +1,433 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
use Espo\ORM\Query\Select;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Compares an expression to a value or another expression. Immutable.
|
||||
*/
|
||||
class Comparison implements WhereItem
|
||||
{
|
||||
private const OPERATOR_EQUAL = '=';
|
||||
private const OPERATOR_NOT_EQUAL = '!=';
|
||||
private const OPERATOR_GREATER = '>';
|
||||
private const OPERATOR_GREATER_OR_EQUAL = '>=';
|
||||
private const OPERATOR_LESS = '<';
|
||||
private const OPERATOR_LESS_OR_EQUAL = '<=';
|
||||
private const OPERATOR_LIKE = '*';
|
||||
private const OPERATOR_NOT_LIKE = '!*';
|
||||
private const OPERATOR_IN_SUB_QUERY = '=s';
|
||||
private const OPERATOR_NOT_IN_SUB_QUERY = '!=s';
|
||||
private const OPERATOR_NOT_EQUAL_ANY = '!=any';
|
||||
private const OPERATOR_GREATER_ANY = '>any';
|
||||
private const OPERATOR_GREATER_OR_EQUAL_ANY = '>=any';
|
||||
private const OPERATOR_LESS_ANY = '<any';
|
||||
private const OPERATOR_LESS_OR_EQUAL_ANY = '<=any';
|
||||
private const OPERATOR_EQUAL_ALL = '=all';
|
||||
private const OPERATOR_GREATER_ALL = '>all';
|
||||
private const OPERATOR_GREATER_OR_EQUAL_ALL = '>=all';
|
||||
private const OPERATOR_LESS_ALL = '<all';
|
||||
private const OPERATOR_LESS_OR_EQUAL_ALL = '<=all';
|
||||
|
||||
private string $rawKey;
|
||||
private mixed $rawValue;
|
||||
|
||||
private function __construct(string $rawKey, mixed $rawValue)
|
||||
{
|
||||
$this->rawKey = $rawKey;
|
||||
$this->rawValue = $rawValue;
|
||||
}
|
||||
|
||||
public function getRaw(): array
|
||||
{
|
||||
return [$this->rawKey => $this->rawValue];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return $this->rawKey;
|
||||
}
|
||||
|
||||
public function getRawValue(): mixed
|
||||
{
|
||||
return $this->rawValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float|bool|null $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function equal(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float|bool|null $argument2
|
||||
): self {
|
||||
|
||||
return self::createComparison(self::OPERATOR_EQUAL, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '!=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float|bool|null $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function notEqual(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float|bool|null $argument2
|
||||
): self {
|
||||
|
||||
return self::createComparison(self::OPERATOR_NOT_EQUAL, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'LIKE' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Expression|string $pattern A pattern.
|
||||
* @return self
|
||||
*/
|
||||
public static function like(Expression $subject, Expression|string $pattern): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LIKE, $subject, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'NOT LIKE' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Expression|string $pattern A pattern.
|
||||
* @return self
|
||||
*/
|
||||
public static function notLike(Expression $subject, Expression|string $pattern): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_NOT_LIKE, $subject, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greater(Expression $argument1, Expression|Select|string|int|float $argument2): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greaterOrEqual(Expression $argument1, Expression|Select|string|int|float $argument2): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER_OR_EQUAL, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function less(Expression $argument1, Expression|Select|string|int|float $argument2): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function lessOrEqual(Expression $argument1, Expression|Select|string|int|float $argument2): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS_OR_EQUAL, $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'IN' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Select|scalar[] $set A set of values. A select query or array of scalars.
|
||||
* @return self
|
||||
*/
|
||||
public static function in(Expression $subject, Select|array $set): self
|
||||
{
|
||||
if ($set instanceof Select) {
|
||||
return self::createInOrNotInSubQuery(self::OPERATOR_IN_SUB_QUERY, $subject, $set);
|
||||
}
|
||||
|
||||
return self::createInOrNotInArray(self::OPERATOR_EQUAL, $subject, $set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'NOT IN' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Select|scalar[] $set A set of values. A select query or array of scalars.
|
||||
* @return self
|
||||
*/
|
||||
public static function notIn(Expression $subject, Select|array $set): self
|
||||
{
|
||||
if ($set instanceof Select) {
|
||||
return self::createInOrNotInSubQuery(self::OPERATOR_NOT_IN_SUB_QUERY, $subject, $set);
|
||||
}
|
||||
|
||||
return self::createInOrNotInArray(self::OPERATOR_NOT_EQUAL, $subject, $set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '!= ANY' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function notEqualAny(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_NOT_EQUAL_ANY, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '> ANY' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greaterAny(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER_ANY, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '< ANY' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function lessAny(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS_ANY, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>= ANY' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greaterOrEqualAny(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER_OR_EQUAL_ANY, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<= ANY' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function lessOrEqualAny(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS_OR_EQUAL_ANY, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '= ALL' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function equalAll(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_EQUAL_ALL, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '> ALL' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greaterAll(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER_ALL, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '< ALL' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function lessAll(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS_ALL, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>= ALL' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function greaterOrEqualAll(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_GREATER_OR_EQUAL_ALL, $argument, $subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<= ALL' comparison.
|
||||
*
|
||||
* @param Expression $argument An expression.
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @return self
|
||||
*/
|
||||
public static function lessOrEqualAll(Expression $argument, Select $subQuery): self
|
||||
{
|
||||
return self::createComparison(self::OPERATOR_LESS_OR_EQUAL_ALL, $argument, $subQuery);
|
||||
}
|
||||
|
||||
private static function createComparison(
|
||||
string $operator,
|
||||
Expression|string $argument1,
|
||||
Expression|Select|string|int|float|bool|null $argument2
|
||||
): self {
|
||||
|
||||
if (is_string($argument1)) {
|
||||
$key = $argument1;
|
||||
|
||||
if ($key === '') {
|
||||
throw new RuntimeException("Expression can't be empty.");
|
||||
}
|
||||
} else {
|
||||
$key = $argument1->getValue();
|
||||
}
|
||||
|
||||
if (str_ends_with($key, ':')) {
|
||||
throw new RuntimeException("Expression should not end with `:`.");
|
||||
}
|
||||
|
||||
$key .= $operator;
|
||||
|
||||
if ($argument2 instanceof Expression) {
|
||||
$key .= ':';
|
||||
|
||||
$value = $argument2->getValue();
|
||||
} else {
|
||||
$value = $argument2;
|
||||
}
|
||||
|
||||
return new self($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param scalar[] $valueList
|
||||
*/
|
||||
private static function createInOrNotInArray(
|
||||
string $operator,
|
||||
Expression|string $argument1,
|
||||
array $valueList
|
||||
): self {
|
||||
|
||||
foreach ($valueList as $item) {
|
||||
if (!is_scalar($item)) {
|
||||
throw new RuntimeException("Array items must be scalar.");
|
||||
}
|
||||
}
|
||||
|
||||
if (is_string($argument1)) {
|
||||
$key = $argument1;
|
||||
|
||||
if ($key === '') {
|
||||
throw new RuntimeException("Expression can't be empty.");
|
||||
}
|
||||
|
||||
if (str_ends_with($key, ':')) {
|
||||
throw new RuntimeException("Expression can't end with `:`.");
|
||||
}
|
||||
} else {
|
||||
$key = $argument1->getValue();
|
||||
}
|
||||
|
||||
$key .= $operator;
|
||||
|
||||
return new self($key, $valueList);
|
||||
}
|
||||
|
||||
private static function createInOrNotInSubQuery(
|
||||
string $operator,
|
||||
Expression|string $argument1,
|
||||
Select $query
|
||||
): self {
|
||||
|
||||
if (is_string($argument1)) {
|
||||
$key = $argument1;
|
||||
|
||||
if ($key === '') {
|
||||
throw new RuntimeException("Expression can't be empty.");
|
||||
}
|
||||
|
||||
if (str_ends_with($key, ':')) {
|
||||
throw new RuntimeException("Expression can't end with `:`.");
|
||||
}
|
||||
} else {
|
||||
$key = $argument1->getValue();
|
||||
}
|
||||
|
||||
$key .= $operator;
|
||||
|
||||
return new self($key, $query);
|
||||
}
|
||||
}
|
||||
61
application/Espo/ORM/Query/Part/Where/Exists.php
Normal file
61
application/Espo/ORM/Query/Part/Where/Exists.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
use Espo\ORM\Query\Select;
|
||||
|
||||
/**
|
||||
* An EXISTS-operator. Immutable.
|
||||
*/
|
||||
class Exists implements WhereItem
|
||||
{
|
||||
private function __construct(private Select $rawValue) {}
|
||||
|
||||
public function getRaw(): array
|
||||
{
|
||||
return ['EXISTS' => $this->getRawValue()];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return 'EXISTS';
|
||||
}
|
||||
|
||||
public function getRawValue(): Select
|
||||
{
|
||||
return $this->rawValue;
|
||||
}
|
||||
|
||||
public static function create(Select $subQuery): self
|
||||
{
|
||||
return new self($subQuery);
|
||||
}
|
||||
}
|
||||
80
application/Espo/ORM/Query/Part/Where/Not.php
Normal file
80
application/Espo/ORM/Query/Part/Where/Not.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
|
||||
/**
|
||||
* A NOT-operator. Immutable.
|
||||
*/
|
||||
class Not implements WhereItem
|
||||
{
|
||||
/** @var array<string|int, mixed> */
|
||||
private $rawValue = [];
|
||||
|
||||
public function getRaw(): array
|
||||
{
|
||||
return ['NOT' => $this->getRawValue()];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return 'NOT';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function getRawValue(): array
|
||||
{
|
||||
return $this->rawValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string|int, mixed> $whereClause
|
||||
*/
|
||||
public static function fromRaw(array $whereClause): self
|
||||
{
|
||||
if (count($whereClause) === 1 && array_keys($whereClause)[0] === 0) {
|
||||
$whereClause = $whereClause[0];
|
||||
}
|
||||
|
||||
$obj = new self();
|
||||
|
||||
$obj->rawValue = $whereClause;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function create(WhereItem $item): self
|
||||
{
|
||||
return self::fromRaw($item->getRaw());
|
||||
}
|
||||
}
|
||||
100
application/Espo/ORM/Query/Part/Where/OrGroup.php
Normal file
100
application/Espo/ORM/Query/Part/Where/OrGroup.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
|
||||
/**
|
||||
* OR-group. Immutable.
|
||||
*/
|
||||
class OrGroup implements WhereItem
|
||||
{
|
||||
|
||||
/** @var array<string|int, mixed> */
|
||||
private $rawValue = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function getRaw(): array
|
||||
{
|
||||
return ['OR' => $this->rawValue];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return 'OR';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function getRawValue(): array
|
||||
{
|
||||
return $this->rawValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a number of items.
|
||||
*/
|
||||
public function getItemCount(): int
|
||||
{
|
||||
return count($this->rawValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string|int, mixed> $whereClause
|
||||
*/
|
||||
public static function fromRaw(array $whereClause): self
|
||||
{
|
||||
$obj = new self();
|
||||
|
||||
$obj->rawValue = $whereClause;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function create(WhereItem ...$itemList): self
|
||||
{
|
||||
$builder = self::createBuilder();
|
||||
|
||||
foreach ($itemList as $item) {
|
||||
$builder->add($item);
|
||||
}
|
||||
|
||||
return $builder->build();
|
||||
}
|
||||
|
||||
public static function createBuilder(): OrGroupBuilder
|
||||
{
|
||||
return new OrGroupBuilder();
|
||||
}
|
||||
}
|
||||
95
application/Espo/ORM/Query/Part/Where/OrGroupBuilder.php
Normal file
95
application/Espo/ORM/Query/Part/Where/OrGroupBuilder.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part\Where;
|
||||
|
||||
use Espo\ORM\Query\Part\WhereItem;
|
||||
|
||||
class OrGroupBuilder
|
||||
{
|
||||
/** @var array<string|int, mixed> */
|
||||
private array $raw = [];
|
||||
|
||||
public function build(): OrGroup
|
||||
{
|
||||
return OrGroup::fromRaw($this->raw);
|
||||
}
|
||||
|
||||
public function add(WhereItem $item): self
|
||||
{
|
||||
$key = $item->getRawKey();
|
||||
$value = $item->getRawValue();
|
||||
|
||||
if ($item instanceof AndGroup) {
|
||||
$this->raw = self::normalizeRaw($this->raw);
|
||||
|
||||
$this->raw[] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (count($this->raw) === 0) {
|
||||
$this->raw[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->raw = self::normalizeRaw($this->raw);
|
||||
|
||||
$this->raw[] = [$key => $value];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with another OrGroup.
|
||||
*/
|
||||
public function merge(OrGroup $orGroup): self
|
||||
{
|
||||
$this->raw = array_merge(
|
||||
self::normalizeRaw($this->raw),
|
||||
self::normalizeRaw($orGroup->getRawValue())
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string|int, mixed> $raw
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
private static function normalizeRaw(array $raw): array
|
||||
{
|
||||
if (count($raw) === 1 && array_keys($raw)[0] !== 0) {
|
||||
return [$raw];
|
||||
}
|
||||
|
||||
return $raw;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user