Search in sources :

Example 1 with ResultTarget

use of com.wplatform.ddal.result.ResultTarget in project jdbc-shards by wplatform.

the class SelectExecutor method executeQuery.

@Override
public LocalResult executeQuery(int maxRows, ResultTarget target) {
    int limitRows = maxRows == 0 ? -1 : maxRows;
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        int l = v == ValueNull.INSTANCE ? -1 : v.getInt();
        if (limitRows < 0) {
            limitRows = l;
        } else if (l >= 0) {
            limitRows = Math.min(l, limitRows);
        }
    }
    int columnCount = expressions.size();
    LocalResult result = null;
    if (target == null || !session.getDatabase().getSettings().optimizeInsertFromSelect) {
        result = createLocalResult(result);
    }
    if (sort != null) {
        result = createLocalResult(result);
        result.setSortOrder(sort);
    }
    if (distinct) {
        result = createLocalResult(result);
        result.setDistinct();
    }
    if (randomAccessResult) {
        result = createLocalResult(result);
    }
    if (isGroupQuery) {
        result = createLocalResult(result);
    }
    if (limitRows >= 0 || offsetExpr != null) {
        result = createLocalResult(result);
    }
    topTableFilter.startQuery(session);
    topTableFilter.reset();
    topTableFilter.lock(session, isForUpdate, isForUpdate);
    ResultTarget to = result != null ? result : target;
    if (limitRows != 0) {
        if (isAccordantQuery) {
            if (isGroupQuery) {
                queryGroupAccordant(columnCount, to);
            } else {
                queryFlatAccordant(columnCount, to, limitRows);
            }
        } else {
            if (isGroupQuery) {
                queryGroup(columnCount, result);
            } else {
                queryFlat(columnCount, to, limitRows);
            }
        }
    }
    if (offsetExpr != null) {
        result.setOffset(offsetExpr.getValue(session).getInt());
    }
    if (result != null) {
        result.done();
        if (target != null) {
            while (result.next()) {
                target.addRow(result.currentRow());
            }
            result.close();
            return null;
        }
        return result;
    }
    return null;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) ResultTarget(com.wplatform.ddal.result.ResultTarget) Value(com.wplatform.ddal.value.Value)

Aggregations

LocalResult (com.wplatform.ddal.result.LocalResult)1 ResultTarget (com.wplatform.ddal.result.ResultTarget)1 Value (com.wplatform.ddal.value.Value)1