Search in sources :

Example 1 with ZQuery

use of lucee.runtime.sql.old.ZQuery in project Lucee by lucee.

the class HSQLUtil method getInvokedTables.

private void getInvokedTables(ZQuery query, Set<String> tablesNames) {
    // print.out("qry:"+query.getSet());
    Vector tables = query.getFrom();
    Enumeration e = tables.elements();
    // from
    while (e.hasMoreElements()) {
        ZFromItem fromItem = (ZFromItem) e.nextElement();
        tablesNames.add(fromItem.getFullName());
    }
    // where
    ZExp where = query.getWhere();
    if (where instanceof ZExpression) {
        parseZExpression((ZExpression) where, tablesNames);
    }
    // set
    ZExpression set = query.getSet();
    if (set != null) {
        isUnion = true;
        ZExp op = set.getOperand(0);
        if (op instanceof ZQuery)
            getInvokedTables((ZQuery) op, tablesNames);
    }
}
Also used : Enumeration(java.util.Enumeration) ZFromItem(lucee.runtime.sql.old.ZFromItem) ZExpression(lucee.runtime.sql.old.ZExpression) ZExp(lucee.runtime.sql.old.ZExp) Vector(java.util.Vector) ZQuery(lucee.runtime.sql.old.ZQuery)

Example 2 with ZQuery

use of lucee.runtime.sql.old.ZQuery in project Lucee by lucee.

the class HSQLUtil method getInvokedTables.

/**
 * return all invoked tables by a sql statement
 * @return invoked tables in a ArrayList
 * @throws ParseException
 */
public Set<String> getInvokedTables() throws ParseException {
    // Read all SQL statements from input
    ZStatement st;
    Set<String> tables = new HashSet<String>();
    while ((st = parser.readStatement()) != null) {
        this.sql = st.toString();
        if (st instanceof ZQuery) {
            // An SQL query: query the DB
            getInvokedTables((ZQuery) st, tables);
        }
        break;
    }
    return tables;
}
Also used : ZStatement(lucee.runtime.sql.old.ZStatement) ZQuery(lucee.runtime.sql.old.ZQuery) HashSet(java.util.HashSet)

Example 3 with ZQuery

use of lucee.runtime.sql.old.ZQuery in project Lucee by lucee.

the class Executer method testExecute.

private Query testExecute(PageContext pc, SQL sql, Query qr, ZQuery query, int maxrows) throws PageException {
    int recCount = qr.getRecordcount();
    Vector vSelects = query.getSelect();
    int selCount = vSelects.size();
    Map<Collection.Key, Object> selects = MapFactory.<Collection.Key, Object>getConcurrentMap();
    boolean isSMS = false;
    // headers
    for (int i = 0; i < selCount; i++) {
        ZSelectItem select = (ZSelectItem) vSelects.get(i);
        if (select.isWildcard() || (isSMS = select.getColumn().equals(SQLPrettyfier.PLACEHOLDER_ASTERIX))) {
            if (!isSMS && !select.getColumn().equals("*"))
                throw new DatabaseException("can't execute this type of query at the moment", null, sql, null);
            // Collection.Key[] keys = qr.keys();
            Iterator<Key> it = qr.keyIterator();
            Key k;
            while (it.hasNext()) {
                k = it.next();
                selects.put(k, k.getString());
            }
            isSMS = false;
        } else {
            // if(SQLPrettyfier.PLACEHOLDER_COUNT.equals(select.getAlias())) select.setAlias("count");
            // if(SQLPrettyfier.PLACEHOLDER_COUNT.equals(select.getColumn())) select.setExpression(new ZConstant("count",ZConstant.COLUMNNAME));
            String alias = select.getAlias();
            String column = select.getColumn();
            if (alias == null)
                alias = column;
            alias = alias.toLowerCase();
            selects.put(KeyImpl.init(alias), select);
        }
    }
    Key[] headers = selects.keySet().toArray(new Collection.Key[selects.size()]);
    // aHeaders.toArray(new String[aHeaders.size()]);
    QueryImpl rtn = new QueryImpl(headers, 0, "query", sql);
    // loop records
    Vector orders = query.getOrderBy();
    ZExp where = query.getWhere();
    // print.out(headers);
    // int newRecCount=0;
    boolean hasMaxrow = maxrows > -1 && (orders == null || orders.size() == 0);
    for (int row = 1; row <= recCount; row++) {
        sql.setPosition(0);
        if (hasMaxrow && maxrows <= rtn.getRecordcount())
            break;
        boolean useRow = where == null || Caster.toBooleanValue(executeExp(pc, sql, qr, where, row));
        if (useRow) {
            rtn.addRow(1);
            for (int cell = 0; cell < headers.length; cell++) {
                Object value = selects.get(headers[cell]);
                rtn.setAt(headers[cell], rtn.getRecordcount(), getValue(pc, sql, qr, row, headers[cell], value));
            }
        }
    }
    // Group By
    if (query.getGroupBy() != null)
        throw new DatabaseException("group by are not supported at the moment", null, sql, null);
    // Order By
    if (orders != null && orders.size() > 0) {
        int len = orders.size();
        for (int i = len - 1; i >= 0; i--) {
            ZOrderBy order = (ZOrderBy) orders.get(i);
            ZConstant name = (ZConstant) order.getExpression();
            rtn.sort(name.getValue().toLowerCase(), order.getAscOrder() ? Query.ORDER_ASC : Query.ORDER_DESC);
        }
        if (maxrows > -1) {
            rtn.cutRowsTo(maxrows);
        }
    }
    // Distinct
    if (query.isDistinct()) {
        String[] keys = rtn.getColumns();
        QueryColumn[] columns = new QueryColumn[keys.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = rtn.getColumn(keys[i]);
        }
        int i;
        outer: for (int row = rtn.getRecordcount(); row > 1; row--) {
            for (i = 0; i < columns.length; i++) {
                if (!Operator.equals(QueryUtil.getValue(columns[i], row), QueryUtil.getValue(columns[i], row - 1), true))
                    continue outer;
            }
            rtn.removeRow(row);
        }
    }
    // UNION // TODO support it
    ZExpression set = query.getSet();
    if (set != null) {
        ZExp op = set.getOperand(0);
        if (op instanceof ZQuery)
            throw new DatabaseException("union is not supported at the moment", null, sql, null);
    // getInvokedTables((ZQuery)op, tablesNames);
    }
    return rtn;
}
Also used : ZOrderBy(lucee.runtime.sql.old.ZOrderBy) ZExpression(lucee.runtime.sql.old.ZExpression) ZExp(lucee.runtime.sql.old.ZExp) ZQuery(lucee.runtime.sql.old.ZQuery) ZConstant(lucee.runtime.sql.old.ZConstant) QueryImpl(lucee.runtime.type.QueryImpl) ZSelectItem(lucee.runtime.sql.old.ZSelectItem) QueryColumn(lucee.runtime.type.QueryColumn) Collection(lucee.runtime.type.Collection) Vector(java.util.Vector) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key)

Example 4 with ZQuery

use of lucee.runtime.sql.old.ZQuery in project Lucee by lucee.

the class HSQLUtil method parseZExpression.

private void parseZExpression(ZExpression expression, Set tablesNames) {
    Vector operands = expression.getOperands();
    Enumeration e = operands.elements();
    while (e.hasMoreElements()) {
        Object el = e.nextElement();
        if (el instanceof ZExpression)
            parseZExpression((ZExpression) el, tablesNames);
        else if (el instanceof ZQuery)
            getInvokedTables((ZQuery) el, tablesNames);
    }
}
Also used : Enumeration(java.util.Enumeration) ZExpression(lucee.runtime.sql.old.ZExpression) Vector(java.util.Vector) ZQuery(lucee.runtime.sql.old.ZQuery)

Aggregations

ZQuery (lucee.runtime.sql.old.ZQuery)4 Vector (java.util.Vector)3 ZExpression (lucee.runtime.sql.old.ZExpression)3 Enumeration (java.util.Enumeration)2 ZExp (lucee.runtime.sql.old.ZExp)2 HashSet (java.util.HashSet)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 ZConstant (lucee.runtime.sql.old.ZConstant)1 ZFromItem (lucee.runtime.sql.old.ZFromItem)1 ZOrderBy (lucee.runtime.sql.old.ZOrderBy)1 ZSelectItem (lucee.runtime.sql.old.ZSelectItem)1 ZStatement (lucee.runtime.sql.old.ZStatement)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