Skip to content

Commit

Permalink
fixed #304
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobe91 committed Dec 14, 2016
1 parent 66a311e commit 4f78c69
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.blazebit.persistence.impl.expression.PathExpression;
import com.blazebit.persistence.impl.expression.PropertyExpression;
import com.blazebit.persistence.impl.query.CTEQuerySpecification;
import com.blazebit.persistence.impl.query.EntityFunctionNode;
import com.blazebit.persistence.impl.query.QuerySpecification;
import com.blazebit.persistence.spi.DbmsStatementType;
import com.blazebit.persistence.spi.SetOperationType;
Expand Down Expand Up @@ -73,9 +74,10 @@ protected void buildExternalQueryString(StringBuilder sbSelectFrom) {

@Override
protected Query getQuery() {
Set<JoinNode> keyRestrictedLeftJoins = joinManager.getKeyRestrictedLeftJoins();
Query query;

if (hasLimit()) {
if (hasLimit() || joinManager.hasEntityFunctions() || !keyRestrictedLeftJoins.isEmpty()) {
// We need to change the underlying sql when doing a limit
query = em.createQuery(getBaseQueryStringWithCheck());

Expand All @@ -93,12 +95,17 @@ protected Query getQuery() {
}
}

List<String> keyRestrictedLeftJoinAliases = getKeyRestrictedLeftJoinAliases(query, keyRestrictedLeftJoins, Collections.EMPTY_SET);
List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(query);

QuerySpecification querySpecification = new CTEQuerySpecification(
this,
query,
parameterListNames,
limit,
offset
offset,
keyRestrictedLeftJoinAliases,
entityFunctionNodes
);

query = new CustomSQLQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
*/
public class CTEQuerySpecification extends CustomQuerySpecification {

public CTEQuerySpecification(AbstractCommonQueryBuilder<?, ?, ?, ?, ?> commonQueryBuilder, Query baseQuery, Set<String> parameterListNames, String limit, String offset) {
super(commonQueryBuilder, baseQuery, parameterListNames, limit, offset, Collections.EMPTY_LIST, Collections.EMPTY_LIST, false, Collections.EMPTY_LIST, false);
public CTEQuerySpecification(AbstractCommonQueryBuilder<?, ?, ?, ?, ?> commonQueryBuilder, Query baseQuery, Set<String> parameterListNames, String limit, String offset,
List<String> keyRestrictedLeftJoinAliases, List<EntityFunctionNode> entityFunctionNodes) {
super(commonQueryBuilder, baseQuery, parameterListNames, limit, offset, keyRestrictedLeftJoinAliases, entityFunctionNodes, false, Collections.EMPTY_LIST, false);
}

@Override
Expand All @@ -40,11 +41,13 @@ public Query getBaseQuery() {

@Override
protected void initialize() {
List<Query> participatingQueries = Arrays.asList(baseQuery);
List<Query> participatingQueries = new ArrayList<Query>();

StringBuilder sqlSb = new StringBuilder(extendedQuerySupport.getSql(em, baseQuery));
String sqlQuery = extendedQuerySupport.getSql(em, baseQuery);
StringBuilder sqlSb = applySqlTransformations(baseQuery, sqlQuery, participatingQueries);
// Need to inline LIMIT and OFFSET
dbmsDialect.appendExtendedSql(sqlSb, statementType, false, true, null, limit, offset, null, null);
participatingQueries.add(baseQuery);

this.sql = sqlSb.toString();
this.participatingQueries = participatingQueries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ private void applyTableNameRemapping(StringBuilder sb, String sqlAlias, String n
final String searchAlias = " " + sqlAlias;
int searchIndex = 0;
while ((searchIndex = sb.indexOf(searchAlias, searchIndex)) > -1) {
char c = sb.charAt(searchIndex + searchAlias.length());
if (c == '.') {
int idx = searchIndex + searchAlias.length();
if (idx < sb.length() && sb.charAt(idx) == '.') {
// This is a dereference of the alias, skip this
} else {
int[] indexRange;
Expand Down

0 comments on commit 4f78c69

Please sign in to comment.