Search in sources :

Example 6 with Expression

use of lucee.runtime.sql.exp.Expression in project Lucee by lucee.

the class SelectParser method bracked.

private Expression bracked(ParserString raw) throws SQLParserException {
    if (!raw.forwardIfCurrent('('))
        return null;
    raw.removeSpace();
    Expression exp = expression(raw);
    raw.removeSpace();
    if (!raw.forwardIfCurrent(')'))
        throw new SQLParserException("missing closing )");
    raw.removeSpace();
    // new BracketExpression(exp);
    return exp;
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression)

Example 7 with Expression

use of lucee.runtime.sql.exp.Expression in project Lucee by lucee.

the class SelectParser method selectExpressions.

// { (selectStatement) [AS] label | tableName [AS] label}
private void selectExpressions(ParserString raw, Select select) throws SQLParserException {
    Expression exp = null;
    do {
        raw.removeSpace();
        exp = expression(raw);
        if (exp == null)
            throw new SQLParserException("missing expression in select part of query");
        raw.removeSpace();
        if (raw.forwardIfCurrent("as ")) {
            String alias = identifier(raw, new RefBooleanImpl(false));
            if (alias == null)
                throw new SQLParserException("missing alias in select part");
            exp.setAlias(alias);
        } else {
            int start = raw.getPos();
            RefBoolean hb = new RefBooleanImpl(false);
            String alias = identifier(raw, hb);
            if (!hb.toBooleanValue() && "from".equalsIgnoreCase(alias))
                raw.setPos(start);
            else if (alias != null)
                exp.setAlias(alias);
        }
        select.addSelectExpression(exp);
        raw.removeSpace();
    } while (raw.forwardIfCurrent(','));
}
Also used : RefBoolean(lucee.commons.lang.types.RefBoolean) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) ParserString(lucee.commons.lang.ParserString) ValueString(lucee.runtime.sql.exp.value.ValueString) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl)

Example 8 with Expression

use of lucee.runtime.sql.exp.Expression in project Lucee by lucee.

the class SelectParser method havingExpressions.

private void havingExpressions(ParserString raw, Select select) throws SQLParserException {
    raw.removeSpace();
    Expression exp = expression(raw);
    if (exp == null)
        throw new SQLParserException("missing having expression");
    if (!(exp instanceof Operation))
        throw new SQLParserException("invalid having expression");
    select.setHaving((Operation) exp);
    raw.removeSpace();
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Operation(lucee.runtime.sql.exp.op.Operation)

Example 9 with Expression

use of lucee.runtime.sql.exp.Expression in project Lucee by lucee.

the class Selects method _toString.

public static String _toString(Selects __selects) {
    Select[] _selects = __selects.getSelects();
    Select s;
    StringBuffer sb = new StringBuffer();
    for (int y = 0; y < _selects.length; y++) {
        s = _selects[y];
        if (y > 0) {
            if (s.isUnionDistinct())
                sb.append("union distinct\n\n");
            else
                sb.append("union\n\n");
        }
        sb.append("select\n\t");
        if (s.isDistinct())
            sb.append("distinct\n\t");
        ValueNumber top = s.getTop();
        if (top != null)
            sb.append("top " + top.getString() + "\n\t");
        // select
        Expression[] sels = s.getSelects();
        Expression exp;
        boolean first = true;
        for (int i = 0; i < sels.length; i++) {
            if (!first)
                sb.append("\t,");
            exp = sels[i];
            sb.append(exp.toString(false) + "\n");
            first = false;
        }
        // from
        sb.append("from\n\t");
        Column[] forms = s.getFroms();
        first = true;
        for (int i = 0; i < forms.length; i++) {
            if (!first)
                sb.append("\t,");
            exp = forms[i];
            sb.append(exp.toString(false) + "\n");
            first = false;
        }
        // where
        if (s.getWhere() != null) {
            sb.append("where \n\t");
            sb.append(s.getWhere().toString(true));
            sb.append("\n");
        }
        // group by
        Column[] gbs = s.getGroupbys();
        if (gbs.length > 0) {
            sb.append("group by\n\t");
            first = true;
            for (int i = 0; i < gbs.length; i++) {
                if (!first)
                    sb.append("\t,");
                exp = gbs[i];
                sb.append(exp.toString(false) + "\n");
                first = false;
            }
        }
        // having
        Operation having = s.getHaving();
        if (having != null) {
            sb.append("having \n\t");
            sb.append(having.toString(true));
            sb.append("\n");
        }
    }
    // order by
    if (__selects.orderbys != null && __selects.orderbys.size() > 0) {
        sb.append("order by\n\t");
        Iterator<Column> it = __selects.orderbys.iterator();
        Expression exp;
        boolean first = true;
        while (it.hasNext()) {
            if (!first)
                sb.append("\t,");
            exp = it.next();
            sb.append(exp.toString(false) + " " + (exp.isDirectionBackward() ? "DESC" : "ASC") + "\n");
            first = false;
        }
    }
    return sb.toString();
}
Also used : Expression(lucee.runtime.sql.exp.Expression) Column(lucee.runtime.sql.exp.Column) ValueNumber(lucee.runtime.sql.exp.value.ValueNumber) Operation(lucee.runtime.sql.exp.op.Operation)

Example 10 with Expression

use of lucee.runtime.sql.exp.Expression 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)

Aggregations

Expression (lucee.runtime.sql.exp.Expression)20 ColumnExpression (lucee.runtime.sql.exp.ColumnExpression)18 Operation2 (lucee.runtime.sql.exp.op.Operation2)9 Column (lucee.runtime.sql.exp.Column)4 Operation (lucee.runtime.sql.exp.op.Operation)4 ParserString (lucee.commons.lang.ParserString)2 RefBoolean (lucee.commons.lang.types.RefBoolean)2 RefBooleanImpl (lucee.commons.lang.types.RefBooleanImpl)2 DatabaseException (lucee.runtime.exp.DatabaseException)2 BracketExpression (lucee.runtime.sql.exp.BracketExpression)2 Operation1 (lucee.runtime.sql.exp.op.Operation1)2 Operation3 (lucee.runtime.sql.exp.op.Operation3)2 OperationN (lucee.runtime.sql.exp.op.OperationN)2 ValueNumber (lucee.runtime.sql.exp.value.ValueNumber)2 ValueString (lucee.runtime.sql.exp.value.ValueString)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Collection (lucee.runtime.type.Collection)1