use of com.orientechnologies.orient.core.sql.operator.OQueryOperator in project orientdb by orientechnologies.
the class OSQLPredicate method extractConditions.
protected Object extractConditions(final OSQLFilterCondition iParentCondition) {
final int oldPosition = parserGetCurrentPosition();
parserNextWord(true, " )=><,\r\n", true);
final String word = parserGetLastWord();
boolean inBraces = word.length() > 0 && word.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN;
if (word.length() > 0 && (word.equalsIgnoreCase("SELECT") || word.equalsIgnoreCase("TRAVERSE"))) {
// SUB QUERY
final StringBuilder embedded = new StringBuilder(256);
OStringSerializerHelper.getEmbedded(parserText, oldPosition - 1, -1, embedded);
parserSetCurrentPosition(oldPosition + embedded.length() + 1);
return new OSQLSynchQuery<Object>(embedded.toString());
}
parserSetCurrentPosition(oldPosition);
OSQLFilterCondition currentCondition = extractCondition();
// CHECK IF THERE IS ANOTHER CONDITION ON RIGHT
while (parserSkipWhiteSpaces()) {
if (!parserIsEnded() && parserGetCurrentChar() == ')')
return currentCondition;
final OQueryOperator nextOperator = extractConditionOperator();
if (nextOperator == null)
return currentCondition;
if (nextOperator.precedence > currentCondition.getOperator().precedence) {
// SWAP ITEMS
final OSQLFilterCondition subCondition = new OSQLFilterCondition(currentCondition.right, nextOperator);
currentCondition.right = subCondition;
subCondition.right = extractConditionItem(false, 1);
} else {
final OSQLFilterCondition parentCondition = new OSQLFilterCondition(currentCondition, nextOperator);
parentCondition.right = extractConditions(parentCondition);
currentCondition = parentCondition;
}
}
currentCondition.inBraces = inBraces;
// END OF TEXT
return currentCondition;
}
use of com.orientechnologies.orient.core.sql.operator.OQueryOperator in project orientdb by orientechnologies.
the class OSQLEngine method getRecordOperators.
public synchronized OQueryOperator[] getRecordOperators() {
if (SORTED_OPERATORS != null) {
return SORTED_OPERATORS;
}
// sort operators, will happen only very few times since we cache the
// result
final Iterator<OQueryOperatorFactory> ite = getOperatorFactories();
final List<OQueryOperator> operators = new ArrayList<OQueryOperator>();
while (ite.hasNext()) {
final OQueryOperatorFactory factory = ite.next();
operators.addAll(factory.getOperators());
}
final List<OQueryOperator> sorted = new ArrayList<OQueryOperator>();
final Set<Pair> pairs = new LinkedHashSet<Pair>();
for (final OQueryOperator ca : operators) {
for (final OQueryOperator cb : operators) {
if (ca != cb) {
switch(ca.compare(cb)) {
case BEFORE:
pairs.add(new Pair(ca, cb));
break;
case AFTER:
pairs.add(new Pair(cb, ca));
break;
}
switch(cb.compare(ca)) {
case BEFORE:
pairs.add(new Pair(cb, ca));
break;
case AFTER:
pairs.add(new Pair(ca, cb));
break;
}
}
}
}
boolean added;
do {
added = false;
scan: for (final Iterator<OQueryOperator> it = operators.iterator(); it.hasNext(); ) {
final OQueryOperator candidate = it.next();
for (final Pair pair : pairs) {
if (pair.after == candidate) {
continue scan;
}
}
sorted.add(candidate);
it.remove();
for (final Iterator<Pair> itp = pairs.iterator(); itp.hasNext(); ) {
if (itp.next().before == candidate) {
itp.remove();
}
}
added = true;
}
} while (added);
if (!operators.isEmpty()) {
throw new ODatabaseException("Invalid sorting. " + OCollections.toString(pairs));
}
SORTED_OPERATORS = sorted.toArray(new OQueryOperator[sorted.size()]);
return SORTED_OPERATORS;
}
use of com.orientechnologies.orient.core.sql.operator.OQueryOperator in project orientdb by orientechnologies.
the class OFilterOptimizer method optimize.
private OSQLFilterCondition optimize(OSQLFilterCondition condition, OIndexSearchResult indexMatch) {
if (condition == null) {
return null;
}
OQueryOperator operator = condition.getOperator();
while (operator == null) {
if (condition.getRight() == null && condition.getLeft() instanceof OSQLFilterCondition) {
condition = (OSQLFilterCondition) condition.getLeft();
operator = condition.getOperator();
} else {
return condition;
}
}
final OIndexReuseType reuseType = operator.getIndexReuseType(condition.getLeft(), condition.getRight());
switch(reuseType) {
case INDEX_METHOD:
if (isCovered(indexMatch, operator, condition.getLeft(), condition.getRight()) || isCovered(indexMatch, operator, condition.getRight(), condition.getLeft())) {
return null;
}
return condition;
case INDEX_INTERSECTION:
if (condition.getLeft() instanceof OSQLFilterCondition)
condition.setLeft(optimize((OSQLFilterCondition) condition.getLeft(), indexMatch));
if (condition.getRight() instanceof OSQLFilterCondition)
condition.setRight(optimize((OSQLFilterCondition) condition.getRight(), indexMatch));
if (condition.getLeft() == null)
return (OSQLFilterCondition) condition.getRight();
if (condition.getRight() == null)
return (OSQLFilterCondition) condition.getLeft();
return condition;
case INDEX_OPERATOR:
if (isCovered(indexMatch, operator, condition.getLeft(), condition.getRight()) || isCovered(indexMatch, operator, condition.getRight(), condition.getLeft())) {
return null;
}
return condition;
default:
return condition;
}
}
use of com.orientechnologies.orient.core.sql.operator.OQueryOperator in project orientdb by orientechnologies.
the class OSQLPredicate method extractCondition.
protected OSQLFilterCondition extractCondition() {
if (!parserSkipWhiteSpaces())
// END OF TEXT
return null;
// EXTRACT ITEMS
Object left = extractConditionItem(true, 1);
if (left != null && checkForEnd(left.toString()))
return null;
OQueryOperator oper;
final Object right;
if (left instanceof OQueryOperator && ((OQueryOperator) left).isUnary()) {
oper = (OQueryOperator) left;
left = extractConditionItem(false, 1);
right = null;
} else {
oper = extractConditionOperator();
if (oper instanceof OQueryOperatorNot)
// SPECIAL CASE: READ NEXT OPERATOR
oper = new OQueryOperatorNot(extractConditionOperator());
right = oper != null ? extractConditionItem(false, oper.expectedRightWords) : null;
}
// CREATE THE CONDITION OBJECT
return new OSQLFilterCondition(left, oper, right);
}
use of com.orientechnologies.orient.core.sql.operator.OQueryOperator in project orientdb by orientechnologies.
the class OSQLPredicate method extractConditionOperator.
private OQueryOperator extractConditionOperator() {
if (!parserSkipWhiteSpaces())
// END OF PARSING: JUST RETURN
return null;
if (parserGetCurrentChar() == ')')
// FOUND ')': JUST RETURN
return null;
final OQueryOperator[] operators = OSQLEngine.getInstance().getRecordOperators();
final String[] candidateOperators = new String[operators.length];
for (int i = 0; i < candidateOperators.length; ++i) candidateOperators[i] = operators[i].keyword;
final int operatorPos = parserNextChars(true, false, candidateOperators);
if (operatorPos == -1) {
parserGoBack();
return null;
}
final OQueryOperator op = operators[operatorPos];
if (op.expectsParameters) {
// PARSE PARAMETERS IF ANY
parserGoBack();
parserNextWord(true, " 0123456789'\"");
final String word = parserGetLastWord();
final List<String> params = new ArrayList<String>();
// CHECK FOR PARAMETERS
if (word.length() > op.keyword.length() && word.charAt(op.keyword.length()) == OStringSerializerHelper.EMBEDDED_BEGIN) {
int paramBeginPos = parserGetCurrentPosition() - (word.length() - op.keyword.length());
parserSetCurrentPosition(OStringSerializerHelper.getParameters(parserText, paramBeginPos, -1, params));
} else if (!word.equals(op.keyword))
throw new OQueryParsingException("Malformed usage of operator '" + op.toString() + "'. Parsed operator is: " + word);
try {
// CONFIGURE COULD INSTANTIATE A NEW OBJECT: ACT AS A FACTORY
return op.configure(params);
} catch (Exception e) {
throw OException.wrapException(new OQueryParsingException("Syntax error using the operator '" + op.toString() + "'. Syntax is: " + op.getSyntax()), e);
}
} else
parserMoveCurrentPosition(+1);
return op;
}
Aggregations