Search in sources :

Example 1 with ViewCursor

use of org.h2.index.ViewCursor in project h2database by h2database.

the class ViewIndex method find.

private Cursor find(Session session, SearchRow first, SearchRow last, SearchRow intersection) {
    if (recursive) {
        return findRecursive(first, last);
    }
    setupQueryParameters(session, first, last, intersection);
    ResultInterface result = query.query(0);
    return new ViewCursor(this, result, first, last);
}
Also used : ResultInterface(org.h2.result.ResultInterface)

Example 2 with ViewCursor

use of org.h2.index.ViewCursor in project h2database by h2database.

the class ViewIndex method findRecursive.

private Cursor findRecursive(SearchRow first, SearchRow last) {
    assert recursive;
    ResultInterface recursiveResult = view.getRecursiveResult();
    if (recursiveResult != null) {
        recursiveResult.reset();
        return new ViewCursor(this, recursiveResult, first, last);
    }
    if (query == null) {
        Parser parser = new Parser(createSession);
        parser.setRightsChecked(true);
        parser.setSuppliedParameterList(originalParameters);
        query = (Query) parser.prepare(querySQL);
        query.setNeverLazy(true);
    }
    if (!query.isUnion()) {
        throw DbException.get(ErrorCode.SYNTAX_ERROR_2, "recursive queries without UNION");
    }
    SelectUnion union = (SelectUnion) query;
    Query left = union.getLeft();
    left.setNeverLazy(true);
    // to ensure the last result is not closed
    left.disableCache();
    ResultInterface resultInterface = left.query(0);
    LocalResult localResult = union.getEmptyResult();
    // ensure it is not written to disk,
    // because it is not closed normally
    localResult.setMaxMemoryRows(Integer.MAX_VALUE);
    while (resultInterface.next()) {
        Value[] cr = resultInterface.currentRow();
        localResult.addRow(cr);
    }
    Query right = union.getRight();
    right.setNeverLazy(true);
    resultInterface.reset();
    view.setRecursiveResult(resultInterface);
    // to ensure the last result is not closed
    right.disableCache();
    while (true) {
        resultInterface = right.query(0);
        if (!resultInterface.hasNext()) {
            break;
        }
        while (resultInterface.next()) {
            Value[] cr = resultInterface.currentRow();
            localResult.addRow(cr);
        }
        resultInterface.reset();
        view.setRecursiveResult(resultInterface);
    }
    view.setRecursiveResult(null);
    localResult.done();
    return new ViewCursor(this, localResult, first, last);
}
Also used : LocalResult(org.h2.result.LocalResult) SelectUnion(org.h2.command.dml.SelectUnion) ResultInterface(org.h2.result.ResultInterface) Query(org.h2.command.dml.Query) Value(org.h2.value.Value) Parser(org.h2.command.Parser)

Aggregations

ResultInterface (org.h2.result.ResultInterface)2 Parser (org.h2.command.Parser)1 Query (org.h2.command.dml.Query)1 SelectUnion (org.h2.command.dml.SelectUnion)1 LocalResult (org.h2.result.LocalResult)1 Value (org.h2.value.Value)1