mirror of https://github.com/apache/openjpa.git
OPENJPA-1995: detect mixed parameter types in JPQL query
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1101289 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c159730005
commit
1148ba809d
|
@ -2469,19 +2469,36 @@ public class JPQLExpressionBuilder
|
|||
}
|
||||
|
||||
|
||||
// throws an exception if there are numeric parameters which do not start with 1.
|
||||
// throws an exception if there are numeric parameters which do not start with 1.
|
||||
private void validateParameters() {
|
||||
if(parameterTypes == null || parameterTypes.isEmpty()) {
|
||||
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()
|
||||
|
||||
boolean numericParms = false;
|
||||
boolean namedParms = false;
|
||||
|
||||
for (Object key : parameterTypes.keySet()) {
|
||||
|
||||
if (key instanceof Number) {
|
||||
if (namedParms) {
|
||||
throw new UserException(_loc.get("mixed-parameter-types", resolver.getQueryContext()
|
||||
.getQueryString(), parameterTypes.keySet().toString()));
|
||||
}
|
||||
numericParms = true;
|
||||
} else {
|
||||
if (numericParms) {
|
||||
throw new UserException(_loc.get("mixed-parameter-types", resolver.getQueryContext()
|
||||
.getQueryString(), parameterTypes.keySet().toString()));
|
||||
}
|
||||
namedParms = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (numericParms) {
|
||||
if (!parameterTypes.keySet().contains(1)) {
|
||||
throw new UserException(_loc.get("missing-positional-parameter", resolver.getQueryContext()
|
||||
.getQueryString(), parameterTypes.keySet().toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,3 +89,5 @@ cant-groupby-key-value-embeddable: Grouping by embeddables: "{0}({1})" is not al
|
|||
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}".
|
||||
mixed-parameter-types: Query "{0}" Contains both named and positional parameters \
|
||||
this is not allowed by the JPA specification. Detected parameters "{1}".
|
||||
|
|
|
@ -145,6 +145,18 @@ public class TestSetParameter extends SingleEMFTestCase {
|
|||
// expected
|
||||
}
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testMixedParameterTypesParameter() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
String query = "UPDATE CompUser e set e.name= :name, e.age = ?1 " + "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,
|
||||
|
|
Loading…
Reference in New Issue