Search in sources :

Example 1 with Column

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

the class SelectParser method orderByExpressions.

private void orderByExpressions(ParserString raw, Selects selects) throws SQLParserException {
    Expression exp = null;
    do {
        raw.removeSpace();
        // print.out(raw.getCurrent());
        exp = expression(raw);
        if (!(exp instanceof Column))
            throw new SQLParserException("invalid order by part of query");
        Column col = (Column) exp;
        raw.removeSpace();
        if (raw.forwardIfCurrent("desc"))
            col.setDirectionBackward(true);
        if (raw.forwardIfCurrent("asc"))
            col.setDirectionBackward(false);
        selects.addOrderByExpression(col);
        raw.removeSpace();
    } while (raw.forwardIfCurrent(','));
    raw.removeSpace();
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Column(lucee.runtime.sql.exp.Column)

Example 2 with Column

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

the class SelectParser method tableList.

private void tableList(ParserString raw, Select select) throws SQLParserException {
    Column column = null;
    Expression exp = null;
    do {
        raw.removeSpace();
        exp = column(raw);
        if (!(exp instanceof Column))
            throw new SQLParserException("invalid table definition");
        column = (Column) exp;
        raw.removeSpace();
        if (raw.forwardIfCurrent("as ")) {
            String alias = identifier(raw, new RefBooleanImpl(false));
            if (alias == null)
                throw new SQLParserException("missing alias in select part");
            column.setAlias(alias);
        } else {
            int start = raw.getPos();
            RefBoolean hasBracked = new RefBooleanImpl(false);
            // TODO having usw
            String alias = identifier(raw, hasBracked);
            if (!hasBracked.toBooleanValue()) {
                if ("where".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if ("group".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if ("having".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if ("union".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if ("order".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if ("limit".equalsIgnoreCase(alias))
                    raw.setPos(start);
                else if (alias != null)
                    column.setAlias(alias);
            } else {
                if (alias != null)
                    column.setAlias(alias);
            }
        }
        select.addFromExpression(column);
        raw.removeSpace();
    } while (raw.forwardIfCurrent(','));
}
Also used : RefBoolean(lucee.commons.lang.types.RefBoolean) Column(lucee.runtime.sql.exp.Column) 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 3 with Column

use of lucee.runtime.sql.exp.Column 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 4 with Column

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

the class SelectParser method groupByExpressions.

private void groupByExpressions(ParserString raw, Select select) throws SQLParserException {
    Expression exp = null;
    do {
        raw.removeSpace();
        // print.out(raw.getCurrent());
        exp = expression(raw);
        if (!(exp instanceof Column))
            throw new SQLParserException("invalid group by part of query");
        Column col = (Column) exp;
        select.addGroupByExpression(col);
        raw.removeSpace();
    } while (raw.forwardIfCurrent(','));
    raw.removeSpace();
}
Also used : ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) Column(lucee.runtime.sql.exp.Column)

Example 5 with Column

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

the class QoQ method execute.

/**
 * execute a SQL Statement against CFML Scopes
 */
public Query execute(PageContext pc, SQL sql, Selects selects, int maxrows) throws PageException {
    Column[] orders = selects.getOrderbys();
    Select[] arrSelects = selects.getSelects();
    QueryImpl target = new QueryImpl(new Collection.Key[0], 0, "query", sql);
    for (int i = 0; i < arrSelects.length; i++) {
        arrSelects[i].getFroms();
        Column[] froms = arrSelects[i].getFroms();
        if (froms.length > 1)
            throw new DatabaseException("can only work with single tables yet", null, sql, null);
        executeSingle(pc, arrSelects[i], getSingleTable(pc, froms[0]), target, arrSelects.length > 1 ? -1 : maxrows, sql, orders.length > 0);
    }
    // Order By
    if (orders.length > 0) {
        order(target, orders);
        if (maxrows > -1)
            target.cutRowsTo(maxrows);
    }
    // Distinct
    if (selects.isDistinct()) {
        // order to filter
        order(target, selects.getDistincts());
        // print.e(selects.getDistincts());
        Key[] _keys = target.getColumnNames();
        QueryColumn[] columns = new QueryColumn[_keys.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = target.getColumn(_keys[i]);
        }
        int i;
        Object l, r;
        outer: for (int row = target.getRecordcount(); row > 1; row--) {
            for (i = 0; i < columns.length; i++) {
                l = columns[i].get(row, null);
                r = columns[i].get(row - 1, null);
                if (l == null || r == null) {
                    if (l != r)
                        continue outer;
                } else if (!Operator.equals(l, r, true))
                    continue outer;
            }
            target.removeRow(row);
        }
    }
    order(target, orders);
    return target;
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) QueryColumn(lucee.runtime.type.QueryColumn) Column(lucee.runtime.sql.exp.Column) QueryColumn(lucee.runtime.type.QueryColumn) Select(lucee.runtime.sql.Select) Collection(lucee.runtime.type.Collection) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Aggregations

Column (lucee.runtime.sql.exp.Column)6 Expression (lucee.runtime.sql.exp.Expression)4 ColumnExpression (lucee.runtime.sql.exp.ColumnExpression)3 QueryColumn (lucee.runtime.type.QueryColumn)2 ParserString (lucee.commons.lang.ParserString)1 RefBoolean (lucee.commons.lang.types.RefBoolean)1 RefBooleanImpl (lucee.commons.lang.types.RefBooleanImpl)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 Select (lucee.runtime.sql.Select)1 Operation (lucee.runtime.sql.exp.op.Operation)1 ValueNumber (lucee.runtime.sql.exp.value.ValueNumber)1 ValueString (lucee.runtime.sql.exp.value.ValueString)1 Collection (lucee.runtime.type.Collection)1 Key (lucee.runtime.type.Collection.Key)1 QueryImpl (lucee.runtime.type.QueryImpl)1