Search in sources :

Example 1 with OQueryOperator

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;
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Example 2 with OQueryOperator

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OQueryOperatorFactory(com.orientechnologies.orient.core.sql.operator.OQueryOperatorFactory) ArrayList(java.util.ArrayList) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) Iterator(java.util.Iterator) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Example 3 with OQueryOperator

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;
    }
}
Also used : OIndexReuseType(com.orientechnologies.orient.core.sql.operator.OIndexReuseType) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Example 4 with OQueryOperator

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);
}
Also used : OQueryOperatorNot(com.orientechnologies.orient.core.sql.operator.OQueryOperatorNot) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Example 5 with OQueryOperator

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;
}
Also used : OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OException(com.orientechnologies.common.exception.OException) OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Aggregations

OQueryOperator (com.orientechnologies.orient.core.sql.operator.OQueryOperator)6 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)1 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)1 OSQLFilterCondition (com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition)1 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OIndexReuseType (com.orientechnologies.orient.core.sql.operator.OIndexReuseType)1 OQueryOperatorEquals (com.orientechnologies.orient.core.sql.operator.OQueryOperatorEquals)1 OQueryOperatorFactory (com.orientechnologies.orient.core.sql.operator.OQueryOperatorFactory)1 OQueryOperatorNot (com.orientechnologies.orient.core.sql.operator.OQueryOperatorNot)1 OQueryOperatorNotEquals (com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals)1 OQueryOperatorNotEquals2 (com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals2)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1