Skip to content

Commit

Permalink
Merge pull request doctrine#5567 from morozov/issues/3164-empty-delet…
Browse files Browse the repository at this point in the history
…e-criteria

Do not require a WHERE in update() and delete() Connection operations
  • Loading branch information
morozov committed Aug 5, 2022
2 parents 65226ec + d5fdf44 commit 20adf5f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 54 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
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
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
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
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

0 comments on commit 20adf5f

Please sign in to comment.