Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDC-2310] [2.4] Fix evaluation of NOLOCK table hint on SQL Server #663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,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 @@ -1012,21 +1012,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 @@ -4,6 +4,7 @@

use Doctrine\DBAL\Platforms\SQLServer2008Platform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\LockMode;

class SQLServerPlatformTest extends AbstractPlatformTestCase
{
Expand Down Expand Up @@ -318,4 +319,28 @@ protected function getQuotedColumnInForeignKeySQL()
'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)'),
);
}
}