openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r669341 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ openjpa-persistence/src/main/resources/org/apache/openjpa/persi...
Date Thu, 19 Jun 2008 00:40:17 GMT
Author: ppoddar
Date: Wed Jun 18 17:40:17 2008
New Revision: 669341

URL: http://svn.apache.org/viewvc?rev=669341&view=rev
Log:
OPENJPA-112,OPENJPA-111: Early check for 1-based positional parameter and no named parameter
in native query. Also more specific error message

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryParameterBinding.java
Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
    openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryParameterBinding.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryParameterBinding.java?rev=669341&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryParameterBinding.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryParameterBinding.java
Wed Jun 18 17:40:17 2008
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.query;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.jdbc.query.domain.Applicant;
+import org.apache.openjpa.persistence.jdbc.query.domain.Application;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests that Native queries use only 1-based positional parameters and 
+ * disallows named parameters.
+ * 
+ * Originally reported in 
+ * <A HRE="http://issues.apache.org/jira/browse/OPENJPA-112>OPENJPA-112</A>
+ *  
+ * @author Pinaki Poddar
+ *
+ */
+public class TestNativeQueryParameterBinding extends SingleEMFTestCase {
+	private static Class NO_ERROR = null;
+	
+	@Override
+	public void setUp() throws Exception {
+		super.setUp(CLEAR_TABLES);
+	}
+	
+	public void testNamedParameterInNativeQueryIsNotValid() {
+		String sql = "SELECT * FROM Application WHERE id=:id";
+		verifyParams(sql, IllegalArgumentException.class, "id", 10);
+	}
+	
+	public void testPositionalParameterInNativeQueryIsValid() {
+		String sql = "SELECT * FROM Application WHERE id=?1";
+		verifyParams(sql, NO_ERROR, 1, 10);
+	}
+	
+	public void testZeroPositionalParameterInNativeQueryIsNotValid() {
+		String sql = "SELECT * FROM Application WHERE id=?1";
+		verifyParams(sql, IllegalArgumentException.class, 0, 10);
+	}
+	
+	void verifyParams(String jpql, Class error, Object...params) {
+		EntityManager em = emf.createEntityManager();
+		em.getTransaction().begin();
+		Query query = em.createNativeQuery(jpql);
+		for (int i=0; params != null && i<params.length; i=+2) {
+			try {
+				if (params[i] instanceof Number) {
+					query.setParameter(((Number)params[i]).intValue(), params[i+1]);
+				} else { 
+					query.setParameter(params[i].toString(), params[i+1]);
+				}
+				if (error != null)
+					fail("Expected " + error.getName());
+			} catch (Exception e) {
+				if (error.isAssignableFrom(e.getClass())) {
+					System.err.println(e.getMessage());
+				} else {
+					e.printStackTrace();
+					fail();
+				}
+			} 
+				
+		}
+		em.getTransaction().commit();
+	}
+}

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=669341&r1=669340&r2=669341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Wed Jun 18 17:40:17 2008
@@ -39,6 +39,7 @@
 import org.apache.openjpa.kernel.DelegatingQuery;
 import org.apache.openjpa.kernel.DelegatingResultList;
 import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.kernel.QueryLanguages;
 import org.apache.openjpa.kernel.QueryOperations;
 import org.apache.openjpa.kernel.exps.AggregateListener;
 import org.apache.openjpa.kernel.exps.FilterListener;
@@ -445,6 +446,10 @@
         _em.assertNotCloseInvoked();
         _query.lock();
         try {
+        	if (isNative() && position < 1) {
+        		throw new IllegalArgumentException(_loc.get("bad-pos-params", 
+        		      position, _query.getQueryString()).toString());
+        	}
             // not allowed to mix positional and named parameters (EDR2 3.6.4)
             if (_named != null)
                 throw new InvalidStateException(_loc.get
@@ -486,6 +491,10 @@
         _em.assertNotCloseInvoked();
         _query.lock();
         try {
+        	if (isNative()) {
+        		throw new IllegalArgumentException(_loc.get("no-named-params", 
+        		    name, _query.getQueryString()).toString());
+        	}
             // not allowed to mix positional and named parameters (EDR2 3.6.4)
             if (_positional != null)
                 throw new InvalidStateException(_loc.get
@@ -500,6 +509,10 @@
             _query.unlock();
         }
     }
+    
+    public boolean isNative() {
+    	return QueryLanguages.LANG_SQL.equals(getLanguage());
+    }
 
     public boolean hasPositionalParameters() {
         return _positional != null;

Modified: openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties?rev=669341&r1=669340&r2=669341&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
Wed Jun 18 17:40:17 2008
@@ -98,6 +98,10 @@
 ser-query: Writing query "{1}".
 ser-sequence: Writing sequence "{0}".
 no-sql: You must provide a SQL string when creating a native query.
+no-named-params: Named parameter "{0}" is invalid for native query "{1}". \
+	Use only 1-based positional parameter in native queries.
+bad-pos-params: Positional parameter "{0}"  is invalid for native query "{1}". \
+	Use only 1-based positional parameter in native queries.
 bad-param-type: The parameter "{0}" is of type "{1}", but the \
 	declaration in the query is for type "{2}". 
 bad-em-prop: Invalid EntityManager property passed to createEntityManager. \



Mime
View raw message