Search in sources :

Example 1 with DBOperation

use of io.micronaut.data.runtime.operations.internal.DBOperation in project micronaut-data by micronaut-projects.

the class DefaultJdbcRepositoryOperations method updateAll.

@NonNull
@Override
public <T> Iterable<T> updateAll(@NonNull UpdateBatchOperation<T> operation) {
    return executeWrite(connection -> {
        final AnnotationMetadata annotationMetadata = operation.getAnnotationMetadata();
        final Class<?> repositoryType = operation.getRepositoryType();
        SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
        final RuntimePersistentEntity<T> persistentEntity = getEntity(operation.getRootEntity());
        DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
        JdbcOperationContext ctx = new JdbcOperationContext(annotationMetadata, repositoryType, queryBuilder.dialect(), connection);
        if (!isSupportsBatchUpdate(persistentEntity, queryBuilder.dialect())) {
            return operation.split().stream().map(updateOp -> {
                JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, persistentEntity, updateOp.getEntity(), dbOperation);
                op.update();
                return op.getEntity();
            }).collect(Collectors.toList());
        }
        JdbcEntitiesOperations<T> op = new JdbcEntitiesOperations<>(ctx, persistentEntity, operation, dbOperation);
        op.update();
        return op.getEntities();
    });
}
Also used : DataType(io.micronaut.data.model.DataType) Arrays(java.util.Arrays) ExecutorReactiveOperations(io.micronaut.data.runtime.operations.ExecutorReactiveOperations) Internal(io.micronaut.core.annotation.Internal) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataJdbcConfiguration(io.micronaut.data.jdbc.config.DataJdbcConfiguration) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Executors(java.util.concurrent.Executors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) Serializable(java.io.Serializable) Stream(java.util.stream.Stream) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) Parameter(io.micronaut.context.annotation.Parameter) SqlDTOMapper(io.micronaut.data.runtime.mapper.sql.SqlDTOMapper) BeanProperty(io.micronaut.core.beans.BeanProperty) ArrayList(java.util.ArrayList) TypeMapper(io.micronaut.data.runtime.mapper.TypeMapper) SQLException(java.sql.SQLException) OperationContext(io.micronaut.data.runtime.operations.internal.OperationContext) Nullable(io.micronaut.core.annotation.Nullable) ReactiveCapableRepository(io.micronaut.data.operations.reactive.ReactiveCapableRepository) ExecutorAsyncOperations(io.micronaut.data.runtime.operations.ExecutorAsyncOperations) StreamSupport(java.util.stream.StreamSupport) Argument(io.micronaut.core.type.Argument) AbstractConversionContext(io.micronaut.data.runtime.support.AbstractConversionContext) JdbcConversionContext(io.micronaut.data.jdbc.convert.JdbcConversionContext) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) ReactiveRepositoryOperations(io.micronaut.data.operations.reactive.ReactiveRepositoryOperations) StoredQuerySqlOperation(io.micronaut.data.runtime.operations.internal.StoredQuerySqlOperation) InsertBatchOperation(io.micronaut.data.model.runtime.InsertBatchOperation) JoinPath(io.micronaut.data.model.query.JoinPath) Statement(java.sql.Statement) InsertOperation(io.micronaut.data.model.runtime.InsertOperation) AbstractSyncEntitiesOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntitiesOperations) UpdateBatchOperation(io.micronaut.data.model.runtime.UpdateBatchOperation) UpdateOperation(io.micronaut.data.model.runtime.UpdateOperation) RuntimePersistentPropertyConversionContext(io.micronaut.data.runtime.convert.RuntimePersistentPropertyConversionContext) Connection(java.sql.Connection) BeanContext(io.micronaut.context.BeanContext) Spliterators(java.util.Spliterators) LoggerFactory(org.slf4j.LoggerFactory) DataSourceUtils(io.micronaut.transaction.jdbc.DataSourceUtils) PreDestroy(javax.annotation.PreDestroy) EachBean(io.micronaut.context.annotation.EachBean) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) DataAccessException(io.micronaut.data.exceptions.DataAccessException) DeleteOperation(io.micronaut.data.model.runtime.DeleteOperation) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) AsyncCapableRepository(io.micronaut.data.operations.async.AsyncCapableRepository) DTOMapper(io.micronaut.data.runtime.mapper.DTOMapper) ConnectionCallback(io.micronaut.data.jdbc.runtime.ConnectionCallback) Predicate(java.util.function.Predicate) SqlResultConsumer(io.micronaut.data.jdbc.mapper.SqlResultConsumer) AbstractSqlRepositoryOperations(io.micronaut.data.runtime.operations.internal.AbstractSqlRepositoryOperations) PreparedStatementCallback(io.micronaut.data.jdbc.runtime.PreparedStatementCallback) Collectors(java.util.stream.Collectors) StoredSqlOperation(io.micronaut.data.runtime.operations.internal.StoredSqlOperation) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) SqlTypeMapper(io.micronaut.data.runtime.mapper.sql.SqlTypeMapper) Spliterator(java.util.Spliterator) Named(jakarta.inject.Named) ColumnIndexResultSetReader(io.micronaut.data.jdbc.mapper.ColumnIndexResultSetReader) QueryParameterBinding(io.micronaut.data.model.runtime.QueryParameterBinding) TransactionOperations(io.micronaut.transaction.TransactionOperations) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) Function(java.util.function.Function) DelegatingDataSource(io.micronaut.transaction.jdbc.DelegatingDataSource) JdbcQueryStatement(io.micronaut.data.jdbc.mapper.JdbcQueryStatement) Page(io.micronaut.data.model.Page) DataSource(javax.sql.DataSource) OpContext(io.micronaut.data.runtime.operations.internal.OpContext) NoSuchElementException(java.util.NoSuchElementException) DateTimeProvider(io.micronaut.data.runtime.date.DateTimeProvider) ExecutorService(java.util.concurrent.ExecutorService) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AbstractSyncEntityOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntityOperations) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) ResultConsumer(io.micronaut.data.runtime.mapper.ResultConsumer) SyncCascadeOperations(io.micronaut.data.runtime.operations.internal.SyncCascadeOperations) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) ConversionContext(io.micronaut.core.convert.ConversionContext) ColumnNameResultSetReader(io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader) DeleteBatchOperation(io.micronaut.data.model.runtime.DeleteBatchOperation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) ResultReader(io.micronaut.data.runtime.mapper.ResultReader) SqlResultEntityTypeMapper(io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation) StoredQuerySqlOperation(io.micronaut.data.runtime.operations.internal.StoredQuerySqlOperation) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with DBOperation

use of io.micronaut.data.runtime.operations.internal.DBOperation in project micronaut-data by micronaut-projects.

the class DefaultJdbcRepositoryOperations method update.

@NonNull
@Override
public <T> T update(@NonNull UpdateOperation<T> operation) {
    final AnnotationMetadata annotationMetadata = operation.getAnnotationMetadata();
    final Class<?> repositoryType = operation.getRepositoryType();
    SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
    DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
    return executeWrite(connection -> {
        JdbcOperationContext ctx = new JdbcOperationContext(annotationMetadata, repositoryType, queryBuilder.dialect(), connection);
        JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, getEntity(operation.getRootEntity()), operation.getEntity(), dbOperation);
        op.update();
        return op.getEntity();
    });
}
Also used : DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) StoredQuerySqlOperation(io.micronaut.data.runtime.operations.internal.StoredQuerySqlOperation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull)

Example 3 with DBOperation

use of io.micronaut.data.runtime.operations.internal.DBOperation in project micronaut-data by micronaut-projects.

the class DefaultR2dbcRepositoryOperations method updateOne.

@Override
public <T> Mono<T> updateOne(R2dbcOperationContext ctx, T value, RuntimePersistentEntity<T> persistentEntity) {
    DBOperation childSqlUpdateOperation = resolveEntityUpdate(ctx.annotationMetadata, ctx.repositoryType, value.getClass(), persistentEntity);
    R2dbcEntityOperations<T> op = new R2dbcEntityOperations<>(ctx, persistentEntity, value, childSqlUpdateOperation);
    op.update();
    return op.getEntity();
}
Also used : DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation)

Example 4 with DBOperation

use of io.micronaut.data.runtime.operations.internal.DBOperation in project micronaut-data by micronaut-projects.

the class DefaultR2dbcRepositoryOperations method persistManyAssociation.

@Override
public Mono<Void> persistManyAssociation(R2dbcOperationContext ctx, RuntimeAssociation runtimeAssociation, Object value, RuntimePersistentEntity<Object> persistentEntity, Object child, RuntimePersistentEntity<Object> childPersistentEntity) {
    DBOperation dbInsertOperation = resolveSqlInsertAssociation(ctx.repositoryType, ctx.dialect, runtimeAssociation, persistentEntity, value);
    R2dbcEntityOperations<Object> assocEntityOp = new R2dbcEntityOperations<>(ctx, childPersistentEntity, child, dbInsertOperation);
    try {
        assocEntityOp.execute();
    } catch (Exception e1) {
        throw new DataAccessException("SQL error executing INSERT: " + e1.getMessage(), e1);
    }
    return assocEntityOp.getEntity().then();
}
Also used : DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation) NoTransactionException(io.micronaut.transaction.exceptions.NoTransactionException) DataAccessException(io.micronaut.data.exceptions.DataAccessException) TransactionUsageException(io.micronaut.transaction.exceptions.TransactionUsageException) NonUniqueResultException(io.micronaut.data.exceptions.NonUniqueResultException) TransactionSystemException(io.micronaut.transaction.exceptions.TransactionSystemException) DataAccessException(io.micronaut.data.exceptions.DataAccessException)

Example 5 with DBOperation

use of io.micronaut.data.runtime.operations.internal.DBOperation in project micronaut-data by micronaut-projects.

the class DefaultR2dbcRepositoryOperations method persistBatch.

@Override
public <T> Flux<T> persistBatch(R2dbcOperationContext ctx, Iterable<T> values, RuntimePersistentEntity<T> persistentEntity, Predicate<T> predicate) {
    DBOperation childSqlPersistOperation = resolveEntityInsert(ctx.annotationMetadata, ctx.repositoryType, persistentEntity.getIntrospection().getBeanType(), persistentEntity);
    R2dbcEntitiesOperations<T> op = new R2dbcEntitiesOperations<>(ctx, childSqlPersistOperation, persistentEntity, values, true);
    if (predicate != null) {
        op.veto(predicate);
    }
    op.persist();
    return op.getEntities();
}
Also used : DBOperation(io.micronaut.data.runtime.operations.internal.DBOperation)

Aggregations

DBOperation (io.micronaut.data.runtime.operations.internal.DBOperation)13 DataAccessException (io.micronaut.data.exceptions.DataAccessException)6 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 NonNull (io.micronaut.core.annotation.NonNull)4 SqlQueryBuilder (io.micronaut.data.model.query.builder.sql.SqlQueryBuilder)4 BeanContext (io.micronaut.context.BeanContext)3 EachBean (io.micronaut.context.annotation.EachBean)3 Parameter (io.micronaut.context.annotation.Parameter)3 Internal (io.micronaut.core.annotation.Internal)3 Nullable (io.micronaut.core.annotation.Nullable)3 BeanProperty (io.micronaut.core.beans.BeanProperty)3 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)3 ConversionContext (io.micronaut.core.convert.ConversionContext)3 Argument (io.micronaut.core.type.Argument)3 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)3 DataJdbcConfiguration (io.micronaut.data.jdbc.config.DataJdbcConfiguration)3 JdbcConversionContext (io.micronaut.data.jdbc.convert.JdbcConversionContext)3 ColumnIndexResultSetReader (io.micronaut.data.jdbc.mapper.ColumnIndexResultSetReader)3 ColumnNameResultSetReader (io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader)3 JdbcQueryStatement (io.micronaut.data.jdbc.mapper.JdbcQueryStatement)3