Search in sources :

Example 1 with Select

use of lucee.runtime.sql.Select 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

DatabaseException (lucee.runtime.exp.DatabaseException)1 Select (lucee.runtime.sql.Select)1 Column (lucee.runtime.sql.exp.Column)1 Collection (lucee.runtime.type.Collection)1 Key (lucee.runtime.type.Collection.Key)1 QueryColumn (lucee.runtime.type.QueryColumn)1 QueryImpl (lucee.runtime.type.QueryImpl)1