Search in sources :

Example 1 with ConstraintImpl

use of org.apache.jackrabbit.oak.query.ast.ConstraintImpl in project jackrabbit-oak by apache.

the class SQL2Parser method parseConditionFunctionIf.

private ConstraintImpl parseConditionFunctionIf(String functionName) throws ParseException {
    ConstraintImpl c;
    if ("CONTAINS".equalsIgnoreCase(functionName)) {
        if (readIf("*")) {
            // strictly speaking, CONTAINS(*, ...) is not supported
            // according to the spec:
            // "If only one selector exists in this query, explicit
            // specification of the selectorName preceding the
            // propertyName is optional"
            // but we anyway support it
            read(",");
            c = factory.fullTextSearch(getOnlySelectorName(), null, parseStaticOperand());
        } else if (readIf(".")) {
            if (!supportSQL1) {
                throw getSyntaxError("selector name, property name, or *");
            }
            read(",");
            c = factory.fullTextSearch(getOnlySelectorName(), null, parseStaticOperand());
        } else {
            String name = readName();
            if (readIf(".")) {
                if (readIf("*")) {
                    read(",");
                    c = factory.fullTextSearch(name, null, parseStaticOperand());
                } else {
                    String selector = name;
                    name = readName();
                    read(",");
                    c = factory.fullTextSearch(selector, name, parseStaticOperand());
                }
            } else {
                read(",");
                c = factory.fullTextSearch(getOnlySelectorName(), name, parseStaticOperand());
            }
        }
    } else if ("ISSAMENODE".equalsIgnoreCase(functionName)) {
        String name = readName();
        if (readIf(",")) {
            c = factory.sameNode(name, readAbsolutePath());
        } else {
            c = factory.sameNode(getOnlySelectorName(), name);
        }
    } else if ("ISCHILDNODE".equalsIgnoreCase(functionName)) {
        String name = readName();
        if (readIf(",")) {
            c = factory.childNode(name, readAbsolutePath());
        } else {
            c = factory.childNode(getOnlySelectorName(), name);
        }
    } else if ("ISDESCENDANTNODE".equalsIgnoreCase(functionName)) {
        String name = readName();
        if (readIf(",")) {
            c = factory.descendantNode(name, readAbsolutePath());
        } else {
            c = factory.descendantNode(getOnlySelectorName(), name);
        }
    } else if ("SIMILAR".equalsIgnoreCase(functionName)) {
        if (readIf(".") || readIf("*")) {
            read(",");
            c = factory.similar(getOnlySelectorName(), null, parseStaticOperand());
        } else {
            String name = readName();
            if (readIf(".")) {
                if (readIf("*")) {
                    read(",");
                    c = factory.fullTextSearch(name, null, parseStaticOperand());
                } else {
                    String selector = name;
                    name = readName();
                    read(",");
                    c = factory.fullTextSearch(selector, name, parseStaticOperand());
                }
            } else {
                read(",");
                c = factory.fullTextSearch(getOnlySelectorName(), name, parseStaticOperand());
            }
        }
    } else if ("NATIVE".equalsIgnoreCase(functionName)) {
        String selectorName;
        if (currentTokenType == IDENTIFIER) {
            selectorName = readName();
            read(",");
        } else {
            selectorName = getOnlySelectorName();
        }
        String language = readString().getValue(Type.STRING);
        read(",");
        c = factory.nativeFunction(selectorName, language, parseStaticOperand());
    } else if ("SPELLCHECK".equalsIgnoreCase(functionName)) {
        String selectorName;
        if (currentTokenType == IDENTIFIER) {
            selectorName = readName();
            read(",");
        } else {
            selectorName = getOnlySelectorName();
        }
        c = factory.spellcheck(selectorName, parseStaticOperand());
    } else if ("SUGGEST".equalsIgnoreCase(functionName)) {
        String selectorName;
        if (currentTokenType == IDENTIFIER) {
            selectorName = readName();
            read(",");
        } else {
            selectorName = getOnlySelectorName();
        }
        c = factory.suggest(selectorName, parseStaticOperand());
    } else {
        return null;
    }
    read(")");
    return c;
}
Also used : ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl)

Example 2 with ConstraintImpl

use of org.apache.jackrabbit.oak.query.ast.ConstraintImpl in project jackrabbit-oak by apache.

the class SQL2Parser method parseSelect.

private QueryImpl parseSelect() throws ParseException {
    read("SELECT");
    boolean distinct = readIf("DISTINCT");
    ArrayList<ColumnOrWildcard> list = parseColumns();
    if (supportSQL1) {
        addColumnIfNecessary(list, QueryImpl.JCR_PATH, QueryImpl.JCR_PATH);
        addColumnIfNecessary(list, QueryImpl.JCR_SCORE, QueryImpl.JCR_SCORE);
    }
    read("FROM");
    SourceImpl source = parseSource();
    ColumnImpl[] columnArray = resolveColumns(list);
    ConstraintImpl constraint = null;
    if (readIf("WHERE")) {
        constraint = parseConstraint();
    }
    QueryImpl q = new QueryImpl(statement, source, constraint, columnArray, namePathMapper, settings);
    q.setDistinct(distinct);
    return q;
}
Also used : ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl)

Example 3 with ConstraintImpl

use of org.apache.jackrabbit.oak.query.ast.ConstraintImpl in project jackrabbit-oak by apache.

the class SimpleExcerptProvider method extractFulltext.

private static Set<String> extractFulltext(ConstraintImpl c) {
    Set<String> tokens = new HashSet<String>();
    // as it will break without us noticing if we extend the AST
    if (c instanceof FullTextSearchImpl) {
        FullTextSearchImpl f = (FullTextSearchImpl) c;
        if (f.getFullTextSearchExpression() instanceof LiteralImpl) {
            LiteralImpl l = (LiteralImpl) f.getFullTextSearchExpression();
            tokens.add(l.getLiteralValue().getValue(Type.STRING));
        }
    }
    if (c instanceof AndImpl) {
        for (ConstraintImpl constraint : ((AndImpl) c).getConstraints()) {
            tokens.addAll(extractFulltext(constraint));
        }
    }
    if (c instanceof OrImpl) {
        for (ConstraintImpl constraint : ((OrImpl) c).getConstraints()) {
            tokens.addAll(extractFulltext(constraint));
        }
    }
    return tokens;
}
Also used : LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) FullTextSearchImpl(org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl) AndImpl(org.apache.jackrabbit.oak.query.ast.AndImpl) OrImpl(org.apache.jackrabbit.oak.query.ast.OrImpl) HashSet(java.util.HashSet)

Example 4 with ConstraintImpl

use of org.apache.jackrabbit.oak.query.ast.ConstraintImpl in project jackrabbit-oak by apache.

the class SQL2Parser method parseCondition.

private ConstraintImpl parseCondition() throws ParseException {
    ConstraintImpl a;
    if (readIf("NOT")) {
        a = factory.not(parseCondition());
    } else if (readIf("(")) {
        a = parseConstraint();
        read(")");
    } else if (currentTokenType == IDENTIFIER) {
        String identifier = readName();
        if (readIf("(")) {
            a = parseConditionFunctionIf(identifier);
            if (a == null) {
                DynamicOperandImpl op = parseExpressionFunction(identifier);
                a = parseCondition(op);
            }
        } else if (readIf(".")) {
            a = parseCondition(factory.propertyValue(identifier, readName()));
        } else {
            a = parseCondition(factory.propertyValue(getOnlySelectorName(), identifier));
        }
    } else if ("[".equals(currentToken)) {
        String name = readName();
        if (readIf(".")) {
            a = parseCondition(factory.propertyValue(name, readName()));
        } else {
            a = parseCondition(factory.propertyValue(getOnlySelectorName(), name));
        }
    } else if (supportSQL1) {
        StaticOperandImpl left = parseStaticOperand();
        if (readIf("IN")) {
            DynamicOperandImpl right = parseDynamicOperand();
            ConstraintImpl c = factory.comparison(right, Operator.EQUAL, left);
            return c;
        } else {
            throw getSyntaxError();
        }
    } else {
        throw getSyntaxError();
    }
    return a;
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl) ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) StaticOperandImpl(org.apache.jackrabbit.oak.query.ast.StaticOperandImpl)

Example 5 with ConstraintImpl

use of org.apache.jackrabbit.oak.query.ast.ConstraintImpl in project jackrabbit-oak by apache.

the class QueryImpl method buildAlternativeQuery.

@Override
public Query buildAlternativeQuery() {
    Query result = this;
    if (constraint != null) {
        Set<ConstraintImpl> unionList = constraint.convertToUnion();
        if (unionList.size() > 1) {
            // there are some cases where multiple ORs simplify into a single one. If we get a
            // union list of just one we don't really have to UNION anything.
            QueryImpl left = null;
            Query right = null;
            // we have something to do here.
            for (ConstraintImpl c : unionList) {
                if (right != null) {
                    right = newAlternativeUnionQuery(left, right);
                } else {
                    // pulling left to the right
                    if (left != null) {
                        right = left;
                    }
                }
                // cloning original query
                left = (QueryImpl) this.copyOf();
                // cloning the constraints and assigning to new query
                left.constraint = (ConstraintImpl) copyElementAndCheckReference(c);
                // re-composing the statement for better debug messages
                left.statement = recomposeStatement(left);
            }
            result = newAlternativeUnionQuery(left, right);
        }
    }
    return result;
}
Also used : ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl)

Aggregations

ConstraintImpl (org.apache.jackrabbit.oak.query.ast.ConstraintImpl)7 AndImpl (org.apache.jackrabbit.oak.query.ast.AndImpl)2 FullTextSearchImpl (org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl)2 LiteralImpl (org.apache.jackrabbit.oak.query.ast.LiteralImpl)2 OrImpl (org.apache.jackrabbit.oak.query.ast.OrImpl)2 StaticOperandImpl (org.apache.jackrabbit.oak.query.ast.StaticOperandImpl)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)1 ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)1 ComparisonImpl (org.apache.jackrabbit.oak.query.ast.ComparisonImpl)1 DescendantNodeImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeImpl)1 DynamicOperandImpl (org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl)1 PropertyValueImpl (org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)1 SourceImpl (org.apache.jackrabbit.oak.query.ast.SourceImpl)1 Test (org.junit.Test)1