openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r1100787 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/ openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/ openjpa-persistence-jdbc/sr...
Date Sun, 08 May 2011 18:39:07 GMT
Author: mikedd
Date: Sun May  8 18:39:07 2011
New Revision: 1100787

URL: http://svn.apache.org/viewvc?rev=1100787&view=rev
Log:
OPENJPA-1992: Throw exception if a JPQL query is missing the first positional parameter

Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ExpressionStoreQuery.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLParser.java
    openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/localizer.properties
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestSetParameter.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ExpressionStoreQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ExpressionStoreQuery.java?rev=1100787&r1=1100786&r2=1100787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ExpressionStoreQuery.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ExpressionStoreQuery.java
Sun May  8 18:39:07 2011
@@ -334,6 +334,7 @@ public class ExpressionStoreQuery
             QueryExpressions exps = assertQueryExpression();    
             ValidateGroupingExpressionVisitor.validate(q.getContext(), exps); 
         }
+        
 
         public void getRange(StoreQuery q, Object[] params, Range range) {
             QueryExpressions exps = assertQueryExpression();
@@ -490,7 +491,7 @@ public class ExpressionStoreQuery
         public boolean isPacking(StoreQuery q) {
             return false;
         }
-
+        
         /**
          * Throws an exception if select or having clauses contain 
          * non-aggregate, non-grouped paths.

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java?rev=1100787&r1=1100786&r2=1100787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
Sun May  8 18:39:07 2011
@@ -45,7 +45,6 @@ import org.apache.openjpa.kernel.QueryOp
 import org.apache.openjpa.kernel.ResultShape;
 import org.apache.openjpa.kernel.StoreContext;
 import org.apache.openjpa.kernel.exps.AbstractExpressionBuilder;
-import org.apache.openjpa.kernel.exps.Constant;
 import org.apache.openjpa.kernel.exps.Context;
 import org.apache.openjpa.kernel.exps.Expression;
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
@@ -311,7 +310,10 @@ public class JPQLExpressionBuilder
 
         exps.accessPath = getAccessPath();
         exps.hasInExpression = this.hasParameterizedInExpression;
-        
+
+        // verify parameters are consistent. 
+        validateParameters();
+
         return exps;
     }
 
@@ -2465,5 +2467,23 @@ public class JPQLExpressionBuilder
 			return this.query;
 		}
 	}
+    
+    
+    // throws an exception if there are numeric parameters which do not start with 1. 
+    private void validateParameters() {
+        if(parameterTypes == null || parameterTypes.isEmpty()) { 
+            return;
+        }
+        
+        Object firstKey = parameterTypes.keySet().iterator().next();
+        if (firstKey != null) { // paranoia
+            if (firstKey instanceof Number) {
+                if (!parameterTypes.keySet().contains(1)) {
+                    throw new UserException(_loc.get("missing-positional-parameter", resolver.getQueryContext()
+                        .getQueryString(), parameterTypes.keySet().toString()));
+                }
+            }
+        }
+    }
 }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLParser.java?rev=1100787&r1=1100786&r2=1100787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLParser.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLParser.java
Sun May  8 18:39:07 2011
@@ -82,3 +82,4 @@ public class JPQLParser
         return JPQLParser.LANG_JPQL;
     }
 }
+

Modified: openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/localizer.properties?rev=1100787&r1=1100786&r2=1100787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/localizer.properties
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/jpql/localizer.properties
Sun May  8 18:39:07 2011
@@ -87,3 +87,5 @@ cant-bulk-update-embeddable: Bulk update
 cant-groupby-embeddable: Grouping by embeddables: "{0}" is not allowed.
 cant-groupby-key-value-embeddable: Grouping by embeddables: "{0}({1})" is not allowed.
 no-constructor: NEW constructor operation could not resolve class named "{0}".
+missing-positional-parameter: Query "{0}" did not contain positional parameter 1. \
+    JPQL positional parameters must start at 1. Detected parameters "{1}".

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestSetParameter.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestSetParameter.java?rev=1100787&r1=1100786&r2=1100787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestSetParameter.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestSetParameter.java
Sun May  8 18:39:07 2011
@@ -18,8 +18,14 @@
  */
 package org.apache.openjpa.persistence.jpql.functions;
 
+import java.util.HashSet;
+import java.util.Set;
+
 import javax.persistence.EntityManager;
 
+import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.persistence.ArgumentException;
 import org.apache.openjpa.persistence.common.apps.Address;
 import org.apache.openjpa.persistence.common.apps.CompUser;
 import org.apache.openjpa.persistence.common.apps.FemaleUser;
@@ -127,10 +133,20 @@ public class TestSetParameter extends Si
           .executeUpdate();
         em.getTransaction().commit();
         assertEquals(1, count);
-        
-        
     }
-
+    
+    public void testMissingFirstPositionalParameter() {
+        EntityManager em = emf.createEntityManager();
+        String query = "UPDATE CompUser e set e.name= ?2, e.age = ?4 " + "WHERE e.userid
= ?3";
+        try {
+            em.createQuery(query);
+            fail("Did not get UserException with invalid JPQL query");
+        } catch (ArgumentException ae) {
+            // expected
+        }
+        em.close();
+    }
+    
     public CompUser createUser(String name, String cName, int age,
         boolean isMale) {
         CompUser user = null;



Mime
View raw message