You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2013/02/26 23:18:00 UTC

svn commit: r1450486 - in /db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque: adapter/AbstractAdapter.java adapter/Adapter.java adapter/MssqlAdapter.java sql/Query.java sql/SqlBuilder.java

Author: tfischer
Date: Tue Feb 26 22:18:00 2013
New Revision: 1450486

URL: http://svn.apache.org/r1450486
Log:
fix "select for update" for mssql

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/Adapter.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/MssqlAdapter.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java?rev=1450486&r1=1450485&r2=1450486&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java Tue Feb 26 22:18:00 2013
@@ -103,6 +103,18 @@ public abstract class AbstractAdapter im
     public abstract String getIDMethodSQL(Object obj);
 
     /**
+     * Returns the clause which acquires a write lock on a row
+     * when doing a select.
+     *
+     * @return the SQL clause to acquire a write lock.
+     *         This implementation returns "FOR UPDATE";
+     */
+    public String getUpdateLockClause()
+    {
+        return "FOR UPDATE";
+    }
+
+    /**
      * Locks the specified table.
      *
      * @param con The JDBC connection to use.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/Adapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/Adapter.java?rev=1450486&r1=1450485&r2=1450486&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/Adapter.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/Adapter.java Tue Feb 26 22:18:00 2013
@@ -106,6 +106,15 @@ public interface Adapter extends Seriali
     String getIDMethodSQL(Object obj);
 
     /**
+     * Returns the clause which acquires a write lock on a row
+     * when doing a select.
+     * Most databases use the "for update" clause.
+     *
+     * @return the SQL clause to acquire a write lock.
+     */
+    String getUpdateLockClause();
+
+    /**
      * Locks the specified table.
      *
      * @param con The JDBC connection to use.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/MssqlAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/MssqlAdapter.java?rev=1450486&r1=1450485&r2=1450486&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/MssqlAdapter.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/MssqlAdapter.java Tue Feb 26 22:18:00 2013
@@ -100,6 +100,18 @@ public class MssqlAdapter extends Abstra
     }
 
     /**
+     * Returns the clause which acquires a write lock on a row
+     * when doing a select.
+     *
+     * @return the SQL clause to acquire a write lock.
+     *         This implementation returns "WITH (UPDLOCK)";
+     */
+    public String getUpdateLockClause()
+    {
+        return "WITH (UPDLOCK)";
+    }
+
+    /**
      * Locks the specified table.
      *
      * @param con The JDBC connection to use.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java?rev=1450486&r1=1450485&r2=1450486&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java Tue Feb 26 22:18:00 2013
@@ -83,8 +83,6 @@ public class Query
     private static final String OFFSET = " OFFSET ";
     /** Constant for text " SET ROWCOUNT ". */
     private static final String SET_ROWCOUNT = " SET ROWCOUNT ";
-    /** Constant for text " FOR UPDATE". */
-    private static final String FOR_UPDATE = " FOR UPDATE";
 
     /**
      * The select modifiers. E.g. DISTINCT.
@@ -154,9 +152,9 @@ public class Query
     private String rowcount;
 
     /**
-     * Whether a FOR UPDATE clause should be rendered.
+     * The FOR UPDATE clause which should be rendered.
      */
-    private boolean forUpdate;
+    private String forUpdate;
 
     /** The type of the statement. */
     private Type type = Type.SELECT;
@@ -394,24 +392,24 @@ public class Query
     }
 
     /**
-     * Sets whether FOR UPDATE clause should be added to the query.
+     * Sets the FOR UPDATE clause which should be added to the query.
      *
-     * @param forUpdate true if a FOR UPDATE clause should be added,
-     *        false if not.
+     * @param forUpdate the FOR UPDATE clause which should be added,
+     *        null if no FOR UPDATE clause should be used.
      *
      * @return this object.
      */
-    public void setForUpdate(boolean forUpdate)
+    public void setForUpdate(String forUpdate)
     {
         this.forUpdate = forUpdate;
     }
 
     /**
-     * Returns whether a FOR UPDATE clause is added.
+     * Returns the FOR UPDATE clause which should be added to the query.
      *
-     * @return true if a FOR UPDATE clause is added, false otherwise.
+     * @return the FOR UPDATE clause, or null if none should be added.
      */
-    public boolean isForUpdate()
+    public String getForUpdate()
     {
         return forUpdate;
     }
@@ -534,6 +532,13 @@ public class Query
             stringBuilder.append(fromElement.toString());
         }
 
+        if (Type.SELECT == type
+                && (forUpdate != null)
+                && !"FOR UPDATE".equals(forUpdate))
+        {
+            stringBuilder.append(" ").append(forUpdate);
+        }
+
         if (Type.UPDATE == type)
         {
             stringBuilder.append(SET)
@@ -583,9 +588,11 @@ public class Query
         {
             stringBuilder.append(postLimit);
         }
-        if (Type.SELECT == type && forUpdate)
+        if (Type.SELECT == type
+                && (forUpdate != null)
+                && "FOR UPDATE".equals(forUpdate))
         {
-            stringBuilder.append(FOR_UPDATE);
+            stringBuilder.append(" ").append(forUpdate);
         }
 
         return stringBuilder;

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java?rev=1450486&r1=1450485&r2=1450486&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java Tue Feb 26 22:18:00 2013
@@ -632,10 +632,12 @@ public final class SqlBuilder
     }
 
     /**
-     * Adds a Limit clause to the query if supported by the database
+     * Adds a Limit clause to the query if supported by the database.
+     *
      * @param criteria the criteria from which the Limit and Offset values
      *        are taken
      * @param query the query to which the Limit clause should be added
+     *
      * @throws TorqueException if the Database adapter cannot be obtained
      */
     private static void processLimits(
@@ -675,16 +677,23 @@ public final class SqlBuilder
     }
 
     /**
-     * Adds a possible FOR UPDATE Clause to the query
+     * Adds a possible FOR UPDATE Clause to the query.
      *
      * @param criteria the criteria from which the query should be built.
      * @param query the query to build.
+     *
+     * @throws TorqueException if the Database adapter cannot be obtained
      */
     private static void processForUpdate(
             final Criteria criteria,
             final Query query)
+            throws TorqueException
     {
-        query.setForUpdate(criteria.isForUpdate());
+        if (criteria.isForUpdate())
+        {
+            Adapter adapter = Torque.getAdapter(criteria.getDbName());
+            query.setForUpdate(adapter.getUpdateLockClause());
+        }
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org