
Property changes on: tests
___________________________________________________________________
Added: svn:ignore
   + mysql.xml
coverage.xml
oracle.xml
.postgres.xml.swp
postgres.xml
run-all.sh
sqlite.xml


Index: tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
===================================================================
--- tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php	(revision 6884)
+++ tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php	(working copy)
@@ -15,11 +15,14 @@
         $this->_em = $this->_getTestEntityManager();
     }
 
-    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
+    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed, array $queryHints=array())
     {
         try {
             $query = $this->_em->createQuery($dqlToBeTested);
             $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
+            foreach ($queryHints AS $name => $value) {
+                $query->setHint($name, $value);
+            }
             parent::assertEquals($sqlToBeConfirmed, $query->getSql());
             $query->free();
         } catch (Doctrine_Exception $e) {
@@ -462,6 +465,31 @@
         
         $this->_em->getConnection()->setDatabasePlatform($oldPlat);
     }
+
+    public function testPessimisticLockQueryHint()
+    {
+        $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' FOR UPDATE",
+            array(Query::HINT_LOCK_MODE => \Doctrine\ORM\LockMode::PESSIMISTIC)
+        );
+    }
+
+    public function testPessimisticNoWaitLockQueryHint()
+    {
+        $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
+        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
+
+        $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' FOR UPDATE NOWAIT",
+            array(Query::HINT_LOCK_MODE => \Doctrine\ORM\LockMode::PESSIMISTIC_NOWAIT)
+        );
+
+        $this->_em->getConnection()->setDatabasePlatform($oldPlat);
+    }
     
     
     /* Not yet implemented, needs more thought
Index: lib/Doctrine/DBAL/Platforms/OraclePlatform.php
===================================================================
--- lib/Doctrine/DBAL/Platforms/OraclePlatform.php	(revision 6884)
+++ lib/Doctrine/DBAL/Platforms/OraclePlatform.php	(working copy)
@@ -34,14 +34,6 @@
 class OraclePlatform extends AbstractPlatform
 {
     /**
-     * Constructor.
-     */
-    public function __construct()
-    {
-        parent::__construct();
-    }
-
-    /**
      * return string to call a function to get a substring inside an SQL statement
      *
      * Note: Not SQL92, but common functionality.
@@ -617,4 +609,9 @@
     {
         return false;
     }
+
+    public function getForUpdateNoWaitSql()
+    {
+        return 'FOR UPDATE NOWAIT';
+    }
 }
Index: lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
===================================================================
--- lib/Doctrine/DBAL/Platforms/SqlitePlatform.php	(revision 6884)
+++ lib/Doctrine/DBAL/Platforms/SqlitePlatform.php	(working copy)
@@ -456,4 +456,9 @@
     {
         return 'sqlite';
     }
+
+    public function getForUpdateSql()
+    {
+        return '';
+    }
 }
Index: lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
===================================================================
--- lib/Doctrine/DBAL/Platforms/AbstractPlatform.php	(revision 6884)
+++ lib/Doctrine/DBAL/Platforms/AbstractPlatform.php	(working copy)
@@ -460,6 +460,11 @@
         return 'FOR UPDATE';
     }
 
+    public function getForUpdateNoWaitSql()
+    {
+        return $this->getForUpdateSql();
+    }
+
     public function getDropDatabaseSql($database)
     {
         return 'DROP DATABASE ' . $database;
Index: lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
===================================================================
--- lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php	(revision 6884)
+++ lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php	(working copy)
@@ -34,16 +34,6 @@
 class PostgreSqlPlatform extends AbstractPlatform
 {
     /**
-     * Constructor.
-     * Creates a new PostgreSqlPlatform.
-     */
-    public function __construct()
-    {
-        parent::__construct();
-    }
-
-
-    /**
      * Returns the md5 sum of a field.
      *
      * Note: Not SQL92, but common functionality
@@ -784,4 +774,9 @@
     {
         return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
     }
+
+    public function getForUpdateNoWaitSql()
+    {
+        return 'FOR UPDATE NOWAIT';
+    }
 }
Index: lib/Doctrine/ORM/Query.php
===================================================================
--- lib/Doctrine/ORM/Query.php	(revision 6901)
+++ lib/Doctrine/ORM/Query.php	(working copy)
@@ -83,6 +83,16 @@
     const HINT_INTERNAL_ITERATION = 'doctrine.internal.iteration';
 
     /**
+     * @var string
+     */
+    const HINT_LOCK_MODE = 'doctrine.lockMode';
+
+    /**
+     * @var string
+     */
+    const HINT_LOCK_VERSION = 'doctrine.lockVersion';
+
+    /**
      * @var integer $_state   The current state of this query.
      */
     private $_state = self::STATE_CLEAN;
Index: lib/Doctrine/ORM/Query/QueryException.php
===================================================================
--- lib/Doctrine/ORM/Query/QueryException.php	(revision 6901)
+++ lib/Doctrine/ORM/Query/QueryException.php	(working copy)
@@ -66,6 +66,7 @@
 
     /**
      * @param Doctrine\ORM\Mapping\AssociationMapping $assoc
+     * @return QueryException
      */
     public static function iterateWithFetchJoinCollectionNotAllowed($assoc)
     {
@@ -74,4 +75,12 @@
             "in class ".$assoc->sourceEntityName." assocation ".$assoc->sourceFieldName
         );
     }
+
+    /**
+     * @return QueryException
+     */
+    public static function aquiredOptimisticLockWihtoutVersion()
+    {
+        return new self("Aquireing an optimistic lock without specifying the 'doctrine.lockVersion' query hint.");
+    }
 }
\ No newline at end of file
Index: lib/Doctrine/ORM/Query/SqlWalker.php
===================================================================
--- lib/Doctrine/ORM/Query/SqlWalker.php	(revision 6901)
+++ lib/Doctrine/ORM/Query/SqlWalker.php	(working copy)
@@ -334,6 +334,14 @@
             $sql, $q->getMaxResults(), $q->getFirstResult()
         );
 
+        if (($lockMode = $q->getHint(Query::HINT_LOCK_MODE)) !== false) {
+            if ($lockMode == \Doctrine\ORM\LockMode::PESSIMISTIC) {
+                $sql .= " " . $this->_platform->getForUpdateSql();
+            } else if ($lockMode == \Doctrine\ORM\LockMode::PESSIMISTIC_NOWAIT) {
+                $sql .= " " . $this->_platform->getForUpdateNoWaitSql();
+            }
+        }
+
         return $sql;
     }
     
