Primo rilascio

This commit is contained in:
2026-03-07 00:15:59 +01:00
commit dd5282dd69
609 changed files with 75246 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Exception;
use Doctrine\DBAL\Query\QueryException;
use function implode;
use function sprintf;
final class NonUniqueAlias extends QueryException
{
/** @param string[] $registeredAliases */
public static function new(string $alias, array $registeredAliases): self
{
return new self(
sprintf(
'The given alias "%s" is not unique in FROM and JOIN clause table. '
. 'The currently registered aliases are: %s.',
$alias,
implode(', ', $registeredAliases),
),
);
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Exception;
use Doctrine\DBAL\Query\QueryException;
use function implode;
use function sprintf;
final class UnknownAlias extends QueryException
{
/** @param string[] $registeredAliases */
public static function new(string $alias, array $registeredAliases): self
{
return new self(
sprintf(
'The given alias "%s" is not part of any FROM or JOIN clause table. '
. 'The currently registered aliases are: %s.',
$alias,
implode(', ', $registeredAliases),
),
);
}
}

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Expression;
use Countable;
use function array_merge;
use function array_values;
use function count;
use function implode;
/**
* Composite expression is responsible to build a group of similar expression.
*
* This class is immutable.
*/
class CompositeExpression implements Countable
{
/**
* Constant that represents an AND composite expression.
*/
final public const TYPE_AND = 'AND';
/**
* Constant that represents an OR composite expression.
*/
final public const TYPE_OR = 'OR';
/**
* Each expression part of the composite expression.
*
* @var array<int, self|string>
*/
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.
*
* @phpstan-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;
}
}

View File

@@ -0,0 +1,244 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Expression;
use Doctrine\DBAL\Connection;
use function implode;
use function sprintf;
/**
* ExpressionBuilder class is responsible to dynamically create SQL query parts.
*/
class ExpressionBuilder
{
final public const EQ = '=';
final public const NEQ = '<>';
final public const LT = '<';
final public const LTE = '<=';
final public const GT = '>';
final public const GTE = '>=';
/**
* Initializes a new <tt>ExpressionBuilder</tt>.
*
* @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 <left expr> = <right expr>. 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 <left expr> <> <right expr>. 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 <left expr> < <right expr>. 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 <left expr> <= <right expr>. 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 <left expr> > <right expr>. 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 <left expr> >= <right expr>. 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);
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode;
/** @internal */
final class ForUpdate
{
public function __construct(
private readonly ConflictResolutionMode $conflictResolutionMode,
) {
}
public function getConflictResolutionMode(): ConflictResolutionMode
{
return $this->conflictResolutionMode;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\ForUpdate;
enum ConflictResolutionMode
{
/**
* Wait for the row to be unlocked
*/
case ORDINARY;
/**
* Skip the row if it is locked
*/
case SKIP_LOCKED;
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
/** @internal */
final class From
{
public function __construct(
public readonly string $table,
public readonly ?string $alias = null,
) {
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
/** @internal */
final class Join
{
private function __construct(
public readonly string $type,
public readonly string $table,
public readonly string $alias,
public readonly ?string $condition,
) {
}
public static function inner(string $table, string $alias, ?string $condition): Join
{
return new self('INNER', $table, $alias, $condition);
}
public static function left(string $table, string $alias, ?string $condition): Join
{
return new self('LEFT', $table, $alias, $condition);
}
public static function right(string $table, string $alias, ?string $condition): Join
{
return new self('RIGHT', $table, $alias, $condition);
}
}

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
final class Limit
{
public function __construct(
private readonly ?int $maxResults,
private readonly int $firstResult,
) {
}
public function isDefined(): bool
{
return $this->maxResults !== null || $this->firstResult !== 0;
}
public function getMaxResults(): ?int
{
return $this->maxResults;
}
public function getFirstResult(): int
{
return $this->firstResult;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
use Doctrine\DBAL\Exception;
class QueryException extends \Exception implements Exception
{
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
/** @internal */
enum QueryType
{
case SELECT;
case DELETE;
case UPDATE;
case INSERT;
case UNION;
}

View File

@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
final class SelectQuery
{
/**
* @internal This class should be instantiated only by {@link QueryBuilder}.
*
* @param string[] $columns
* @param string[] $from
* @param string[] $groupBy
* @param string[] $orderBy
*/
public function __construct(
private readonly bool $distinct,
private readonly array $columns,
private readonly array $from,
private readonly ?string $where,
private readonly array $groupBy,
private readonly ?string $having,
private readonly array $orderBy,
private readonly Limit $limit,
private readonly ?ForUpdate $forUpdate,
) {
}
public function isDistinct(): bool
{
return $this->distinct;
}
/** @return string[] */
public function getColumns(): array
{
return $this->columns;
}
/** @return string[] */
public function getFrom(): array
{
return $this->from;
}
public function getWhere(): ?string
{
return $this->where;
}
/** @return string[] */
public function getGroupBy(): array
{
return $this->groupBy;
}
public function getHaving(): ?string
{
return $this->having;
}
/** @return string[] */
public function getOrderBy(): array
{
return $this->orderBy;
}
public function getLimit(): Limit
{
return $this->limit;
}
public function getForUpdate(): ?ForUpdate
{
return $this->forUpdate;
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
/** @internal */
final class Union
{
public function __construct(
public readonly string|QueryBuilder $query,
public readonly ?UnionType $type = null,
) {
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
final class UnionQuery
{
/**
* @internal This class should be instantiated only by {@link QueryBuilder}.
*
* @param Union[] $unionParts
* @param string[] $orderBy
*/
public function __construct(
private readonly array $unionParts,
private readonly array $orderBy,
private readonly Limit $limit,
) {
}
/** @return Union[] */
public function getUnionParts(): array
{
return $this->unionParts;
}
/** @return string[] */
public function getOrderBy(): array
{
return $this->orderBy;
}
public function getLimit(): Limit
{
return $this->limit;
}
}

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
enum UnionType
{
case ALL;
case DISTINCT;
}