Initial commit
This commit is contained in:
217
application/Espo/ORM/Query/Part/Condition.php
Normal file
217
application/Espo/ORM/Query/Part/Condition.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?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;
|
||||
|
||||
use Espo\ORM\Query\Part\Where\AndGroup;
|
||||
use Espo\ORM\Query\Part\Where\Comparison;
|
||||
use Espo\ORM\Query\Part\Where\Exists;
|
||||
use Espo\ORM\Query\Part\Where\Not;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
|
||||
use Espo\ORM\Query\Select;
|
||||
|
||||
/**
|
||||
* A util-class for creating items that can be used as a where-clause.
|
||||
*/
|
||||
class Condition
|
||||
{
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Create 'AND' group.
|
||||
*/
|
||||
public static function and(WhereItem ...$items): AndGroup
|
||||
{
|
||||
return AndGroup::create(...$items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'OR' group.
|
||||
*/
|
||||
public static function or(WhereItem ...$items): OrGroup
|
||||
{
|
||||
return OrGroup::create(...$items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'NOT'.
|
||||
*/
|
||||
public static function not(WhereItem $item): Not
|
||||
{
|
||||
return Not::create($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create `EXISTS`.
|
||||
*/
|
||||
public static function exists(Select $subQuery): Exists
|
||||
{
|
||||
return Exists::create($subQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a column reference expression.
|
||||
*
|
||||
* @param string $expression Examples: `columnName`, `alias.columnName`.
|
||||
*/
|
||||
public static function column(string $expression): Expression
|
||||
{
|
||||
return Expression::column($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float|bool|null $argument2 A scalar, expression or sub-query.
|
||||
*/
|
||||
public static function equal(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float|bool|null $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::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.
|
||||
*/
|
||||
public static function notEqual(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float|bool|null $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::notEqual($argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'LIKE' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Expression|string $pattern A pattern.
|
||||
*/
|
||||
public static function like(Expression $subject, Expression|string $pattern): Comparison
|
||||
{
|
||||
return Comparison::like($subject, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create 'NOT LIKE' comparison.
|
||||
*
|
||||
* @param Expression $subject What to test.
|
||||
* @param Expression|string $pattern A pattern.
|
||||
*/
|
||||
public static function notLike(Expression $subject, Expression|string $pattern): Comparison
|
||||
{
|
||||
return Comparison::notLike($subject, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
*/
|
||||
public static function greater(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::greater($argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '>=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
*/
|
||||
public static function greaterOrEqual(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::greaterOrEqual($argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
*/
|
||||
public static function less(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::less($argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create '<=' comparison.
|
||||
*
|
||||
* @param Expression $argument1 An expression.
|
||||
* @param Expression|Select|string|int|float $argument2 A scalar, expression or sub-query.
|
||||
*/
|
||||
public static function lessOrEqual(
|
||||
Expression $argument1,
|
||||
Expression|Select|string|int|float $argument2
|
||||
): Comparison {
|
||||
|
||||
return Comparison::lessOrEqual($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.
|
||||
*/
|
||||
public static function in(Expression $subject, Select|array $set): Comparison
|
||||
{
|
||||
return Comparison::in($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.
|
||||
*/
|
||||
public static function notIn(Expression $subject, Select|array $set): Comparison
|
||||
{
|
||||
return Comparison::notIn($subject, $set);
|
||||
}
|
||||
}
|
||||
820
application/Espo/ORM/Query/Part/Expression.php
Normal file
820
application/Espo/ORM/Query/Part/Expression.php
Normal file
@@ -0,0 +1,820 @@
|
||||
<?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;
|
||||
|
||||
use Espo\ORM\Query\Part\Expression\Util;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* A complex expression. Can be a function or a simple column reference. Immutable.
|
||||
*/
|
||||
class Expression implements WhereItem
|
||||
{
|
||||
private string $expression;
|
||||
|
||||
public function __construct(string $expression)
|
||||
{
|
||||
if ($expression === '') {
|
||||
throw new RuntimeException("Expression can't be empty.");
|
||||
}
|
||||
|
||||
if (str_ends_with($expression, ':')) {
|
||||
throw new RuntimeException("Expression should not end with `:`.");
|
||||
}
|
||||
|
||||
$this->expression = $expression;
|
||||
}
|
||||
|
||||
public function getRaw(): array
|
||||
{
|
||||
return [$this->getRawKey() => null];
|
||||
}
|
||||
|
||||
public function getRawKey(): string
|
||||
{
|
||||
return $this->expression . ':';
|
||||
}
|
||||
|
||||
public function getRawValue(): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string expression.
|
||||
*/
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an expression from a string.
|
||||
*/
|
||||
public static function create(string $expression): self
|
||||
{
|
||||
return new self($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an expression from a scalar value or NULL.
|
||||
*
|
||||
* @param string|float|int|bool|null $value A scalar or NULL.
|
||||
*/
|
||||
public static function value(string|float|int|bool|null $value): self
|
||||
{
|
||||
return self::create(self::stringifyArgument($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a column reference expression.
|
||||
*
|
||||
* @param string $expression Examples: `columnName`, `alias.columnName`.
|
||||
*/
|
||||
public static function column(string $expression): self
|
||||
{
|
||||
$string = $expression;
|
||||
|
||||
if (strlen($string) && $string[0] === '@') {
|
||||
$string = substr($string, 1);
|
||||
}
|
||||
|
||||
if ($string === '') {
|
||||
throw new RuntimeException("Empty column.");
|
||||
}
|
||||
|
||||
if (!preg_match('/^[a-zA-Z\d.]+$/', $string)) {
|
||||
throw new RuntimeException("Bad column. Must be of letters, digits. Can have a dot.");
|
||||
}
|
||||
|
||||
return self::create($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an alias reference expression.
|
||||
*
|
||||
* @param string $expression Examples: `someAlias`, `subQueryAlias.someAlias`.
|
||||
* @since 8.1.0
|
||||
*/
|
||||
public static function alias(string $expression): self
|
||||
{
|
||||
if ($expression === '') {
|
||||
throw new RuntimeException("Empty alias.");
|
||||
}
|
||||
|
||||
if (!preg_match('/^[a-zA-Z\d.]+$/', $expression)) {
|
||||
throw new RuntimeException("Bad alias expression. Must be of letters, digits. Can have a dot.");
|
||||
}
|
||||
|
||||
if (str_contains($expression, '.')) {
|
||||
[$left, $right] = explode('.', $expression, 2);
|
||||
|
||||
return self::create($left . '.#' . $right);
|
||||
}
|
||||
|
||||
return self::create('#' . $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'COUNT' function.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function count(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('COUNT', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MIN' function.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function min(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('MIN', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MAX' function.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function max(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('MAX', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'SUM' function.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function sum(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('SUM', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'AVG' function.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function average(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('AVG', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'IF' function. Return $then if a condition is true, $else otherwise.
|
||||
*
|
||||
* @param Expression $condition A condition.
|
||||
* @param Expression|string|int|float|bool|null $then Then.
|
||||
* @param Expression|string|int|float|bool|null $else Else.
|
||||
*/
|
||||
public static function if(
|
||||
Expression $condition,
|
||||
Expression|string|int|float|bool|null $then,
|
||||
Expression|string|int|float|bool|null $else
|
||||
): self {
|
||||
|
||||
return self::composeFunction('IF', $condition, $then, $else);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'CASE' expression. Even arguments define 'WHEN' conditions, following odd arguments
|
||||
* define 'THEN' values. The last unmatched argument defines the 'ELSE' value.
|
||||
*
|
||||
* @param Expression|scalar|null ...$arguments Arguments.
|
||||
*/
|
||||
public static function switch(Expression|string|int|float|bool|null ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('SWITCH', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'CASE' expression that maps keys to values. The first argument is the value to map.
|
||||
* Odd arguments define keys, the following even arguments define mapped values.
|
||||
* The last unmatched argument defines the 'ELSE' value.
|
||||
*
|
||||
* @param Expression|scalar|null ...$arguments Arguments.
|
||||
*/
|
||||
public static function map(Expression|string|int|float|bool|null ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 3) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('MAP', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'IFNULL' function. If the first argument is not NULL, returns it,
|
||||
* otherwise returns the second argument.
|
||||
*
|
||||
* @param Expression $value A value.
|
||||
* @param Expression|string|int|float|bool $fallbackValue A fallback value.
|
||||
*/
|
||||
public static function ifNull(Expression $value, Expression|string|int|float|bool $fallbackValue): self
|
||||
{
|
||||
return self::composeFunction('IFNULL', $value, $fallbackValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'NULLIF' function. If $arg1 = $arg2, returns NULL,
|
||||
* otherwise returns the first argument.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function nullIf(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('NULLIF', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'LIKE' operator.
|
||||
*
|
||||
* Example: `like(Expression:column('test'), 'test%'`.
|
||||
*
|
||||
* @param Expression $subject A subject.
|
||||
* @param Expression|string $pattern A pattern.
|
||||
*/
|
||||
public static function like(Expression $subject, Expression|string $pattern): self
|
||||
{
|
||||
return self::composeFunction('LIKE', $subject, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* '=' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function equal(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('EQUAL', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* '<>' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function notEqual(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('NOT_EQUAL', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* '>' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function greater(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('GREATER_THAN', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* '<' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function less(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('LESS_THAN', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* '>=' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function greaterOrEqual(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('GREATER_THAN_OR_EQUAL', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* '<=' operator.
|
||||
*
|
||||
* @param Expression|string|int|float|bool $argument1
|
||||
* @param Expression|string|int|float|bool $argument2
|
||||
*/
|
||||
public static function lessOrEqual(
|
||||
Expression|string|int|float|bool $argument1,
|
||||
Expression|string|int|float|bool $argument2
|
||||
): self {
|
||||
|
||||
return self::composeFunction('LESS_THAN_OR_EQUAL', $argument1, $argument2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'IS NULL' operator.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function isNull(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('IS_NULL', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'IS NOT NULL' operator.
|
||||
*
|
||||
* @param Expression $expression
|
||||
*/
|
||||
public static function isNotNull(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('IS_NOT_NULL', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'IN' operator. Check whether a value is within a set of values.
|
||||
*
|
||||
* @param Expression $expression
|
||||
* @param Expression[]|string[]|int[]|float[]|bool[] $values
|
||||
*/
|
||||
public static function in(Expression $expression, array $values): self
|
||||
{
|
||||
return self::composeFunction('IN', $expression, ...$values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'NOT IN' operator. Check whether a value is not within a set of values.
|
||||
*
|
||||
* @param Expression $expression
|
||||
* @param Expression[]|string[]|int[]|float[]|bool[] $values
|
||||
*/
|
||||
public static function notIn(Expression $expression, array $values): self
|
||||
{
|
||||
return self::composeFunction('NOT_IN', $expression, ...$values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'COALESCE' function. Returns the first non-NULL value in the list.
|
||||
*/
|
||||
public static function coalesce(Expression ...$expressions): self
|
||||
{
|
||||
return self::composeFunction('COALESCE', ...$expressions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MONTH' function. Returns a month number of a passed date or date-time.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function month(Expression $date): self
|
||||
{
|
||||
return self::composeFunction('MONTH_NUMBER', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'WEEK' function. Returns a week number of a passed date or date-time.
|
||||
*
|
||||
* @param Expression $date
|
||||
* @param int $weekStart A week start. `0` for Sunday, `1` for Monday.
|
||||
*/
|
||||
public static function week(Expression $date, int $weekStart = 0): self
|
||||
{
|
||||
if ($weekStart !== 0 && $weekStart !== 1) {
|
||||
throw new RuntimeException("Week start can be only 0 or 1.");
|
||||
}
|
||||
|
||||
if ($weekStart === 1) {
|
||||
return self::composeFunction('WEEK_NUMBER_1', $date);
|
||||
}
|
||||
|
||||
return self::composeFunction('WEEK_NUMBER', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'DAYOFWEEK' function. A day of week of a passed date or date-time. 1..7.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function dayOfWeek(Expression $date): self
|
||||
{
|
||||
return self::composeFunction('DAYOFWEEK', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'DAYOFMONTH' function. A day of month of a passed date or date-time. 1..31.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function dayOfMonth(Expression $date): self
|
||||
{
|
||||
return self::composeFunction('DAYOFMONTH', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'YEAR' function. A year number of a passed date or date-time.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function year(Expression $date): self
|
||||
{
|
||||
return self::composeFunction('YEAR', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'YEAR' function taking into account a fiscal year start.
|
||||
*
|
||||
* @param Expression $date
|
||||
* @param int $fiscalYearStart A month number of a fiscal year start. 1..12.
|
||||
*/
|
||||
public static function yearFiscal(Expression $date, int $fiscalYearStart = 1): self
|
||||
{
|
||||
if ($fiscalYearStart < 1 || $fiscalYearStart > 12) {
|
||||
throw new RuntimeException("Bad fiscal year start.");
|
||||
}
|
||||
|
||||
return self::composeFunction('YEAR_' . strval($fiscalYearStart), $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'QUARTER' function. A quarter number of a passed date or date-time. 1..4.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function quarter(Expression $date): self
|
||||
{
|
||||
return self::composeFunction('QUARTER_NUMBER', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'HOUR' function. A hour number of a passed date-time. 0..23.
|
||||
*
|
||||
* @param Expression $dateTime
|
||||
*/
|
||||
public static function hour(Expression $dateTime): self
|
||||
{
|
||||
return self::composeFunction('HOUR', $dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MINUTE' function. A minute number of a passed date-time. 0..59.
|
||||
*
|
||||
* @param Expression $dateTime
|
||||
*/
|
||||
public static function minute(Expression $dateTime): self
|
||||
{
|
||||
return self::composeFunction('MINUTE', $dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'SECOND' function. A second number of a passed date-time. 0..59.
|
||||
*
|
||||
* @param Expression $dateTime
|
||||
*/
|
||||
public static function second(Expression $dateTime): self
|
||||
{
|
||||
return self::composeFunction('SECOND', $dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'UNIX_TIMESTAMP' function. Seconds.
|
||||
*
|
||||
* @param Expression $dateTime
|
||||
* @since 9.0.0
|
||||
*/
|
||||
public static function unixTimestamp(Expression $dateTime): self
|
||||
{
|
||||
return self::composeFunction('UNIX_TIMESTAMP', $dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'NOW' function. A current date and time.
|
||||
*/
|
||||
public static function now(): self
|
||||
{
|
||||
return self::composeFunction('NOW');
|
||||
}
|
||||
|
||||
/**
|
||||
* 'DATE' function. Returns a date part of a date-time.
|
||||
*
|
||||
* @param Expression $dateTime
|
||||
*/
|
||||
public static function date(Expression $dateTime): self
|
||||
{
|
||||
return self::composeFunction('DATE', $dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Time zone conversion function. Converts a passed data-time applying a hour offset.
|
||||
*
|
||||
* @param Expression $date
|
||||
*/
|
||||
public static function convertTimezone(Expression $date, float $offset): self
|
||||
{
|
||||
return self::composeFunction('TZ', $date, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'CONCAT' function. Concatenates multiple strings.
|
||||
*
|
||||
* @param Expression|string ...$strings Strings.
|
||||
*/
|
||||
public static function concat(Expression|string ...$strings): self
|
||||
{
|
||||
return self::composeFunction('CONCAT', ...$strings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'LEFT' function. Returns a specified number of characters from the left of a string.
|
||||
*/
|
||||
public static function left(Expression $string, int $offset): self
|
||||
{
|
||||
return self::composeFunction('LEFT', $string, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'LOWER' function. Converts a string to a lower case.
|
||||
*/
|
||||
public static function lowerCase(Expression $string): self
|
||||
{
|
||||
return self::composeFunction('LOWER', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'UPPER' function. Converts a string to an upper case.
|
||||
*/
|
||||
public static function upperCase(Expression $string): self
|
||||
{
|
||||
return self::composeFunction('UPPER', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'TRIM' function. Removes leading and trailing spaces.
|
||||
*/
|
||||
public static function trim(Expression $string): self
|
||||
{
|
||||
return self::composeFunction('TRIM', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'BINARY' function. Converts a string value to a binary string.
|
||||
*/
|
||||
public static function binary(Expression $string): self
|
||||
{
|
||||
return self::composeFunction('BINARY', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'CHAR_LENGTH' function. A number of characters in a string.
|
||||
*/
|
||||
public static function charLength(Expression $string): self
|
||||
{
|
||||
return self::composeFunction('CHAR_LENGTH', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'REPLACE' function. Replaces all the occurrences of a sub-string within a string.
|
||||
*
|
||||
* @param Expression $haystack A subject.
|
||||
* @param Expression|string $needle A string to be replaced.
|
||||
* @param Expression|string $replaceWith A string to replace with.
|
||||
*/
|
||||
public static function replace(
|
||||
Expression $haystack,
|
||||
Expression|string $needle,
|
||||
Expression|string $replaceWith
|
||||
): self {
|
||||
|
||||
return self::composeFunction('REPLACE', $haystack, $needle, $replaceWith);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'FIELD' operator (in MySQL). Returns an index (position) of an expression
|
||||
* in a list. Returns `0` if not found. The first index is `1`.
|
||||
*
|
||||
* @param Expression $expression
|
||||
* @param Expression[]|string[]|int[]|float[] $list
|
||||
*/
|
||||
public static function positionInList(Expression $expression, array $list): self
|
||||
{
|
||||
return self::composeFunction('POSITION_IN_LIST', $expression, ...$list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'ADD' function. Adds two or more numbers.
|
||||
*
|
||||
* @param Expression|int|float ...$arguments
|
||||
*/
|
||||
public static function add(Expression|int|float ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('ADD', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'SUB' function. Subtraction.
|
||||
*
|
||||
* @param Expression|int|float ...$arguments
|
||||
*/
|
||||
public static function subtract(Expression|int|float ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('SUB', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MUL' function. Multiplication.
|
||||
*
|
||||
* @param Expression|int|float ...$arguments
|
||||
*/
|
||||
public static function multiply(Expression|int|float ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('MUL', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'DIV' function. Division.
|
||||
*
|
||||
* @param Expression|int|float ...$arguments
|
||||
*/
|
||||
public static function divide(Expression|int|float ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('DIV', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'MOD' function. Returns a remainder of a number divided by another number.
|
||||
*
|
||||
* @param Expression|int|float ...$arguments
|
||||
*/
|
||||
public static function modulo(Expression|int|float ...$arguments): self
|
||||
{
|
||||
if (count($arguments) < 2) {
|
||||
throw new RuntimeException("Too few arguments.");
|
||||
}
|
||||
|
||||
return self::composeFunction('MOD', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'FLOOR' function. The largest integer value not greater than the argument.
|
||||
*/
|
||||
public static function floor(Expression $number): self
|
||||
{
|
||||
return self::composeFunction('FLOOR', $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'CEIL' function. The largest integer value not greater than the argument.
|
||||
*/
|
||||
public static function ceil(Expression $number): self
|
||||
{
|
||||
return self::composeFunction('CEIL', $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'ROUND' function. Rounds a number to a specified number of decimal places.
|
||||
*/
|
||||
public static function round(Expression $number, int $precision = 0): self
|
||||
{
|
||||
return self::composeFunction('ROUND', $number, $precision);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'GREATEST' function. A max value from a list of expressions.
|
||||
*/
|
||||
public static function greatest(Expression ...$arguments): self
|
||||
{
|
||||
return self::composeFunction('GREATEST', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'LEAST' function. A min value from a list of expressions.
|
||||
*/
|
||||
public static function least(Expression ...$arguments): self
|
||||
{
|
||||
return self::composeFunction('LEAST', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'ANY_VALUE' function.
|
||||
*
|
||||
* @since 9.1.6
|
||||
*/
|
||||
public function anyValue(Expression $expression): self
|
||||
{
|
||||
return self::composeFunction('ANY_VALUE', $expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'AND' operator. Returns TRUE if all arguments are TRUE.
|
||||
*/
|
||||
public static function and(Expression ...$arguments): self
|
||||
{
|
||||
return self::composeFunction('AND', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'OR' operator. Returns TRUE if at least one argument is TRUE.
|
||||
*/
|
||||
public static function or(Expression ...$arguments): self
|
||||
{
|
||||
return self::composeFunction('OR', ...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'NOT' operator. Negates an expression.
|
||||
*/
|
||||
public static function not(Expression $argument): self
|
||||
{
|
||||
return self::composeFunction('NOT', $argument);
|
||||
}
|
||||
|
||||
/**
|
||||
* 'ROW' constructor.
|
||||
*/
|
||||
public static function row(Expression ...$arguments): self
|
||||
{
|
||||
return self::composeFunction('ROW', ...$arguments);
|
||||
}
|
||||
|
||||
private static function composeFunction(
|
||||
string $function,
|
||||
Expression|bool|int|float|string|null ...$arguments
|
||||
): self {
|
||||
|
||||
return Util::composeFunction($function, ...$arguments);
|
||||
}
|
||||
|
||||
private static function stringifyArgument(Expression|bool|int|float|string|null $argument): string
|
||||
{
|
||||
return Util::stringifyArgument($argument);
|
||||
}
|
||||
}
|
||||
88
application/Espo/ORM/Query/Part/Expression/Util.php
Normal file
88
application/Espo/ORM/Query/Part/Expression/Util.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?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\Expression;
|
||||
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
|
||||
class Util
|
||||
{
|
||||
/**
|
||||
* Compose an expression by a function name and arguments.
|
||||
*
|
||||
* @param Expression|bool|int|float|string|null ...$arguments Arguments
|
||||
*/
|
||||
public static function composeFunction(
|
||||
string $function,
|
||||
Expression|bool|int|float|string|null ...$arguments
|
||||
): Expression {
|
||||
|
||||
$stringifiedItems = array_map(
|
||||
function ($item) {
|
||||
return self::stringifyArgument($item);
|
||||
},
|
||||
$arguments
|
||||
);
|
||||
|
||||
$expression = $function . ':(' . implode(', ', $stringifiedItems) . ')';
|
||||
|
||||
return Expression::create($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stringify an argument.
|
||||
*
|
||||
* @param Expression|bool|int|float|string|null $argument
|
||||
*/
|
||||
public static function stringifyArgument(Expression|bool|int|float|string|null $argument): string
|
||||
{
|
||||
|
||||
if ($argument instanceof Expression) {
|
||||
return $argument->getValue();
|
||||
}
|
||||
|
||||
if (is_null($argument)) {
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
if (is_bool($argument)) {
|
||||
return $argument ? 'TRUE': 'FALSE';
|
||||
}
|
||||
|
||||
if (is_int($argument)) {
|
||||
return strval($argument);
|
||||
}
|
||||
|
||||
if (is_float($argument)) {
|
||||
return strval($argument);
|
||||
}
|
||||
|
||||
return '\'' . str_replace('\'', '\\\'', $argument) . '\'';
|
||||
}
|
||||
}
|
||||
300
application/Espo/ORM/Query/Part/Join.php
Normal file
300
application/Espo/ORM/Query/Part/Join.php
Normal file
@@ -0,0 +1,300 @@
|
||||
<?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;
|
||||
|
||||
use Espo\ORM\Query\Part\Join\JoinType;
|
||||
use Espo\ORM\Query\Select;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* A join item. Immutable.
|
||||
*/
|
||||
class Join
|
||||
{
|
||||
/** A table join. */
|
||||
public const MODE_TABLE = 0;
|
||||
/** A relation join. */
|
||||
public const MODE_RELATION = 1;
|
||||
/** A sub-query join. */
|
||||
public const MODE_SUB_QUERY = 3;
|
||||
|
||||
private ?WhereItem $conditions = null;
|
||||
private bool $onlyMiddle = false;
|
||||
private bool $isLateral = false;
|
||||
private ?JoinType $type = null;
|
||||
|
||||
private function __construct(
|
||||
private string|Select $target,
|
||||
private ?string $alias = null
|
||||
) {
|
||||
if ($target === '' || $alias === '') {
|
||||
throw new RuntimeException("Bad join.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a join target. A relation name, table or sub-query.
|
||||
* A relation name is in camelCase, a table is in CamelCase.
|
||||
*/
|
||||
public function getTarget(): string|Select
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an alias.
|
||||
*/
|
||||
public function getAlias(): ?string
|
||||
{
|
||||
return $this->alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get join conditions.
|
||||
*/
|
||||
public function getConditions(): ?WhereItem
|
||||
{
|
||||
return $this->conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a sub-query join.
|
||||
*/
|
||||
public function isSubQuery(): bool
|
||||
{
|
||||
return !is_string($this->target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a table join.
|
||||
*/
|
||||
public function isTable(): bool
|
||||
{
|
||||
return is_string($this->target) && $this->target[0] === ucfirst($this->target[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a relation join.
|
||||
*/
|
||||
public function isRelation(): bool
|
||||
{
|
||||
return !$this->isSubQuery() && !$this->isTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a join mode.
|
||||
*
|
||||
* @return self::MODE_TABLE|self::MODE_RELATION|self::MODE_SUB_QUERY
|
||||
*/
|
||||
public function getMode(): int
|
||||
{
|
||||
if ($this->isSubQuery()) {
|
||||
return self::MODE_SUB_QUERY;
|
||||
}
|
||||
|
||||
if ($this->isRelation()) {
|
||||
return self::MODE_RELATION;
|
||||
}
|
||||
|
||||
return self::MODE_TABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is only middle table to be joined.
|
||||
*/
|
||||
public function isOnlyMiddle(): bool
|
||||
{
|
||||
return $this->onlyMiddle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is LATERAL.
|
||||
*
|
||||
* @since 9.1.6
|
||||
*/
|
||||
public function isLateral(): bool
|
||||
{
|
||||
return $this->isLateral;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a join type.
|
||||
*
|
||||
* @return JoinType|null
|
||||
*
|
||||
* @since 9.2.0
|
||||
*/
|
||||
public function getType(): ?JoinType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create.
|
||||
*
|
||||
* @param string|Select $target
|
||||
* A relation name, table or sub-query. A relation name should be in camelCase, a table in CamelCase.
|
||||
* When joining a table or sub-query, conditions should be specified.
|
||||
* When joining a relation, conditions will be applied automatically, additional conditions can
|
||||
* be specified as well.
|
||||
* @param ?string $alias An alias.
|
||||
*/
|
||||
public static function create(string|Select $target, ?string $alias = null): self
|
||||
{
|
||||
return new self($target, $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create with a table target.
|
||||
*
|
||||
* @param string $table A table name. Should start with an upper case letter.
|
||||
* @param ?string $alias An alias.
|
||||
*/
|
||||
public static function createWithTableTarget(string $table, ?string $alias = null): self
|
||||
{
|
||||
return self::create(ucfirst($table), $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create with a relation target. Conditions will be applied automatically.
|
||||
*
|
||||
* @param string $relation A relation name. Should start with a lower case letter.
|
||||
* @param ?string $alias An alias.
|
||||
*/
|
||||
public static function createWithRelationTarget(string $relation, ?string $alias = null): self
|
||||
{
|
||||
return self::create(lcfirst($relation), $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create with a sub-query.
|
||||
*
|
||||
* @param Select $subQuery A sub-query.
|
||||
* @param string $alias An alias.
|
||||
*/
|
||||
public static function createWithSubQuery(Select $subQuery, string $alias): self
|
||||
{
|
||||
return new self($subQuery, $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with an alias.
|
||||
*/
|
||||
public function withAlias(?string $alias): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->alias = $alias;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with join conditions.
|
||||
*/
|
||||
public function withConditions(?WhereItem $conditions): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->conditions = $conditions;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join only middle table. For many-to-many relationships.
|
||||
*/
|
||||
public function withOnlyMiddle(bool $onlyMiddle = true): self
|
||||
{
|
||||
if (!$this->isRelation()) {
|
||||
throw new LogicException("Only-middle is compatible only with relation joins.");
|
||||
}
|
||||
|
||||
$obj = clone $this;
|
||||
$obj->onlyMiddle = $onlyMiddle;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* With LATERAL. Only for a sub-query join.
|
||||
*
|
||||
* @since 9.1.6
|
||||
*/
|
||||
public function withLateral(bool $isLateral = true): self
|
||||
{
|
||||
if (!$this->isSubQuery()) {
|
||||
throw new LogicException("Lateral can be used only with sub-query joins.");
|
||||
}
|
||||
|
||||
$obj = clone $this;
|
||||
$obj->isLateral = $isLateral;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* With LEFT type.
|
||||
*
|
||||
* @since 9.2.0.
|
||||
*/
|
||||
public function withLeft(): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->type = JoinType::left;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* With INNER type.
|
||||
*
|
||||
* @since 9.2.0.
|
||||
*/
|
||||
public function withInner(): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->type = JoinType::inner;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* With a join type.
|
||||
*
|
||||
* @since 9.2.0.
|
||||
*/
|
||||
public function withType(JoinType $type): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->type = $type;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
46
application/Espo/ORM/Query/Part/Join/JoinType.php
Normal file
46
application/Espo/ORM/Query/Part/Join/JoinType.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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\Join;
|
||||
|
||||
/**
|
||||
* @since 9.2.0
|
||||
*/
|
||||
enum JoinType: string
|
||||
{
|
||||
/**
|
||||
* An INNER join.
|
||||
*/
|
||||
case inner = 'inner';
|
||||
|
||||
/**
|
||||
* A LEFT join.
|
||||
*/
|
||||
case left = 'left';
|
||||
}
|
||||
156
application/Espo/ORM/Query/Part/Order.php
Normal file
156
application/Espo/ORM/Query/Part/Order.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?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;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* An order item. Immutable.
|
||||
*
|
||||
* Immutable.
|
||||
*/
|
||||
class Order
|
||||
{
|
||||
public const ASC = 'ASC';
|
||||
public const DESC = 'DESC';
|
||||
|
||||
private Expression $expression;
|
||||
private bool $isDesc = false;
|
||||
|
||||
private function __construct(Expression $expression)
|
||||
{
|
||||
$this->expression = $expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an expression.
|
||||
*/
|
||||
public function getExpression(): Expression
|
||||
{
|
||||
return $this->expression;
|
||||
}
|
||||
|
||||
public function isDesc(): bool
|
||||
{
|
||||
return $this->isDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a direction.
|
||||
*
|
||||
* @return self::DESC|self::ASC
|
||||
*/
|
||||
public function getDirection(): string
|
||||
{
|
||||
return $this->isDesc ? self::DESC : self::ASC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create.
|
||||
*/
|
||||
public static function create(Expression $expression): self
|
||||
{
|
||||
return new self($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from a string expression.
|
||||
*/
|
||||
public static function fromString(string $expression): self
|
||||
{
|
||||
return self::create(
|
||||
Expression::create($expression)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an order by position in list.
|
||||
* Note: Reverses the list and applies DESC order.
|
||||
*
|
||||
* @param string[]|int[]|float[] $list
|
||||
*/
|
||||
public static function createByPositionInList(Expression $expression, array $list): self
|
||||
{
|
||||
$orderExpression = Expression::positionInList($expression, array_reverse($list));
|
||||
|
||||
return self::create($orderExpression)->withDesc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with an ascending direction.
|
||||
*/
|
||||
public function withAsc(): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->isDesc = false;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with a descending direction.
|
||||
*/
|
||||
public function withDesc(): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->isDesc = true;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with a direction.
|
||||
*
|
||||
* @params self::ASC|self::DESC $direction
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function withDirection(string $direction): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->isDesc = strtoupper($direction) === self::DESC;
|
||||
|
||||
if (!in_array(strtoupper($direction), [self::DESC, self::ASC])) {
|
||||
throw new RuntimeException("Bad order direction.");
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with a reverse direction.
|
||||
*/
|
||||
public function withReverseDirection(): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->isDesc = !$this->isDesc;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
96
application/Espo/ORM/Query/Part/OrderList.php
Normal file
96
application/Espo/ORM/Query/Part/OrderList.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
|
||||
/**
|
||||
* A list of order items.
|
||||
*
|
||||
* Immutable.
|
||||
*
|
||||
* @implements Iterator<Order>
|
||||
*/
|
||||
class OrderList implements Iterator
|
||||
{
|
||||
private int $position = 0;
|
||||
/** @var Order[] */
|
||||
private array $list;
|
||||
|
||||
/**
|
||||
* @param Order[] $list
|
||||
*/
|
||||
private function __construct(array $list)
|
||||
{
|
||||
foreach ($list as $item) {
|
||||
if (!$item instanceof Order) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
$this->list = $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance.
|
||||
*
|
||||
* @param Order[] $list
|
||||
*/
|
||||
public static function create(array $list): self
|
||||
{
|
||||
return new self($list);
|
||||
}
|
||||
|
||||
public function rewind(): void
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
public function current(): Order
|
||||
{
|
||||
return $this->list[$this->position];
|
||||
}
|
||||
|
||||
public function key(): int
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
public function next(): void
|
||||
{
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
public function valid(): bool
|
||||
{
|
||||
return isset($this->list[$this->position]);
|
||||
}
|
||||
}
|
||||
73
application/Espo/ORM/Query/Part/Selection.php
Normal file
73
application/Espo/ORM/Query/Part/Selection.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* A select item. Immutable.
|
||||
*
|
||||
* Immutable.
|
||||
*/
|
||||
class Selection
|
||||
{
|
||||
private function __construct(
|
||||
private Expression $expression,
|
||||
private ?string $alias = null
|
||||
) {}
|
||||
|
||||
public function getExpression(): Expression
|
||||
{
|
||||
return $this->expression;
|
||||
}
|
||||
|
||||
public function getAlias(): ?string
|
||||
{
|
||||
return $this->alias;
|
||||
}
|
||||
|
||||
public static function create(Expression $expression, ?string $alias = null): self
|
||||
{
|
||||
return new self($expression, $alias);
|
||||
}
|
||||
|
||||
public static function fromString(string $expression): self
|
||||
{
|
||||
return self::create(
|
||||
Expression::create($expression)
|
||||
);
|
||||
}
|
||||
|
||||
public function withAlias(?string $alias): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->alias = $alias;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
45
application/Espo/ORM/Query/Part/WhereClause.php
Normal file
45
application/Espo/ORM/Query/Part/WhereClause.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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;
|
||||
|
||||
use Espo\ORM\Query\Part\Where\AndGroup;
|
||||
|
||||
/**
|
||||
* A where-clause. Immutable.
|
||||
*
|
||||
* Immutable.
|
||||
*/
|
||||
class WhereClause extends AndGroup
|
||||
{
|
||||
public function getRaw(): array
|
||||
{
|
||||
return $this->getRawValue();
|
||||
}
|
||||
}
|
||||
45
application/Espo/ORM/Query/Part/WhereItem.php
Normal file
45
application/Espo/ORM/Query/Part/WhereItem.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Can be used as a where-clause.
|
||||
*/
|
||||
interface WhereItem
|
||||
{
|
||||
/**
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function getRaw(): array;
|
||||
|
||||
public function getRawKey(): string;
|
||||
|
||||
public function getRawValue(): mixed;
|
||||
}
|
||||
Reference in New Issue
Block a user