All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.data.operations.RepositoryOperations Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.data.operations;

import io.micronaut.context.ApplicationContextProvider;
import io.micronaut.core.annotation.NextMajorVersion;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ConversionServiceProvider;
import io.micronaut.data.exceptions.DataAccessException;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.runtime.DeleteBatchOperation;
import io.micronaut.data.model.runtime.DeleteOperation;
import io.micronaut.data.model.runtime.InsertBatchOperation;
import io.micronaut.data.model.runtime.InsertOperation;
import io.micronaut.data.model.runtime.PagedQuery;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.data.model.runtime.UpdateBatchOperation;
import io.micronaut.data.model.runtime.UpdateOperation;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Common interface for repository implementations to implement.
 *
 * @author graemerocher
 * @since 1.0
 */
public interface RepositoryOperations extends HintsCapableRepository, ApplicationContextProvider, ConversionServiceProvider {

    /**
     * Retrieves the entity for the given type.
     * @param type The type
     * @param  The generic Type
     * @return The entity
     * @throws io.micronaut.core.beans.exceptions.IntrospectionException if no entity exists of the given type
     */
    default @NonNull  RuntimePersistentEntity getEntity(@NonNull Class type) {
        return PersistentEntity.of(type);
    }

    /**
     * Find one by ID.
     *
     * @param type The type
     * @param id The id
     * @param  The generic type
     * @return A result or null
     */
    @Nullable  T findOne(@NonNull Class type, @NonNull Object id);

    /**
     * Find one by Query.
     *
     * @param preparedQuery The prepared query
     * @param  The generic resultType
     * @param  The result type
     * @return A result or null
     */
    @Nullable  R findOne(@NonNull PreparedQuery preparedQuery);

    /**
     * Execute a query that checks for existence.
     *
     * @param preparedQuery The prepared query
     * @param  The generic resultType
     * @return A result or null
     */
    @NextMajorVersion("Switch boolean to ? as it doesn't require specific result")
     boolean exists(@NonNull PreparedQuery preparedQuery);

    /**
     * Finds all results for the given query.
     * @param query The root entity
     * @param  The generic type
     * @return An iterable result
     */
    @NonNull  Iterable findAll(
            @NonNull PagedQuery query
    );

    /**
     * Counts all results for the given query.
     * @param pagedQuery The paged query
     * @param  The generic type
     * @return An iterable result
     */
     long count(PagedQuery pagedQuery);

    /**
     * Finds all results for the given query.
     * @param preparedQuery The prepared query
     * @param  The entity type
     * @param  The result type
     * @return An iterable result
     */
    @NonNull  Iterable findAll(@NonNull PreparedQuery preparedQuery);


    /**
     * Finds all results for the given query.
     * @param preparedQuery The prepared query
     * @param  The entity type
     * @param  The result type
     * @return An iterable result
     */
    @NonNull  Stream findStream(@NonNull PreparedQuery preparedQuery);

    /**
     * Finds a stream for the given arguments.
     * @param query The query
     * @param  The generic type
     * @return The stream
     */
    @NonNull  Stream findStream(@NonNull PagedQuery query);

    /**
     * Find a page for the given entity and pageable.
     * @param query The query
     * @param  The entity generic type
     * @return The page type
     */
     Page findPage(@NonNull PagedQuery query);

    /**
     * Persist the operation returning a possibly new entity.
     * @param operation The operation
     * @param  The generic type
     * @return The operation
     */
    @NonNull  T persist(@NonNull InsertOperation operation);

    /**
     * Updates the entity for the given operation.
     *
     * @param operation The operation
     * @param  The generic type
     * @return The operation
     */
    @NonNull  T update(@NonNull UpdateOperation operation);

    /**
     * Updates the entities for the given operation.
     *
     * @param operation The operation
     * @param  The generic type
     * @return The updated entities
     */
    @NonNull
    default  Iterable updateAll(@NonNull UpdateBatchOperation operation) {
        return operation.split().stream()
                .map(this::update)
                .toList();
    }

    /**
     * Persist all the given entities.
     * @param operation The operation
     * @param  The generic type
     * @return The entities, possibly mutated
     */
    @NonNull
    default  Iterable persistAll(@NonNull InsertBatchOperation operation) {
        return operation.split().stream()
                .map(this::persist)
                .toList();
    }

    /**
     * Executes an update for the given query and parameter values. If it is possible to
     * return the number of objects updated, then do so.
     * @param preparedQuery The prepared query
     * @return An optional number with the count of the number of records updated
     */
    @NonNull
    Optional executeUpdate(@NonNull PreparedQuery preparedQuery);

    /**
     * Executes a delete for the given query and parameter values. If it is possible to
     * return the number of objects deleted, then do so.
     * @param preparedQuery The prepared query
     * @return An optional number with the count of the number of records updated
     */
    @NonNull
    default Optional executeDelete(@NonNull PreparedQuery preparedQuery) {
        return executeUpdate(preparedQuery);
    }

    /**
     * Executes the given query with parameter values returning a result.
     *
     * @param preparedQuery The prepared query
     * @param            The result type
     * @return The result
     * @since 4.2.0
     */
    @NonNull
    default  List execute(@NonNull PreparedQuery preparedQuery) {
        throw new DataAccessException("Current repository: " + getClass() + " doesn't support method 'execute'!");
    }

    /**
     * Deletes the entity.
     *
     * @param operation The operation
     * @param  The generic type
     * @return The number of entities deleted
     */
     int delete(@NonNull DeleteOperation operation);

    /**
     * Deletes all the entities of the given type.
     * @param operation The operation
     * @param  The generic type
     * @return The number of entities deleted
     */
     Optional deleteAll(@NonNull DeleteBatchOperation operation);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy