Skip to content

Commit

Permalink
Merge pull request #508 from deeky666/fix-sql-server-nolock-hint
Browse files Browse the repository at this point in the history
[DDC-2310] Fix evaluation of NOLOCK table hint on SQL Server
  • Loading branch information
beberlei committed Jan 14, 2014
2 parents 8ac0db0 + 69931fa commit 72eb3a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,9 @@ public function getForUpdateSQL()
/**
* Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
*
* @param string $fromClause
* @param integer $lockMode
* @param string $fromClause The FROM clause to append the hint for the given lock mode to.
* @param integer|null $lockMode One of the Doctrine\DBAL\LockMode::* constants. If null is given, nothing will
* be appended to the FROM clause.
*
* @return string
*/
Expand Down
24 changes: 11 additions & 13 deletions lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,21 +1347,19 @@ public function rollbackSavePoint($savepoint)
*/
public function appendLockHint($fromClause, $lockMode)
{
switch ($lockMode) {
case LockMode::NONE:
$lockClause = ' WITH (NOLOCK)';
break;
case LockMode::PESSIMISTIC_READ:
$lockClause = ' WITH (HOLDLOCK, ROWLOCK)';
break;
case LockMode::PESSIMISTIC_WRITE:
$lockClause = ' WITH (UPDLOCK, ROWLOCK)';
break;
switch (true) {
case LockMode::NONE === $lockMode:
return $fromClause . ' WITH (NOLOCK)';

case LockMode::PESSIMISTIC_READ === $lockMode:
return $fromClause . ' WITH (HOLDLOCK, ROWLOCK)';

case LockMode::PESSIMISTIC_WRITE === $lockMode:
return $fromClause . ' WITH (UPDLOCK, ROWLOCK)';

default:
$lockClause = '';
return $fromClause;
}

return $fromClause . $lockClause;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\SQLServerPlatform;

class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase
Expand All @@ -11,4 +12,28 @@ public function createPlatform()
return new SQLServerPlatform;
}

/**
* @group DDC-2310
* @dataProvider getLockHints
*/
public function testAppendsLockHint($lockMode, $lockHint)
{
$fromClause = 'FROM users';
$expectedResult = $fromClause . $lockHint;

$this->assertSame($expectedResult, $this->_platform->appendLockHint($fromClause, $lockMode));
}

public function getLockHints()
{
return array(
array(null, ''),
array(false, ''),
array(true, ''),
array(LockMode::NONE, ' WITH (NOLOCK)'),
array(LockMode::OPTIMISTIC, ''),
array(LockMode::PESSIMISTIC_READ, ' WITH (HOLDLOCK, ROWLOCK)'),
array(LockMode::PESSIMISTIC_WRITE, ' WITH (UPDLOCK, ROWLOCK)'),
);
}
}

0 comments on commit 72eb3a7

Please sign in to comment.