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);
}
}
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);
}
}
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());
}
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)));
}
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())));
}
Aggregations