Fix for #174 - Incorrect SQL generated with syntax error at the "order by" clause

This commit is contained in:
rbygrave
2014-07-21 21:49:31 +12:00
parent 562c6fafc9
commit f7ece0f13c
7 changed files with 218 additions and 29 deletions
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.deploy;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
@@ -8,33 +9,33 @@ import java.util.Set;
*/
public final class DeployPropertyParserMap extends DeployParser {
private final Map<String,String> map;
private final Map<String, String> map;
public DeployPropertyParserMap(Map<String,String> map) {
this.map = map;
}
public DeployPropertyParserMap(Map<String, String> map) {
this.map = map;
}
/**
* Returns null for raw sql queries.
*/
public Set<String> getIncludes() {
return null;
}
/**
* Returns null for raw sql queries.
*/
public Set<String> getIncludes() {
return Collections.emptySet();
}
public String convertWord() {
String r = getDeployWord(word);
return r == null ? word : r;
}
public String convertWord() {
String r = getDeployWord(word);
return r == null ? word : r;
}
@Override
public String getDeployWord(String expression) {
String deployExpr = map.get(expression);
if (deployExpr == null) {
return null;
} else {
return deployExpr;
}
@Override
public String getDeployWord(String expression) {
String deployExpr = map.get(expression);
if (deployExpr == null) {
return null;
} else {
return deployExpr;
}
}
}
@@ -2,8 +2,12 @@ package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.BindParams.OrderedList;
import com.avaje.ebeaninternal.api.SpiExpressionList;
@@ -17,8 +21,6 @@ import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.util.BindParamsParser;
import com.avaje.ebeaninternal.util.DefaultExpressionRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Compile Query Predicates.
@@ -121,6 +123,8 @@ public class CQueryPredicates {
* Includes from where and order by clauses.
*/
private Set<String> predicateIncludes;
private Set<String> orderByIncludes;
public CQueryPredicates(Binder binder, OrmQueryRequest<?> request) {
this.binder = binder;
@@ -316,16 +320,20 @@ public class CQueryPredicates {
*/
private void parsePropertiesToDbColumns(DeployParser deployParser) {
dbWhere = deriveWhere(deployParser);
dbFilterMany = deriveFilterMany(deployParser);
dbHaving = deriveHaving(deployParser);
// order by is dependent on the manyProperty (if there is one)
logicalOrderBy = deriveOrderByWithMany(request.getManyProperty());
if (logicalOrderBy != null) {
dbOrderBy = deployParser.parse(logicalOrderBy);
}
// create a copy of the includes required to support the orderBy
orderByIncludes = new HashSet<String>(deployParser.getIncludes());
dbWhere = deriveWhere(deployParser);
dbFilterMany = deriveFilterMany(deployParser);
dbHaving = deriveHaving(deployParser);
// all includes including ones for manyWhere clause
predicateIncludes = deployParser.getIncludes();
}
@@ -504,6 +512,13 @@ public class CQueryPredicates {
return predicateIncludes;
}
/**
* Return the orderBy includes.
*/
public Set<String> getOrderByIncludes() {
return orderByIncludes;
}
/**
* The where sql with named bind parameters converted to ?.
*/
@@ -291,6 +291,7 @@ public class SqlTreeBuilder {
// remove ManyWhereJoins from the predicateIncludes
predicateIncludes.removeAll(manyWhereJoins.getPropertyNames());
predicateIncludes.addAll(predicates.getOrderByIncludes());
// look for predicateIncludes that are not in selectIncludes and add
// them as extra joins to the query