Commit 029730d2 authored by jeroendedauw's avatar jeroendedauw

Fix behaviour of Connection::delete when an empty array of criteria is provided

parent ec6da750
......@@ -575,21 +575,35 @@ class Connection implements DriverConnection
{
$this->connect();
$criteria = array();
foreach (array_keys($identifier) as $columnName) {
$criteria[] = $columnName . ' = ?';
}
$query = 'DELETE FROM ' . $tableExpression . $this->getWhereSql($identifier);
if (is_string(key($types))) {
$types = $this->extractTypeValues($identifier, $types);
}
$query = 'DELETE FROM ' . $tableExpression . ' WHERE ' . implode(' AND ', $criteria);
return $this->executeUpdate($query, array_values($identifier), $types);
}
/**
* @param array $identifier An associative array containing column-value pairs.
*
* @return string
*/
private function getWhereSql(array $identifier)
{
if (empty($identifier)) {
return '';
}
$criteria = array();
foreach (array_keys($identifier) as $columnName) {
$criteria[] = $columnName . ' = ?';
}
return ' WHERE ' . implode(' AND ', $criteria);
}
/**
* Closes the connection.
*
......
......@@ -466,4 +466,20 @@ SQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\"");
$this->assertTrue($conn->isConnected(), "Connection is not connected after passing external PDO");
}
public function testCallingDeleteWithNoDeletionCriteriaResultsInSqlWithoutWhereClause()
{
$pdoMock = $this->getMock('Doctrine\DBAL\Driver\Connection');
$pdoMock->expects($this->once())
->method('exec')
->with($this->equalTo('DELETE FROM kittens'));
$conn = new Connection(
array('pdo' => $pdoMock),
$this->getMock('Doctrine\DBAL\Driver')
);
$conn->delete('kittens', array());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment