Commit f15c4823 authored by Guilherme Blanco's avatar Guilherme Blanco

Merge pull request #663 from deeky666/fix-sql-server-nolock-hint-2.4

[DDC-2310] [2.4] Fix evaluation of NOLOCK table hint on SQL Server
parents 1b62e9f7 18f140a9
...@@ -1038,8 +1038,9 @@ abstract class AbstractPlatform ...@@ -1038,8 +1038,9 @@ abstract class AbstractPlatform
/** /**
* Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification. * 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 string $fromClause The FROM clause to append the hint for the given lock mode to.
* @param integer $lockMode * @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 * @return string
*/ */
......
...@@ -1012,21 +1012,19 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1012,21 +1012,19 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
public function appendLockHint($fromClause, $lockMode) public function appendLockHint($fromClause, $lockMode)
{ {
switch ($lockMode) { switch (true) {
case LockMode::NONE: case LockMode::NONE === $lockMode:
$lockClause = ' WITH (NOLOCK)'; return $fromClause . ' WITH (NOLOCK)';
break;
case LockMode::PESSIMISTIC_READ: case LockMode::PESSIMISTIC_READ === $lockMode:
$lockClause = ' WITH (HOLDLOCK, ROWLOCK)'; return $fromClause . ' WITH (HOLDLOCK, ROWLOCK)';
break;
case LockMode::PESSIMISTIC_WRITE: case LockMode::PESSIMISTIC_WRITE === $lockMode:
$lockClause = ' WITH (UPDLOCK, ROWLOCK)'; return $fromClause . ' WITH (UPDLOCK, ROWLOCK)';
break;
default: default:
$lockClause = ''; return $fromClause;
} }
return $fromClause . $lockClause;
} }
/** /**
......
...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Platforms; ...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\SQLServer2008Platform; use Doctrine\DBAL\Platforms\SQLServer2008Platform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\LockMode;
class SQLServerPlatformTest extends AbstractPlatformTestCase class SQLServerPlatformTest extends AbstractPlatformTestCase
{ {
...@@ -318,4 +319,28 @@ class SQLServerPlatformTest extends AbstractPlatformTestCase ...@@ -318,4 +319,28 @@ class SQLServerPlatformTest extends AbstractPlatformTestCase
'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ([create], foo, [bar]) REFERENCES [foo-bar] ([create], bar, [foo-bar])', 'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ([create], foo, [bar]) REFERENCES [foo-bar] ([create], bar, [foo-bar])',
); );
} }
/**
* @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)'),
);
}
} }
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