Search in sources :

Example 26 with ResultInterface

use of org.h2.result.ResultInterface in project h2database by h2database.

the class Query method query.

/**
 * Execute the query, writing the result to the target result.
 *
 * @param limit the maximum number of rows to return
 * @param target the target result (null will return the result)
 * @return the result set (if the target is not set).
 */
public final ResultInterface query(int limit, ResultTarget target) {
    if (isUnion()) {
        // right queries
        return queryWithoutCacheLazyCheck(limit, target);
    }
    fireBeforeSelectTriggers();
    if (noCache || !session.getDatabase().getOptimizeReuseResults() || session.isLazyQueryExecution()) {
        return queryWithoutCacheLazyCheck(limit, target);
    }
    Value[] params = getParameterValues();
    long now = session.getDatabase().getModificationDataId();
    if (isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) {
        if (lastResult != null && !lastResult.isClosed() && limit == lastLimit) {
            if (sameResultAsLast(session, params, lastParameters, lastEvaluated)) {
                lastResult = lastResult.createShallowCopy(session);
                if (lastResult != null) {
                    lastResult.reset();
                    return lastResult;
                }
            }
        }
    }
    lastParameters = params;
    closeLastResult();
    ResultInterface r = queryWithoutCacheLazyCheck(limit, target);
    lastResult = r;
    this.lastEvaluated = now;
    lastLimit = limit;
    return r;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value)

Example 27 with ResultInterface

use of org.h2.result.ResultInterface in project h2database by h2database.

the class ScriptCommand method queryMeta.

@Override
public ResultInterface queryMeta() {
    LocalResult r = createResult();
    r.done();
    return r;
}
Also used : LocalResult(org.h2.result.LocalResult)

Example 28 with ResultInterface

use of org.h2.result.ResultInterface in project h2database by h2database.

the class SelectUnion method queryMeta.

@Override
public ResultInterface queryMeta() {
    int columnCount = left.getColumnCount();
    LocalResult result = new LocalResult(session, expressionArray, columnCount);
    result.done();
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult)

Example 29 with ResultInterface

use of org.h2.result.ResultInterface in project h2database by h2database.

the class SelectUnion method queryWithoutCache.

@Override
protected ResultInterface queryWithoutCache(int maxRows, ResultTarget target) {
    if (maxRows != 0) {
        // maxRows is set (maxRows 0 means no limit)
        int l;
        if (limitExpr == null) {
            l = -1;
        } else {
            Value v = limitExpr.getValue(session);
            l = v == ValueNull.INSTANCE ? -1 : v.getInt();
        }
        if (l < 0) {
            // for limitExpr, 0 means no rows, and -1 means no limit
            l = maxRows;
        } else {
            l = Math.min(l, maxRows);
        }
        limitExpr = ValueExpression.get(ValueInt.get(l));
    }
    if (session.getDatabase().getSettings().optimizeInsertFromSelect) {
        if (unionType == UnionType.UNION_ALL && target != null) {
            if (sort == null && !distinct && maxRows == 0 && offsetExpr == null && limitExpr == null) {
                left.query(0, target);
                right.query(0, target);
                return null;
            }
        }
    }
    int columnCount = left.getColumnCount();
    if (session.isLazyQueryExecution() && unionType == UnionType.UNION_ALL && !distinct && sort == null && !randomAccessResult && !isForUpdate && offsetExpr == null && isReadOnly()) {
        int limit = -1;
        if (limitExpr != null) {
            Value v = limitExpr.getValue(session);
            if (v != ValueNull.INSTANCE) {
                limit = v.getInt();
            }
        }
        // limit 0 means no rows
        if (limit != 0) {
            LazyResultUnion lazyResult = new LazyResultUnion(expressionArray, columnCount);
            if (limit > 0) {
                lazyResult.setLimit(limit);
            }
            return lazyResult;
        }
    }
    LocalResult result = new LocalResult(session, expressionArray, columnCount);
    if (sort != null) {
        result.setSortOrder(sort);
    }
    if (distinct) {
        left.setDistinct(true);
        right.setDistinct(true);
        result.setDistinct();
    }
    if (randomAccessResult) {
        result.setRandomAccess();
    }
    switch(unionType) {
        case UNION:
        case EXCEPT:
            left.setDistinct(true);
            right.setDistinct(true);
            result.setDistinct();
            break;
        case UNION_ALL:
            break;
        case INTERSECT:
            left.setDistinct(true);
            right.setDistinct(true);
            break;
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    ResultInterface l = left.query(0);
    ResultInterface r = right.query(0);
    l.reset();
    r.reset();
    switch(unionType) {
        case UNION_ALL:
        case UNION:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.addRow(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case EXCEPT:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.removeDistinct(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case INTERSECT:
            {
                LocalResult temp = new LocalResult(session, expressionArray, columnCount);
                temp.setDistinct();
                temp.setRandomAccess();
                while (l.next()) {
                    temp.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    Value[] values = convert(r.currentRow(), columnCount);
                    if (temp.containsDistinct(values)) {
                        result.addRow(values);
                    }
                }
                temp.close();
                break;
            }
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    if (offsetExpr != null) {
        result.setOffset(offsetExpr.getValue(session).getInt());
    }
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        if (v != ValueNull.INSTANCE) {
            result.setLimit(v.getInt());
        }
    }
    l.close();
    r.close();
    result.done();
    if (target != null) {
        while (result.next()) {
            target.addRow(result.currentRow());
        }
        result.close();
        return null;
    }
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult) ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value)

Example 30 with ResultInterface

use of org.h2.result.ResultInterface in project h2database by h2database.

the class SessionRemote method readSerializationSettings.

/**
 * Read the serializer name from the persistent database settings.
 *
 * @return the serializer
 */
private String readSerializationSettings() {
    String javaObjectSerializerFQN = null;
    CommandInterface ci = prepareCommand("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS " + " WHERE NAME='JAVA_OBJECT_SERIALIZER'", Integer.MAX_VALUE);
    try {
        ResultInterface result = ci.executeQuery(0, false);
        if (result.next()) {
            Value[] row = result.currentRow();
            javaObjectSerializerFQN = row[0].getString();
        }
    } finally {
        ci.close();
    }
    return javaObjectSerializerFQN;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value) CommandInterface(org.h2.command.CommandInterface)

Aggregations

ResultInterface (org.h2.result.ResultInterface)34 Value (org.h2.value.Value)15 DbException (org.h2.message.DbException)11 LocalResult (org.h2.result.LocalResult)9 SQLException (java.sql.SQLException)8 CommandInterface (org.h2.command.CommandInterface)7 Expression (org.h2.expression.Expression)5 Column (org.h2.table.Column)5 SQLClientInfoException (java.sql.SQLClientInfoException)4 Prepared (org.h2.command.Prepared)4 Database (org.h2.engine.Database)4 ResultWithGeneratedKeys (org.h2.result.ResultWithGeneratedKeys)4 Row (org.h2.result.Row)4 IOException (java.io.IOException)3 ValueString (org.h2.value.ValueString)3 Savepoint (java.sql.Savepoint)2 GeneratedKeys (org.h2.engine.GeneratedKeys)2 ExpressionColumn (org.h2.expression.ExpressionColumn)2 SequenceValue (org.h2.expression.SequenceValue)2 ResultRemote (org.h2.result.ResultRemote)2