Search in sources :

Example 21 with ResultInterface

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

the class JdbcConnection method isReadOnly.

/**
 * Returns true if the database is read-only.
 *
 * @return if the database is read-only
 * @throws SQLException if the connection is closed
 */
@Override
public boolean isReadOnly() throws SQLException {
    try {
        debugCodeCall("isReadOnly");
        checkClosed();
        getReadOnly = prepareCommand("CALL READONLY()", getReadOnly);
        ResultInterface result = getReadOnly.executeQuery(0, false);
        result.next();
        return result.currentRow()[0].getBoolean();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 22 with ResultInterface

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

the class JdbcConnection method getQueryTimeout.

/**
 * INTERNAL
 */
int getQueryTimeout() throws SQLException {
    try {
        if (queryTimeoutCache == -1) {
            checkClosed();
            getQueryTimeout = prepareCommand("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS " + "WHERE NAME=?", getQueryTimeout);
            getQueryTimeout.getParameters().get(0).setValue(ValueString.get("QUERY_TIMEOUT"), false);
            ResultInterface result = getQueryTimeout.executeQuery(0, false);
            result.next();
            int queryTimeout = result.currentRow()[0].getInt();
            result.close();
            if (queryTimeout != 0) {
                // round to the next second, otherwise 999 millis would
                // return 0 seconds
                queryTimeout = (queryTimeout + 999) / 1000;
            }
            queryTimeoutCache = queryTimeout;
        }
        return queryTimeoutCache;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 23 with ResultInterface

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

the class Insert method insertRows.

private int insertRows() {
    session.getUser().checkRight(table, Right.INSERT);
    setCurrentRowNumber(0);
    table.fire(session, Trigger.INSERT, true);
    rowNumber = 0;
    GeneratedKeys generatedKeys = session.getGeneratedKeys();
    generatedKeys.initialize(table);
    int listSize = list.size();
    if (listSize > 0) {
        int columnLen = columns.length;
        for (int x = 0; x < listSize; x++) {
            session.startStatementWithinTransaction();
            generatedKeys.nextRow();
            Row newRow = table.getTemplateRow();
            Expression[] expr = list.get(x);
            setCurrentRowNumber(x + 1);
            for (int i = 0; i < columnLen; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    e = e.optimize(session);
                    try {
                        Value v = c.convert(e.getValue(session), session.getDatabase().getMode());
                        newRow.setValue(index, v);
                        if (e instanceof SequenceValue) {
                            generatedKeys.add(c);
                        }
                    } catch (DbException ex) {
                        throw setRow(ex, x, getSQL(expr));
                    }
                }
            }
            rowNumber++;
            table.validateConvertUpdateSequence(session, newRow);
            boolean done = table.fireBeforeRow(session, null, newRow);
            if (!done) {
                table.lock(session, true, false);
                try {
                    table.addRow(session, newRow);
                } catch (DbException de) {
                    if (handleOnDuplicate(de)) {
                        // MySQL returns 2 for updated row
                        // TODO: detect no-op change
                        rowNumber++;
                    } else {
                        // INSERT IGNORE case
                        rowNumber--;
                    }
                    continue;
                }
                generatedKeys.confirmRow(newRow);
                session.log(table, UndoLogRecord.INSERT, newRow);
                table.fireAfterRow(session, null, newRow, false);
            }
        }
    } else {
        table.lock(session, true, false);
        if (insertFromSelect) {
            query.query(0, this);
        } else {
            ResultInterface rows = query.query(0);
            while (rows.next()) {
                generatedKeys.nextRow();
                Value[] r = rows.currentRow();
                Row newRow = addRowImpl(r);
                if (newRow != null) {
                    generatedKeys.confirmRow(newRow);
                }
            }
            rows.close();
        }
    }
    table.fire(session, Trigger.INSERT, false);
    return rowNumber;
}
Also used : SequenceValue(org.h2.expression.SequenceValue) ResultInterface(org.h2.result.ResultInterface) Expression(org.h2.expression.Expression) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) Value(org.h2.value.Value) SequenceValue(org.h2.expression.SequenceValue) GeneratedKeys(org.h2.engine.GeneratedKeys) Row(org.h2.result.Row) DbException(org.h2.message.DbException)

Example 24 with ResultInterface

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

the class MergeUsing method update.

@Override
public int update() {
    // clear list of source table keys & rowids we have processed already
    targetRowidsRemembered.clear();
    if (targetTableFilter != null) {
        targetTableFilter.startQuery(session);
        targetTableFilter.reset();
    }
    if (sourceTableFilter != null) {
        sourceTableFilter.startQuery(session);
        sourceTableFilter.reset();
    }
    sourceQueryRowNumber = 0;
    checkRights();
    setCurrentRowNumber(0);
    // process source select query data for row creation
    ResultInterface rows = query.query(0);
    targetTable.fire(session, evaluateTriggerMasks(), true);
    targetTable.lock(session, true, false);
    while (rows.next()) {
        sourceQueryRowNumber++;
        Value[] sourceRowValues = rows.currentRow();
        Row sourceRow = new RowImpl(sourceRowValues, 0);
        setCurrentRowNumber(sourceQueryRowNumber);
        merge(sourceRow);
    }
    rows.close();
    targetTable.fire(session, evaluateTriggerMasks(), false);
    return countUpdatedRows;
}
Also used : RowImpl(org.h2.result.RowImpl) ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value) Row(org.h2.result.Row)

Example 25 with ResultInterface

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

the class MergeUsing method isTargetRowFound.

private boolean isTargetRowFound() {
    ResultInterface rows = targetMatchQuery.query(0);
    int countTargetRowsFound = 0;
    Value[] targetRowIdValue = null;
    while (rows.next()) {
        countTargetRowsFound++;
        targetRowIdValue = rows.currentRow();
        // throw and exception if we have processed this _ROWID_ before...
        if (targetRowidsRemembered.containsKey(targetRowIdValue[0])) {
            throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Merge using ON column expression, " + "duplicate _ROWID_ target record already updated, deleted or inserted:_ROWID_=" + targetRowIdValue[0].toString() + ":in:" + targetTableFilter.getTable() + ":conflicting source row number:" + targetRowidsRemembered.get(targetRowIdValue[0]));
        } else {
            // remember the source column values we have used before (they
            // are the effective ON clause keys
            // and should not be repeated
            targetRowidsRemembered.put(targetRowIdValue[0], sourceQueryRowNumber);
        }
    }
    rows.close();
    if (countTargetRowsFound > 1) {
        throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Duplicate key updated " + countTargetRowsFound + " rows at once, only 1 expected:_ROWID_=" + targetRowIdValue[0].toString() + ":in:" + targetTableFilter.getTable() + ":conflicting source row number:" + targetRowidsRemembered.get(targetRowIdValue[0]));
    }
    return countTargetRowsFound > 0;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value)

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