Search in sources :

Example 11 with QueryColumn

use of lucee.runtime.type.QueryColumn in project Lucee by lucee.

the class QoQ method executeSingle.

private void executeSingle(PageContext pc, Select select, Query qr, QueryImpl target, int maxrows, SQL sql, boolean hasOrders) throws PageException {
    ValueNumber oTop = select.getTop();
    if (oTop != null) {
        int top = (int) oTop.getValueAsDouble();
        if (maxrows == -1 || maxrows > top)
            maxrows = top;
    }
    int recCount = qr.getRecordcount();
    Expression[] expSelects = select.getSelects();
    int selCount = expSelects.length;
    Map<Collection.Key, Object> selects = new HashMap<Collection.Key, Object>();
    Iterator<Key> it;
    Key k;
    // headers
    for (int i = 0; i < selCount; i++) {
        Expression expSelect = expSelects[i];
        if (expSelect.getAlias().equals("*")) {
            it = qr.keyIterator();
            while (it.hasNext()) {
                k = it.next();
                selects.put(k, k);
                queryAddColumn(target, k, qr.getColumn(k).getType());
            }
        } else {
            Key alias = Caster.toKey(expSelect.getAlias());
            selects.put(alias, expSelect);
            int type = Types.OTHER;
            if (expSelect instanceof ColumnExpression)
                type = qr.getColumn(Caster.toKey(((ColumnExpression) expSelect).getColumnName())).getType();
            queryAddColumn(target, alias, type);
        }
    }
    Collection.Key[] headers = selects.keySet().toArray(new Collection.Key[selects.size()]);
    // loop records
    Operation where = select.getWhere();
    boolean hasMaxrow = maxrows > -1 && !hasOrders;
    // get target columns
    QueryColumn[] trgColumns = new QueryColumn[headers.length];
    Object[] trgValues = new Object[headers.length];
    for (int cell = 0; cell < headers.length; cell++) {
        trgColumns[cell] = target.getColumn(headers[cell]);
        trgValues[cell] = selects.get(headers[cell]);
    }
    for (int row = 1; row <= recCount; row++) {
        sql.setPosition(0);
        if (hasMaxrow && maxrows <= target.getRecordcount())
            break;
        boolean useRow = where == null || Caster.toBooleanValue(executeExp(pc, sql, qr, where, row));
        if (useRow) {
            target.addRow(1);
            for (int cell = 0; cell < headers.length; cell++) {
                // Object value = selects.get(headers[cell]);
                trgColumns[cell].set(target.getRecordcount(), getValue(pc, sql, qr, row, headers[cell], trgValues[cell]));
            /*target.setAt(
							headers[cell],
							target.getRecordcount(),
							getValue(pc,sql,qr,row,headers[cell],trgValues[cell])
						);*/
            }
        }
    }
    // Group By
    if (select.getGroupbys().length > 0)
        throw new DatabaseException("group by are not supported at the moment", null, sql, null);
    if (select.getHaving() != null)
        throw new DatabaseException("having is not supported at the moment", null, sql, null);
}
Also used : HashMap(java.util.HashMap) Operation(lucee.runtime.sql.exp.op.Operation) BracketExpression(lucee.runtime.sql.exp.BracketExpression) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) Expression(lucee.runtime.sql.exp.Expression) ColumnExpression(lucee.runtime.sql.exp.ColumnExpression) QueryColumn(lucee.runtime.type.QueryColumn) ValueNumber(lucee.runtime.sql.exp.value.ValueNumber) Collection(lucee.runtime.type.Collection) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Example 12 with QueryColumn

use of lucee.runtime.type.QueryColumn in project Lucee by lucee.

the class DebugEntryTemplatePartComparator method getUsage.

private static Struct getUsage(QueryEntry qe) throws PageException {
    Query qry = qe.getQry();
    QueryColumn c;
    DebugQueryColumn dqc;
    outer: if (qry != null) {
        Struct usage = null;
        Collection.Key[] columnNames = qry.getColumnNames();
        Collection.Key columnName;
        for (int i = 0; i < columnNames.length; i++) {
            columnName = columnNames[i];
            c = qry.getColumn(columnName);
            if (!(c instanceof DebugQueryColumn))
                break outer;
            dqc = (DebugQueryColumn) c;
            if (usage == null)
                usage = new StructImpl();
            usage.setEL(columnName, Caster.toBoolean(dqc.isUsed()));
        }
        return usage;
    }
    return null;
}
Also used : DebugQueryColumn(lucee.runtime.type.DebugQueryColumn) StructImpl(lucee.runtime.type.StructImpl) Query(lucee.runtime.type.Query) QueryColumn(lucee.runtime.type.QueryColumn) DebugQueryColumn(lucee.runtime.type.DebugQueryColumn) Collection(lucee.runtime.type.Collection) Key(lucee.runtime.type.Collection.Key) Struct(lucee.runtime.type.Struct)

Example 13 with QueryColumn

use of lucee.runtime.type.QueryColumn in project Lucee by lucee.

the class JavaProxy method _toCFML.

public static Object _toCFML(Object value) throws PageException {
    if (value instanceof Date || value instanceof Calendar) {
        // do not change to caster.isDate
        return Caster.toDate(value, null);
    }
    if (value instanceof Object[]) {
        Object[] arr = (Object[]) value;
        if (!ArrayUtil.isEmpty(arr)) {
            boolean allTheSame = true;
            // byte
            if (arr[0] instanceof Byte) {
                for (int i = 1; i < arr.length; i++) {
                    if (!(arr[i] instanceof Byte)) {
                        allTheSame = false;
                        break;
                    }
                }
                if (allTheSame) {
                    byte[] bytes = new byte[arr.length];
                    for (int i = 0; i < arr.length; i++) {
                        bytes[i] = Caster.toByteValue(arr[i]);
                    }
                    return bytes;
                }
            }
        }
    }
    if (value instanceof Byte[]) {
        Byte[] arr = (Byte[]) value;
        if (!ArrayUtil.isEmpty(arr)) {
            byte[] bytes = new byte[arr.length];
            for (int i = 0; i < arr.length; i++) {
                bytes[i] = arr[i].byteValue();
            }
            return bytes;
        }
    }
    if (value instanceof byte[]) {
        return value;
    }
    if (Decision.isArray(value)) {
        Array a = Caster.toArray(value);
        int len = a.size();
        Object o;
        for (int i = 1; i <= len; i++) {
            o = a.get(i, null);
            if (o != null)
                a.setEL(i, toCFML(o));
        }
        return a;
    }
    if (value instanceof Map) {
        Struct sct = new StructImpl();
        Iterator it = ((Map) value).entrySet().iterator();
        Map.Entry entry;
        while (it.hasNext()) {
            entry = (Entry) it.next();
            sct.setEL(Caster.toString(entry.getKey()), toCFML(entry.getValue()));
        }
        return sct;
    // return StructUtil.copyToStruct((Map)value);
    }
    if (Decision.isQuery(value)) {
        Query q = Caster.toQuery(value);
        int recorcount = q.getRecordcount();
        String[] strColumns = q.getColumns();
        QueryColumn col;
        int row;
        for (int i = 0; i < strColumns.length; i++) {
            col = q.getColumn(strColumns[i]);
            for (row = 1; row <= recorcount; row++) {
                col.set(row, toCFML(col.get(row, null)));
            }
        }
        return q;
    }
    return value;
}
Also used : Entry(java.util.Map.Entry) Query(lucee.runtime.type.Query) Calendar(java.util.Calendar) Date(java.util.Date) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) StructImpl(lucee.runtime.type.StructImpl) QueryColumn(lucee.runtime.type.QueryColumn) Iterator(java.util.Iterator) Map(java.util.Map)

Example 14 with QueryColumn

use of lucee.runtime.type.QueryColumn in project Lucee by lucee.

the class Caster method toQuery.

/**
 * cast a Object to a Query Object
 * @param o Object to cast
 * @param duplicate duplicate the object or not
 * @return casted Query Object
 * @throws PageException
 */
public static Query toQuery(Object o, boolean duplicate) throws PageException {
    if (o instanceof Query) {
        if (duplicate) {
            Query src = (Query) o;
            Query trg = new QueryImpl(src.getColumnNames(), src.getRowCount(), "query");
            Collection.Key[] keys = src.getColumnNames();
            QueryColumn[] columnsSrc = new QueryColumn[keys.length];
            for (int i = 0; i < columnsSrc.length; i++) {
                columnsSrc[i] = src.getColumn(keys[i]);
            }
            keys = trg.getColumnNames();
            QueryColumn[] columnsTrg = new QueryColumn[keys.length];
            for (int i = 0; i < columnsTrg.length; i++) {
                columnsTrg[i] = trg.getColumn(keys[i]);
            }
            int i;
            for (int row = trg.getRecordcount(); row > 0; row--) {
                for (i = 0; i < columnsTrg.length; i++) {
                    columnsTrg[i].set(row, columnsSrc[i].get(row, null));
                }
            }
            return trg;
        }
        return (Query) o;
    } else if (o instanceof ObjectWrap) {
        return toQuery(((ObjectWrap) o).getEmbededObject(), duplicate);
    }
    throw new CasterException(o, "query");
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) Query(lucee.runtime.type.Query) QueryColumn(lucee.runtime.type.QueryColumn) Key(lucee.runtime.type.Collection.Key)

Aggregations

QueryColumn (lucee.runtime.type.QueryColumn)14 Key (lucee.runtime.type.Collection.Key)7 Query (lucee.runtime.type.Query)7 Array (lucee.runtime.type.Array)6 Collection (lucee.runtime.type.Collection)5 QueryImpl (lucee.runtime.type.QueryImpl)5 DatabaseException (lucee.runtime.exp.DatabaseException)4 ArrayImpl (lucee.runtime.type.ArrayImpl)4 Struct (lucee.runtime.type.Struct)4 StructImpl (lucee.runtime.type.StructImpl)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 QueryBean (coldfusion.xml.rpc.QueryBean)2 Calendar (java.util.Calendar)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1