Skip to content

Commit

Permalink
Connection: Support IS NULL in update/delete criteria
Browse files Browse the repository at this point in the history
If you pass a null to the where array of update or delete it's
pretty obvious what you want.

Most (all?) databases won't match anything if you actually try
to compare with NULL making this a silent failure without it.
  • Loading branch information
jnvsor committed Mar 25, 2017
1 parent e0169b2 commit 04919c9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,13 @@ public function delete($tableExpression, array $identifier, array $types = array
$paramValues = array();

foreach ($identifier as $columnName => $value) {
$columnList[] = $columnName;
$criteria[] = $columnName . ' = ?';
$paramValues[] = $value;
if (is_null($value)) {
$criteria[] = $this->getDatabasePlatform()->getIsNullExpression($columnName);
} else {
$columnList[] = $columnName;
$paramValues[] = $value;
$criteria[] = $columnName . ' = ?';
}
}

return $this->executeUpdate(
Expand Down Expand Up @@ -662,9 +666,13 @@ public function update($tableExpression, array $data, array $identifier, array $
}

foreach ($identifier as $columnName => $value) {
$columnList[] = $columnName;
$criteria[] = $columnName . ' = ?';
$paramValues[] = $value;
if (is_null($value)) {
$criteria[] = $this->getDatabasePlatform()->getIsNullExpression($columnName);
} else {
$columnList[] = $columnName;
$paramValues[] = $value;
$criteria[] = $columnName . ' = ?';
}
}

if (is_string(key($types))) {
Expand Down
51 changes: 51 additions & 0 deletions tests/Doctrine/Tests/DBAL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,57 @@ public function testUpdateWithSameColumnInDataAndIdentifiers()
);
}

/**
* @group DBAL-2511
*/
public function testUpdateWithIsNull()
{
$driverMock = $this->createMock('Doctrine\DBAL\Driver');

$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(new DriverConnectionMock()));

$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
->setMethods(array('executeUpdate'))
->setConstructorArgs(array(array('platform' => new Mocks\MockPlatform()), $driverMock))
->getMock();

$conn->expects($this->once())
->method('executeUpdate')
->with(
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id IS NULL AND name = ?',
[
'some text',
null,
'foo',
],
[
'string',
'null',
'string',
]
);

$conn->update(
'TestTable',
[
'text' => 'some text',
'is_edited' => null,
],
[
'id' => null,
'name' => 'foo',
],
[
'text' => 'string',
'is_edited' => 'null',
'id' => 'null',
'name' => 'string',
]
);
}

public function testFetchAssoc()
{
$statement = 'SELECT * FROM foo WHERE bar = ?';
Expand Down

0 comments on commit 04919c9

Please sign in to comment.