|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://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.jdbc.repository.query; |
| 17 | + |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import org.reactivestreams.Publisher; |
| 22 | +import org.springframework.core.convert.converter.Converter; |
| 23 | +import org.springframework.data.convert.EntityInstantiators; |
| 24 | +import org.springframework.data.jdbc.core.function.DatabaseClient; |
| 25 | +import org.springframework.data.jdbc.core.function.DatabaseClient.GenericExecuteSpec; |
| 26 | +import org.springframework.data.jdbc.core.function.FetchSpec; |
| 27 | +import org.springframework.data.jdbc.core.function.MappingR2dbcConverter; |
| 28 | +import org.springframework.data.jdbc.repository.query.R2dbcQueryExecution.ResultProcessingConverter; |
| 29 | +import org.springframework.data.jdbc.repository.query.R2dbcQueryExecution.ResultProcessingExecution; |
| 30 | +import org.springframework.data.repository.query.ParameterAccessor; |
| 31 | +import org.springframework.data.repository.query.RepositoryQuery; |
| 32 | +import org.springframework.data.repository.query.ResultProcessor; |
| 33 | +import org.springframework.data.repository.query.ReturnedType; |
| 34 | +import org.springframework.util.Assert; |
| 35 | + |
| 36 | +/** |
| 37 | + * Base class for reactive {@link RepositoryQuery} implementations for R2DBC. |
| 38 | + * |
| 39 | + * @author Mark Paluch |
| 40 | + */ |
| 41 | +public abstract class AbstractR2dbcQuery implements RepositoryQuery { |
| 42 | + |
| 43 | + private final R2dbcQueryMethod method; |
| 44 | + private final DatabaseClient databaseClient; |
| 45 | + private final MappingR2dbcConverter converter; |
| 46 | + private final EntityInstantiators instantiators; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new {@link AbstractR2dbcQuery} from the given {@link R2dbcQueryMethod} and {@link DatabaseClient}. |
| 50 | + * |
| 51 | + * @param method must not be {@literal null}. |
| 52 | + * @param databaseClient must not be {@literal null}. |
| 53 | + * @param converter must not be {@literal null}. |
| 54 | + */ |
| 55 | + public AbstractR2dbcQuery(R2dbcQueryMethod method, DatabaseClient databaseClient, MappingR2dbcConverter converter) { |
| 56 | + |
| 57 | + Assert.notNull(method, "R2dbcQueryMethod must not be null!"); |
| 58 | + Assert.notNull(databaseClient, "DatabaseClient must not be null!"); |
| 59 | + Assert.notNull(converter, "MappingR2dbcConverter must not be null!"); |
| 60 | + |
| 61 | + this.method = method; |
| 62 | + this.databaseClient = databaseClient; |
| 63 | + this.converter = converter; |
| 64 | + this.instantiators = new EntityInstantiators(); |
| 65 | + } |
| 66 | + |
| 67 | + /* |
| 68 | + * (non-Javadoc) |
| 69 | + * @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod() |
| 70 | + */ |
| 71 | + public R2dbcQueryMethod getQueryMethod() { |
| 72 | + return method; |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * (non-Javadoc) |
| 77 | + * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[]) |
| 78 | + */ |
| 79 | + public Object execute(Object[] parameters) { |
| 80 | + |
| 81 | + return method.hasReactiveWrapperParameter() ? executeDeferred(parameters) |
| 82 | + : execute(new JdbcParametersParameterAccessor(method, parameters)); |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + private Object executeDeferred(Object[] parameters) { |
| 87 | + |
| 88 | + R2dbcParameterAccessor parameterAccessor = new R2dbcParameterAccessor(method, parameters); |
| 89 | + |
| 90 | + if (getQueryMethod().isCollectionQuery()) { |
| 91 | + return Flux.defer(() -> (Publisher<Object>) execute(parameterAccessor)); |
| 92 | + } |
| 93 | + |
| 94 | + return Mono.defer(() -> (Mono<Object>) execute(parameterAccessor)); |
| 95 | + } |
| 96 | + |
| 97 | + private Object execute(JdbcParameterAccessor parameterAccessor) { |
| 98 | + |
| 99 | + // TODO: ConvertingParameterAccessor |
| 100 | + BindableQuery query = createQuery(parameterAccessor); |
| 101 | + |
| 102 | + ResultProcessor processor = method.getResultProcessor().withDynamicProjection(parameterAccessor); |
| 103 | + GenericExecuteSpec boundQuery = query.bind(databaseClient.execute().sql(query)); |
| 104 | + FetchSpec<?> fetchSpec = boundQuery.as(resolveResultType(processor)).fetch(); |
| 105 | + |
| 106 | + String tableName = method.getEntityInformation().getTableName(); |
| 107 | + |
| 108 | + R2dbcQueryExecution execution = getExecution( |
| 109 | + new ResultProcessingConverter(processor, converter.getMappingContext(), instantiators)); |
| 110 | + |
| 111 | + return execution.execute(fetchSpec, processor.getReturnedType().getDomainType(), tableName); |
| 112 | + } |
| 113 | + |
| 114 | + private Class<?> resolveResultType(ResultProcessor resultProcessor) { |
| 115 | + |
| 116 | + ReturnedType returnedType = resultProcessor.getReturnedType(); |
| 117 | + |
| 118 | + return returnedType.isProjecting() ? returnedType.getDomainType() : returnedType.getReturnedType(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns the execution instance to use. |
| 123 | + * |
| 124 | + * @param resultProcessing must not be {@literal null}. |
| 125 | + * @return |
| 126 | + */ |
| 127 | + private R2dbcQueryExecution getExecution(Converter<Object, Object> resultProcessing) { |
| 128 | + return new ResultProcessingExecution(getExecutionToWrap(), resultProcessing); |
| 129 | + } |
| 130 | + |
| 131 | + private R2dbcQueryExecution getExecutionToWrap() { |
| 132 | + |
| 133 | + if (method.isCollectionQuery()) { |
| 134 | + return (q, t, c) -> q.all(); |
| 135 | + } |
| 136 | + |
| 137 | + return (q, t, c) -> q.one(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Creates a {@link BindableQuery} instance using the given {@link ParameterAccessor} |
| 142 | + * |
| 143 | + * @param accessor must not be {@literal null}. |
| 144 | + * @return |
| 145 | + */ |
| 146 | + protected abstract BindableQuery createQuery(JdbcParameterAccessor accessor); |
| 147 | +} |
0 commit comments