From b8b5aa243e82f121bed9435f60c9bf30aa3b4132 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 13:06:46 -0700 Subject: [PATCH 1/3] Remove QueryBuilder::getType() and ::getState() --- UPGRADE.md | 8 ++++++-- psalm.xml.dist | 6 ------ src/Query/QueryBuilder.php | 35 -------------------------------- tests/Query/QueryBuilderTest.php | 25 ----------------------- 4 files changed, 6 insertions(+), 68 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index f258faa9a07..8d1ae0668d0 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -289,9 +289,13 @@ 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. -The `QueryBuilder::execute()` method has been removed. +The following `QueryBuilder` methods have been removed: + +1. `execute()`, +2. `getState()`, +3. `getType()`. ## BC BREAK: Removed the `Constraint` interface. diff --git a/psalm.xml.dist b/psalm.xml.dist index e08f580416a..0f871581af5 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -38,7 +38,6 @@ TODO: remove in 4.0.0 --> - @@ -48,11 +47,6 @@ See https://github.com/doctrine/dbal/pull/4317 --> - - - diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 7fbc7db3e8b..11d7f57e787 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -208,23 +208,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 +216,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. diff --git a/tests/Query/QueryBuilderTest.php b/tests/Query/QueryBuilderTest.php index 16816080f0b..6961bdd2235 100644 --- a/tests/Query/QueryBuilderTest.php +++ b/tests/Query/QueryBuilderTest.php @@ -351,7 +351,6 @@ public function testEmptySelect(): void $qb2 = $qb->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()); } From 1a15fb8c259fd2be94130ef3199d3c1d88c5d468 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 10:08:25 -0700 Subject: [PATCH 2/3] Rework tracking state in QueryBuilder --- UPGRADE.md | 7 +++- src/Query/QueryBuilder.php | 70 +++++++++++++++----------------------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 8d1ae0668d0..6c37151262d 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -289,7 +289,7 @@ DBAL is now tested only with SQL Server 2017 and newer. The `Statement::execute()` method has been marked private. -## BC BREAK: Removed `QueryBuilder` methods. +## BC BREAK: Removed `QueryBuilder` methods and contstants. The following `QueryBuilder` methods have been removed: @@ -297,6 +297,11 @@ The following `QueryBuilder` methods have been removed: 2. `getState()`, 3. `getType()`. +The following `QueryBuilder` constants have been removed: + +1. `STATE_DIRTY`, +2. `STATE_CLEAN`. + ## BC BREAK: Removed the `Constraint` interface. The `Constraint` interface has been removed. The `ForeignKeyConstraint`, `Index` and `UniqueConstraint` classes diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 11d7f57e787..0ca626b0bc0 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -57,16 +57,6 @@ class QueryBuilder */ 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. */ @@ -91,11 +81,6 @@ class QueryBuilder */ private int $type = self::SELECT; - /** - * The state of the query object. Can be dirty or clean. - */ - private int $state = self::STATE_CLEAN; - /** * The index of the first result to retrieve. */ @@ -358,7 +343,7 @@ public function executeStatement(): int|string */ public function getSQL(): string { - if ($this->sql !== null && $this->state === self::STATE_CLEAN) { + if ($this->sql !== null) { return $this->sql; } @@ -369,8 +354,7 @@ public function getSQL(): string default => $this->getSQLForSelect(), }; - $this->state = self::STATE_CLEAN; - $this->sql = $sql; + $this->sql = $sql; return $sql; } @@ -494,9 +478,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; } @@ -519,9 +504,10 @@ public function getFirstResult(): int */ public function setMaxResults(?int $maxResults): self { - $this->state = self::STATE_DIRTY; $this->maxResults = $maxResults; + $this->sql = null; + return $this; } @@ -561,7 +547,7 @@ public function select(string ...$expressions): self $this->select = $expressions; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -582,7 +568,7 @@ public function distinct(): self { $this->distinct = true; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -609,7 +595,7 @@ public function addSelect(string $expression, string ...$expressions): self $this->select = array_merge($this->select, [$expression], $expressions); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -635,7 +621,7 @@ public function delete(string $table): self $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -661,7 +647,7 @@ public function update(string $table): self $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -691,7 +677,7 @@ public function insert(string $table): self $this->table = $table; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -715,7 +701,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; } @@ -763,7 +749,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; } @@ -789,7 +775,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; } @@ -815,7 +801,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; } @@ -839,7 +825,7 @@ public function set(string $key, string $value): self { $this->set[] = $key . ' = ' . $value; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -875,7 +861,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; } @@ -908,7 +894,7 @@ public function andWhere(string|CompositeExpression $predicate, string|Composite ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -936,7 +922,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; } @@ -961,7 +947,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; } @@ -986,7 +972,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; } @@ -1040,7 +1026,7 @@ public function values(array $values): self { $this->values = $values; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1058,7 +1044,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; } @@ -1081,7 +1067,7 @@ public function andHaving(string|CompositeExpression $predicate, string|Composit ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1104,7 +1090,7 @@ public function orHaving(string|CompositeExpression $predicate, string|Composite ...$predicates ); - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1163,7 +1149,7 @@ public function orderBy(string $sort, ?string $order = null): self $this->orderBy = [$orderBy]; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } @@ -1186,7 +1172,7 @@ public function addOrderBy(string $sort, ?string $order = null): self $this->orderBy[] = $orderBy; - $this->state = self::STATE_DIRTY; + $this->sql = null; return $this; } From e2c6f56ecf90d2ce2a8beac6ec74e2b91cbe4f06 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 10:01:35 -0700 Subject: [PATCH 3/3] Represent query type as enum in QueryBuilder --- UPGRADE.md | 8 ++++-- psalm.xml.dist | 8 ------ src/Query/QueryBuilder.php | 50 +++++++++----------------------------- src/Query/QueryType.php | 16 ++++++++++++ 4 files changed, 33 insertions(+), 49 deletions(-) create mode 100644 src/Query/QueryType.php diff --git a/UPGRADE.md b/UPGRADE.md index 6c37151262d..5ac8df9a786 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -299,8 +299,12 @@ The following `QueryBuilder` methods have been removed: The following `QueryBuilder` constants have been removed: -1. `STATE_DIRTY`, -2. `STATE_CLEAN`. +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 0f871581af5..f6e62d924bb 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -32,14 +32,6 @@ - - - - - -