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
Modifier and TypeClassDescriptionprotected static interface
Functional interface used to supply a statement. -
Field Summary
Modifier 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
ModifierConstructorDescriptionprotected
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 String
getJsonColumn
(QueryResultInfo queryResultInfo) Gets column name for JSON result.protected final JsonDataType
getJsonDataType
(QueryResultInfo queryResultInfo) Gets JSON data type for JSON result.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 final boolean
isJsonEntityGeneratedId
(StoredQuery<?, ?> storedQuery, PersistentEntity persistentEntity) Inserting JSON entity representation (like Oracle Json View) can generate new id, and we support retrieval only numeric auto generated ids.protected final boolean
isJsonResult
(PreparedQuery<?, ?> preparedQuery, QueryResultInfo queryResultInfo) Return an indicator telling whether prepared query result produces JSON result.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, @NonNull 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> @NonNull 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.protected <E> @NonNull 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.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 @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> @NonNull SqlStoredQuery<E,E> resolveEntityInsert(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull @NonNull Class<E> rootEntity, @NonNull @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> @NonNull SqlStoredQuery<E,E> resolveEntityUpdate(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull @NonNull Class<E> rootEntity, @NonNull @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
-
isJsonResult
protected final boolean isJsonResult(PreparedQuery<?, ?> preparedQuery, QueryResultInfo queryResultInfo) Return an indicator telling whether prepared query result produces JSON result.- Parameters:
preparedQuery
- the prepared queryqueryResultInfo
- the query result info, if not null will hold info about result type- Returns:
- true if result is JSON
-
isJsonEntityGeneratedId
protected final boolean isJsonEntityGeneratedId(StoredQuery<?, ?> storedQuery, PersistentEntity persistentEntity) Inserting JSON entity representation (like Oracle Json View) can generate new id, and we support retrieval only numeric auto generated ids.- Parameters:
storedQuery
- the stored querypersistentEntity
- the persistent entity- Returns:
- true if entity being inserted is JSON entity representation with auto generated numeric id
-
getJsonColumn
Gets column name for JSON result. IfQueryResult
annotation is present, takes column value from there, otherwise defaults to 'DATA' column name.- Parameters:
queryResultInfo
- the query result info from theQueryResult
annotation, null if annotation not present- Returns:
- the JSON column name
-
getJsonDataType
Gets JSON data type for JSON result. IfQueryResult
annotation is present, takes data type value from there, otherwise defaults toJsonDataType.DEFAULT
.- Parameters:
queryResultInfo
- the query result info from theQueryResult
annotation, null if annotation not present- Returns:
- the JSON data type
-