Search in sources :

Example 1 with SimpleQuery

use of lucee.runtime.type.query.SimpleQuery in project Lucee by lucee.

the class Query method executeDatasoure.

private QueryResult executeDatasoure(SQL sql, boolean createUpdateData, TimeZone tz) throws PageException {
    DatasourceManagerImpl manager = (DatasourceManagerImpl) pageContext.getDataSourceManager();
    DatasourceConnection dc = manager.getConnection(pageContext, datasource, username, password);
    try {
        if (lazy && !createUpdateData && cachedWithin == null && cachedAfter == null && result == null) {
            if (returntype != RETURN_TYPE_QUERY)
                throw new DatabaseException("only return type query is allowed when lazy is set to true", null, sql, dc);
            return new SimpleQuery(pageContext, dc, sql, maxrows, blockfactor, timeout, getName(), getPageSource().getDisplayPath(), tz);
        }
        if (returntype == RETURN_TYPE_ARRAY)
            return QueryImpl.toArray(pageContext, dc, sql, maxrows, blockfactor, timeout, getName(), getPageSource().getDisplayPath(), createUpdateData, true);
        if (returntype == RETURN_TYPE_STRUCT) {
            if (columnName == null)
                throw new ApplicationException("attribute columnKey is required when return type is set to struct");
            return QueryImpl.toStruct(pageContext, dc, sql, columnName, maxrows, blockfactor, timeout, getName(), getPageSource().getDisplayPath(), createUpdateData, true);
        }
        return new QueryImpl(pageContext, dc, sql, maxrows, blockfactor, timeout, getName(), getPageSource().getDisplayPath(), createUpdateData, true);
    } finally {
        manager.releaseConnection(pageContext, dc);
    }
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SimpleQuery(lucee.runtime.type.query.SimpleQuery) DatasourceManagerImpl(lucee.runtime.db.DatasourceManagerImpl) DatabaseException(lucee.runtime.exp.DatabaseException)

Example 2 with SimpleQuery

use of lucee.runtime.type.query.SimpleQuery in project Lucee by lucee.

the class QueryUtil method toDumpData.

public static DumpData toDumpData(Query query, PageContext pageContext, int maxlevel, DumpProperties dp) {
    maxlevel--;
    Collection.Key[] keys = CollectionUtil.keys(query);
    DumpData[] heads = new DumpData[keys.length + 1];
    // int tmp=1;
    heads[0] = new SimpleDumpData("");
    for (int i = 0; i < keys.length; i++) {
        heads[i + 1] = new SimpleDumpData(keys[i].getString());
    }
    StringBuilder comment = new StringBuilder();
    // table.appendRow(1, new SimpleDumpData("SQL"), new SimpleDumpData(sql.toString()));
    String template = query.getTemplate();
    if (!StringUtil.isEmpty(template))
        comment.append("Template: ").append(template).append("\n");
    // table.appendRow(1, new SimpleDumpData("Template"), new SimpleDumpData(template));
    // in Query dump maxlevel is used as Top
    int top = dp.getMaxlevel();
    comment.append("Execution Time: ").append(Caster.toString(FormatUtil.formatNSAsMSDouble(query.getExecutionTime()))).append(" ms \n");
    comment.append("Record Count: ").append(Caster.toString(query.getRecordcount()));
    if (query.getRecordcount() > top)
        comment.append(" (showing top ").append(Caster.toString(top)).append(")");
    comment.append("\n");
    comment.append("Cached: ").append(query.isCached() ? "Yes\n" : "No\n");
    if (query.isCached() && query instanceof Query) {
        comment.append("Cache Type: ").append(query.getCacheType()).append("\n");
    }
    comment.append("Lazy: ").append(query instanceof SimpleQuery ? "Yes\n" : "No\n");
    SQL sql = query.getSql();
    if (sql != null)
        comment.append("SQL: ").append("\n").append(StringUtil.suppressWhiteSpace(sql.toString().trim())).append("\n");
    // table.appendRow(1, new SimpleDumpData("Execution Time (ms)"), new SimpleDumpData(exeTime));
    // table.appendRow(1, new SimpleDumpData("recordcount"), new SimpleDumpData(getRecordcount()));
    // table.appendRow(1, new SimpleDumpData("cached"), new SimpleDumpData(isCached()?"Yes":"No"));
    DumpTable recs = new DumpTable("query", "#cc99cc", "#ffccff", "#000000");
    recs.setTitle("Query");
    if (dp.getMetainfo())
        recs.setComment(comment.toString());
    recs.appendRow(new DumpRow(-1, heads));
    // body
    DumpData[] items;
    int recordcount = query.getRecordcount();
    int columncount = query.getColumnNames().length;
    for (int i = 0; i < recordcount; i++) {
        items = new DumpData[columncount + 1];
        items[0] = new SimpleDumpData(i + 1);
        for (int y = 0; y < keys.length; y++) {
            try {
                Object o = query.getAt(keys[y], i + 1);
                if (o instanceof String)
                    items[y + 1] = new SimpleDumpData(o.toString());
                else if (o instanceof Number)
                    items[y + 1] = new SimpleDumpData(Caster.toString(((Number) o)));
                else if (o instanceof Boolean)
                    items[y + 1] = new SimpleDumpData(((Boolean) o).booleanValue());
                else if (o instanceof Date)
                    items[y + 1] = new SimpleDumpData(Caster.toString(o));
                else if (o instanceof Clob)
                    items[y + 1] = new SimpleDumpData(Caster.toString(o));
                else
                    items[y + 1] = DumpUtil.toDumpData(o, pageContext, maxlevel, dp);
            } catch (PageException e) {
                items[y + 1] = new SimpleDumpData("[empty]");
            }
        }
        recs.appendRow(new DumpRow(1, items));
        if (i == top - 1)
            break;
    }
    if (!dp.getMetainfo())
        return recs;
    // table.appendRow(1, new SimpleDumpData("result"), recs);
    return recs;
}
Also used : PageException(lucee.runtime.exp.PageException) Query(lucee.runtime.type.Query) SimpleQuery(lucee.runtime.type.query.SimpleQuery) SimpleQuery(lucee.runtime.type.query.SimpleQuery) DumpRow(lucee.runtime.dump.DumpRow) DumpData(lucee.runtime.dump.DumpData) SimpleDumpData(lucee.runtime.dump.SimpleDumpData) Date(java.util.Date) SQL(lucee.runtime.db.SQL) DumpTable(lucee.runtime.dump.DumpTable) SimpleDumpData(lucee.runtime.dump.SimpleDumpData) Clob(java.sql.Clob) Key(lucee.runtime.type.Collection.Key)

Aggregations

SimpleQuery (lucee.runtime.type.query.SimpleQuery)2 Clob (java.sql.Clob)1 Date (java.util.Date)1 DatasourceConnection (lucee.runtime.db.DatasourceConnection)1 DatasourceManagerImpl (lucee.runtime.db.DatasourceManagerImpl)1 SQL (lucee.runtime.db.SQL)1 DumpData (lucee.runtime.dump.DumpData)1 DumpRow (lucee.runtime.dump.DumpRow)1 DumpTable (lucee.runtime.dump.DumpTable)1 SimpleDumpData (lucee.runtime.dump.SimpleDumpData)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 PageException (lucee.runtime.exp.PageException)1 Key (lucee.runtime.type.Collection.Key)1 Query (lucee.runtime.type.Query)1 QueryImpl (lucee.runtime.type.QueryImpl)1