Skip to content

Commit

Permalink
[DDC-1693] Fix fatal errors in DQL when using Optimistic or None lock…
Browse files Browse the repository at this point in the history
… modes. Added tests.
  • Loading branch information
beberlei committed Mar 11, 2012
1 parent f6a61b1 commit 79d9c07
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,15 @@ public function walkSelectStatement(AST\SelectStatement $AST)
$sql .= ' ' . $this->_platform->getWriteLockSQL();
break;

case LockMode::PESSIMISTIC_OPTIMISTIC:
case LockMode::OPTIMISTIC:
foreach ($this->_selectedClasses AS $selectedClass) {
if ( ! $class->isVersioned) {
if ( ! $selectedClass['class']->isVersioned) {
throw \Doctrine\ORM\OptimisticLockException::lockFailed($selectedClass['class']->name);
}
}
break;
case LockMode::NONE:
break;

default:
throw \Doctrine\ORM\Query\QueryException::invalidLockMode();
Expand Down
19 changes: 17 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

require_once __DIR__ . '/../../../TestInit.php';

/**
* @group locking
*/
class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
protected function setUp() {
$this->useModelSet('cms');
Expand Down Expand Up @@ -139,7 +142,6 @@ public function testLockPessimisticWrite() {

/**
* @group DDC-178
* @group locking
*/
public function testLockPessimisticRead() {
$readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSql();
Expand All @@ -166,4 +168,17 @@ public function testLockPessimisticRead() {
$query = array_pop( $this->_sqlLoggerStack->queries );
$this->assertContains($readLockSql, $query['sql']);
}
}

/**
* @group DDC-1693
*/
public function testLockOptimisticNonVersionedThrowsExceptionInDQL()
{
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'";

$this->setExpectedException('Doctrine\ORM\OptimisticLockException', 'The optimistic lock on an entity failed.');
$sql = $this->_em->createQuery($dql)->setHint(
\Doctrine\ORM\Query::HINT_LOCK_MODE, \Doctrine\DBAL\LockMode::OPTIMISTIC
)->getSQL();
}
}
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,20 @@ public function testPessimisticReadLockQueryHintPostgreSql()
);
}

/**
* @group DDC-1693
* @group locking
*/
public function testLockModeNoneQueryHint()
{
$this->assertSqlGeneration(
"SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
"SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
"FROM cms_users c0_ WHERE c0_.username = 'gblanco'",
array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::NONE)
);
}

/**
* @group DDC-430
*/
Expand Down

0 comments on commit 79d9c07

Please sign in to comment.