Search in sources :

Example 1 with SpannerPageableQueryOptions

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);
}
Also used : Param(org.springframework.data.repository.query.Param) Arrays(java.util.Arrays) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) ParserContext(org.springframework.expression.ParserContext) LiteralExpression(org.springframework.expression.common.LiteralExpression) HashMap(java.util.HashMap) Parameters(org.springframework.data.repository.query.Parameters) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) QueryMethodEvaluationContextProvider(org.springframework.data.repository.query.QueryMethodEvaluationContextProvider) Matcher(java.util.regex.Matcher) CompositeStringExpression(org.springframework.expression.common.CompositeStringExpression) Map(java.util.Map) Parameter(org.springframework.data.repository.query.Parameter) StreamSupport(java.util.stream.StreamSupport) SpannerTemplate(org.springframework.cloud.gcp.data.spanner.core.SpannerTemplate) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) Builder(com.google.cloud.spanner.Struct.Builder) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) StructAccessor(org.springframework.cloud.gcp.data.spanner.core.convert.StructAccessor) Set(java.util.Set) EvaluationContext(org.springframework.expression.EvaluationContext) Statement(com.google.cloud.spanner.Statement) List(java.util.List) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Struct(com.google.cloud.spanner.Struct) Expression(org.springframework.expression.Expression) Optional(java.util.Optional) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) StructAccessor(org.springframework.cloud.gcp.data.spanner.core.convert.StructAccessor) Statement(com.google.cloud.spanner.Statement) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions)

Example 2 with SpannerPageableQueryOptions

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);
}
Also used : Trade(org.springframework.cloud.gcp.data.spanner.test.domain.Trade) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Test(org.junit.Test) AbstractSpannerIntegrationTest(org.springframework.cloud.gcp.data.spanner.test.AbstractSpannerIntegrationTest)

Example 3 with SpannerPageableQueryOptions

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());
}
Also used : Pageable(org.springframework.data.domain.Pageable) ArrayList(java.util.ArrayList) Sort(org.springframework.data.domain.Sort) Key(com.google.cloud.spanner.Key) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Test(org.junit.Test)

Example 4 with SpannerPageableQueryOptions

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());
}
Also used : Sort(org.springframework.data.domain.Sort) Key(com.google.cloud.spanner.Key) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Test(org.junit.Test)

Aggregations

SpannerPageableQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions)4 Test (org.junit.Test)3 Sort (org.springframework.data.domain.Sort)3 Key (com.google.cloud.spanner.Key)2 ArrayList (java.util.ArrayList)2 Pageable (org.springframework.data.domain.Pageable)2 Statement (com.google.cloud.spanner.Statement)1 Struct (com.google.cloud.spanner.Struct)1 Builder (com.google.cloud.spanner.Struct.Builder)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1