From 424bf11478bcdc42d3f9dadb471cc980078334dd Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 2 Apr 2021 20:04:52 +0200 Subject: [PATCH 1/3] Add Statement::executeQuery and Statement::executeStatement --- lib/Doctrine/DBAL/Statement.php | 29 ++++++++++++++ .../Tests/DBAL/Functional/StatementTest.php | 38 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 79e984e30a0..4d07511b2d2 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Exception\NoKeyValue; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Result as BaseResult; use Doctrine\DBAL\Types\Type; use Doctrine\Deprecations\Deprecation; use IteratorAggregate; @@ -181,6 +182,34 @@ public function execute($params = null) return $stmt; } + /** + * Executes the statement with the currently bound parameters and return result. + * + * @param mixed[]|null $params + + * @throws Exception + */ + public function executeQuery(?array $params = null): BaseResult + { + $this->execute($params); + + return new ForwardCompatibility\Result($this); + } + + /** + * Executes the statement with the currently bound parameters and return affected rows. + * + * @param mixed[]|null $params + + * @throws Exception + */ + public function executeStatement(?array $params = null): int + { + $this->execute($params); + + return $this->rowCount(); + } + /** * Closes the cursor, freeing the database resources used by this statement. * diff --git a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php index 1be228045b7..01c30c98300 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php @@ -326,4 +326,42 @@ public function testFetchInColumnMode(): void self::assertEquals(1, $result); } + + public function testExecuteQuery(): void + { + $platform = $this->connection->getDatabasePlatform(); + $query = $platform->getDummySelectSQL(); + $result = $this->connection->prepare($query)->executeQuery()->fetchOne(); + + self::assertEquals(1, $result); + } + + public function testExecuteQueryWithParams(): void + { + $this->connection->insert('stmt_test', ['id' => 1]); + + $query = 'SELECT id FROM stmt_test WHERE id = ?'; + $result = $this->connection->prepare($query)->executeQuery([1])->fetchOne(); + + self::assertEquals(1, $result); + } + + public function testExecuteStatement(): void + { + $this->connection->insert('stmt_test', ['id' => 1]); + + $query = 'UPDATE stmt_test SET name = ? WHERE id = 1'; + $stmt = $this->connection->prepare($query); + + $stmt->bindValue(1, 'bar'); + + $result = $stmt->executeStatement(); + + $this->assertEquals(1, $result); + + $query = 'UPDATE stmt_test SET name = ? WHERE id = ?'; + $result = $this->connection->prepare($query)->executeStatement(['foo', 1]); + + $this->assertEquals(1, $result); + } } From 8a502f32e640da1471454a8630396aa15fde4b06 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 2 Apr 2021 23:49:39 +0200 Subject: [PATCH 2/3] Deprecate Statement::execute() --- lib/Doctrine/DBAL/Statement.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 4d07511b2d2..bcc907dfa2b 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -148,6 +148,8 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le /** * Executes the statement with the currently bound parameters. * + * @deprecated Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead + * * @param mixed[]|null $params * * @return bool TRUE on success, FALSE on failure. @@ -156,6 +158,12 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le */ public function execute($params = null) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4580', + 'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead' + ); + if (is_array($params)) { $this->params = $params; } From 499199f56d61ac5235aabcf2c54fb733026c06ef Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 5 Apr 2021 11:36:02 +0200 Subject: [PATCH 3/3] Improve API by removing nullability --- lib/Doctrine/DBAL/Statement.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index bcc907dfa2b..60309343a5e 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -193,12 +193,16 @@ public function execute($params = null) /** * Executes the statement with the currently bound parameters and return result. * - * @param mixed[]|null $params - + * @param mixed[] $params + * * @throws Exception */ - public function executeQuery(?array $params = null): BaseResult + public function executeQuery(array $params = []): BaseResult { + if ($params === []) { + $params = null; // Workaround as long execute() exists and used internally. + } + $this->execute($params); return new ForwardCompatibility\Result($this); @@ -207,12 +211,16 @@ public function executeQuery(?array $params = null): BaseResult /** * Executes the statement with the currently bound parameters and return affected rows. * - * @param mixed[]|null $params - + * @param mixed[] $params + * * @throws Exception */ - public function executeStatement(?array $params = null): int + public function executeStatement(array $params = []): int { + if ($params === []) { + $params = null; // Workaround as long execute() exists and used internally. + } + $this->execute($params); return $this->rowCount();