Search in sources :

Example 1 with BindVariableValueImpl

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

the class SQL2Parser method parse.

/**
     * Parse the statement and return the query.
     *
     * @param query the query string
     * @param initialise if performing the query init ({@code true}) or not ({@code false})
     * @return the query
     * @throws ParseException if parsing fails
     */
public Query parse(final String query, final boolean initialise) throws ParseException {
    // TODO possibly support union,... as available at
    // http://docs.jboss.org/modeshape/latest/manuals/reference/html/jcr-query-and-search.html
    initialize(query);
    selectors.clear();
    expected = new ArrayList<String>();
    bindVariables = new HashMap<String, BindVariableValueImpl>();
    read();
    boolean explain = false, measure = false;
    if (readIf("EXPLAIN")) {
        explain = true;
    }
    if (readIf("MEASURE")) {
        measure = true;
    }
    Query q = parseSelect();
    while (true) {
        if (!readIf("UNION")) {
            break;
        }
        boolean unionAll = readIf("ALL");
        QueryImpl q2 = parseSelect();
        q = new UnionQueryImpl(unionAll, q, q2, settings);
    }
    OrderingImpl[] orderings = null;
    if (readIf("ORDER")) {
        read("BY");
        orderings = parseOrder();
    }
    QueryOptions options = new QueryOptions();
    if (readIf("OPTION")) {
        read("(");
        if (readIf("TRAVERSAL")) {
            String n = readName().toUpperCase(Locale.ENGLISH);
            options.traversal = Traversal.valueOf(n);
        }
        read(")");
    }
    if (!currentToken.isEmpty()) {
        throw getSyntaxError("<end>");
    }
    q.setOrderings(orderings);
    q.setExplain(explain);
    q.setMeasure(measure);
    q.setInternal(isInternal(query));
    q.setQueryOptions(options);
    if (initialise) {
        try {
            q.init();
        } catch (Exception e) {
            ParseException e2 = new ParseException(statement + ": " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    }
    return q;
}
Also used : OrderingImpl(org.apache.jackrabbit.oak.query.ast.OrderingImpl) RepositoryException(javax.jcr.RepositoryException) ParseException(java.text.ParseException) BindVariableValueImpl(org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl) ParseException(java.text.ParseException)

Example 2 with BindVariableValueImpl

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

the class QueryImpl method init.

@Override
public void init() {
    final QueryImpl query = this;
    if (constraint != null) {
        // need to do this *before* the visitation below, as the
        // simplify() method does not always keep the query reference
        // passed in setQuery(). TODO: avoid that mutability concern
        constraint = constraint.simplify();
    }
    new AstVisitorBase() {

        @Override
        public boolean visit(BindVariableValueImpl node) {
            node.setQuery(query);
            bindVariableMap.put(node.getBindVariableName(), null);
            return true;
        }

        @Override
        public boolean visit(ChildNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(ChildNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(ColumnImpl node) {
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(DescendantNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(DescendantNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(EquiJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(FullTextSearchImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(NativeFunctionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SimilarImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SpellcheckImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(SuggestImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return super.visit(node);
        }

        @Override
        public boolean visit(FullTextSearchScoreImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(LiteralImpl node) {
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(NodeLocalNameImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(NodeNameImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyExistenceImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyInexistenceImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(PropertyValueImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SameNodeImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SameNodeJoinConditionImpl node) {
            node.setQuery(query);
            node.bindSelector(source);
            return true;
        }

        @Override
        public boolean visit(SelectorImpl node) {
            String name = node.getSelectorName();
            if (selectorIndexes.put(name, selectors.size()) != null) {
                throw new IllegalArgumentException("Two selectors with the same name: " + name);
            }
            selectors.add(node);
            node.setQuery(query);
            return true;
        }

        @Override
        public boolean visit(LengthImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(UpperCaseImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(LowerCaseImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(ComparisonImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(InImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(AndImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(OrImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }

        @Override
        public boolean visit(NotImpl node) {
            node.setQuery(query);
            return super.visit(node);
        }
    }.visit(this);
    source.setQueryConstraint(constraint);
    for (ColumnImpl column : columns) {
        column.bindSelector(source);
    }
    distinctColumns = new boolean[columns.length];
    for (int i = 0; i < columns.length; i++) {
        ColumnImpl c = columns[i];
        boolean distinct = true;
        if (JCR_SCORE.equals(c.getPropertyName())) {
            distinct = false;
        }
        distinctColumns[i] = distinct;
    }
    init = true;
}
Also used : SpellcheckImpl(org.apache.jackrabbit.oak.query.ast.SpellcheckImpl) NodeLocalNameImpl(org.apache.jackrabbit.oak.query.ast.NodeLocalNameImpl) AstVisitorBase(org.apache.jackrabbit.oak.query.ast.AstVisitorBase) FullTextSearchScoreImpl(org.apache.jackrabbit.oak.query.ast.FullTextSearchScoreImpl) PropertyInexistenceImpl(org.apache.jackrabbit.oak.query.ast.PropertyInexistenceImpl) UpperCaseImpl(org.apache.jackrabbit.oak.query.ast.UpperCaseImpl) InImpl(org.apache.jackrabbit.oak.query.ast.InImpl) DescendantNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl) NodeNameImpl(org.apache.jackrabbit.oak.query.ast.NodeNameImpl) BindVariableValueImpl(org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl) SuggestImpl(org.apache.jackrabbit.oak.query.ast.SuggestImpl) ComparisonImpl(org.apache.jackrabbit.oak.query.ast.ComparisonImpl) PropertyExistenceImpl(org.apache.jackrabbit.oak.query.ast.PropertyExistenceImpl) SameNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl) ChildNodeImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeImpl) SimilarImpl(org.apache.jackrabbit.oak.query.ast.SimilarImpl) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl) SameNodeImpl(org.apache.jackrabbit.oak.query.ast.SameNodeImpl) NotImpl(org.apache.jackrabbit.oak.query.ast.NotImpl) DescendantNodeImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeImpl) NativeFunctionImpl(org.apache.jackrabbit.oak.query.ast.NativeFunctionImpl) ChildNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl) LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) FullTextSearchImpl(org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl) SelectorImpl(org.apache.jackrabbit.oak.query.ast.SelectorImpl) LowerCaseImpl(org.apache.jackrabbit.oak.query.ast.LowerCaseImpl) LengthImpl(org.apache.jackrabbit.oak.query.ast.LengthImpl) AndImpl(org.apache.jackrabbit.oak.query.ast.AndImpl) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl) OrImpl(org.apache.jackrabbit.oak.query.ast.OrImpl) EquiJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)

Example 3 with BindVariableValueImpl

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

the class SQL2Parser method parseStaticOperand.

private StaticOperandImpl parseStaticOperand() throws ParseException {
    if (currentTokenType == PLUS) {
        read();
        if (currentTokenType != VALUE) {
            throw getSyntaxError("number");
        }
        int valueType = currentValue.getType().tag();
        switch(valueType) {
            case PropertyType.LONG:
                currentValue = PropertyValues.newLong(currentValue.getValue(Type.LONG));
                break;
            case PropertyType.DOUBLE:
                currentValue = PropertyValues.newDouble(currentValue.getValue(Type.DOUBLE));
                break;
            case PropertyType.DECIMAL:
                currentValue = PropertyValues.newDecimal(currentValue.getValue(Type.DECIMAL).negate());
                break;
            default:
                throw getSyntaxError("Illegal operation: + " + currentValue);
        }
    } else if (currentTokenType == MINUS) {
        read();
        if (currentTokenType != VALUE) {
            throw getSyntaxError("number");
        }
        int valueType = currentValue.getType().tag();
        switch(valueType) {
            case PropertyType.LONG:
                currentValue = PropertyValues.newLong(-currentValue.getValue(Type.LONG));
                break;
            case PropertyType.DOUBLE:
                currentValue = PropertyValues.newDouble(-currentValue.getValue(Type.DOUBLE));
                break;
            case PropertyType.BOOLEAN:
                currentValue = PropertyValues.newBoolean(!currentValue.getValue(Type.BOOLEAN));
                break;
            case PropertyType.DECIMAL:
                currentValue = PropertyValues.newDecimal(currentValue.getValue(Type.DECIMAL).negate());
                break;
            default:
                throw getSyntaxError("Illegal operation: -" + currentValue);
        }
    }
    if (currentTokenType == VALUE) {
        LiteralImpl literal = getUncastLiteral(currentValue);
        read();
        return literal;
    } else if (currentTokenType == PARAMETER) {
        read();
        String name = readName();
        if (readIf(":")) {
            name = name + ':' + readName();
        }
        BindVariableValueImpl var = bindVariables.get(name);
        if (var == null) {
            var = factory.bindVariable(name);
            bindVariables.put(name, var);
        }
        return var;
    } else if (readIf("TRUE")) {
        LiteralImpl literal = getUncastLiteral(PropertyValues.newBoolean(true));
        return literal;
    } else if (readIf("FALSE")) {
        LiteralImpl literal = getUncastLiteral(PropertyValues.newBoolean(false));
        return literal;
    } else if (readIf("CAST")) {
        read("(");
        StaticOperandImpl op = parseStaticOperand();
        if (!(op instanceof LiteralImpl)) {
            throw getSyntaxError("literal");
        }
        LiteralImpl literal = (LiteralImpl) op;
        PropertyValue value = literal.getLiteralValue();
        read("AS");
        value = parseCastAs(value);
        read(")");
        // CastLiteral
        literal = factory.literal(value);
        return literal;
    } else {
        if (supportSQL1) {
            if (readIf("TIMESTAMP")) {
                StaticOperandImpl op = parseStaticOperand();
                if (!(op instanceof LiteralImpl)) {
                    throw getSyntaxError("literal");
                }
                LiteralImpl literal = (LiteralImpl) op;
                PropertyValue value = literal.getLiteralValue();
                value = PropertyValues.newDate(value.getValue(Type.DATE));
                literal = factory.literal(value);
                return literal;
            }
        }
        throw getSyntaxError("static operand");
    }
}
Also used : LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) BindVariableValueImpl(org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl) StaticOperandImpl(org.apache.jackrabbit.oak.query.ast.StaticOperandImpl) PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue)

Aggregations

BindVariableValueImpl (org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl)3 LiteralImpl (org.apache.jackrabbit.oak.query.ast.LiteralImpl)2 ParseException (java.text.ParseException)1 RepositoryException (javax.jcr.RepositoryException)1 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)1 AndImpl (org.apache.jackrabbit.oak.query.ast.AndImpl)1 AstVisitorBase (org.apache.jackrabbit.oak.query.ast.AstVisitorBase)1 ChildNodeImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeImpl)1 ChildNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl)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 DescendantNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl)1 EquiJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)1 FullTextSearchImpl (org.apache.jackrabbit.oak.query.ast.FullTextSearchImpl)1 FullTextSearchScoreImpl (org.apache.jackrabbit.oak.query.ast.FullTextSearchScoreImpl)1 InImpl (org.apache.jackrabbit.oak.query.ast.InImpl)1 LengthImpl (org.apache.jackrabbit.oak.query.ast.LengthImpl)1 LowerCaseImpl (org.apache.jackrabbit.oak.query.ast.LowerCaseImpl)1 NativeFunctionImpl (org.apache.jackrabbit.oak.query.ast.NativeFunctionImpl)1