Search in sources :

Example 41 with ResultInterface

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

the class ConditionInSelect method getValue.

@Override
public Value getValue(Session session) {
    query.setSession(session);
    if (!query.hasOrder()) {
        query.setDistinct(true);
    }
    ResultInterface rows = query.query(0);
    Value l = left.getValue(session);
    if (!rows.hasNext()) {
        return ValueBoolean.get(all);
    } else if (l == ValueNull.INSTANCE) {
        return l;
    }
    if (!session.getDatabase().getSettings().optimizeInSelect) {
        return getValueSlow(rows, l);
    }
    if (all || (compareType != Comparison.EQUAL && compareType != Comparison.EQUAL_NULL_SAFE)) {
        return getValueSlow(rows, l);
    }
    int dataType = rows.getColumnType(0);
    if (dataType == Value.NULL) {
        return ValueBoolean.FALSE;
    }
    l = l.convertTo(dataType);
    if (rows.containsDistinct(new Value[] { l })) {
        return ValueBoolean.TRUE;
    }
    if (rows.containsDistinct(new Value[] { ValueNull.INSTANCE })) {
        return ValueNull.INSTANCE;
    }
    return ValueBoolean.FALSE;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value)

Example 42 with ResultInterface

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

the class ConditionInSelect method getValueSlow.

private Value getValueSlow(ResultInterface rows, Value l) {
    // this only returns the correct result if the result has at least one
    // row, and if l is not null
    boolean hasNull = false;
    boolean result = all;
    while (rows.next()) {
        boolean value;
        Value r = rows.currentRow()[0];
        if (r == ValueNull.INSTANCE) {
            value = false;
            hasNull = true;
        } else {
            value = Comparison.compareNotNull(database, l, r, compareType);
        }
        if (!value && all) {
            result = false;
            break;
        } else if (value && !all) {
            result = true;
            break;
        }
    }
    if (!result && hasNull) {
        return ValueNull.INSTANCE;
    }
    return ValueBoolean.get(result);
}
Also used : Value(org.h2.value.Value)

Example 43 with ResultInterface

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

the class JdbcPreparedStatement method execute.

/**
 * Executes an arbitrary statement. If another result set exists for this
 * statement, this will be closed (even if this statement fails). If auto
 * commit is on, and the statement is not a select, this statement will be
 * committed.
 *
 * @return true if a result set is available, false if not
 * @throws SQLException if this object is closed or invalid
 */
@Override
public boolean execute() throws SQLException {
    try {
        int id = getNextId(TraceObject.RESULT_SET);
        if (isDebugEnabled()) {
            debugCodeCall("execute");
        }
        checkClosedForWrite();
        try {
            boolean returnsResultSet;
            synchronized (conn.getSession()) {
                closeOldResultSet();
                boolean lazy = false;
                try {
                    setExecutingStatement(command);
                    if (command.isQuery()) {
                        returnsResultSet = true;
                        boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
                        boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
                        ResultInterface result = command.executeQuery(maxRows, scrollable);
                        lazy = result.isLazy();
                        resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
                    } else {
                        returnsResultSet = false;
                        ResultWithGeneratedKeys result = command.executeUpdate(generatedKeysRequest);
                        updateCount = result.getUpdateCount();
                        ResultInterface gk = result.getGeneratedKeys();
                        if (gk != null) {
                            generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
                        }
                    }
                } finally {
                    if (!lazy) {
                        setExecutingStatement(null);
                    }
                }
            }
            return returnsResultSet;
        } finally {
            afterWriting();
        }
    } catch (Throwable e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

Example 44 with ResultInterface

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

the class JdbcPreparedStatement 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).
 *
 * @return the result set
 * @throws SQLException if this object is closed or invalid
 */
@Override
public ResultSet executeQuery() throws SQLException {
    try {
        int id = getNextId(TraceObject.RESULT_SET);
        if (isDebugEnabled()) {
            debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery()");
        }
        batchIdentities = null;
        synchronized (session) {
            checkClosed();
            closeOldResultSet();
            ResultInterface result;
            boolean lazy = false;
            boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
            boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
            try {
                setExecutingStatement(command);
                result = command.executeQuery(maxRows, scrollable);
                lazy = result.isLazy();
            } finally {
                if (!lazy) {
                    setExecutingStatement(null);
                }
            }
            resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
        }
        return resultSet;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 45 with ResultInterface

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

the class JdbcStatement method executeInternal.

private boolean executeInternal(String sql, Object generatedKeysRequest) throws SQLException {
    int id = getNextId(TraceObject.RESULT_SET);
    checkClosedForWrite();
    try {
        closeOldResultSet();
        sql = JdbcConnection.translateSQL(sql, escapeProcessing);
        CommandInterface command = conn.prepareCommand(sql, fetchSize);
        boolean lazy = false;
        boolean returnsResultSet;
        synchronized (session) {
            setExecutingStatement(command);
            try {
                if (command.isQuery()) {
                    returnsResultSet = true;
                    boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
                    boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
                    ResultInterface result = command.executeQuery(maxRows, scrollable);
                    lazy = result.isLazy();
                    resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable);
                } else {
                    returnsResultSet = false;
                    ResultWithGeneratedKeys result = command.executeUpdate(conn.scopeGeneratedKeys() ? false : generatedKeysRequest);
                    updateCount = result.getUpdateCount();
                    ResultInterface gk = result.getGeneratedKeys();
                    if (gk != null) {
                        generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
                    }
                }
            } finally {
                if (!lazy) {
                    setExecutingStatement(null);
                }
            }
        }
        if (!lazy) {
            command.close();
        }
        return returnsResultSet;
    } finally {
        afterWriting();
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

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