|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.r2dbc.core; |
| 17 | + |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import org.springframework.dao.DataAccessException; |
| 22 | +import org.springframework.dao.TransientDataAccessResourceException; |
| 23 | +import org.springframework.data.r2dbc.query.Query; |
| 24 | +import org.springframework.data.r2dbc.query.Update; |
| 25 | + |
| 26 | +/** |
| 27 | + * Interface specifying a basic set of reactive R2DBC operations using entities. Implemented by |
| 28 | + * {@link R2dbcEntityTemplate}. Not often used directly, but a useful option to enhance testability, as it can easily be |
| 29 | + * mocked or stubbed. |
| 30 | + * |
| 31 | + * @author Mark Paluch |
| 32 | + * @since 1.1 |
| 33 | + * @see DatabaseClient |
| 34 | + */ |
| 35 | +public interface R2dbcEntityOperations extends FluentR2dbcOperations { |
| 36 | + |
| 37 | + /** |
| 38 | + * Expose the underlying {@link DatabaseClient} to allow SQL operations. |
| 39 | + * |
| 40 | + * @return the underlying {@link DatabaseClient}. |
| 41 | + * @see DatabaseClient |
| 42 | + */ |
| 43 | + DatabaseClient getDatabaseClient(); |
| 44 | + |
| 45 | + // ------------------------------------------------------------------------- |
| 46 | + // Methods dealing with org.springframework.data.r2dbc.query.Query |
| 47 | + // ------------------------------------------------------------------------- |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the number of rows for the given entity class applying {@link Query}. This overridden method allows users |
| 51 | + * to further refine the selection Query using a {@link Query} predicate to determine how many entities of the given |
| 52 | + * {@link Class type} match the Query. |
| 53 | + * |
| 54 | + * @param query user-defined count {@link Query} to execute; must not be {@literal null}. |
| 55 | + * @param entityClass {@link Class type} of the entity; must not be {@literal null}. |
| 56 | + * @return the number of existing entities. |
| 57 | + * @throws DataAccessException if any problem occurs while executing the query. |
| 58 | + */ |
| 59 | + Mono<Long> count(Query query, Class<?> entityClass) throws DataAccessException; |
| 60 | + |
| 61 | + /** |
| 62 | + * Determine whether the result for {@code entityClass} {@link Query} yields at least one row. |
| 63 | + * |
| 64 | + * @param query user-defined exists {@link Query} to execute; must not be {@literal null}. |
| 65 | + * @param entityClass {@link Class type} of the entity; must not be {@literal null}. |
| 66 | + * @return {@literal true} if the object exists. |
| 67 | + * @throws DataAccessException if any problem occurs while executing the query. |
| 68 | + * @since 2.1 |
| 69 | + */ |
| 70 | + Mono<Boolean> exists(Query query, Class<?> entityClass) throws DataAccessException; |
| 71 | + |
| 72 | + /** |
| 73 | + * Execute a {@code SELECT} query and convert the resulting items to a stream of entities. |
| 74 | + * |
| 75 | + * @param query must not be {@literal null}. |
| 76 | + * @param entityClass The entity type must not be {@literal null}. |
| 77 | + * @return the result objects returned by the action. |
| 78 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 79 | + */ |
| 80 | + <T> Flux<T> select(Query query, Class<T> entityClass) throws DataAccessException; |
| 81 | + |
| 82 | + /** |
| 83 | + * Execute a {@code SELECT} query and convert the resulting item to an entity. |
| 84 | + * |
| 85 | + * @param query must not be {@literal null}. |
| 86 | + * @param entityClass The entity type must not be {@literal null}. |
| 87 | + * @return the result object returned by the action or {@link Mono#empty()}. |
| 88 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 89 | + */ |
| 90 | + <T> Mono<T> selectOne(Query query, Class<T> entityClass) throws DataAccessException; |
| 91 | + |
| 92 | + /** |
| 93 | + * Update the queried entities and return {@literal true} if the update was applied. |
| 94 | + * |
| 95 | + * @param query must not be {@literal null}. |
| 96 | + * @param update must not be {@literal null}. |
| 97 | + * @param entityClass The entity type must not be {@literal null}. |
| 98 | + * @return the number of affected rows. |
| 99 | + * @throws DataAccessException if there is any problem executing the query. |
| 100 | + */ |
| 101 | + Mono<Integer> update(Query query, Update update, Class<?> entityClass) throws DataAccessException; |
| 102 | + |
| 103 | + /** |
| 104 | + * Remove entities (rows)/columns from the table by {@link Query}. |
| 105 | + * |
| 106 | + * @param query must not be {@literal null}. |
| 107 | + * @param entityClass The entity type must not be {@literal null}. |
| 108 | + * @return the number of affected rows. |
| 109 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 110 | + */ |
| 111 | + Mono<Integer> delete(Query query, Class<?> entityClass) throws DataAccessException; |
| 112 | + |
| 113 | + // ------------------------------------------------------------------------- |
| 114 | + // Methods dealing with entities |
| 115 | + // ------------------------------------------------------------------------- |
| 116 | + |
| 117 | + /** |
| 118 | + * Insert the given entity and emit the entity if the insert was applied. |
| 119 | + * |
| 120 | + * @param entity The entity to insert, must not be {@literal null}. |
| 121 | + * @return the inserted entity. |
| 122 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 123 | + */ |
| 124 | + <T> Mono<T> insert(T entity) throws DataAccessException; |
| 125 | + |
| 126 | + /** |
| 127 | + * Update the given entity and emit the entity if the update was applied. |
| 128 | + * |
| 129 | + * @param entity The entity to update, must not be {@literal null}. |
| 130 | + * @return the updated entity. |
| 131 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 132 | + * @throws TransientDataAccessResourceException if the update did not affect any rows. |
| 133 | + */ |
| 134 | + <T> Mono<T> update(T entity) throws DataAccessException; |
| 135 | + |
| 136 | + /** |
| 137 | + * Delete the given entity and emit the entity if the delete was applied. |
| 138 | + * |
| 139 | + * @param entity must not be {@literal null}. |
| 140 | + * @return the deleted entity. |
| 141 | + * @throws DataAccessException if there is any problem issuing the execution. |
| 142 | + */ |
| 143 | + <T> Mono<T> delete(T entity) throws DataAccessException; |
| 144 | +} |
0 commit comments