Search in sources :

Example 1 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class AbstractSpannerIntegrationTest method createDummyEntity.

protected <T> SpannerPersistentEntity createDummyEntity(Class<T> type) {
    TypeInformation<T> typeInformation = mock(TypeInformation.class);
    when(typeInformation.getType()).thenReturn(type);
    SpannerPersistentEntity dummyTrade = new SpannerPersistentEntityImpl<>(typeInformation);
    dummyTrade.setApplicationContext(this.applicationContext);
    return dummyTrade;
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerPersistentEntityImpl(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntityImpl)

Example 2 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method read.

@Override
public <T> List<T> read(Class<T> entityClass, KeySet keys, SpannerReadOptions options) {
    SpannerPersistentEntity<T> persistentEntity = (SpannerPersistentEntity<T>) this.mappingContext.getPersistentEntity(entityClass);
    List<T> entities;
    if (persistentEntity.hasEagerlyLoadedProperties() || persistentEntity.hasWhere()) {
        entities = executeReadQueryAndResolveChildren(keys, persistentEntity, toQueryOption(keys, options), options != null ? options.getIndex() : null);
    } else {
        entities = mapToListAndResolveChildren(executeRead(persistentEntity.tableName(), keys, persistentEntity.columns(), options), entityClass, (options != null) ? options.getIncludeProperties() : null, options != null && options.isAllowPartialRead());
    }
    maybeEmitEvent(new AfterReadEvent(entities, keys, options));
    return entities;
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent)

Example 3 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity 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 4 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class SpannerQueryLookupStrategyTests method getColumnsStringForSelectMultipleTest.

@Test
@SuppressWarnings("unchecked")
public void getColumnsStringForSelectMultipleTest() {
    final SpannerPersistentEntity<TestEntity> entity = (SpannerPersistentEntity<TestEntity>) this.spannerMappingContext.getPersistentEntity(TestEntity.class);
    Statement childrenRowsQuery = SpannerStatementQueryExecutor.buildQuery(KeySet.newBuilder().addKey(Key.of("k1.1", "k1.2")).addKey(Key.of("k2.1", "k2.2")).build(), entity, new SpannerWriteConverter(), this.spannerMappingContext, entity.getWhere());
    assertThat(childrenRowsQuery.getSql()).isEqualTo("SELECT other, deleted, id, custom_col, id_2, ARRAY (SELECT AS STRUCT deleted, id3, id, id_2 " + "FROM child_test_table WHERE (child_test_table.id = custom_test_table.id " + "AND child_test_table.id_2 = custom_test_table.id_2) AND (deleted = false)) AS childEntities " + "FROM custom_test_table WHERE ((id = @tag0 AND id_2 = @tag1) " + "OR (id = @tag2 AND id_2 = @tag3)) AND (deleted = false)");
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) Statement(com.google.cloud.spanner.Statement) SpannerWriteConverter(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerWriteConverter) Test(org.junit.Test)

Example 5 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class SpannerStatementQueryExecutor method buildQuery.

/**
 * Builds a query that returns the rows associated with a key set with additional SQL-where.
 * The {@link org.springframework.cloud.gcp.data.spanner.core.mapping.Where} of the {@code persistentEntity} parameter
 * is ignored, you should pass the SQL-where as a {@code whereClause} parameter.
 * The secondary {@code index} will be used instead of the table name when the corresponding parameter is not null.
 * @param keySet the key set whose members to get.
 * @param persistentEntity the persistent entity of the table.
 * @param <T> the type of the persistent entity
 * @param writeConverter a converter to convert key values as needed to bind to the query statement.
 * @param mappingContext mapping context
 * @param whereClause SQL where clause
 * @param index the secondary index name
 * @return the Spanner statement to perform the retrieval.
 */
public static <T> Statement buildQuery(KeySet keySet, SpannerPersistentEntity<T> persistentEntity, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext, String whereClause, String index) {
    List<String> orParts = new ArrayList<>();
    List<String> tags = new ArrayList<>();
    List keyParts = new ArrayList();
    int tagNum = 0;
    List<SpannerPersistentProperty> keyProperties = persistentEntity.getFlattenedPrimaryKeyProperties();
    for (Key key : keySet.getKeys()) {
        StringJoiner andJoiner = new StringJoiner(" AND ");
        Iterator parentKeyParts = key.getParts().iterator();
        while (parentKeyParts.hasNext()) {
            SpannerPersistentProperty keyProp = keyProperties.get(tagNum % keyProperties.size());
            String tagName = "tag" + tagNum;
            andJoiner.add(keyProp.getColumnName() + " = @" + tagName);
            tags.add(tagName);
            keyParts.add(parentKeyParts.next());
            tagNum++;
        }
        orParts.add(andJoiner.toString());
    }
    String keyClause = orParts.stream().map(s -> "(" + s + ")").collect(Collectors.joining(" OR "));
    String condition = combineWithAnd(keyClause, whereClause);
    String sb = "SELECT " + getColumnsStringForSelect(persistentEntity, mappingContext, true) + " FROM " + (StringUtils.isEmpty(index) ? persistentEntity.tableName() : String.format("%s@{FORCE_INDEX=%s}", persistentEntity.tableName(), index)) + (condition.isEmpty() ? "" : " WHERE " + condition);
    return buildStatementFromSqlWithArgs(sb, tags, null, writeConverter, keyParts.toArray(), null);
}
Also used : IgnoreCaseType(org.springframework.data.repository.query.parser.Part.IgnoreCaseType) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) HashMap(java.util.HashMap) Function(java.util.function.Function) ConversionUtils(org.springframework.cloud.gcp.data.spanner.core.convert.ConversionUtils) ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) Key(com.google.cloud.spanner.Key) 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) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) Collectors(java.util.stream.Collectors) KeySet(com.google.cloud.spanner.KeySet) Statement(com.google.cloud.spanner.Statement) List(java.util.List) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Where(org.springframework.cloud.gcp.data.spanner.core.mapping.Where) ParameterizedType(java.lang.reflect.ParameterizedType) Struct(com.google.cloud.spanner.Struct) StringJoiner(java.util.StringJoiner) ValueBinder(com.google.cloud.spanner.ValueBinder) ConverterAwareMappingSpannerEntityWriter(org.springframework.cloud.gcp.data.spanner.core.convert.ConverterAwareMappingSpannerEntityWriter) SpannerCustomConverter(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerCustomConverter) StringUtils(org.springframework.util.StringUtils) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.cloud.spanner.Key) StringJoiner(java.util.StringJoiner) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Aggregations

SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)10 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)6 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)6 Set (java.util.Set)5 SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)5 Struct (com.google.cloud.spanner.Struct)4 List (java.util.List)4 Map (java.util.Map)4 BiFunction (java.util.function.BiFunction)4 Statement (com.google.cloud.spanner.Statement)3 ArrayList (java.util.ArrayList)3 StringJoiner (java.util.StringJoiner)3 PropertyHandler (org.springframework.data.mapping.PropertyHandler)3 ByteArray (com.google.cloud.ByteArray)2 Date (com.google.cloud.Date)2 Timestamp (com.google.cloud.Timestamp)2 Key (com.google.cloud.spanner.Key)2 Type (com.google.cloud.spanner.Type)2 ValueBinder (com.google.cloud.spanner.ValueBinder)2 ImmutableMap (com.google.common.collect.ImmutableMap)2