Search in sources :

Example 6 with ExecuteBatchDmlRequest

use of com.google.spanner.v1.ExecuteBatchDmlRequest in project java-spanner by googleapis.

the class MockSpannerServiceImpl method executeBatchDml.

@Override
public void executeBatchDml(ExecuteBatchDmlRequest request, StreamObserver<ExecuteBatchDmlResponse> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        executeBatchDmlExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        // Get or start transaction
        ByteString transactionId = getTransactionId(session, request.getTransaction());
        if (isPartitionedDmlTransaction(transactionId)) {
            throw Status.FAILED_PRECONDITION.withDescription("This transaction is a partitioned DML transaction and cannot be used for batch DML updates.").asRuntimeException();
        }
        simulateAbort(session, transactionId);
        List<StatementResult> results = new ArrayList<>();
        com.google.rpc.Status status = com.google.rpc.Status.newBuilder().setCode(Code.OK_VALUE).build();
        resultLoop: for (com.google.spanner.v1.ExecuteBatchDmlRequest.Statement statement : request.getStatementsList()) {
            try {
                Statement spannerStatement = buildStatement(statement.getSql(), statement.getParamTypesMap(), statement.getParams());
                StatementResult res = getResult(spannerStatement);
                switch(res.getType()) {
                    case EXCEPTION:
                        status = com.google.rpc.Status.newBuilder().setCode(res.getException().getStatus().getCode().value()).setMessage(res.getException().getMessage()).build();
                        break resultLoop;
                    case RESULT_SET:
                        throw Status.INVALID_ARGUMENT.withDescription("Not a DML statement: " + statement.getSql()).asRuntimeException();
                    case UPDATE_COUNT:
                        results.add(res);
                        break;
                    default:
                        throw new IllegalStateException("Unknown result type: " + res.getType());
                }
            } catch (StatusRuntimeException e) {
                status = com.google.rpc.Status.newBuilder().setCode(e.getStatus().getCode().value()).setMessage(e.getMessage()).build();
                break;
            } catch (Exception e) {
                status = com.google.rpc.Status.newBuilder().setCode(Code.UNKNOWN_VALUE).setMessage(e.getMessage()).build();
                break;
            }
        }
        ExecuteBatchDmlResponse.Builder builder = ExecuteBatchDmlResponse.newBuilder();
        for (StatementResult res : results) {
            builder.addResultSets(ResultSet.newBuilder().setStats(ResultSetStats.newBuilder().setRowCountExact(res.getUpdateCount()).build()).setMetadata(ResultSetMetadata.newBuilder().setTransaction(ignoreNextInlineBeginRequest.getAndSet(false) ? Transaction.getDefaultInstance() : Transaction.newBuilder().setId(transactionId).build()).build()).build());
        }
        builder.setStatus(status);
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) StatusRuntimeException(io.grpc.StatusRuntimeException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecuteBatchDmlResponse(com.google.spanner.v1.ExecuteBatchDmlResponse) Session(com.google.spanner.v1.Session)

Example 7 with ExecuteBatchDmlRequest

use of com.google.spanner.v1.ExecuteBatchDmlRequest in project java-spanner by googleapis.

the class SpannerClientTest method executeBatchDmlExceptionTest.

@Test
public void executeBatchDmlExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSpanner.addException(exception);
    try {
        ExecuteBatchDmlRequest request = ExecuteBatchDmlRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).addAllStatements(new ArrayList<ExecuteBatchDmlRequest.Statement>()).setSeqno(109325920).setRequestOptions(RequestOptions.newBuilder().build()).build();
        client.executeBatchDml(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ArrayList(java.util.ArrayList) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Aggregations

ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 AbstractMessage (com.google.protobuf.AbstractMessage)2 ExecuteBatchDmlResponse (com.google.spanner.v1.ExecuteBatchDmlResponse)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 ApiFuture (com.google.api.core.ApiFuture)1 SettableApiFuture (com.google.api.core.SettableApiFuture)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)1 CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)1 ReadyCallback (com.google.cloud.spanner.AsyncResultSet.ReadyCallback)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 ForceCloseSpannerFunction (com.google.cloud.spanner.ForceCloseSpannerFunction)1 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)1 Mutation (com.google.cloud.spanner.Mutation)1 ResultSet (com.google.cloud.spanner.ResultSet)1 SpannerApiFutures (com.google.cloud.spanner.SpannerApiFutures)1 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)1 SpannerException (com.google.cloud.spanner.SpannerException)1