Class SpannerStatementQueryExecutor
java.lang.Object
com.google.cloud.spring.data.spanner.repository.query.SpannerStatementQueryExecutor
Executes Cloud Spanner query statements using
PartTree parsed method definitions.- Since:
- 1.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringBuilderapplySort(org.springframework.data.domain.Sort sort, StringBuilder sql, SpannerPersistentEntity<?> persistentEntity) static <T> StringapplySortingPagingQueryOptions(Class<T> entityClass, SpannerPageableQueryOptions options, String sql, SpannerMappingContext mappingContext, boolean fetchInterleaved) Apply paging and sorting options to a query string.static <T> com.google.cloud.spanner.StatementbuildQuery(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.static <T> com.google.cloud.spanner.StatementbuildQuery(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.static <T> com.google.cloud.spanner.StatementbuildQuery(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.static com.google.cloud.spanner.StatementbuildStatementFromSqlWithArgs(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.static StringbuildWhere(SpannerPersistentEntity<?> entity) Builds an SQL where clause for the persistent entity.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.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.static com.google.cloud.spanner.StatementgetChildrenRowsQuery(com.google.cloud.spanner.Key parentKey, SpannerPersistentProperty spannerPersistentProperty, SpannerCustomConverter writeConverter, SpannerMappingContext mappingContext) Gets aStatementthat returns the rows associated with a parent entity.static StringgetColumnsStringForSelect(SpannerPersistentEntity<?> spannerPersistentEntity, SpannerMappingContext mappingContext, boolean fetchInterleaved)
-
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 entitytree- the parsed metadata of the queryparameterAccessor- the parameters of this specific queryqueryMethodParamsMetadata- parameter metadata from Query MethodspannerTemplate- used to execute the queryspannerMappingContext- 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 paramsT- 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 entitytree- the parsed metadata of the queryparameterAccessor- the parameters of this specific queryqueryMethodParamsMetadata- parameter metadata from Query MethodspannerTemplate- used to execute the queryspannerMappingContext- 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 optionssql- 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- whentrueadditional subqueries will be added to fetch eager-Interleaved lists with a single query. Please note, it doesn't make sense to pass it astruewhen thesqlalready contains a complete lists of all eager-Interleaved properties generated by the methodgetColumnsStringForSelect(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
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 aStatementthat 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 hasWhereannotation 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. TheWhereof thepersistentEntityparameter is ignored, you should pass the SQL-where as awhereClauseparameter.- 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 contextwhereClause- 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. TheWhereof thepersistentEntityparameter is ignored, you should pass the SQL-where as awhereClauseparameter. The secondaryindexwill 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 contextwhereClause- SQL where clauseindex- 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 toStructobjects 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. ifnullthen 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)
-