Class SpannerStatementQueryExecutor

java.lang.Object
com.google.cloud.spring.data.spanner.repository.query.SpannerStatementQueryExecutor

public final class SpannerStatementQueryExecutor extends Object
Executes Cloud Spanner query statements using PartTree parsed method definitions.
Since:
1.1
  • Method Details

    • executeQuery

      public static <T> List<T> executeQuery(Class<T> type, org.springframework.data.repository.query.parser.PartTree tree, org.springframework.data.repository.query.ParameterAccessor parameterAccessor, Parameter[] queryMethodParamsMetadata, SpannerTemplate spannerTemplate, SpannerMappingContext spannerMappingContext)
      Executes a PartTree-based query.
      Type Parameters:
      T - the type of the underlying entity
      Parameters:
      type - the type of the underlying entity
      tree - the parsed metadata of the query
      parameterAccessor - the parameters of this specific query
      queryMethodParamsMetadata - parameter metadata from Query Method
      spannerTemplate - used to execute the query
      spannerMappingContext - used to get metadata about the entity type
      Returns:
      list of entities.
    • executeQuery

      public static <A, T> List<A> executeQuery(Function<com.google.cloud.spanner.Struct,A> rowFunc, Class<T> type, org.springframework.data.repository.query.parser.PartTree tree, org.springframework.data.repository.query.ParameterAccessor parameterAccessor, Parameter[] queryMethodParamsMetadata, SpannerTemplate spannerTemplate, SpannerMappingContext spannerMappingContext)
      Executes a PartTree-based query and applies a custom row-mapping function to the result.
      Type Parameters:
      A - the type to which to convert Struct params
      T - the type of the underlying entity on which to query
      Parameters:
      rowFunc - the function to apply to each row of the result.
      type - the type of the underlying entity
      tree - the parsed metadata of the query
      parameterAccessor - the parameters of this specific query
      queryMethodParamsMetadata - parameter metadata from Query Method
      spannerTemplate - used to execute the query
      spannerMappingContext - used to get metadata about the entity type
      Returns:
      list of objects mapped using the given function.
    • applySortingPagingQueryOptions

      public static <T> String applySortingPagingQueryOptions(Class<T> entityClass, SpannerPageableQueryOptions options, String sql, SpannerMappingContext mappingContext, boolean fetchInterleaved)
      Apply paging and sorting options to a query string.
      Type Parameters:
      T - the domain type.
      Parameters:
      entityClass - the domain type whose table is being queried.
      options - query options containing the sorting and paging options
      sql - the sql that will be wrapped with sorting and paging options.
      mappingContext - a mapping context to convert between Cloud Spanner column names and underlying property names.
      fetchInterleaved - when true additional subqueries will be added to fetch eager-Interleaved lists with a single query. Please note, it doesn't make sense to pass it as true when the sql already contains a complete lists of all eager-Interleaved properties generated by the method getColumnsStringForSelect(com.google.cloud.spring.data.spanner.core.mapping.SpannerPersistentEntity<?>, com.google.cloud.spring.data.spanner.core.mapping.SpannerMappingContext, boolean).
      Returns:
      the final SQL string with paging and sorting applied.
      See Also:
    • buildWhere

      public static String buildWhere(SpannerPersistentEntity<?> entity)
      Builds an SQL where clause for the persistent entity.
      Parameters:
      entity - the persistent entity instance
      Returns:
      SQL where clause for the persistent entity instance.
      See Also:
    • getChildrenRowsQuery

      public static com.google.cloud.spanner.Statement getChildrenRowsQuery(com.google.cloud.spanner.Key parentKey, SpannerPersistentProperty spannerPersistentProperty, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext)
      Gets a Statement that returns the rows associated with a parent entity. This function is intended to be used with parent-child interleaved tables, so that the retrieval of all child rows having the parent's key values is efficient.
      Parameters:
      parentKey - the parent key whose children to get.
      spannerPersistentProperty - the property with interleaved list of child entries in the parent entity.
      writeConverter - a converter to convert key values as needed to bind to the query statement.
      mappingContext - mapping context
      Returns:
      the Spanner statement to perform the retrieval.
    • buildQuery

      public static <T> com.google.cloud.spanner.Statement buildQuery(com.google.cloud.spanner.KeySet keySet, SpannerPersistentEntity<T> persistentEntity, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext)
      Builds a query that returns the rows associated with a key set. If the entity class has Where annotation it will be used to build the query.
      Type Parameters:
      T - the type of the persistent entity
      Parameters:
      keySet - the key set whose members to get.
      persistentEntity - the persistent entity of the table.
      writeConverter - a converter to convert key values as needed to bind to the query statement.
      mappingContext - mapping context
      Returns:
      the Spanner statement to perform the retrieval.
    • buildQuery

      public static <T> com.google.cloud.spanner.Statement buildQuery(com.google.cloud.spanner.KeySet keySet, SpannerPersistentEntity<T> persistentEntity, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext, String whereClause)
      Builds a query that returns the rows associated with a key set with additional SQL-where. The Where of the persistentEntity parameter is ignored, you should pass the SQL-where as a whereClause parameter.
      Type Parameters:
      T - the type of the persistent entity
      Parameters:
      keySet - the key set whose members to get.
      persistentEntity - the persistent entity of the table.
      writeConverter - a converter to convert key values as needed to bind to the query statement.
      mappingContext - mapping context
      whereClause - SQL where clause
      Returns:
      the Spanner statement to perform the retrieval.
    • buildQuery

      public static <T> com.google.cloud.spanner.Statement buildQuery(com.google.cloud.spanner.KeySet keySet, SpannerPersistentEntity<T> persistentEntity, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext, String whereClause, String index)
      Builds a query that returns the rows associated with a key set with additional SQL-where. The Where of the persistentEntity parameter is ignored, you should pass the SQL-where as a whereClause parameter. The secondary index will be used instead of the table name when the corresponding parameter is not null.
      Type Parameters:
      T - the type of the persistent entity
      Parameters:
      keySet - the key set whose members to get.
      persistentEntity - the persistent entity of the table.
      writeConverter - a converter to convert key values as needed to bind to the query statement.
      mappingContext - mapping context
      whereClause - SQL where clause
      index - the secondary index name
      Returns:
      the Spanner statement to perform the retrieval.
    • buildStatementFromSqlWithArgs

      public static com.google.cloud.spanner.Statement buildStatementFromSqlWithArgs(String sql, List<String> tags, Function<Object,com.google.cloud.spanner.Struct> paramStructConvertFunc, SpannerCustomConverter spannerCustomConverter, Object[] params, Map<String,Parameter> queryMethodParams)
      Creates a Cloud Spanner statement.
      Parameters:
      sql - the SQL string with tags.
      tags - the tags that appear in the SQL string.
      paramStructConvertFunc - a function to use to convert params to Struct objects if they cannot be directly mapped to Cloud Spanner supported param types. If null then this last-attempt conversion is skipped.
      spannerCustomConverter - a converter used to convert params that aren't Cloud Spanner native types. if null then this conversion is not attempted.
      params - the parameters to substitute the tags. The ordering must be the same as the tags.
      queryMethodParams - the parameter metadata from Query Method if available.
      Returns:
      an SQL statement ready to use with Spanner.
      Throws:
      IllegalArgumentException - if the number of tags does not match the number of params, or if a param of an unsupported type is given.
    • getColumnsStringForSelect

      public static String getColumnsStringForSelect(SpannerPersistentEntity<?> spannerPersistentEntity, SpannerMappingContext mappingContext, boolean fetchInterleaved)
    • applySort

      public static StringBuilder applySort(org.springframework.data.domain.Sort sort, StringBuilder sql, SpannerPersistentEntity<?> persistentEntity)