Class SpannerTemplate
java.lang.Object
com.google.cloud.spring.data.spanner.core.SpannerTemplate
- All Implemented Interfaces:
SpannerOperations,org.springframework.beans.factory.Aware,org.springframework.context.ApplicationEventPublisherAware
public class SpannerTemplate
extends Object
implements SpannerOperations, org.springframework.context.ApplicationEventPublisherAware
An implementation of
SpannerOperations.- Since:
- 1.1
-
Constructor Summary
ConstructorsConstructorDescriptionSpannerTemplate(Supplier<com.google.cloud.spanner.DatabaseClient> databaseClientProvider, SpannerMappingContext mappingContext, SpannerEntityProcessor spannerEntityProcessor, SpannerMutationFactory spannerMutationFactory, SpannerSchemaUtils spannerSchemaUtils) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidapplyMutations(Collection<com.google.cloud.spanner.Mutation> mutations) <T> longCount how many objects are stored of the given type.<T> voidDeletes an object based on a key.<T> voidDeletes objects given a set of keys.voidDeletes an object from storage.voidDeletes objects from storage in a batch.longexecuteDmlStatement(com.google.cloud.spanner.Statement statement) Execute a DML statement on Cloud Spanner.longexecutePartitionedDmlStatement(com.google.cloud.spanner.Statement statement) Execute a DML statement in partitioned mode.longexecutePartitionedDmlStatement(com.google.cloud.spanner.Statement statement, com.google.cloud.spanner.Options.UpdateOption... options) Execute a DML statement in partitioned mode.com.google.cloud.spanner.ResultSetexecuteQuery(com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) <T> booleanexistsById(Class<T> entityClass, com.google.cloud.spanner.Key key) Returns whether an entity with the given id exists.protected com.google.cloud.spanner.ReadContextprotected com.google.cloud.spanner.ReadContextgetReadContext(com.google.cloud.spanner.TimestampBound timestampBound) voidInsert an object into storage.voidInsert objects into storage in batch.<T> TperformReadOnlyTransaction(Function<SpannerTemplate, T> operations, SpannerReadOptions readOptions) Performs multiple read-only operations in a single transaction.<T> TperformReadWriteTransaction(Function<SpannerTemplate, T> operations) Performs multiple read and write operations in a single transaction.<T> List<T>query(Class<T> entityClass, com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) Finds objects by using an SQL statement.<A> List<A>query(Function<com.google.cloud.spanner.Struct, A> rowFunc, com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) Executes a given query string with tags and parameters and applies a given function to each row of the result.<T> List<T>queryAll(Class<T> entityClass, SpannerPageableQueryOptions options) Finds all objects of the given type.<T> TFinds a single stored object using a key.<T> Tread(Class<T> entityClass, com.google.cloud.spanner.Key key, SpannerReadOptions options) Finds a single stored object using a key.<T> List<T>Finds objects stored from their keys.<T> List<T>read(Class<T> entityClass, com.google.cloud.spanner.KeySet keys, SpannerReadOptions options) Finds objects stored from their keys.<T> List<T>Finds all objects of the given type.<T> List<T>readAll(Class<T> entityClass, SpannerReadOptions options) Finds all objects of the given type.voidsetApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) voidUpdate an object already in storage.voidUpdate an object in storage.voidUpdate an object in storage.voidUpdate objects in batch.voidUpdate or insert an object into storage.voidUpdate or insert an object into storage.voidUpdate or insert an object into storage.voidUpdate or insert objects into storage in batch.
-
Constructor Details
-
SpannerTemplate
public SpannerTemplate(Supplier<com.google.cloud.spanner.DatabaseClient> databaseClientProvider, SpannerMappingContext mappingContext, SpannerEntityProcessor spannerEntityProcessor, SpannerMutationFactory spannerMutationFactory, SpannerSchemaUtils spannerSchemaUtils)
-
-
Method Details
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) - Specified by:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware
-
getReadContext
protected com.google.cloud.spanner.ReadContext getReadContext() -
getReadContext
protected com.google.cloud.spanner.ReadContext getReadContext(com.google.cloud.spanner.TimestampBound timestampBound) -
getMappingContext
-
getSpannerEntityProcessor
-
executeDmlStatement
public long executeDmlStatement(com.google.cloud.spanner.Statement statement) Description copied from interface:SpannerOperationsExecute a DML statement on Cloud Spanner. This must always be done in a transaction and one will be started if needed. SeeSpannerOperations.executePartitionedDmlStatement(Statement)for executing partitioned DML without a transaction.- Specified by:
executeDmlStatementin interfaceSpannerOperations- Parameters:
statement- the DML statement to execute.- Returns:
- the number of rows affected.
-
executePartitionedDmlStatement
public long executePartitionedDmlStatement(com.google.cloud.spanner.Statement statement) Description copied from interface:SpannerOperationsExecute a DML statement in partitioned mode. This is not available inside of transactions.- Specified by:
executePartitionedDmlStatementin interfaceSpannerOperations- Parameters:
statement- the DML statement to execute.- Returns:
- the lower-bound of number of rows affected.
-
executePartitionedDmlStatement
public long executePartitionedDmlStatement(com.google.cloud.spanner.Statement statement, com.google.cloud.spanner.Options.UpdateOption... options) Description copied from interface:SpannerOperationsExecute a DML statement in partitioned mode. This is not available inside of transactions.- Specified by:
executePartitionedDmlStatementin interfaceSpannerOperations- Parameters:
statement- the DML statement to execute.options- marks options applicable to update operation.- Returns:
- the lower-bound of number of rows affected.
-
read
Description copied from interface:SpannerOperationsFinds a single stored object using a key.- Specified by:
readin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.key- the key of the object.- Returns:
- an object of the requested type. Returns null if no object could be found stored with the given key.
-
existsById
Description copied from interface:SpannerOperationsReturns whether an entity with the given id exists.- Specified by:
existsByIdin interfaceSpannerOperations- Type Parameters:
T- the type of the object to check.- Parameters:
entityClass- the type of the entitykey- the key of the object- Returns:
- true if an entity with the given key exists, false otherwise.
-
read
public <T> T read(Class<T> entityClass, com.google.cloud.spanner.Key key, SpannerReadOptions options) Description copied from interface:SpannerOperationsFinds a single stored object using a key.- Specified by:
readin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.key- the key of the object.options- the Cloud Spanner read options with which to conduct the read operation.- Returns:
- an object of the requested type. Returns null if no object could be found stored with the given key.
-
read
Description copied from interface:SpannerOperationsFinds objects stored from their keys.- Specified by:
readin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.keys- the keys of the objects to retrieve.- Returns:
- a list of objects that could be found using the given keys. If no keys could be found the list will be empty.
-
read
public <T> List<T> read(Class<T> entityClass, com.google.cloud.spanner.KeySet keys, SpannerReadOptions options) Description copied from interface:SpannerOperationsFinds objects stored from their keys. When the entity has aWhereclass annotation or any of the properties is eagerly interleaved, the SQL query will be performed instead of theReadContext.read(java.lang.String, com.google.cloud.spanner.KeySet, java.lang.Iterable<java.lang.String>, com.google.cloud.spanner.Options.ReadOption...)to fetch such properties and satisfy thesql wherecondition.- Specified by:
readin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.keys- the keys of the objects to retrieve.options- the Cloud Spanner read options with which to conduct the read operation.- Returns:
- a list of objects that could be found using the given keys. If no keys could be found the list will be empty.
-
query
public <A> List<A> query(Function<com.google.cloud.spanner.Struct, A> rowFunc, com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) Description copied from interface:SpannerOperationsExecutes a given query string with tags and parameters and applies a given function to each row of the result.- Specified by:
queryin interfaceSpannerOperations- Type Parameters:
A- the type to convert each row Struct into.- Parameters:
rowFunc- the function to apply to each row of the result.statement- the SQL statement used to select the objects.options- the options with which to run this query.- Returns:
- a list of the rows each transformed with the given function.
-
query
public <T> List<T> query(Class<T> entityClass, com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) Description copied from interface:SpannerOperationsFinds objects by using an SQL statement.- Specified by:
queryin interfaceSpannerOperations- Type Parameters:
T- the type of object to retrieve.- Parameters:
entityClass- the type of object to retrieve.statement- the SQL statement used to select the objects.options- the Cloud Spanner read options with which to conduct the read operation.- Returns:
- a list of the objects found. If no keys could be found the list will be empty.
-
readAll
Description copied from interface:SpannerOperationsFinds all objects of the given type.- Specified by:
readAllin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.options- the Cloud Spanner read options with which to conduct the read operation.- Returns:
- a list of all objects stored of the given type. If there are no objects an empty list is returned.
-
readAll
Description copied from interface:SpannerOperationsFinds all objects of the given type.- Specified by:
readAllin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.- Returns:
- a list of all objects stored of the given type. If there are no objects an empty list is returned.
-
queryAll
Description copied from interface:SpannerOperationsFinds all objects of the given type.- Specified by:
queryAllin interfaceSpannerOperations- Type Parameters:
T- the type of the object to retrieve.- Parameters:
entityClass- the type of the object to retrieve.options- the Cloud Spanner query options with which to conduct the query operation.- Returns:
- a list of all objects stored of the given type. If there are no objects an empty list is returned.
-
insert
Description copied from interface:SpannerOperationsInsert an object into storage.- Specified by:
insertin interfaceSpannerOperations- Parameters:
object- the object to insert.
-
insertAll
Description copied from interface:SpannerOperationsInsert objects into storage in batch.- Specified by:
insertAllin interfaceSpannerOperations- Parameters:
objects- the objects to insert.
-
update
Description copied from interface:SpannerOperationsUpdate an object already in storage.- Specified by:
updatein interfaceSpannerOperations- Parameters:
object- the object to update.
-
updateAll
Description copied from interface:SpannerOperationsUpdate objects in batch.- Specified by:
updateAllin interfaceSpannerOperations- Parameters:
objects- the objects to update.
-
update
Description copied from interface:SpannerOperationsUpdate an object in storage.- Specified by:
updatein interfaceSpannerOperations- Parameters:
object- the object to update.includeProperties- the properties to upsert. if none are given then all properties are used
-
update
Description copied from interface:SpannerOperationsUpdate an object in storage.- Specified by:
updatein interfaceSpannerOperations- Parameters:
object- the object to update.includeProperties- the properties to update. If null is given, then all properties are used. Note that an emptySetmeans that no properties will be used.
-
upsert
Description copied from interface:SpannerOperationsUpdate or insert an object into storage.- Specified by:
upsertin interfaceSpannerOperations- Parameters:
object- the object to update or insert.
-
upsertAll
Description copied from interface:SpannerOperationsUpdate or insert objects into storage in batch.- Specified by:
upsertAllin interfaceSpannerOperations- Parameters:
objects- the objects to update or insert.
-
upsert
Description copied from interface:SpannerOperationsUpdate or insert an object into storage.- Specified by:
upsertin interfaceSpannerOperations- Parameters:
object- the object to update or insert.includeProperties- the properties to upsert. if none are given then all properties are upserted.
-
upsert
Description copied from interface:SpannerOperationsUpdate or insert an object into storage.- Specified by:
upsertin interfaceSpannerOperations- Parameters:
object- the object to update or insert.includeProperties- the properties to update. If null is given, then all properties are used. Note that an emptySetmeans that no properties will be used.
-
delete
Description copied from interface:SpannerOperationsDeletes an object from storage.- Specified by:
deletein interfaceSpannerOperations- Parameters:
entity- the object to delete from storage.
-
deleteAll
Description copied from interface:SpannerOperationsDeletes objects from storage in a batch.- Specified by:
deleteAllin interfaceSpannerOperations- Parameters:
objects- the objects to delete from storage.
-
delete
Description copied from interface:SpannerOperationsDeletes an object based on a key.- Specified by:
deletein interfaceSpannerOperations- Type Parameters:
T- the type of the object to delete.- Parameters:
entityClass- the type of the object to delete.key- the key of the object to delete from storage.
-
delete
Description copied from interface:SpannerOperationsDeletes objects given a set of keys.- Specified by:
deletein interfaceSpannerOperations- Type Parameters:
T- the type of the object to delete.- Parameters:
entityClass- the type of object to delete.keys- the keys of the objects to delete.
-
count
Description copied from interface:SpannerOperationsCount how many objects are stored of the given type.- Specified by:
countin interfaceSpannerOperations- Type Parameters:
T- the type of the object to count.- Parameters:
entityClass- the type of object to count.- Returns:
- the number of stored objects.
-
performReadWriteTransaction
Description copied from interface:SpannerOperationsPerforms multiple read and write operations in a single transaction.- Specified by:
performReadWriteTransactionin interfaceSpannerOperations- Type Parameters:
T- the final return type of the operations.- Parameters:
operations- the function representing the operations to perform using a SpannerOperations based on a single transaction.- Returns:
- the final result of the transaction.
-
performReadOnlyTransaction
public <T> T performReadOnlyTransaction(Function<SpannerTemplate, T> operations, SpannerReadOptions readOptions) Description copied from interface:SpannerOperationsPerforms multiple read-only operations in a single transaction.- Specified by:
performReadOnlyTransactionin interfaceSpannerOperations- Type Parameters:
T- the final return type of the operations.- Parameters:
operations- the function representing the operations to perform using a SpannerOperations based on a single transaction.readOptions- allows the user to specify staleness for the read transaction- Returns:
- the final result of the transaction.
-
executeQuery
public com.google.cloud.spanner.ResultSet executeQuery(com.google.cloud.spanner.Statement statement, SpannerQueryOptions options) -
applyMutations
-