Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not require a WHERE in update() and delete() Connection operations #5567

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 4.0

## BC BREAK: a non-empty WHERE clause is not enforced in data manipulation `Connection` methods.

The `Connection::update()` and `::delete()` methods no longer enforce a non-empty WHERE clause. If modification
of all table rows should not be allowed, it should be implemented in the application code.

## BC BREAK: removed wrapper- and driver-level `Statement::bindParam()` methods.

The following methods have been removed:
Expand Down
24 changes: 14 additions & 10 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Doctrine\DBAL\Exception\CommitFailedRollbackOnly;
use Doctrine\DBAL\Exception\ConnectionLost;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\EmptyCriteriaNotAllowed;
use Doctrine\DBAL\Exception\InvalidPlatformType;
use Doctrine\DBAL\Exception\NoActiveTransaction;
use Doctrine\DBAL\Exception\SavepointsNotSupported;
Expand Down Expand Up @@ -463,18 +462,20 @@ private function addCriteriaCondition(
*
* @throws Exception
*/
public function delete(string $table, array $criteria, array $types = []): int|string
public function delete(string $table, array $criteria = [], array $types = []): int|string
{
if (count($criteria) === 0) {
throw EmptyCriteriaNotAllowed::new();
}

$columns = $values = $conditions = [];

$this->addCriteriaCondition($criteria, $columns, $values, $conditions);

$sql = 'DELETE FROM ' . $table;

if ($conditions !== []) {
$sql .= ' WHERE ' . implode(' AND ', $conditions);
}

return $this->executeStatement(
'DELETE FROM ' . $table . ' WHERE ' . implode(' AND ', $conditions),
$sql,
$values,
is_string(key($types)) ? $this->extractTypeValues($columns, $types) : $types
);
Expand Down Expand Up @@ -528,7 +529,7 @@ public function getTransactionIsolation(): TransactionIsolationLevel
*
* @throws Exception
*/
public function update(string $table, array $data, array $criteria, array $types = []): int|string
public function update(string $table, array $data, array $criteria = [], array $types = []): int|string
{
$columns = $values = $conditions = $set = [];

Expand All @@ -544,8 +545,11 @@ public function update(string $table, array $data, array $criteria, array $types
$types = $this->extractTypeValues($columns, $types);
}

$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' AND ', $conditions);
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $set);

if ($conditions !== []) {
$sql .= ' WHERE ' . implode(' AND ', $conditions);
}

return $this->executeStatement($sql, $values, $types);
}
Expand Down
16 changes: 0 additions & 16 deletions src/Exception/EmptyCriteriaNotAllowed.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Doctrine\DBAL\Event\TransactionRollBackEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
Expand Down Expand Up @@ -629,15 +628,6 @@ static function (Connection $connection, string $query, array $params, array $ty
];
}

public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException(): void
{
$driver = $this->createMock(Driver::class);
$conn = new Connection([], $driver);

$this->expectException(InvalidArgumentException::class);
$conn->delete('kittens', []);
}

public function testCallConnectOnce(): void
{
$driver = $this->createMock(Driver::class);
Expand Down
18 changes: 0 additions & 18 deletions tests/Exception/EmptyCriteriaNotAllowedTest.php

This file was deleted.

18 changes: 18 additions & 0 deletions tests/Functional/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public function testDelete(): void
self::assertCount(0, $this->connection->fetchAllAssociative('SELECT * FROM write_table'));
}

public function testDeleteAll(): void
{
$this->insertRows();

self::assertEquals(2, $this->connection->delete('write_table'));
self::assertCount(0, $this->connection->fetchAllAssociative('SELECT * FROM write_table'));
}

public function testUpdate(): void
{
$this->insertRows();
Expand All @@ -143,6 +151,16 @@ public function testUpdate(): void
));
}

public function testUpdateAll(): void
{
$this->insertRows();

self::assertEquals(2, $this->connection->update(
'write_table',
['test_string' => 'baz']
));
}

public function testLastInsertId(): void
{
if (! $this->connection->getDatabasePlatform()->supportsIdentityColumns()) {
Expand Down