OPENJPA-112,OPENJPA-111: Early check for 1-based positional parameter and no named parameter in native query. Also more specific error message

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@669341 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2008-06-19 00:40:17 +00:00
parent f62ea1ab0d
commit d1a165bc61
3 changed files with 103 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -39,6 +39,7 @@ import org.apache.openjpa.enhance.Reflection;
import org.apache.openjpa.kernel.DelegatingQuery; import org.apache.openjpa.kernel.DelegatingQuery;
import org.apache.openjpa.kernel.DelegatingResultList; import org.apache.openjpa.kernel.DelegatingResultList;
import org.apache.openjpa.kernel.Filters; import org.apache.openjpa.kernel.Filters;
import org.apache.openjpa.kernel.QueryLanguages;
import org.apache.openjpa.kernel.QueryOperations; import org.apache.openjpa.kernel.QueryOperations;
import org.apache.openjpa.kernel.exps.AggregateListener; import org.apache.openjpa.kernel.exps.AggregateListener;
import org.apache.openjpa.kernel.exps.FilterListener; import org.apache.openjpa.kernel.exps.FilterListener;
@ -445,6 +446,10 @@ public class QueryImpl
_em.assertNotCloseInvoked(); _em.assertNotCloseInvoked();
_query.lock(); _query.lock();
try { 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) // not allowed to mix positional and named parameters (EDR2 3.6.4)
if (_named != null) if (_named != null)
throw new InvalidStateException(_loc.get throw new InvalidStateException(_loc.get
@ -486,6 +491,10 @@ public class QueryImpl
_em.assertNotCloseInvoked(); _em.assertNotCloseInvoked();
_query.lock(); _query.lock();
try { 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) // not allowed to mix positional and named parameters (EDR2 3.6.4)
if (_positional != null) if (_positional != null)
throw new InvalidStateException(_loc.get throw new InvalidStateException(_loc.get
@ -501,6 +510,10 @@ public class QueryImpl
} }
} }
public boolean isNative() {
return QueryLanguages.LANG_SQL.equals(getLanguage());
}
public boolean hasPositionalParameters() { public boolean hasPositionalParameters() {
return _positional != null; return _positional != null;
} }

View File

@ -98,6 +98,10 @@ ser-cls-query: Writing query "{1}" in class "{0}".
ser-query: Writing query "{1}". ser-query: Writing query "{1}".
ser-sequence: Writing sequence "{0}". ser-sequence: Writing sequence "{0}".
no-sql: You must provide a SQL string when creating a native query. 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 \ bad-param-type: The parameter "{0}" is of type "{1}", but the \
declaration in the query is for type "{2}". declaration in the query is for type "{2}".
bad-em-prop: Invalid EntityManager property passed to createEntityManager. \ bad-em-prop: Invalid EntityManager property passed to createEntityManager. \