diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 79e984e30a0..60309343a5e 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; @@ -147,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. @@ -155,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; } @@ -181,6 +190,42 @@ public function execute($params = null) return $stmt; } + /** + * Executes the statement with the currently bound parameters and return result. + * + * @param mixed[] $params + * + * @throws Exception + */ + 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); + } + + /** + * Executes the statement with the currently bound parameters and return affected rows. + * + * @param mixed[] $params + * + * @throws Exception + */ + public function executeStatement(array $params = []): int + { + if ($params === []) { + $params = null; // Workaround as long execute() exists and used internally. + } + + $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); + } }