use of org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQuery method executeReadSql.
private List executeReadSql(Pageable pageable, Sort sort, QueryTagValue queryTagValue) {
SpannerPageableQueryOptions spannerQueryOptions = new SpannerPageableQueryOptions().setAllowPartialRead(true);
if (sort != null && sort.isSorted()) {
spannerQueryOptions.setSort(sort);
}
if (pageable != null && pageable.isPaged()) {
spannerQueryOptions.setOffset(pageable.getOffset()).setLimit(pageable.getPageSize());
}
final Class<?> returnedType = getReturnedType();
final SpannerPersistentEntity<?> entity = returnedType == null ? null : this.spannerMappingContext.getPersistentEntity(returnedType);
queryTagValue.sql = SpannerStatementQueryExecutor.applySortingPagingQueryOptions(this.entityType, spannerQueryOptions, queryTagValue.sql, this.spannerMappingContext, entity != null && entity.hasEagerlyLoadedProperties());
Statement statement = buildStatementFromQueryAndTags(queryTagValue);
return (getReturnedSimpleConvertableItemType() != null) ? this.spannerTemplate.query((struct) -> new StructAccessor(struct).getSingleValue(0), statement, spannerQueryOptions) : this.spannerTemplate.query(this.entityType, statement, spannerQueryOptions);
}
use of org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateIntegrationTests method insertAndDeleteSequence.
@Test
public void insertAndDeleteSequence() {
this.spannerOperations.delete(Trade.class, KeySet.all());
assertThat(this.spannerOperations.count(Trade.class)).isEqualTo(0L);
Trade trade = Trade.aTrade(null, 1);
this.spannerOperations.insert(trade);
assertThat(this.spannerOperations.count(Trade.class)).isEqualTo(1L);
List<Trade> trades = this.spannerOperations.queryAll(Trade.class, new SpannerPageableQueryOptions());
assertThat(trades).containsExactly(trade);
Trade retrievedTrade = this.spannerOperations.read(Trade.class, Key.of(trade.getId(), trade.getTraderId()));
assertThat(retrievedTrade).isEqualTo(trade);
trades = this.spannerOperations.readAll(Trade.class);
assertThat(trades).containsExactly(trade);
Trade trade2 = Trade.aTrade(null, 1);
this.spannerOperations.insert(trade2);
trades = this.spannerOperations.read(Trade.class, KeySet.newBuilder().addKey(Key.of(trade.getId(), trade.getTraderId())).addKey(Key.of(trade2.getId(), trade2.getTraderId())).build());
assertThat(trades).containsExactlyInAnyOrder(trade, trade2);
this.spannerOperations.deleteAll(Arrays.asList(trade, trade2));
assertThat(this.spannerOperations.count(Trade.class)).isEqualTo(0L);
}
use of org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions in project spring-cloud-gcp by spring-cloud.
the class SpannerRepositoryImplTests method findAllPageableTest.
@Test
public void findAllPageableTest() {
Pageable pageable = mock(Pageable.class);
Sort sort = mock(Sort.class);
when(pageable.getSort()).thenReturn(sort);
when(pageable.getOffset()).thenReturn(3L);
when(pageable.getPageSize()).thenReturn(5);
when(this.template.queryAll(eq(Object.class), any())).thenAnswer((invocation) -> {
SpannerPageableQueryOptions spannerQueryOptions = invocation.getArgument(1);
assertThat(spannerQueryOptions.getSort()).isSameAs(sort);
assertThat(spannerQueryOptions.getOffset()).isEqualTo(3);
assertThat(spannerQueryOptions.getLimit()).isEqualTo(5);
return new ArrayList<>();
});
new SimpleSpannerRepository<Object, Key>(this.template, Object.class).findAll(pageable);
verify(this.template, times(1)).queryAll(eq(Object.class), any());
}
use of org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions in project spring-cloud-gcp by spring-cloud.
the class SpannerRepositoryImplTests method findAllSortTest.
@Test
public void findAllSortTest() {
Sort sort = mock(Sort.class);
when(this.template.queryAll(eq(Object.class), any())).thenAnswer((invocation) -> {
SpannerPageableQueryOptions spannerQueryOptions = invocation.getArgument(1);
assertThat(spannerQueryOptions.getSort()).isSameAs(sort);
return null;
});
new SimpleSpannerRepository<Object, Key>(this.template, Object.class).findAll(sort);
verify(this.template, times(1)).queryAll(eq(Object.class), any());
}
Aggregations