use of com.google.cloud.spanner.Options.QueryOption in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplate method getQueryLogMessageWithOptions.
private String getQueryLogMessageWithOptions(Statement statement, SpannerQueryOptions options) {
String message;
StringBuilder logSb = new StringBuilder("Executing query");
if (options.getTimestampBound() != null) {
logSb.append(" at timestamp ").append(options.getTimestampBound());
}
for (QueryOption queryOption : options.getOptions()) {
logSb.append(" with option: ").append(queryOption);
}
logSb.append(" : ").append(statement);
message = logSb.toString();
return message;
}
use of com.google.cloud.spanner.Options.QueryOption in project spring-cloud-gcp by spring-cloud.
the class SpannerSortPageQueryOptionsTests method addQueryOptionTest.
@Test
public void addQueryOptionTest() {
SpannerPageableQueryOptions spannerQueryOptions = new SpannerPageableQueryOptions();
QueryOption r1 = mock(QueryOption.class);
QueryOption r2 = mock(QueryOption.class);
spannerQueryOptions.addQueryOption(r1).addQueryOption(r2);
assertThat(Arrays.asList(spannerQueryOptions.getOptions())).containsExactlyInAnyOrder(r1, r2);
}
use of com.google.cloud.spanner.Options.QueryOption in project spring-cloud-gcp by spring-cloud.
the class SpannerQueryOptionsTests method addQueryOptionTest.
@Test
public void addQueryOptionTest() {
SpannerQueryOptions spannerQueryOptions = new SpannerQueryOptions();
QueryOption r1 = mock(QueryOption.class);
QueryOption r2 = mock(QueryOption.class);
spannerQueryOptions.addQueryOption(r1).addQueryOption(r2);
assertThat(Arrays.asList(spannerQueryOptions.getQueryOptions()), containsInAnyOrder(r1, r2));
}
use of com.google.cloud.spanner.Options.QueryOption in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method findByStatementTest.
@Test
public void findByStatementTest() {
ResultSet results = mock(ResultSet.class);
QueryOption queryOption = mock(QueryOption.class);
Statement statement = Statement.of("test");
SpannerQueryOptions options = new SpannerQueryOptions().addQueryOption(queryOption);
when(this.readContext.executeQuery(any(), any())).thenReturn(results);
this.spannerTemplate.find(TestEntity.class, statement, options);
verify(this.objectMapper, times(1)).mapToList(same(results), eq(TestEntity.class));
verify(this.readContext, times(1)).executeQuery(same(statement), same(queryOption));
}
use of com.google.cloud.spanner.Options.QueryOption in project spring-cloud-gcp by spring-cloud.
the class SpannerTransactionManager method doBegin.
@Override
protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException {
if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
throw new IllegalStateException("SpannerTransactionManager supports only isolation level TransactionDefinition.ISOLATION_DEFAULT");
}
if (transactionDefinition.getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) {
throw new IllegalStateException("SpannerTransactionManager supports only propagation behavior " + "TransactionDefinition.PROPAGATION_REQUIRED");
}
Tx tx = (Tx) transactionObject;
if (transactionDefinition.isReadOnly()) {
final ReadContext targetTransactionContext = this.databaseClientProvider.get().readOnlyTransaction();
tx.isReadOnly = true;
tx.transactionManager = null;
tx.transactionContext = new TransactionContext() {
@Override
public void buffer(Mutation mutation) {
throw new IllegalStateException("Spanner transaction cannot apply" + " mutation because it is in readonly mode");
}
@Override
public void buffer(Iterable<Mutation> iterable) {
throw new IllegalStateException("Spanner transaction cannot apply" + " mutations because it is in readonly mode");
}
@Override
public long executeUpdate(Statement statement) {
throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
}
@Override
public ApiFuture<Long> executeUpdateAsync(Statement statement) {
throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
}
@Override
public long[] batchUpdate(Iterable<Statement> iterable) {
throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
}
@Override
public ApiFuture<long[]> batchUpdateAsync(Iterable<Statement> iterable) {
throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
}
@Override
public ResultSet read(String s, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
return targetTransactionContext.read(s, keySet, iterable, readOptions);
}
@Override
public AsyncResultSet readAsync(String s, KeySet keySet, Iterable<String> iterable, ReadOption... readOptions) {
return targetTransactionContext.readAsync(s, keySet, iterable, readOptions);
}
@Override
public ResultSet readUsingIndex(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
return targetTransactionContext.readUsingIndex(s, s1, keySet, iterable, readOptions);
}
@Override
public AsyncResultSet readUsingIndexAsync(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
return targetTransactionContext.readUsingIndexAsync(s, s1, keySet, iterable, readOptions);
}
@Nullable
@Override
public Struct readRow(String s, Key key, Iterable<String> iterable) {
return targetTransactionContext.readRow(s, key, iterable);
}
@Override
public ApiFuture<Struct> readRowAsync(String s, Key key, Iterable<String> iterable) {
return targetTransactionContext.readRowAsync(s, key, iterable);
}
@Nullable
@Override
public Struct readRowUsingIndex(String s, String s1, Key key, Iterable<String> iterable) {
return targetTransactionContext.readRowUsingIndex(s, s1, key, iterable);
}
@Override
public ApiFuture<Struct> readRowUsingIndexAsync(String s, String s1, Key key, Iterable<String> iterable) {
return targetTransactionContext.readRowUsingIndexAsync(s, s1, key, iterable);
}
@Override
public ResultSet executeQuery(Statement statement, Options.QueryOption... queryOptions) {
return targetTransactionContext.executeQuery(statement, queryOptions);
}
@Override
public AsyncResultSet executeQueryAsync(Statement statement, QueryOption... queryOptions) {
return targetTransactionContext.executeQueryAsync(statement, queryOptions);
}
@Override
public ResultSet analyzeQuery(Statement statement, QueryAnalyzeMode queryAnalyzeMode) {
return targetTransactionContext.analyzeQuery(statement, queryAnalyzeMode);
}
@Override
public void close() {
targetTransactionContext.close();
}
};
} else {
tx.transactionManager = tx.databaseClient.transactionManager();
tx.transactionContext = tx.getTransactionManager().begin();
tx.isReadOnly = false;
}
TransactionSynchronizationManager.bindResource(tx.getDatabaseClient(), tx);
}
Aggregations