use of org.apache.jackrabbit.oak.query.xpath.Statement.UnionStatement in project jackrabbit-oak by apache.
the class XPathToSQL2Converter method convertToUnion.
private Statement convertToUnion(String query, Statement statement, int startParseIndex) throws ParseException {
int start = query.indexOf("(", startParseIndex);
String begin = query.substring(0, start);
XPathToSQL2Converter converter = new XPathToSQL2Converter();
String partList = query.substring(start);
converter.initialize(partList);
converter.read();
int lastParseIndex = converter.parseIndex;
int lastOrIndex = lastParseIndex;
converter.read("(");
int level = 0;
ArrayList<String> parts = new ArrayList<String>();
int parseIndex;
while (true) {
parseIndex = converter.parseIndex;
if (converter.readIf("(")) {
level++;
} else if (converter.readIf(")")) {
if (level-- <= 0) {
break;
}
} else if (converter.readIf("|") && level == 0) {
String or = partList.substring(lastOrIndex, parseIndex - 1);
parts.add(or);
lastOrIndex = parseIndex;
} else if (converter.currentTokenType == END) {
throw getSyntaxError("empty query or missing ')'");
} else {
converter.read();
}
}
String or = partList.substring(lastOrIndex, parseIndex - 1);
parts.add(or);
String end = partList.substring(parseIndex);
Statement result = null;
ArrayList<Order> orderList = null;
QueryOptions queryOptions = null;
for (String p : parts) {
String q = begin + p + end;
converter = new XPathToSQL2Converter();
Statement stat = converter.convertToStatement(q);
orderList = stat.orderList;
queryOptions = stat.queryOptions;
// reset fields that are used in the union,
// but no longer in the individual statements
// (can not use clear, because it is shared)
stat.orderList = new ArrayList<Order>();
stat.queryOptions = null;
if (result == null) {
result = stat;
} else {
UnionStatement union = new UnionStatement(result, stat);
result = union;
}
}
result.orderList = orderList;
result.queryOptions = queryOptions;
result.setExplain(statement.explain);
result.setMeasure(statement.measure);
return result;
}
Aggregations