Search in sources :

Example 1 with Operation2

use of lucee.runtime.sql.exp.op.Operation2 in project Lucee by lucee.

the class SelectParser method andOp.

private Expression andOp(ParserString raw) throws SQLParserException {
    Expression expr = notOp(raw);
    while (raw.forwardIfCurrentAndNoWordNumberAfter("and")) {
        raw.removeSpace();
        expr = new Operation2(expr, notOp(raw), Operation.OPERATION2_AND);
    }
    return expr;
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Operation2(lucee.runtime.sql.exp.op.Operation2)

Example 2 with Operation2

use of lucee.runtime.sql.exp.op.Operation2 in project Lucee by lucee.

the class SelectParser method orOp.

private Expression orOp(ParserString raw) throws SQLParserException {
    Expression expr = andOp(raw);
    while (raw.forwardIfCurrentAndNoWordNumberAfter("or")) {
        raw.removeSpace();
        expr = new Operation2(expr, andOp(raw), Operation.OPERATION2_OR);
    }
    return expr;
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Operation2(lucee.runtime.sql.exp.op.Operation2)

Example 3 with Operation2

use of lucee.runtime.sql.exp.op.Operation2 in project Lucee by lucee.

the class QoQ method executeOperation.

private Object executeOperation(PageContext pc, SQL sql, Query qr, Operation operation, int row) throws PageException {
    if (operation instanceof Operation2) {
        Operation2 op2 = (Operation2) operation;
        switch(op2.getOperator()) {
            case Operation.OPERATION2_AND:
                return executeAnd(pc, sql, qr, op2, row);
            case Operation.OPERATION2_OR:
                return executeOr(pc, sql, qr, op2, row);
            case Operation.OPERATION2_XOR:
                return executeXor(pc, sql, qr, op2, row);
            case Operation.OPERATION2_EQ:
                return executeEQ(pc, sql, qr, op2, row);
            case Operation.OPERATION2_NEQ:
                return executeNEQ(pc, sql, qr, op2, row);
            case Operation.OPERATION2_LTGT:
                return executeNEQ(pc, sql, qr, op2, row);
            case Operation.OPERATION2_LT:
                return executeLT(pc, sql, qr, op2, row);
            case Operation.OPERATION2_LTE:
                return executeLTE(pc, sql, qr, op2, row);
            case Operation.OPERATION2_GT:
                return executeGT(pc, sql, qr, op2, row);
            case Operation.OPERATION2_GTE:
                return executeGTE(pc, sql, qr, op2, row);
            case Operation.OPERATION2_MINUS:
                return executeMinus(pc, sql, qr, op2, row);
            case Operation.OPERATION2_PLUS:
                return executePlus(pc, sql, qr, op2, row);
            case Operation.OPERATION2_DIVIDE:
                return executeDivide(pc, sql, qr, op2, row);
            case Operation.OPERATION2_MULTIPLY:
                return executeMultiply(pc, sql, qr, op2, row);
            case Operation.OPERATION2_EXP:
                return executeExponent(pc, sql, qr, op2, row);
            case Operation.OPERATION2_LIKE:
                return Caster.toBoolean(executeLike(pc, sql, qr, op2, row));
            case Operation.OPERATION2_NOT_LIKE:
                return Caster.toBoolean(!executeLike(pc, sql, qr, op2, row));
            case Operation.OPERATION2_MOD:
                return executeMod(pc, sql, qr, op2, row);
        }
    }
    if (operation instanceof Operation1) {
        Operation1 op1 = (Operation1) operation;
        int o = op1.getOperator();
        if (o == Operation.OPERATION1_IS_NULL) {
            Object value = executeExp(pc, sql, qr, op1.getExp(), row, null);
            return Caster.toBoolean(value == null);
        }
        if (o == Operation.OPERATION1_IS_NOT_NULL) {
            Object value = executeExp(pc, sql, qr, op1.getExp(), row, null);
            return Caster.toBoolean(value != null);
        }
        Object value = executeExp(pc, sql, qr, op1.getExp(), row);
        if (o == Operation.OPERATION1_MINUS)
            return Caster.toDouble(-Caster.toDoubleValue(value));
        if (o == Operation.OPERATION1_PLUS)
            return Caster.toDouble(value);
        if (o == Operation.OPERATION1_NOT)
            return Caster.toBoolean(!Caster.toBooleanValue(value));
    }
    if (operation instanceof Operation3) {
        Operation3 op3 = (Operation3) operation;
        int o = op3.getOperator();
        if (o == Operation.OPERATION3_BETWEEN)
            return executeBetween(pc, sql, qr, op3, row);
        if (o == Operation.OPERATION3_LIKE)
            return executeLike(pc, sql, qr, op3, row);
    }
    if (!(operation instanceof OperationN))
        throw new DatabaseException("invalid syntax for SQL Statement", null, sql, null);
    OperationN opn = (OperationN) operation;
    String op = StringUtil.toLowerCase(opn.getOperator());
    Expression[] operators = opn.getOperants();
    // 11111111111111111111111111111111111111111111111111111
    if (operators.length == 1) {
        Object value = executeExp(pc, sql, qr, operators[0], row);
        // Functions
        switch(op.charAt(0)) {
            case 'a':
                if (op.equals("abs"))
                    return new Double(MathUtil.abs(Caster.toDoubleValue(value)));
                if (op.equals("acos"))
                    return new Double(Math.acos(Caster.toDoubleValue(value)));
                if (op.equals("asin"))
                    return new Double(Math.asin(Caster.toDoubleValue(value)));
                if (op.equals("atan"))
                    return new Double(Math.atan(Caster.toDoubleValue(value)));
                break;
            case 'c':
                if (op.equals("ceiling"))
                    return new Double(Math.ceil(Caster.toDoubleValue(value)));
                if (op.equals("cos"))
                    return new Double(Math.cos(Caster.toDoubleValue(value)));
                if (op.equals("cast"))
                    return Caster.castTo(pc, CFTypes.toShort(operators[0].getAlias(), true, CFTypes.TYPE_UNKNOW), operators[0].getAlias(), value);
                break;
            case 'e':
                if (op.equals("exp"))
                    return new Double(Math.exp(Caster.toDoubleValue(value)));
                break;
            case 'f':
                if (op.equals("floor"))
                    return new Double(Math.floor(Caster.toDoubleValue(value)));
                break;
            case 'u':
                if (op.equals("upper") || op.equals("ucase"))
                    return Caster.toString(value).toUpperCase();
                break;
            case 'l':
                if (op.equals("lower") || op.equals("lcase"))
                    return Caster.toString(value).toLowerCase();
                if (op.equals("ltrim"))
                    return StringUtil.ltrim(Caster.toString(value), null);
                if (op.equals("length"))
                    return new Double(Caster.toString(value).length());
                break;
            case 'r':
                if (op.equals("rtrim"))
                    return StringUtil.rtrim(Caster.toString(value), null);
                break;
            case 's':
                if (op.equals("sign"))
                    return new Double(MathUtil.sgn(Caster.toDoubleValue(value)));
                if (op.equals("sin"))
                    return new Double(Math.sin(Caster.toDoubleValue(value)));
                if (op.equals("soundex"))
                    return StringUtil.soundex(Caster.toString(value));
                if (op.equals("sin"))
                    return new Double(Math.sqrt(Caster.toDoubleValue(value)));
                break;
            case 't':
                if (op.equals("tan"))
                    return new Double(Math.tan(Caster.toDoubleValue(value)));
                if (op.equals("trim"))
                    return Caster.toString(value).trim();
                break;
        }
    } else // 22222222222222222222222222222222222222222222222222222
    if (operators.length == 2) {
        // if(op.equals("=") || op.equals("in")) return executeEQ(pc,sql,qr,expression,row);
        Object left = executeExp(pc, sql, qr, operators[0], row);
        Object right = executeExp(pc, sql, qr, operators[1], row);
        // Functions
        switch(op.charAt(0)) {
            case 'a':
                if (op.equals("atan2"))
                    return new Double(Math.atan2(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
                break;
            case 'b':
                if (op.equals("bitand"))
                    return new Double(Operator.bitand(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
                if (op.equals("bitor"))
                    return new Double(Operator.bitor(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
                break;
            case 'c':
                if (op.equals("concat"))
                    return Caster.toString(left).concat(Caster.toString(right));
                break;
            case 'm':
                if (op.equals("mod"))
                    return new Double(Operator.modulus(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
                break;
        }
    // throw new DatabaseException("unsopprted sql statement ["+op+"]",null,sql);
    }
    if (op.equals("in"))
        return executeIn(pc, sql, qr, opn, row, false);
    if (op.equals("not_in"))
        return executeIn(pc, sql, qr, opn, row, true);
    // print(expression);
    throw new DatabaseException("unsopprted sql statement (" + op + ") ", null, sql, null);
}
Also used : BracketExpression(lucee.runtime.sql.exp.BracketExpression) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) OperationN(lucee.runtime.sql.exp.op.OperationN) Operation2(lucee.runtime.sql.exp.op.Operation2) Operation1(lucee.runtime.sql.exp.op.Operation1) Operation3(lucee.runtime.sql.exp.op.Operation3) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 4 with Operation2

use of lucee.runtime.sql.exp.op.Operation2 in project Lucee by lucee.

the class SelectParser method decsionOp.

private Expression decsionOp(ParserString raw) throws SQLParserException {
    Expression expr = plusMinusOp(raw);
    boolean hasChanged = false;
    do {
        hasChanged = false;
        // value BETWEEN value AND value
        if (raw.forwardIfCurrent("between ")) {
            raw.removeSpace();
            Expression left = plusMinusOp(raw);
            raw.removeSpace();
            if (!raw.forwardIfCurrent("and "))
                throw new SQLParserException("invalid operation (between) missing operator and");
            raw.removeSpace();
            Expression right = plusMinusOp(raw);
            raw.removeSpace();
            expr = new Operation3(expr, left, right, Operation.OPERATION3_BETWEEN);
            hasChanged = true;
        } else // value like value [escape value]
        if (raw.forwardIfCurrentAndNoWordNumberAfter("like")) {
            raw.removeSpace();
            Expression left = plusMinusOp(raw);
            raw.removeSpace();
            if (raw.forwardIfCurrentAndNoWordNumberAfter("escape ")) {
                raw.removeSpace();
                Expression right = plusMinusOp(raw);
                raw.removeSpace();
                expr = new Operation3(expr, left, right, Operation.OPERATION3_LIKE);
            } else {
                raw.removeSpace();
                expr = new Operation2(expr, left, Operation.OPERATION2_LIKE);
            }
            hasChanged = true;
        } else // IS [NOT] NULL
        if (raw.isCurrent("is ")) {
            int start = raw.getPos();
            if (raw.forwardIfCurrentAndNoWordNumberAfter("is null")) {
                raw.removeSpace();
                return new Operation1(expr, Operation.OPERATION1_IS_NULL);
            } else if (raw.forwardIfCurrentAndNoWordNumberAfter("is not null")) {
                raw.removeSpace();
                return new Operation1(expr, Operation.OPERATION1_IS_NOT_NULL);
            } else {
                raw.setPos(start);
                raw.removeSpace();
            }
        } else // not in
        if (raw.forwardIfCurrent("not in", '(')) {
            expr = new OperationN("not_in", readArguments(raw, expr));
            hasChanged = true;
        } else // in
        if (raw.forwardIfCurrent("in", '(')) {
            expr = new OperationN("in", readArguments(raw, expr));
            hasChanged = true;
        }
        // not like
        if (raw.forwardIfCurrentAndNoWordNumberAfter("not like")) {
            expr = decisionOpCreate(raw, Operation.OPERATION2_NOT_LIKE, expr);
            hasChanged = true;
        } else // =
        if (raw.forwardIfCurrent('=')) {
            expr = decisionOpCreate(raw, Operation.OPERATION2_EQ, expr);
            hasChanged = true;
        } else // !=
        if (raw.forwardIfCurrent("!=")) {
            expr = decisionOpCreate(raw, Operation.OPERATION2_NEQ, expr);
            hasChanged = true;
        } else // <>
        if (raw.forwardIfCurrent("<>")) {
            expr = decisionOpCreate(raw, Operation.OPERATION2_LTGT, expr);
            hasChanged = true;
        } else // <, <=
        if (raw.isCurrent('<')) {
            if (raw.forwardIfCurrent("<=")) {
                expr = decisionOpCreate(raw, Operation.OPERATION2_LTE, expr);
                hasChanged = true;
            } else {
                raw.next();
                expr = decisionOpCreate(raw, Operation.OPERATION2_LT, expr);
                hasChanged = true;
            }
        } else // >, =>
        if (raw.isCurrent('>')) {
            if (raw.forwardIfCurrent("=>")) {
                expr = decisionOpCreate(raw, Operation.OPERATION2_GTE, expr);
                hasChanged = true;
            }
            if (raw.forwardIfCurrent(">=")) {
                expr = decisionOpCreate(raw, Operation.OPERATION2_GTE, expr);
                hasChanged = true;
            } else {
                raw.next();
                expr = decisionOpCreate(raw, Operation.OPERATION2_GT, expr);
                hasChanged = true;
            }
        }
    } while (hasChanged);
    return expr;
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) OperationN(lucee.runtime.sql.exp.op.OperationN) Operation2(lucee.runtime.sql.exp.op.Operation2) Operation1(lucee.runtime.sql.exp.op.Operation1) Operation3(lucee.runtime.sql.exp.op.Operation3)

Example 5 with Operation2

use of lucee.runtime.sql.exp.op.Operation2 in project Lucee by lucee.

the class SelectParser method modOp.

private Expression modOp(ParserString raw) throws SQLParserException {
    Expression expr = divMultiOp(raw);
    // Modulus Operation
    while (raw.forwardIfCurrent('%')) {
        raw.removeSpace();
        expr = new Operation2(expr, divMultiOp(raw), Operation.OPERATION2_MOD);
    }
    return expr;
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Operation2(lucee.runtime.sql.exp.op.Operation2)

Aggregations

ColumnExpression (lucee.runtime.sql.exp.ColumnExpression)9 Expression (lucee.runtime.sql.exp.Expression)9 Operation2 (lucee.runtime.sql.exp.op.Operation2)9 Operation1 (lucee.runtime.sql.exp.op.Operation1)2 Operation3 (lucee.runtime.sql.exp.op.Operation3)2 OperationN (lucee.runtime.sql.exp.op.OperationN)2 DatabaseException (lucee.runtime.exp.DatabaseException)1 BracketExpression (lucee.runtime.sql.exp.BracketExpression)1