diff --git a/UPGRADE.md b/UPGRADE.md index f258faa9a07..5ac8df9a786 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -289,9 +289,22 @@ DBAL is now tested only with SQL Server 2017 and newer. The `Statement::execute()` method has been marked private. -## BC BREAK: Removed `QueryBuilder::execute()`. +## BC BREAK: Removed `QueryBuilder` methods and contstants. -The `QueryBuilder::execute()` method has been removed. +The following `QueryBuilder` methods have been removed: + +1. `execute()`, +2. `getState()`, +3. `getType()`. + +The following `QueryBuilder` constants have been removed: + +1. `SELECT`, +2. `DELETE`, +3. `UPDATE`, +4. `INSERT`, +5. `STATE_DIRTY`, +6. `STATE_CLEAN`. ## BC BREAK: Removed the `Constraint` interface. diff --git a/psalm.xml.dist b/psalm.xml.dist index e08f580416a..f6e62d924bb 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -32,15 +32,6 @@ - - - - - - - - - - diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 7fbc7db3e8b..60d6178d9aa 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -37,36 +37,6 @@ */ class QueryBuilder { - /** - * @deprecated - */ - final public const SELECT = 0; - - /** - * @deprecated - */ - final public const DELETE = 1; - - /** - * @deprecated - */ - final public const UPDATE = 2; - - /** - * @deprecated - */ - final public const INSERT = 3; - - /** - * @deprecated - */ - final public const STATE_DIRTY = 0; - - /** - * @deprecated - */ - final public const STATE_CLEAN = 1; - /** * The complete SQL string for this query. */ @@ -89,12 +59,7 @@ class QueryBuilder /** * The type of query this is. Can be select, update or delete. */ - private int $type = self::SELECT; - - /** - * The state of the query object. Can be dirty or clean. - */ - private int $state = self::STATE_CLEAN; + private QueryType $type = QueryType::SELECT; /** * The index of the first result to retrieve. @@ -208,23 +173,6 @@ public function expr(): ExpressionBuilder return $this->connection->createExpressionBuilder(); } - /** - * Gets the type of the currently built query. - * - * @deprecated If necessary, track the type of the query being built outside of the builder. - */ - public function getType(): int - { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5551', - 'Relying on the type of the query being built is deprecated.' - . ' If necessary, track the type of the query being built outside of the builder.' - ); - - return $this->type; - } - /** * Gets the associated DBAL Connection for this query builder. */ @@ -233,24 +181,6 @@ public function getConnection(): Connection return $this->connection; } - /** - * Gets the state of this query builder instance. - * - * @deprecated The builder state is an internal concern. - * - * @return int Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. - */ - public function getState(): int - { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5551', - 'Relying on the query builder state is deprecated as it is an internal concern.' - ); - - return $this->state; - } - /** * Prepares and executes an SQL query and returns the first row of the result * as an associative array. @@ -393,21 +323,12 @@ public function executeStatement(): int|string */ public function getSQL(): string { - if ($this->sql !== null && $this->state === self::STATE_CLEAN) { - return $this->sql; - } - - $sql = match ($this->type) { - self::INSERT => $this->getSQLForInsert(), - self::DELETE => $this->getSQLForDelete(), - self::UPDATE => $this->getSQLForUpdate(), - default => $this->getSQLForSelect(), + return $this->sql ??= match ($this->type) { + QueryType::INSERT => $this->getSQLForInsert(), + QueryType::DELETE => $this->getSQLForDelete(), + QueryType::UPDATE => $this->getSQLForUpdate(), + QueryType::SELECT => $this->getSQLForSelect(), }; - - $this->state = self::STATE_CLEAN; - $this->sql = $sql; - - return $sql; } /** @@ -529,9 +450,10 @@ public function getParameterType(int|string $key): int|string|Type|null */ public function setFirstResult(int $firstResult): self { - $this->state = self::STATE_DIRTY; $this->firstResult = $firstResult; + $this->sql = null; + return $this; } @@ -554,9 +476,10 @@ public function getFirstResult(): int */ public function setMaxResults(?int $maxResults): self { - $this->state = self::STATE_DIRTY; $this->maxResults = $maxResults; + $this->sql = null; + return $this; } @@ -588,7 +511,7 @@ public function getMaxResults(): ?int */ public function select(string ...$expressions): self { - $this->type = self::SELECT; + $this->type = QueryType::SELECT; if (count($expressions) < 1) { return $this; @@ -596,7 +519,7 @@ public function select(string ...$expressions): self $this->select = $expressions; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -617,7 +540,7 @@ public function distinct(): self { $this->distinct = true; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -640,11 +563,11 @@ public function distinct(): self */ public function addSelect(string $expression, string ...$expressions): self { - $this->type = self::SELECT; + $this->type = QueryType::SELECT; $this->select = array_merge($this->select, [$expression], $expressions); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -666,11 +589,11 @@ public function addSelect(string $expression, string ...$expressions): self */ public function delete(string $table): self { - $this->type = self::DELETE; + $this->type = QueryType::DELETE; $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -692,11 +615,11 @@ public function delete(string $table): self */ public function update(string $table): self { - $this->type = self::UPDATE; + $this->type = QueryType::UPDATE; $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -722,11 +645,11 @@ public function update(string $table): self */ public function insert(string $table): self { - $this->type = self::INSERT; + $this->type = QueryType::INSERT; $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -750,7 +673,7 @@ public function from(string $table, ?string $alias = null): self { $this->from[] = new From($table, $alias); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -798,7 +721,7 @@ public function innerJoin(string $fromAlias, string $join, string $alias, ?strin { $this->join[$fromAlias][] = Join::inner($join, $alias, $condition); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -824,7 +747,7 @@ public function leftJoin(string $fromAlias, string $join, string $alias, ?string { $this->join[$fromAlias][] = Join::left($join, $alias, $condition); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -850,7 +773,7 @@ public function rightJoin(string $fromAlias, string $join, string $alias, ?strin { $this->join[$fromAlias][] = Join::right($join, $alias, $condition); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -874,7 +797,7 @@ public function set(string $key, string $value): self { $this->set[] = $key . ' = ' . $value; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -910,7 +833,7 @@ public function where(string|CompositeExpression $predicate, string|CompositeExp { $this->where = $this->createPredicate($predicate, ...$predicates); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -943,7 +866,7 @@ public function andWhere(string|CompositeExpression $predicate, string|Composite ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -971,7 +894,7 @@ public function orWhere(string|CompositeExpression $predicate, string|CompositeE { $this->where = $this->appendToPredicate($this->where, CompositeExpression::TYPE_OR, $predicate, ...$predicates); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -996,7 +919,7 @@ public function groupBy(string $expression, string ...$expressions): self { $this->groupBy = array_merge([$expression], $expressions); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1021,7 +944,7 @@ public function addGroupBy(string $expression, string ...$expressions): self { $this->groupBy = array_merge($this->groupBy, [$expression], $expressions); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1075,7 +998,7 @@ public function values(array $values): self { $this->values = $values; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1093,7 +1016,7 @@ public function having(string|CompositeExpression $predicate, string|CompositeEx { $this->having = $this->createPredicate($predicate, ...$predicates); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1116,7 +1039,7 @@ public function andHaving(string|CompositeExpression $predicate, string|Composit ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1139,7 +1062,7 @@ public function orHaving(string|CompositeExpression $predicate, string|Composite ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1198,7 +1121,7 @@ public function orderBy(string $sort, ?string $order = null): self $this->orderBy = [$orderBy]; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1221,7 +1144,7 @@ public function addOrderBy(string $sort, ?string $order = null): self $this->orderBy[] = $orderBy; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } diff --git a/src/Query/QueryType.php b/src/Query/QueryType.php new file mode 100644 index 00000000000..5544ffd8bd4 --- /dev/null +++ b/src/Query/QueryType.php @@ -0,0 +1,16 @@ +select(); self::assertSame($qb, $qb2); - self::assertEquals(QueryBuilder::SELECT, $qb->getType()); $this->expectException(QueryException::class); $qb->getSQL(); @@ -387,7 +386,6 @@ public function testUpdate(): void ->set('foo', '?') ->set('bar', '?'); - self::assertEquals(QueryBuilder::UPDATE, $qb->getType()); self::assertEquals('UPDATE users SET foo = ?, bar = ?', (string) $qb); } @@ -406,7 +404,6 @@ public function testDelete(): void $qb = new QueryBuilder($this->conn); $qb->delete('users'); - self::assertEquals(QueryBuilder::DELETE, $qb->getType()); self::assertEquals('DELETE FROM users', (string) $qb); } @@ -430,7 +427,6 @@ public function testInsertValues(): void ] ); - self::assertEquals(QueryBuilder::INSERT, $qb->getType()); self::assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb); } @@ -451,7 +447,6 @@ public function testInsertReplaceValues(): void ] ); - self::assertEquals(QueryBuilder::INSERT, $qb->getType()); self::assertEquals('INSERT INTO users (bar, foo) VALUES(?, ?)', (string) $qb); } @@ -463,7 +458,6 @@ public function testInsertSetValue(): void ->setValue('bar', '?') ->setValue('foo', '?'); - self::assertEquals(QueryBuilder::INSERT, $qb->getType()); self::assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb); } @@ -476,7 +470,6 @@ public function testInsertValuesSetValue(): void ) ->setValue('bar', '?'); - self::assertEquals(QueryBuilder::INSERT, $qb->getType()); self::assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb); } @@ -486,22 +479,6 @@ public function testGetConnection(): void self::assertSame($this->conn, $qb->getConnection()); } - public function testGetState(): void - { - $qb = new QueryBuilder($this->conn); - - self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState()); - - $qb->select('u.*')->from('users', 'u'); - - self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState()); - - $sql1 = $qb->getSQL(); - - self::assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState()); - self::assertEquals($sql1, $qb->getSQL()); - } - /** * @dataProvider maxResultsProvider */ @@ -510,7 +487,6 @@ public function testSetMaxResults(?int $maxResults): void $qb = new QueryBuilder($this->conn); $qb->setMaxResults($maxResults); - self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState()); self::assertEquals($maxResults, $qb->getMaxResults()); } @@ -530,7 +506,6 @@ public function testSetFirstResult(): void $qb = new QueryBuilder($this->conn); $qb->setFirstResult(10); - self::assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState()); self::assertEquals(10, $qb->getFirstResult()); }