Search in sources :

Example 1 with MergedResultSet

use of org.h2.util.MergedResultSet in project h2database by h2database.

the class JdbcPreparedStatement method executeBatch.

/**
 * Executes the batch.
 * If one of the batched statements fails, this database will continue.
 *
 * @return the array of update counts
 */
@Override
public int[] executeBatch() throws SQLException {
    try {
        debugCodeCall("executeBatch");
        if (batchParameters == null) {
            // TODO batch: check what other database do if no parameters are
            // set
            batchParameters = New.arrayList();
        }
        batchIdentities = new MergedResultSet();
        int size = batchParameters.size();
        int[] result = new int[size];
        boolean error = false;
        SQLException next = null;
        checkClosedForWrite();
        try {
            for (int i = 0; i < size; i++) {
                Value[] set = batchParameters.get(i);
                ArrayList<? extends ParameterInterface> parameters = command.getParameters();
                for (int j = 0; j < set.length; j++) {
                    Value value = set[j];
                    ParameterInterface param = parameters.get(j);
                    param.setValue(value, false);
                }
                try {
                    result[i] = executeUpdateInternal();
                    // Cannot use own implementation, it returns batch identities
                    ResultSet rs = super.getGeneratedKeys();
                    batchIdentities.add(rs);
                } catch (Exception re) {
                    SQLException e = logAndConvert(re);
                    if (next == null) {
                        next = e;
                    } else {
                        e.setNextException(next);
                        next = e;
                    }
                    result[i] = Statement.EXECUTE_FAILED;
                    error = true;
                }
            }
            batchParameters = null;
            if (error) {
                throw new JdbcBatchUpdateException(next, result);
            }
            return result;
        } finally {
            afterWriting();
        }
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) MergedResultSet(org.h2.util.MergedResultSet) SQLException(java.sql.SQLException) Value(org.h2.value.Value) ResultSet(java.sql.ResultSet) MergedResultSet(org.h2.util.MergedResultSet) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ParameterInterface (org.h2.expression.ParameterInterface)1 DbException (org.h2.message.DbException)1 MergedResultSet (org.h2.util.MergedResultSet)1 Value (org.h2.value.Value)1