Class AbstractSqlRepositoryOperations<RS,PS,Exc extends Exception>
java.lang.Object
io.micronaut.data.runtime.operations.internal.AbstractRepositoryOperations
io.micronaut.data.runtime.operations.internal.sql.AbstractSqlRepositoryOperations<RS,PS,Exc>
- Type Parameters:
RS
- The result set typePS
- The prepared statement typeExc
- The exception type
- All Implemented Interfaces:
io.micronaut.context.ApplicationContextProvider
,HintsCapableRepository
,MethodContextAwareStoredQueryDecorator
,PreparedQueryDecorator
- Direct Known Subclasses:
DefaultJdbcRepositoryOperations
@Internal
public abstract class AbstractSqlRepositoryOperations<RS,PS,Exc extends Exception>
extends AbstractRepositoryOperations
implements io.micronaut.context.ApplicationContextProvider, PreparedQueryDecorator, MethodContextAwareStoredQueryDecorator, HintsCapableRepository
Abstract SQL repository implementation not specifically bound to JDBC.
- Since:
- 1.0.0
- Author:
- graemerocher, Denis Stepanov
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
Functional interface used to supply a statement. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ResultReader<RS,
Integer> protected final ResultReader<RS,
String> protected final String
protected final io.micronaut.json.JsonMapper
protected final QueryStatement<PS,
Integer> protected static final org.slf4j.Logger
protected final Map<Class,
SqlQueryBuilder> protected final SqlJsonColumnMapperProvider<RS>
Fields inherited from class io.micronaut.data.runtime.operations.internal.AbstractRepositoryOperations
attributeConverterRegistry, conversionService, dateTimeProvider, entityEventRegistry, runtimeEntityRegistry
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractSqlRepositoryOperations
(String dataSourceName, ResultReader<RS, String> columnNameResultSetReader, ResultReader<RS, Integer> columnIndexResultSetReader, QueryStatement<PS, Integer> preparedStatementWriter, DateTimeProvider<Object> dateTimeProvider, RuntimeEntityRegistry runtimeEntityRegistry, io.micronaut.context.BeanContext beanContext, DataConversionService conversionService, AttributeConverterRegistry attributeConverterRegistry, io.micronaut.json.JsonMapper jsonMapper, SqlJsonColumnMapperProvider<RS> sqlJsonColumnMapperProvider) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected final <T,
R> SqlTypeMapper<RS, R> createQueryResultMapper
(SqlPreparedQuery<?, ?> sqlPreparedQuery, String columnName, JsonDataType jsonDataType, Class<RS> resultSetType, RuntimePersistentEntity<T> persistentEntity, BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener) CreatesSqlTypeMapper
for reading results from single column into an entity.<E,
R> StoredQuery<E, R> decorate
(io.micronaut.aop.MethodInvocationContext<?, ?> context, StoredQuery<E, R> storedQuery) Decorate stored query.<E,
R> PreparedQuery<E, R> decorate
(PreparedQuery<E, R> preparedQuery) Decorate prepared query.protected final <E,
R> SqlPreparedQuery<E, R> getSqlPreparedQuery
(PreparedQuery<E, R> preparedQuery) protected final <E,
R> SqlStoredQuery<E, R> getSqlStoredQuery
(StoredQuery<E, R> storedQuery) protected static Throwable
handleSqlException
(SQLException sqlException, Dialect dialect) Handles SQL exception, used in context of update but could be used elsewhere.protected boolean
isSupportsBatchDelete
(PersistentEntity persistentEntity, Dialect dialect) Does supports batch for delete queries.protected boolean
isSupportsBatchInsert
(PersistentEntity persistentEntity, Dialect dialect) Does supports batch for update queries.protected boolean
isSupportsBatchUpdate
(PersistentEntity persistentEntity, Dialect dialect) Does supports batch for update queries.protected final <R,
T> R mapQueryColumnResult
(SqlPreparedQuery<?, ?> sqlPreparedQuery, RS rs, String columnName, JsonDataType jsonDataType, RuntimePersistentEntity<T> persistentEntity, Class<R> resultType, Class<RS> resultSetType, BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener) Reads an object from the result set and given column.protected <T,
R> PS prepareStatement
(AbstractSqlRepositoryOperations.StatementSupplier<PS> statementFunction, PreparedQuery<T, R> preparedQuery, boolean isUpdate, boolean isSingleResult) Prepare a statement for execution.protected <T> String
resolveAssociationInsert
(Class repositoryType, RuntimePersistentEntity<T> persistentEntity, RuntimeAssociation<T> association) Builds a join table insert.protected <E> SqlStoredQuery<E,
E> resolveEntityInsert
(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, Class<E> rootEntity, RuntimePersistentEntity<E> persistentEntity) Resolves a stored insert for the given entity.protected <E> SqlStoredQuery<E,
E> resolveEntityUpdate
(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, Class<E> rootEntity, RuntimePersistentEntity<E> persistentEntity) Resolves a stored update for the given entity.protected <T> SqlStoredQuery<T,
?> resolveSqlInsertAssociation
(Class<?> repositoryType, RuntimeAssociation<T> association, RuntimePersistentEntity<T> persistentEntity, T entity) Resolve SQL insert association operation.protected void
setStatementParameter
(PS preparedStatement, int index, DataType dataType, JsonDataType jsonDataType, Object value, SqlStoredQuery<?, ?> storedQuery) Set the parameter value on the given statement.Methods inherited from class io.micronaut.data.runtime.operations.internal.AbstractRepositoryOperations
checkOptimisticLocking, getApplicationContext, getConversionService, getEntity, getIdReader, isOnlySingleEndedJoins, triggerPostLoad
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.context.ApplicationContextProvider
getApplicationContext
Methods inherited from interface io.micronaut.data.operations.HintsCapableRepository
getQueryHints
-
Field Details
-
QUERY_LOG
protected static final org.slf4j.Logger QUERY_LOG -
dataSourceName
-
columnNameResultSetReader
-
columnIndexResultSetReader
-
preparedStatementWriter
-
jsonMapper
protected final io.micronaut.json.JsonMapper jsonMapper -
sqlJsonColumnMapperProvider
-
queryBuilders
-
repositoriesWithHardcodedDataSource
-
-
Constructor Details
-
AbstractSqlRepositoryOperations
protected AbstractSqlRepositoryOperations(String dataSourceName, ResultReader<RS, String> columnNameResultSetReader, ResultReader<RS, Integer> columnIndexResultSetReader, QueryStatement<PS, Integer> preparedStatementWriter, DateTimeProvider<Object> dateTimeProvider, RuntimeEntityRegistry runtimeEntityRegistry, io.micronaut.context.BeanContext beanContext, DataConversionService conversionService, AttributeConverterRegistry attributeConverterRegistry, io.micronaut.json.JsonMapper jsonMapper, SqlJsonColumnMapperProvider<RS> sqlJsonColumnMapperProvider) Default constructor.- Parameters:
dataSourceName
- The datasource namecolumnNameResultSetReader
- The column name result readercolumnIndexResultSetReader
- The column index result readerpreparedStatementWriter
- The prepared statement writerdateTimeProvider
- The date time providerruntimeEntityRegistry
- The entity registrybeanContext
- The bean contextconversionService
- The conversion serviceattributeConverterRegistry
- The attribute converter registryjsonMapper
- The JSON mappersqlJsonColumnMapperProvider
- The SQL JSON column mapper provider
-
-
Method Details
-
decorate
Description copied from interface:PreparedQueryDecorator
Decorate prepared query.- Specified by:
decorate
in interfacePreparedQueryDecorator
- Type Parameters:
E
- The entity typeR
- The result type- Parameters:
preparedQuery
- The query to be decorated- Returns:
- decorated prepared query
-
decorate
public <E,R> StoredQuery<E,R> decorate(io.micronaut.aop.MethodInvocationContext<?, ?> context, StoredQuery<E, R> storedQuery) Description copied from interface:MethodContextAwareStoredQueryDecorator
Decorate stored query.- Specified by:
decorate
in interfaceMethodContextAwareStoredQueryDecorator
- Type Parameters:
E
- The entity typeR
- The result type- Parameters:
context
- The contextstoredQuery
- The query to be decorated- Returns:
- decorated stored query
-
prepareStatement
protected <T,R> PS prepareStatement(AbstractSqlRepositoryOperations.StatementSupplier<PS> statementFunction, @NonNull PreparedQuery<T, R> preparedQuery, boolean isUpdate, boolean isSingleResult) throws ExcPrepare a statement for execution. -
setStatementParameter
protected void setStatementParameter(PS preparedStatement, int index, DataType dataType, JsonDataType jsonDataType, Object value, SqlStoredQuery<?, ?> storedQuery) Set the parameter value on the given statement.- Parameters:
preparedStatement
- The prepared statementindex
- The indexdataType
- The data typejsonDataType
- The JSON representation type if data type is JSONvalue
- The valuestoredQuery
- The SQL stored query
-
resolveEntityInsert
@NonNull protected <E> SqlStoredQuery<E,E> resolveEntityInsert(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull Class<E> rootEntity, @NonNull RuntimePersistentEntity<E> persistentEntity) Resolves a stored insert for the given entity.- Type Parameters:
E
- The entity type- Parameters:
annotationMetadata
- The repository annotation metadatarepositoryType
- The repository typerootEntity
- The root entitypersistentEntity
- The persistent entity- Returns:
- The insert
-
resolveAssociationInsert
protected <T> String resolveAssociationInsert(Class repositoryType, RuntimePersistentEntity<T> persistentEntity, RuntimeAssociation<T> association) Builds a join table insert.- Type Parameters:
T
- The entity generic type- Parameters:
repositoryType
- The repository typepersistentEntity
- The entityassociation
- The association- Returns:
- The insert statement
-
resolveEntityUpdate
@NonNull protected <E> SqlStoredQuery<E,E> resolveEntityUpdate(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull Class<E> rootEntity, @NonNull RuntimePersistentEntity<E> persistentEntity) Resolves a stored update for the given entity.- Type Parameters:
E
- The entity type- Parameters:
annotationMetadata
- The repository annotation metadatarepositoryType
- The repository typerootEntity
- The root entitypersistentEntity
- The persistent entity- Returns:
- The insert
-
resolveSqlInsertAssociation
protected <T> SqlStoredQuery<T,?> resolveSqlInsertAssociation(Class<?> repositoryType, RuntimeAssociation<T> association, RuntimePersistentEntity<T> persistentEntity, T entity) Resolve SQL insert association operation.- Type Parameters:
T
- The entity type- Parameters:
repositoryType
- The repository typeassociation
- The associationpersistentEntity
- The persistent entityentity
- The entity- Returns:
- The operation
-
getSqlPreparedQuery
-
getSqlStoredQuery
-
isSupportsBatchInsert
Does supports batch for update queries.- Parameters:
persistentEntity
- The persistent entitydialect
- The dialect- Returns:
- true if supported
-
isSupportsBatchUpdate
Does supports batch for update queries.- Parameters:
persistentEntity
- The persistent entitydialect
- The dialect- Returns:
- true if supported
-
isSupportsBatchDelete
Does supports batch for delete queries.- Parameters:
persistentEntity
- The persistent entitydialect
- The dialect- Returns:
- true if supported
-
createQueryResultMapper
protected final <T,R> SqlTypeMapper<RS,R> createQueryResultMapper(SqlPreparedQuery<?, ?> sqlPreparedQuery, String columnName, JsonDataType jsonDataType, Class<RS> resultSetType, RuntimePersistentEntity<T> persistentEntity, BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener) CreatesSqlTypeMapper
for reading results from single column into an entity. For now, we support reading from JSON column, however in support we might add XML support etc.- Type Parameters:
T
- the entity typeR
- the result type- Parameters:
sqlPreparedQuery
- the SQL prepared querycolumnName
- the column name where we are reading fromjsonDataType
- the JSON representation typeresultSetType
- resultSetType the result set type (different for R2dbc and Jdbc)persistentEntity
- the persistent entityloadListener
- the load listener if needed after entity loaded- Returns:
- the
SqlTypeMapper
able to decode from column value into given type
-
mapQueryColumnResult
protected final <R,T> R mapQueryColumnResult(SqlPreparedQuery<?, ?> sqlPreparedQuery, RS rs, String columnName, JsonDataType jsonDataType, RuntimePersistentEntity<T> persistentEntity, Class<R> resultType, Class<RS> resultSetType, BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener) Reads an object from the result set and given column.- Type Parameters:
R
- the result typeT
- the entity type- Parameters:
sqlPreparedQuery
- the SQL prepared queryrs
- the result setcolumnName
- the column name where we are reading fromjsonDataType
- the JSON representation typepersistentEntity
- the persistent entityresultType
- the result typeresultSetType
- the result set typeloadListener
- the load listener if needed after entity loaded- Returns:
- an object read from the result set column
-
handleSqlException
Handles SQL exception, used in context of update but could be used elsewhere. It can throw custom exception based on theSQLException
.- Parameters:
sqlException
- the SQL exceptiondialect
- the SQL dialect- Returns:
- custom exception based on
SQLException
that was thrown or that same exception if nothing specific was about it
-