From bf6655a534a6775d30cafa67bd801276bda1d98d Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 13 Aug 2024 23:45:21 +0200 Subject: =?UTF-8?q?VERSION=200.2=20doctrine=20ORM=20et=20entit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Query/Expression/CompositeExpression.php | 98 +++++++++ .../src/Query/Expression/ExpressionBuilder.php | 244 +++++++++++++++++++++ 2 files changed, 342 insertions(+) create mode 100644 vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php create mode 100644 vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php (limited to 'vendor/doctrine/dbal/src/Query/Expression') diff --git a/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php new file mode 100644 index 0000000..efe307a --- /dev/null +++ b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php @@ -0,0 +1,98 @@ + + */ + private array $parts; + + /** @internal Use the and() / or() factory methods. */ + public function __construct( + private readonly string $type, + self|string $part, + self|string ...$parts, + ) { + $this->parts = array_merge([$part], array_values($parts)); + } + + public static function and(self|string $part, self|string ...$parts): self + { + return new self(self::TYPE_AND, $part, ...$parts); + } + + public static function or(self|string $part, self|string ...$parts): self + { + return new self(self::TYPE_OR, $part, ...$parts); + } + + /** + * Returns a new CompositeExpression with the given parts added. + */ + public function with(self|string $part, self|string ...$parts): self + { + $that = clone $this; + + $that->parts = array_merge($that->parts, [$part], array_values($parts)); + + return $that; + } + + /** + * Retrieves the amount of expressions on composite expression. + * + * @psalm-return int<0, max> + */ + public function count(): int + { + return count($this->parts); + } + + /** + * Retrieves the string representation of this composite expression. + */ + public function __toString(): string + { + if ($this->count() === 1) { + return (string) $this->parts[0]; + } + + return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; + } + + /** + * Returns the type of this composite expression (AND/OR). + */ + public function getType(): string + { + return $this->type; + } +} diff --git a/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php b/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php new file mode 100644 index 0000000..14942b2 --- /dev/null +++ b/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php @@ -0,0 +1,244 @@ +'; + final public const LT = '<'; + final public const LTE = '<='; + final public const GT = '>'; + final public const GTE = '>='; + + /** + * Initializes a new ExpressionBuilder. + * + * @param Connection $connection The DBAL Connection. + */ + public function __construct(private readonly Connection $connection) + { + } + + /** + * Creates a conjunction of the given expressions. + */ + public function and( + string|CompositeExpression $expression, + string|CompositeExpression ...$expressions, + ): CompositeExpression { + return CompositeExpression::and($expression, ...$expressions); + } + + /** + * Creates a disjunction of the given expressions. + */ + public function or( + string|CompositeExpression $expression, + string|CompositeExpression ...$expressions, + ): CompositeExpression { + return CompositeExpression::or($expression, ...$expressions); + } + + /** + * Creates a comparison expression. + * + * @param string $x The left expression. + * @param string $operator The comparison operator. + * @param string $y The right expression. + */ + public function comparison(string $x, string $operator, string $y): string + { + return $x . ' ' . $operator . ' ' . $y; + } + + /** + * Creates an equality comparison expression with the given arguments. + * + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a = . Example: + * + * [php] + * // u.id = ? + * $expr->eq('u.id', '?'); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function eq(string $x, string $y): string + { + return $this->comparison($x, self::EQ, $y); + } + + /** + * Creates a non equality comparison expression with the given arguments. + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a <> . Example: + * + * [php] + * // u.id <> 1 + * $q->where($q->expr()->neq('u.id', '1')); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function neq(string $x, string $y): string + { + return $this->comparison($x, self::NEQ, $y); + } + + /** + * Creates a lower-than comparison expression with the given arguments. + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a < . Example: + * + * [php] + * // u.id < ? + * $q->where($q->expr()->lt('u.id', '?')); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function lt(string $x, string $y): string + { + return $this->comparison($x, self::LT, $y); + } + + /** + * Creates a lower-than-equal comparison expression with the given arguments. + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a <= . Example: + * + * [php] + * // u.id <= ? + * $q->where($q->expr()->lte('u.id', '?')); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function lte(string $x, string $y): string + { + return $this->comparison($x, self::LTE, $y); + } + + /** + * Creates a greater-than comparison expression with the given arguments. + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a > . Example: + * + * [php] + * // u.id > ? + * $q->where($q->expr()->gt('u.id', '?')); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function gt(string $x, string $y): string + { + return $this->comparison($x, self::GT, $y); + } + + /** + * Creates a greater-than-equal comparison expression with the given arguments. + * First argument is considered the left expression and the second is the right expression. + * When converted to string, it will generated a >= . Example: + * + * [php] + * // u.id >= ? + * $q->where($q->expr()->gte('u.id', '?')); + * + * @param string $x The left expression. + * @param string $y The right expression. + */ + public function gte(string $x, string $y): string + { + return $this->comparison($x, self::GTE, $y); + } + + /** + * Creates an IS NULL expression with the given arguments. + * + * @param string $x The expression to be restricted by IS NULL. + */ + public function isNull(string $x): string + { + return $x . ' IS NULL'; + } + + /** + * Creates an IS NOT NULL expression with the given arguments. + * + * @param string $x The expression to be restricted by IS NOT NULL. + */ + public function isNotNull(string $x): string + { + return $x . ' IS NOT NULL'; + } + + /** + * Creates a LIKE comparison expression. + * + * @param string $expression The expression to be inspected by the LIKE comparison + * @param string $pattern The pattern to compare against + */ + public function like(string $expression, string $pattern, ?string $escapeChar = null): string + { + return $this->comparison($expression, 'LIKE', $pattern) . + ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : ''); + } + + /** + * Creates a NOT LIKE comparison expression + * + * @param string $expression The expression to be inspected by the NOT LIKE comparison + * @param string $pattern The pattern to compare against + */ + public function notLike(string $expression, string $pattern, ?string $escapeChar = null): string + { + return $this->comparison($expression, 'NOT LIKE', $pattern) . + ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : ''); + } + + /** + * Creates an IN () comparison expression with the given arguments. + * + * @param string $x The SQL expression to be matched against the set. + * @param string|string[] $y The SQL expression or an array of SQL expressions representing the set. + */ + public function in(string $x, string|array $y): string + { + return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')'); + } + + /** + * Creates a NOT IN () comparison expression with the given arguments. + * + * @param string $x The SQL expression to be matched against the set. + * @param string|string[] $y The SQL expression or an array of SQL expressions representing the set. + */ + public function notIn(string $x, string|array $y): string + { + return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')'); + } + + /** + * Creates an SQL literal expression from the string. + * + * The usage of this method is discouraged. Use prepared statements + * or {@see AbstractPlatform::quoteStringLiteral()} instead. + */ + public function literal(string $input): string + { + return $this->connection->quote($input); + } +} -- cgit v1.2.3