Skip to content

Commit

Permalink
Merge branch '2.13.x' into 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Apr 17, 2021
2 parents c09dfce + f4a801b commit 41d7dd9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions psalm.xml.dist
Expand Up @@ -61,6 +61,7 @@
<file name="src/Query/Expression/CompositeExpression.php"/>
<file name="tests/Query/Expression/CompositeExpressionTest.php"/>
<file name="tests/Query/Expression/ExpressionBuilderTest.php"/>
<referencedMethod name="Doctrine\DBAL\Statement::execute"/>
</errorLevel>
</DeprecatedMethod>
<DocblockTypeContradiction>
Expand Down
41 changes: 41 additions & 0 deletions src/Statement.php
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;

use function is_string;

Expand Down Expand Up @@ -155,12 +156,20 @@ 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
*
* @throws Exception
*/
public function execute($params = null): Result
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4580',
'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead'
);

if ($params !== null) {
$this->params = $params;
}
Expand All @@ -184,6 +193,38 @@ public function execute($params = null): Result
}
}

/**
* Executes the statement with the currently bound parameters and return result.
*
* @param mixed[] $params
*
* @throws Exception
*/
public function executeQuery(array $params = []): Result
{
if ($params === []) {
$params = null; // Workaround as long execute() exists and used internally.
}

return $this->execute($params);
}

/**
* 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.
}

return $this->execute($params)->rowCount();
}

/**
* Gets the wrapped driver statement.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Functional/StatementTest.php
Expand Up @@ -300,4 +300,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);
}
}
21 changes: 20 additions & 1 deletion tests/Query/QueryBuilderTest.php
Expand Up @@ -7,11 +7,13 @@
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\QueryException;
use Doctrine\DBAL\Result;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class QueryBuilderTest extends TestCase
{
/** @var Connection */
/** @var Connection&MockObject */
protected $conn;

protected function setUp(): void
Expand Down Expand Up @@ -949,4 +951,21 @@ public function testJoinWithNonUniqueAliasThrowsException(): void

$qb->getSQL();
}

public function testExecuteSelect(): void
{
$qb = new QueryBuilder($this->conn);

$this->conn
->expects($this->any())
->method('executeQuery')
->willReturn($this->createMock(Result::class));

$result = $qb
->select('id')
->from('foo')
->execute();

self::assertInstanceOf(Result::class, $result);
}
}

0 comments on commit 41d7dd9

Please sign in to comment.