Search in sources :

Example 6 with SpannerPersistentProperty

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

the class SpannerSchemaUtils method getDdlStringForInterleavedHierarchy.

private void getDdlStringForInterleavedHierarchy(String parentTable, Class entityClass, List<String> ddlStrings, Set<Class> seenClasses, BiFunction<Class, String, String> generateSingleDdlStringFunc, boolean prependDdlString) {
    if (seenClasses.contains(entityClass)) {
        return;
    }
    seenClasses.add(entityClass);
    ddlStrings.add(prependDdlString ? 0 : ddlStrings.size(), generateSingleDdlStringFunc.apply(entityClass, parentTable));
    SpannerPersistentEntity spannerPersistentEntity = this.mappingContext.getPersistentEntity(entityClass);
    spannerPersistentEntity.doWithInterleavedProperties((PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> getDdlStringForInterleavedHierarchy(spannerPersistentEntity.tableName(), spannerPersistentProperty.getColumnInnerType(), ddlStrings, seenClasses, generateSingleDdlStringFunc, prependDdlString));
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerTypeMapper(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerTypeMapper) SpannerEntityProcessor(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerEntityProcessor) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) Type(com.google.cloud.spanner.Type) ConversionUtils(org.springframework.cloud.gcp.data.spanner.core.convert.ConversionUtils) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) HashSet(java.util.HashSet) OptionalLong(java.util.OptionalLong) List(java.util.List) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) StringJoiner(java.util.StringJoiner) Key(com.google.cloud.spanner.Key) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) Assert(org.springframework.util.Assert) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Example 7 with SpannerPersistentProperty

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

the class SpannerStatementQueryExecutor method applySort.

public static StringBuilder applySort(Sort sort, StringBuilder sql, SpannerPersistentEntity<?> persistentEntity) {
    if (sort == null || sort.isUnsorted()) {
        return sql;
    }
    sql.append(" ORDER BY ");
    StringJoiner sj = new StringJoiner(" , ");
    sort.iterator().forEachRemaining((o) -> {
        SpannerPersistentProperty property = persistentEntity.getPersistentProperty(o.getProperty());
        String sortedPropertyName = (property != null) ? property.getColumnName() : o.getProperty();
        String sortedProperty = o.isIgnoreCase() ? "LOWER(" + sortedPropertyName + ")" : sortedPropertyName;
        sj.add(sortedProperty + (o.isAscending() ? " ASC" : " DESC"));
    });
    return sql.append(sj);
}
Also used : StringJoiner(java.util.StringJoiner) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Example 8 with SpannerPersistentProperty

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

the class SpannerSchemaUtilsTests method assertColumnDdl.

private void assertColumnDdl(Class clazz, Class innerClazz, String name, OptionalLong length, String expectedDDL) {
    SpannerPersistentProperty spannerPersistentProperty = mock(SpannerPersistentProperty.class);
    // @formatter:off
    Mockito.<Class>when(spannerPersistentProperty.getType()).thenReturn(clazz);
    Mockito.<Class>when(spannerPersistentProperty.getColumnInnerType()).thenReturn(innerClazz);
    // @formatter:on
    when(spannerPersistentProperty.getColumnName()).thenReturn(name);
    when(spannerPersistentProperty.getMaxColumnLength()).thenReturn(length);
    assertThat(this.spannerSchemaUtils.getColumnDdlString(spannerPersistentProperty, this.spannerEntityProcessor)).isEqualTo(expectedDDL);
}
Also used : SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Aggregations

SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)8 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)6 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)5 SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)5 Set (java.util.Set)4 StringJoiner (java.util.StringJoiner)4 BiFunction (java.util.function.BiFunction)4 PropertyHandler (org.springframework.data.mapping.PropertyHandler)4 Struct (com.google.cloud.spanner.Struct)3 List (java.util.List)3 Map (java.util.Map)3 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)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 ArrayList (java.util.ArrayList)2