Search in sources :

Example 46 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class SingleUseTransaction method executeBatchUpdateAsync.

@Override
public ApiFuture<long[]> executeBatchUpdateAsync(Iterable<ParsedStatement> updates, UpdateOption... options) {
    Preconditions.checkNotNull(updates);
    for (ParsedStatement update : updates) {
        Preconditions.checkArgument(update.isUpdate(), "Statement is not an update statement: " + update.getSqlWithoutComments());
    }
    ConnectionPreconditions.checkState(!isReadOnly(), "Batch update statements are not allowed in read-only mode");
    checkAndMarkUsed();
    switch(autocommitDmlMode) {
        case TRANSACTIONAL:
            return executeTransactionalBatchUpdateAsync(updates, options);
        case PARTITIONED_NON_ATOMIC:
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "Batch updates are not allowed in " + autocommitDmlMode);
        default:
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "Unknown dml mode: " + autocommitDmlMode);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)

Example 47 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionStatementWithOneParameterTest method testExecuteSetReadOnlyStaleness.

@Test
public void testExecuteSetReadOnlyStaleness() {
    ParsedStatement subject = parser.parse(Statement.of("set read_only_staleness='foo'"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetReadOnlyStaleness(any(TimestampBound.class))).thenCallRealMethod();
    for (TimestampBound val : new TimestampBound[] { TimestampBound.strong(), TimestampBound.ofReadTimestamp(Timestamp.now()), TimestampBound.ofMinReadTimestamp(Timestamp.now()), TimestampBound.ofExactStaleness(1000L, TimeUnit.SECONDS), TimestampBound.ofMaxStaleness(2000L, TimeUnit.MICROSECONDS) }) {
        subject.getClientSideStatement().execute(executor, String.format("set read_only_staleness='%s'", timestampBoundToString(val)));
        verify(connection, times(1)).setReadOnlyStaleness(val);
    }
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 48 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class DdlBatchTest method testAbort.

@Test
public void testAbort() {
    DdlClient client = createDefaultMockDdlClient();
    DdlBatch batch = createSubject(client);
    batch.abortBatch();
    assertThat(batch.getState(), is(UnitOfWorkState.ABORTED));
    verify(client, never()).executeDdl(anyString());
    verify(client, never()).executeDdl(anyList());
    ParsedStatement statement = mock(ParsedStatement.class);
    when(statement.getType()).thenReturn(StatementType.DDL);
    when(statement.getStatement()).thenReturn(Statement.of("CREATE TABLE FOO"));
    when(statement.getSqlWithoutComments()).thenReturn("CREATE TABLE FOO");
    client = createDefaultMockDdlClient();
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    batch.abortBatch();
    verify(client, never()).executeDdl(anyList());
    client = createDefaultMockDdlClient();
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    batch.executeDdlAsync(statement);
    batch.abortBatch();
    verify(client, never()).executeDdl(anyList());
    client = createDefaultMockDdlClient();
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    batch.executeDdlAsync(statement);
    batch.abortBatch();
    verify(client, never()).executeDdl(anyList());
    boolean exception = false;
    try {
        get(batch.runBatchAsync());
    } catch (SpannerException e) {
        if (e.getErrorCode() != ErrorCode.FAILED_PRECONDITION) {
            throw e;
        }
        exception = true;
    }
    assertThat(exception, is(true));
    verify(client, never()).executeDdl(anyList());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 49 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class DdlBatchTest method testRunBatch.

@Test
public void testRunBatch() {
    DdlClient client = createDefaultMockDdlClient();
    DdlBatch batch = createSubject(client);
    get(batch.runBatchAsync());
    assertThat(batch.getState(), is(UnitOfWorkState.RAN));
    verify(client, never()).executeDdl(anyString());
    verify(client, never()).executeDdl(argThat(isEmptyListOfStrings()));
    ParsedStatement statement = mock(ParsedStatement.class);
    when(statement.getType()).thenReturn(StatementType.DDL);
    when(statement.getStatement()).thenReturn(Statement.of("CREATE TABLE FOO"));
    when(statement.getSqlWithoutComments()).thenReturn("CREATE TABLE FOO");
    client = createDefaultMockDdlClient();
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    get(batch.runBatchAsync());
    verify(client).executeDdl(argThat(isListOfStringsWithSize(1)));
    client = createDefaultMockDdlClient();
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    batch.executeDdlAsync(statement);
    get(batch.runBatchAsync());
    verify(client).executeDdl(argThat(isListOfStringsWithSize(2)));
    assertThat(batch.getState(), is(UnitOfWorkState.RAN));
    boolean exception = false;
    try {
        get(batch.runBatchAsync());
    } catch (SpannerException e) {
        if (e.getErrorCode() != ErrorCode.FAILED_PRECONDITION) {
            throw e;
        }
        exception = true;
    }
    assertThat(exception, is(true));
    assertThat(batch.getState(), is(UnitOfWorkState.RAN));
    exception = false;
    try {
        batch.executeDdlAsync(statement);
    } catch (SpannerException e) {
        if (e.getErrorCode() != ErrorCode.FAILED_PRECONDITION) {
            throw e;
        }
        exception = true;
    }
    assertThat(exception, is(true));
    exception = false;
    try {
        batch.executeDdlAsync(statement);
    } catch (SpannerException e) {
        if (e.getErrorCode() != ErrorCode.FAILED_PRECONDITION) {
            throw e;
        }
        exception = true;
    }
    assertThat(exception, is(true));
    client = createDefaultMockDdlClient(true);
    batch = createSubject(client);
    batch.executeDdlAsync(statement);
    batch.executeDdlAsync(statement);
    exception = false;
    try {
        get(batch.runBatchAsync());
    } catch (SpannerException e) {
        exception = true;
    }
    assertThat(exception, is(true));
    assertThat(batch.getState(), is(UnitOfWorkState.RUN_FAILED));
    verify(client).executeDdl(argThat(isListOfStringsWithSize(2)));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 50 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class DdlBatchTest method testExecuteMetadataQuery.

@Test
public void testExecuteMetadataQuery() {
    Statement statement = Statement.of("SELECT * FROM INFORMATION_SCHEMA.TABLES");
    ParsedStatement parsedStatement = mock(ParsedStatement.class);
    when(parsedStatement.isQuery()).thenReturn(true);
    when(parsedStatement.getStatement()).thenReturn(statement);
    DatabaseClient dbClient = mock(DatabaseClient.class);
    ReadContext singleUse = mock(ReadContext.class);
    ResultSet resultSet = mock(ResultSet.class);
    when(singleUse.executeQuery(statement)).thenReturn(resultSet);
    when(dbClient.singleUse()).thenReturn(singleUse);
    DdlBatch batch = createSubject(createDefaultMockDdlClient(), dbClient);
    assertThat(get(batch.executeQueryAsync(parsedStatement, AnalyzeMode.NONE, InternalMetadataQuery.INSTANCE)).hashCode(), is(equalTo(resultSet.hashCode())));
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Aggregations

ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)75 Test (org.junit.Test)61 Statement (com.google.cloud.spanner.Statement)23 SpannerException (com.google.cloud.spanner.SpannerException)20 ResultSet (com.google.cloud.spanner.ResultSet)12 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 TimestampBound (com.google.cloud.spanner.TimestampBound)6 LinkedList (java.util.LinkedList)3 Mockito.anyString (org.mockito.Mockito.anyString)3 AbortedException (com.google.cloud.spanner.AbortedException)2 QueryOption (com.google.cloud.spanner.Options.QueryOption)2 Mutation (com.google.cloud.spanner.Mutation)1 ReadContext (com.google.cloud.spanner.ReadContext)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionManager (com.google.cloud.spanner.TransactionManager)1 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 BigDecimal (java.math.BigDecimal)1 TimeUnit (java.util.concurrent.TimeUnit)1