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 type
PS - The prepared statement type
Exc - 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
  • Field Details

    • QUERY_LOG

      protected static final org.slf4j.Logger QUERY_LOG
    • dataSourceName

      protected final String dataSourceName
    • columnNameResultSetReader

      protected final ResultReader<RS,String> columnNameResultSetReader
    • columnIndexResultSetReader

      protected final ResultReader<RS,Integer> columnIndexResultSetReader
    • preparedStatementWriter

      protected final QueryStatement<PS,Integer> preparedStatementWriter
    • jsonMapper

      protected final io.micronaut.json.JsonMapper jsonMapper
    • sqlJsonColumnMapperProvider

      protected final SqlJsonColumnMapperProvider<RS> sqlJsonColumnMapperProvider
    • queryBuilders

      protected final Map<Class,SqlQueryBuilder2> queryBuilders
    • repositoriesWithHardcodedDataSource

      protected final Map<Class,String> 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 name
      columnNameResultSetReader - The column name result reader
      columnIndexResultSetReader - The column index result reader
      preparedStatementWriter - The prepared statement writer
      dateTimeProvider - The date time provider
      runtimeEntityRegistry - The entity registry
      beanContext - The bean context
      conversionService - The conversion service
      attributeConverterRegistry - The attribute converter registry
      jsonMapper - The JSON mapper
      sqlJsonColumnMapperProvider - The SQL JSON column mapper provider
  • Method Details

    • createColumnNameResultSetReaderWithColumnExistenceAware

      protected ResultReader<RS,String> createColumnNameResultSetReaderWithColumnExistenceAware()
      Returns:
      The result reader that will check for the column existence and return null for ResultReader.readDynamic(Object, Object, DataType)
    • decorate

      public <E, R> PreparedQuery<E,R> decorate(PreparedQuery<E,R> preparedQuery)
      Description copied from interface: PreparedQueryDecorator
      Decorate prepared query.
      Specified by:
      decorate in interface PreparedQueryDecorator
      Type Parameters:
      E - The entity type
      R - 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 interface MethodContextAwareStoredQueryDecorator
      Type Parameters:
      E - The entity type
      R - The result type
      Parameters:
      context - The context
      storedQuery - 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 Exc
      Prepare a statement for execution.
      Type Parameters:
      T - The query declaring type
      R - The query result type
      Parameters:
      statementFunction - The statement function
      preparedQuery - The prepared query
      isUpdate - Is this an update
      isSingleResult - Is it a single result
      Returns:
      The prepared statement
      Throws:
      Exc extends Exception
    • 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 statement
      index - The index
      dataType - The data type
      jsonDataType - The JSON representation type if data type is JSON
      value - The value
      storedQuery - 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 metadata
      repositoryType - The repository type
      rootEntity - The root entity
      persistentEntity - 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 type
      persistentEntity - The entity
      association - 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 metadata
      repositoryType - The repository type
      rootEntity - The root entity
      persistentEntity - 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 type
      association - The association
      persistentEntity - The persistent entity
      entity - The entity
      Returns:
      The operation
    • getSqlPreparedQuery

      protected final <E, R> SqlPreparedQuery<E,R> getSqlPreparedQuery(PreparedQuery<E,R> preparedQuery)
    • getSqlStoredQuery

      protected final <E, R> SqlStoredQuery<E,R> getSqlStoredQuery(StoredQuery<E,R> storedQuery)
    • isSupportsBatchInsert

      protected boolean isSupportsBatchInsert(PersistentEntity persistentEntity, SqlStoredQuery<?,?> sqlStoredQuery)
      Does supports batch for update queries.
      Parameters:
      persistentEntity - The persistent entity
      sqlStoredQuery - The sqlStoredQuery
      Returns:
      true if supported
    • isSupportsBatchInsert

      protected boolean isSupportsBatchInsert(PersistentEntity persistentEntity, Dialect dialect)
      Does supports batch for update queries.
      Parameters:
      persistentEntity - The persistent entity
      dialect - The dialect
      Returns:
      true if supported
    • isSupportsBatchUpdate

      protected boolean isSupportsBatchUpdate(PersistentEntity persistentEntity, SqlStoredQuery<?,?> sqlStoredQuery)
      Does supports batch for update queries.
      Parameters:
      persistentEntity - The persistent entity
      sqlStoredQuery - The sqlStoredQuery
      Returns:
      true if supported
    • isSupportsBatchDelete

      protected boolean isSupportsBatchDelete(PersistentEntity persistentEntity, Dialect dialect)
      Does supports batch for delete queries.
      Parameters:
      persistentEntity - The persistent entity
      dialect - The dialect
      Returns:
      true if supported
    • createQueryResultMapper

      protected final <T, R> SqlTypeMapper<RS,R> createQueryResultMapper(SqlStoredQuery<?,?> sqlStoredQuery, String columnName, JsonDataType jsonDataType, Class<RS> resultSetType, RuntimePersistentEntity<T> persistentEntity, BiFunction<RuntimePersistentEntity<Object>,Object,Object> loadListener)
      Creates SqlTypeMapper 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 type
      R - the result type
      Parameters:
      sqlStoredQuery - the SQL prepared query
      columnName - the column name where we are reading from
      jsonDataType - the JSON representation type
      resultSetType - resultSetType the result set type (different for R2DBC and JDBC)
      persistentEntity - the persistent entity
      loadListener - the load listener if needed after entity loaded
      Returns:
      the SqlTypeMapper able to decode from column value into given type
    • isJsonResult

      protected final boolean isJsonResult(StoredQuery<?,?> preparedQuery, QueryResultInfo queryResultInfo)
      Return an indicator telling whether prepared query result produces JSON result.
      Parameters:
      preparedQuery - the prepared query
      queryResultInfo - 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 query
      persistentEntity - the persistent entity
      Returns:
      true if entity being inserted is JSON entity representation with auto generated numeric id
    • getJsonColumn

      protected final String getJsonColumn(QueryResultInfo queryResultInfo)
      Gets column name for JSON result. If QueryResult annotation is present, takes column value from there, otherwise defaults to 'DATA' column name.
      Parameters:
      queryResultInfo - the query result info from the QueryResult annotation, null if annotation not present
      Returns:
      the JSON column name
    • getJsonDataType

      protected final JsonDataType getJsonDataType(QueryResultInfo queryResultInfo)
      Gets JSON data type for JSON result. If QueryResult annotation is present, takes data type value from there, otherwise defaults to JsonDataType.DEFAULT.
      Parameters:
      queryResultInfo - the query result info from the QueryResult annotation, null if annotation not present
      Returns:
      the JSON data type
    • getFirstResultSetIndex

      protected abstract Integer getFirstResultSetIndex()
      Returns:
      The first result set index.
      Since:
      4.2.0
    • createTupleMapper

      protected abstract SqlTypeMapper<RS,jakarta.persistence.Tuple> createTupleMapper()
    • createMapper

      protected <E, R> SqlTypeMapper<RS,R> createMapper(SqlStoredQuery<E,R> preparedQuery, Class<RS> rsType)
      Creates a result mapper.
      Type Parameters:
      E - The entity type
      R - The result type
      Parameters:
      preparedQuery - The prepared query
      rsType - The result set type
      Returns:
      The new mapper
      Since:
      4.2.0