Skip to content

Commit

Permalink
Merge pull request #4580 from beberlei/StatementExecuteQuery
Browse files Browse the repository at this point in the history
Deprecate Statement::execute in favor of Statement::executeStatement/executeQuery
  • Loading branch information
beberlei committed Apr 10, 2021
2 parents 6fd2757 + 499199f commit 68a6f8e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/Doctrine/DBAL/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/StatementTest.php
Expand Up @@ -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);
}
}

0 comments on commit 68a6f8e

Please sign in to comment.