// Process the order-by clause ...
OrderBy orderBy = pathExpression.getOrderBy();
if (orderBy != null) {
OrderByBuilder orderByBuilder = builder.orderBy();
for (OrderBySpec spec : orderBy) {
OrderByOperandBuilder operandBuilder = null;
switch (spec.getOrder()) {
case ASCENDING:
operandBuilder = orderByBuilder.ascending();
break;
case DESCENDING:
operandBuilder = orderByBuilder.descending();
break;
}
assert operandBuilder != null;
if (spec.getAttributeName() != null) {
// This order by is defined by an attribute ...
NameTest attribute = spec.getAttributeName();
assert !attribute.isWildcard();
if (attribute.matches("jcr", "path")) {
String pathOf = tableName;
if (pathOf == null) pathOf = aliases.iterator().next();
operandBuilder.path(pathOf);
} else {
operandBuilder.propertyValue(tableName, attribute.toString());
builder.select(tableName + "." + attribute.toString());
}
} else if (spec.getScoreFunction() != null) {
// This order-by is defined by a "jcr:score" function ...
FunctionCall scoreFunction = spec.getScoreFunction();
assert scoreFunction != null;
List<Component> args = scoreFunction.getParameters();
String nameOfTableToScore = tableName;
if (!args.isEmpty()) {
if (args.size() == 1 && args.get(0) instanceof NameTest) {
// Just the table name ...
NameTest tableNameTest = (NameTest)args.get(0);
nameOfTableToScore = tableNameTest.toString();
}
}
operandBuilder.fullTextSearchScore(nameOfTableToScore);
} else {
PathExpression axes = spec.getPath();
List<StepExpression> axesSteps = axes.getSteps();
if (axesSteps.size() != 2) {
throw new InvalidQueryException("Only child axis supported in ORDER BY clause");
}
NameTest childNode = (NameTest) ((AxisStep)axesSteps.get(0)).getNodeTest();
String alias = childNode.getLocalTest();
NameTest attribute = ((AttributeNameTest) ((AxisStep)axesSteps.get(1)).getNodeTest()).getNameTest();
builder.select(tableName + "." + attribute.toString());
builder.join("nt:base as " + alias).onChildNode(tableName, alias);
operandBuilder.propertyValue(alias, attribute.toString());
}
}
orderByBuilder.end();
}
// Try building this query, because we need to check the # of columns selected and the # of sources ...