return this;
}
private FilterItem findFilterItem(String expression) {
final QueryPartCollectionProcessor collectionProcessor = new QueryPartCollectionProcessor();
new QueryPartParser(collectionProcessor, expression, " AND ", " OR ").parse();
final List<String> tokens = collectionProcessor.getTokens();
final List<String> delims = collectionProcessor.getDelims();
if (tokens.size() == 1) {
expression = tokens.get(0);
} else {
final LogicalOperator logicalOperator = LogicalOperator.valueOf(delims.get(1).trim());
final List<FilterItem> filterItems = new ArrayList<FilterItem>();
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i);
FilterItem filterItem = findFilterItem(token);
filterItems.add(filterItem);
}
return new FilterItem(logicalOperator, filterItems);
}
OperatorType operator = null;
String leftSide = null;
final String rightSide;
{
String rightSideCandidate = null;
final OperatorType[] operators = OperatorType.values();
for (OperatorType operatorCandidate : operators) {
final int operatorIndex = expression.indexOf(' ' + operatorCandidate.toSql() + ' ');
if (operatorIndex > 0) {
operator = operatorCandidate;
leftSide = expression.substring(0, operatorIndex).trim();
rightSideCandidate = expression.substring(operatorIndex + operator.toSql().length() + 2).trim();
break;
}
}
if (operator == null) {
// check special cases for IS NULL and IS NOT NULL
if (expression.endsWith(" IS NOT NULL")) {
operator = OperatorType.DIFFERENT_FROM;
leftSide = expression.substring(0, expression.lastIndexOf(" IS NOT NULL")).trim();
rightSideCandidate = "NULL";
} else if (expression.endsWith(" IS NULL")) {
operator = OperatorType.EQUALS_TO;
leftSide = expression.substring(0, expression.lastIndexOf(" IS NULL")).trim();
rightSideCandidate = "NULL";
}
}
rightSide = rightSideCandidate;
}
if (operator == null) {
return new FilterItem(expression);
}
final SelectItem selectItem = findSelectItem(leftSide, false);
if (selectItem == null) {
return new FilterItem(expression);
}
final Object operand;
if (operator == OperatorType.IN) {
final List<Object> list = new ArrayList<Object>();
new QueryPartParser(new QueryPartProcessor() {
@Override
public void parse(String delim, String itemToken) {
Object operand = createOperand(itemToken, selectItem, false);
list.add(operand);
}