Search in sources :

Example 1 with BatchExecuteResult

use of org.apache.ignite.client.proto.query.event.BatchExecuteResult in project ignite-3 by apache.

the class JdbcQueryEventHandlerImpl method batchPrepStatementAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<BatchExecuteResult> batchPrepStatementAsync(BatchPreparedStmntRequest req) {
    IntList res = new IntArrayList(req.getArgs().size());
    QueryContext context = createQueryContext(JdbcStatementType.UPDATE_STATEMENT_TYPE);
    try {
        for (Object[] arg : req.getArgs()) {
            executeAndCollectUpdateCount(context, req.schemaName(), req.getQuery(), arg, res);
        }
    } catch (Exception e) {
        return handleBatchException(e, req.getQuery(), res);
    }
    return CompletableFuture.completedFuture(new BatchExecuteResult(res.toIntArray()));
}
Also used : QueryContext(org.apache.ignite.internal.sql.engine.QueryContext) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) BatchExecuteResult(org.apache.ignite.client.proto.query.event.BatchExecuteResult) QueryValidationException(org.apache.ignite.internal.sql.engine.exec.QueryValidationException) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 2 with BatchExecuteResult

use of org.apache.ignite.client.proto.query.event.BatchExecuteResult in project ignite-3 by apache.

the class JdbcPreparedStatement method executeBatch.

/**
 * {@inheritDoc}
 */
@Override
public int[] executeBatch() throws SQLException {
    ensureNotClosed();
    closeResults();
    if (CollectionUtils.nullOrEmpty(batchedArgs) || StringUtil.isNullOrEmpty(sql)) {
        return INT_EMPTY_ARRAY;
    }
    BatchPreparedStmntRequest req = new BatchPreparedStmntRequest(conn.getSchema(), sql, batchedArgs);
    try {
        BatchExecuteResult res = conn.handler().batchPrepStatementAsync(req).join();
        if (!res.hasResults()) {
            throw new BatchUpdateException(res.err(), IgniteQueryErrorCode.codeToSqlState(res.getErrorCode()), res.getErrorCode(), res.updateCounts());
        }
        return res.updateCounts();
    } finally {
        batchedArgs = null;
    }
}
Also used : BatchPreparedStmntRequest(org.apache.ignite.client.proto.query.event.BatchPreparedStmntRequest) BatchExecuteResult(org.apache.ignite.client.proto.query.event.BatchExecuteResult) BatchUpdateException(java.sql.BatchUpdateException)

Example 3 with BatchExecuteResult

use of org.apache.ignite.client.proto.query.event.BatchExecuteResult in project ignite-3 by apache.

the class JdbcStatement method executeBatch.

/**
 * {@inheritDoc}
 */
@Override
public int[] executeBatch() throws SQLException {
    ensureNotClosed();
    closeResults();
    if (CollectionUtils.nullOrEmpty(batch)) {
        return INT_EMPTY_ARRAY;
    }
    BatchExecuteRequest req = new BatchExecuteRequest(conn.getSchema(), batch);
    try {
        BatchExecuteResult res = conn.handler().batchAsync(req).join();
        if (!res.hasResults()) {
            throw new BatchUpdateException(res.err(), IgniteQueryErrorCode.codeToSqlState(res.getErrorCode()), res.getErrorCode(), res.updateCounts());
        }
        return res.updateCounts();
    } finally {
        batch = null;
    }
}
Also used : BatchExecuteRequest(org.apache.ignite.client.proto.query.event.BatchExecuteRequest) BatchExecuteResult(org.apache.ignite.client.proto.query.event.BatchExecuteResult) BatchUpdateException(java.sql.BatchUpdateException)

Example 4 with BatchExecuteResult

use of org.apache.ignite.client.proto.query.event.BatchExecuteResult in project ignite-3 by apache.

the class JdbcQueryEventHandlerImpl method batchAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<BatchExecuteResult> batchAsync(BatchExecuteRequest req) {
    List<String> queries = req.queries();
    IntList res = new IntArrayList(queries.size());
    QueryContext context = createQueryContext(JdbcStatementType.UPDATE_STATEMENT_TYPE);
    for (String query : queries) {
        try {
            executeAndCollectUpdateCount(context, req.schemaName(), query, OBJECT_EMPTY_ARRAY, res);
        } catch (Exception e) {
            return handleBatchException(e, query, res);
        }
    }
    return CompletableFuture.completedFuture(new BatchExecuteResult(res.toIntArray()));
}
Also used : QueryContext(org.apache.ignite.internal.sql.engine.QueryContext) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) BatchExecuteResult(org.apache.ignite.client.proto.query.event.BatchExecuteResult) QueryValidationException(org.apache.ignite.internal.sql.engine.exec.QueryValidationException) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 5 with BatchExecuteResult

use of org.apache.ignite.client.proto.query.event.BatchExecuteResult in project ignite-3 by apache.

the class JdbcQueryEventHandlerImpl method handleBatchException.

private CompletableFuture<BatchExecuteResult> handleBatchException(Exception e, String query, IntList res) {
    StringWriter sw = getWriterWithStackTrace(e);
    String error;
    if (e instanceof ClassCastException) {
        error = "Unexpected result after query:" + query + ". Not an upsert statement? " + sw;
    } else {
        error = sw.toString();
    }
    return CompletableFuture.completedFuture(new BatchExecuteResult(Response.STATUS_FAILED, UNKNOWN, error, res.toIntArray()));
}
Also used : StringWriter(java.io.StringWriter) BatchExecuteResult(org.apache.ignite.client.proto.query.event.BatchExecuteResult)

Aggregations

BatchExecuteResult (org.apache.ignite.client.proto.query.event.BatchExecuteResult)5 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)2 IntList (it.unimi.dsi.fastutil.ints.IntList)2 BatchUpdateException (java.sql.BatchUpdateException)2 QueryContext (org.apache.ignite.internal.sql.engine.QueryContext)2 QueryValidationException (org.apache.ignite.internal.sql.engine.exec.QueryValidationException)2 StringWriter (java.io.StringWriter)1 BatchExecuteRequest (org.apache.ignite.client.proto.query.event.BatchExecuteRequest)1 BatchPreparedStmntRequest (org.apache.ignite.client.proto.query.event.BatchPreparedStmntRequest)1