Search in sources :

Example 1 with StaticOperandImpl

use of org.apache.jackrabbit.oak.query.ast.StaticOperandImpl 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)

Example 2 with StaticOperandImpl

use of org.apache.jackrabbit.oak.query.ast.StaticOperandImpl 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 3 with StaticOperandImpl

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

the class SQL2Parser method parseCondition.

private ConstraintImpl parseCondition(DynamicOperandImpl left) throws ParseException {
    ConstraintImpl c;
    if (readIf("=")) {
        c = factory.comparison(left, Operator.EQUAL, parseStaticOperand());
    } else if (readIf("<>")) {
        c = factory.comparison(left, Operator.NOT_EQUAL, parseStaticOperand());
    } else if (readIf("<")) {
        c = factory.comparison(left, Operator.LESS_THAN, parseStaticOperand());
    } else if (readIf(">")) {
        c = factory.comparison(left, Operator.GREATER_THAN, parseStaticOperand());
    } else if (readIf("<=")) {
        c = factory.comparison(left, Operator.LESS_OR_EQUAL, parseStaticOperand());
    } else if (readIf(">=")) {
        c = factory.comparison(left, Operator.GREATER_OR_EQUAL, parseStaticOperand());
    } else if (readIf("LIKE")) {
        c = factory.comparison(left, Operator.LIKE, parseStaticOperand());
        if (supportSQL1) {
            if (readIf("ESCAPE")) {
                StaticOperandImpl esc = parseStaticOperand();
                if (!(esc instanceof LiteralImpl)) {
                    throw getSyntaxError("only ESCAPE '\' is supported");
                }
                PropertyValue v = ((LiteralImpl) esc).getLiteralValue();
                if (!v.getValue(Type.STRING).equals("\\")) {
                    throw getSyntaxError("only ESCAPE '\' is supported");
                }
            }
        }
    } else if (readIf("IN")) {
        read("(");
        ArrayList<StaticOperandImpl> list = new ArrayList<StaticOperandImpl>();
        do {
            StaticOperandImpl x = parseStaticOperand();
            list.add(x);
        } while (readIf(","));
        read(")");
        c = factory.in(left, list);
    } else if (readIf("IS")) {
        boolean not = readIf("NOT");
        read("NULL");
        if (!(left instanceof PropertyValueImpl)) {
            throw getSyntaxError("propertyName (NOT NULL is only supported for properties)");
        }
        PropertyValueImpl p = (PropertyValueImpl) left;
        if (not) {
            c = getPropertyExistence(p);
        } else {
            c = getPropertyInexistence(p);
        }
    } else if (readIf("NOT")) {
        if (readIf("IS")) {
            read("NULL");
            if (!(left instanceof PropertyValueImpl)) {
                throw new ParseException("Only property values can be tested for NOT IS NULL; got: " + left.getClass().getName(), parseIndex);
            }
            PropertyValueImpl pv = (PropertyValueImpl) left;
            c = getPropertyExistence(pv);
        } else {
            read("LIKE");
            c = factory.comparison(left, Operator.LIKE, parseStaticOperand());
            c = factory.not(c);
        }
    } else {
        throw getSyntaxError();
    }
    return c;
}
Also used : LiteralImpl(org.apache.jackrabbit.oak.query.ast.LiteralImpl) ConstraintImpl(org.apache.jackrabbit.oak.query.ast.ConstraintImpl) StaticOperandImpl(org.apache.jackrabbit.oak.query.ast.StaticOperandImpl) ArrayList(java.util.ArrayList) PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue) ParseException(java.text.ParseException) PropertyValueImpl(org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)

Aggregations

StaticOperandImpl (org.apache.jackrabbit.oak.query.ast.StaticOperandImpl)3 PropertyValue (org.apache.jackrabbit.oak.api.PropertyValue)2 ConstraintImpl (org.apache.jackrabbit.oak.query.ast.ConstraintImpl)2 LiteralImpl (org.apache.jackrabbit.oak.query.ast.LiteralImpl)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 BindVariableValueImpl (org.apache.jackrabbit.oak.query.ast.BindVariableValueImpl)1 DynamicOperandImpl (org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl)1 PropertyValueImpl (org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)1