From 8f97b2248a2c3af6bd063dd04f3721c4378e26e9 Mon Sep 17 00:00:00 2001 From: Catalina Wei Date: Wed, 10 Dec 2008 23:34:55 +0000 Subject: [PATCH] OPENJPA-805 JPQL updates - iteration 1 Sync up with JPQL.jjt to pass regression git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@725485 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/kernel/exps/QueryExpressions.java | 1 + .../kernel/jpql/JPQLExpressionBuilder.java | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/QueryExpressions.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/QueryExpressions.java index ed59ba804..231d8f6dc 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/QueryExpressions.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/QueryExpressions.java @@ -61,6 +61,7 @@ public class QueryExpressions public Value[] ordering = EMPTY_VALUES; public boolean[] ascending = StoreQuery.EMPTY_BOOLEANS; public String[] orderingClauses = StoreQuery.EMPTY_STRINGS; + public String[] orderingAliases = StoreQuery.EMPTY_STRINGS; public LinkedMap parameterTypes = StoreQuery.EMPTY_PARAMS; public int operation = QueryOperations.OP_SELECT; public ClassMetaData[] accessPath = StoreQuery.EMPTY_METAS; diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java index 1bf5af382..8610d8357 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java @@ -321,11 +321,17 @@ public class JPQLExpressionBuilder Expression exp = null; for (int i = 0; i < count; i++) { JPQLNode parent = parametersNode.getChild(i); - JPQLNode node = onlyChild(parent); + JPQLNode node = firstChild(parent); + JPQLNode aliasNode = parent.children.length > 1 ? right(parent) + : null;; Value proj = getValue(node); + String alias = aliasNode == null ? nextAlias() + : aliasNode.text; exps.projections[i] = proj; - exps.projectionClauses[i] = assemble(node); - exps.projectionAliases[i] = nextAlias(); + exps.projectionClauses[i] = aliasNode == null ? + assemble(node.id == JJTSCALAREXPRESSION ? firstChild(node) + : node) : alias; + exps.projectionAliases[i] = alias; } return exp; } @@ -377,15 +383,30 @@ public class JPQLExpressionBuilder int ordercount = orderby.getChildCount(); exps.ordering = new Value[ordercount]; exps.orderingClauses = new String[ordercount]; + exps.orderingAliases = new String[ordercount]; exps.ascending = new boolean[ordercount]; for (int i = 0; i < ordercount; i++) { JPQLNode node = orderby.getChild(i); exps.ordering[i] = getValue(firstChild(node)); exps.orderingClauses[i] = assemble(firstChild(node)); + exps.orderingAliases[i] = firstChild(node).text; // ommission of ASC/DESC token implies ascending exps.ascending[i] = node.getChildCount() <= 1 || lastChild(node).id == JJTASCENDING ? true : false; } + // check if order by selec item alias + for (int i = 0; i < ordercount; i++) { + if (exps.orderingClauses[i] != null && + !exps.orderingClauses[i].equals("")) + continue; + for (int j = 0; j < exps.projections.length; j++) { + if (exps.projectionAliases[j].equalsIgnoreCase( + exps.orderingAliases[i])) { + exps.ordering[i] = exps.projections[j]; + break; + } + } + } } } @@ -719,6 +740,9 @@ public class JPQLExpressionBuilder boolean not = node.not; switch (node.id) { + case JJTSCALAREXPRESSION: + return eval(onlyChild(node)); + case JJTWHERE: // top-level WHERE clause return getExpression(onlyChild(node));