Search in sources :

Example 16 with ResultInterface

use of org.h2.result.ResultInterface 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)

Example 17 with ResultInterface

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

the class JdbcStatement method executeUpdateInternal.

private int executeUpdateInternal(String sql, Object generatedKeysRequest) throws SQLException {
    checkClosedForWrite();
    try {
        closeOldResultSet();
        sql = JdbcConnection.translateSQL(sql, escapeProcessing);
        CommandInterface command = conn.prepareCommand(sql, fetchSize);
        synchronized (session) {
            setExecutingStatement(command);
            try {
                ResultWithGeneratedKeys result = command.executeUpdate(conn.scopeGeneratedKeys() ? false : generatedKeysRequest);
                updateCount = result.getUpdateCount();
                ResultInterface gk = result.getGeneratedKeys();
                if (gk != null) {
                    int id = getNextId(TraceObject.RESULT_SET);
                    generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
                }
            } finally {
                setExecutingStatement(null);
            }
        }
        command.close();
        return updateCount;
    } finally {
        afterWriting();
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

Example 18 with ResultInterface

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

the class JdbcStatement method executeQuery.

/**
 * Executes a query (select statement) and returns the result set.
 * If another result set exists for this statement, this will be closed
 * (even if this statement fails).
 *
 * @param sql the SQL statement to execute
 * @return the result set
 */
@Override
public ResultSet executeQuery(String sql) throws SQLException {
    try {
        int id = getNextId(TraceObject.RESULT_SET);
        if (isDebugEnabled()) {
            debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery(" + quote(sql) + ")");
        }
        synchronized (session) {
            checkClosed();
            closeOldResultSet();
            sql = JdbcConnection.translateSQL(sql, escapeProcessing);
            CommandInterface command = conn.prepareCommand(sql, fetchSize);
            ResultInterface result;
            boolean lazy = false;
            boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
            boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
            setExecutingStatement(command);
            try {
                result = command.executeQuery(maxRows, scrollable);
                lazy = result.isLazy();
            } finally {
                if (!lazy) {
                    setExecutingStatement(null);
                }
            }
            if (!lazy) {
                command.close();
            }
            resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable);
        }
        return resultSet;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 19 with ResultInterface

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

the class JdbcConnection method getGeneratedKeys.

/**
 * INTERNAL
 */
ResultSet getGeneratedKeys(JdbcStatement stat, int id) {
    getGeneratedKeys = prepareCommand("SELECT SCOPE_IDENTITY() " + "WHERE SCOPE_IDENTITY() IS NOT NULL", getGeneratedKeys);
    ResultInterface result = getGeneratedKeys.executeQuery(0, false);
    return new JdbcResultSet(this, stat, getGeneratedKeys, result, id, false, true, false);
}
Also used : ResultInterface(org.h2.result.ResultInterface)

Example 20 with ResultInterface

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

the class JdbcConnection method getCatalog.

/**
 * Gets the current catalog name.
 *
 * @return the catalog name
 * @throws SQLException if the connection is closed
 */
@Override
public String getCatalog() throws SQLException {
    try {
        debugCodeCall("getCatalog");
        checkClosed();
        if (catalog == null) {
            CommandInterface cat = prepareCommand("CALL DATABASE()", Integer.MAX_VALUE);
            ResultInterface result = cat.executeQuery(0, false);
            result.next();
            catalog = result.currentRow()[0].getString();
            cat.close();
        }
        return catalog;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

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