|
| 1 | +/* |
| 2 | + * Copyright 2025 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.mongodb.aot.generated; |
| 17 | + |
| 18 | +import java.lang.reflect.Parameter; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.springframework.data.mongodb.BindableMongoExpression; |
| 24 | +import org.springframework.data.mongodb.core.ExecutableFindOperation.TerminatingFind; |
| 25 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 26 | +import org.springframework.data.mongodb.core.query.BasicQuery; |
| 27 | +import org.springframework.data.mongodb.repository.Hint; |
| 28 | +import org.springframework.data.mongodb.repository.ReadPreference; |
| 29 | +import org.springframework.data.repository.aot.generate.AotRepositoryMethodBuilder.MethodGenerationMetadata; |
| 30 | +import org.springframework.data.repository.core.RepositoryInformation; |
| 31 | +import org.springframework.data.support.PageableExecutionUtils; |
| 32 | +import org.springframework.javapoet.CodeBlock; |
| 33 | +import org.springframework.javapoet.CodeBlock.Builder; |
| 34 | +import org.springframework.javapoet.TypeName; |
| 35 | +import org.springframework.util.ObjectUtils; |
| 36 | +import org.springframework.util.StringUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Christoph Strobl |
| 40 | + */ |
| 41 | +public class MongoBlocks { |
| 42 | + |
| 43 | + public static QueryBlockBuilder queryBlockBuilder(RepositoryInformation repositoryInformation, |
| 44 | + MethodGenerationMetadata metadata) { |
| 45 | + return new QueryBlockBuilder(repositoryInformation, metadata); |
| 46 | + } |
| 47 | + |
| 48 | + public static QueryExecutionBlockBuilder queryExecutionBlockBuilder(RepositoryInformation repositoryInformation, |
| 49 | + MethodGenerationMetadata metadata) { |
| 50 | + return new QueryExecutionBlockBuilder(repositoryInformation, metadata); |
| 51 | + } |
| 52 | + |
| 53 | + static class QueryExecutionBlockBuilder { |
| 54 | + |
| 55 | + RepositoryInformation repositoryInformation; |
| 56 | + MethodGenerationMetadata metadata; |
| 57 | + |
| 58 | + public QueryExecutionBlockBuilder(RepositoryInformation repositoryInformation, MethodGenerationMetadata metadata) { |
| 59 | + this.repositoryInformation = repositoryInformation; |
| 60 | + this.metadata = metadata; |
| 61 | + } |
| 62 | + |
| 63 | + CodeBlock build(String queryVariableName) { |
| 64 | + |
| 65 | + String mongoOpsRef = metadata.fieldNameOf(MongoOperations.class); |
| 66 | + |
| 67 | + Builder builder = CodeBlock.builder(); |
| 68 | + |
| 69 | + boolean isProjecting = metadata.getActualReturnType() != null && !ObjectUtils |
| 70 | + .nullSafeEquals(TypeName.get(repositoryInformation.getDomainType()), metadata.getActualReturnType()); |
| 71 | + Object actualReturnType = isProjecting ? metadata.getActualReturnType() : repositoryInformation.getDomainType(); |
| 72 | + |
| 73 | + if (isProjecting) { |
| 74 | + builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class).matching($L)", TerminatingFind.class, |
| 75 | + actualReturnType, mongoOpsRef, repositoryInformation.getDomainType(), actualReturnType, queryVariableName); |
| 76 | + } else { |
| 77 | + builder.addStatement("$T<$T> finder = $L.query($T.class).matching($L)", TerminatingFind.class, actualReturnType, |
| 78 | + mongoOpsRef, repositoryInformation.getDomainType(), queryVariableName); |
| 79 | + } |
| 80 | + |
| 81 | + String terminatingMethod = "all()"; |
| 82 | + if (metadata.returnsSingleValue()) { |
| 83 | + if (metadata.returnsOptionalValue()) { |
| 84 | + terminatingMethod = "one()"; |
| 85 | + } else { |
| 86 | + terminatingMethod = "oneValue()"; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!metadata.returnsPage()) { |
| 91 | + builder.addStatement("return finder.$L", terminatingMethod); |
| 92 | + } else { |
| 93 | + builder.addStatement("return $T.getPage(finder.$L, $L, () -> finder.count())", PageableExecutionUtils.class, terminatingMethod, |
| 94 | + metadata.getPageableParameterName()); |
| 95 | + } |
| 96 | + |
| 97 | + return builder.build(); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + static class QueryBlockBuilder { |
| 103 | + |
| 104 | + MethodGenerationMetadata metadata; |
| 105 | + String queryString; |
| 106 | + List<String> arguments; |
| 107 | + // MongoParameters argumentSource; |
| 108 | + |
| 109 | + public QueryBlockBuilder(RepositoryInformation repositoryInformation, MethodGenerationMetadata metadata) { |
| 110 | + this.metadata = metadata; |
| 111 | + this.arguments = Arrays.stream(metadata.getRepositoryMethod().getParameters()).map(Parameter::getName) |
| 112 | + .collect(Collectors.toList()); |
| 113 | + |
| 114 | + // ParametersSource parametersSource = ParametersSource.of(repositoryInformation, metadata.getRepositoryMethod()); |
| 115 | + // this.argumentSource = new MongoParameters(parametersSource, false); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + public QueryBlockBuilder filter(String filter) { |
| 120 | + this.queryString = filter; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + CodeBlock build(String queryVariableName) { |
| 125 | + |
| 126 | + String mongoOpsRef = metadata.fieldNameOf(MongoOperations.class); |
| 127 | + |
| 128 | + CodeBlock.Builder builder = CodeBlock.builder(); |
| 129 | + |
| 130 | + builder.addStatement("$T filter = new $T($S, $L.getConverter(), new $T[]{ $L })", BindableMongoExpression.class, |
| 131 | + BindableMongoExpression.class, queryString, mongoOpsRef, Object.class, |
| 132 | + StringUtils.collectionToCommaDelimitedString(arguments)); |
| 133 | + builder.addStatement("$T $L = new $T(filter.toDocument())", |
| 134 | + org.springframework.data.mongodb.core.query.Query.class, queryVariableName, BasicQuery.class); |
| 135 | + |
| 136 | + String sortParameter = metadata.getSortParameterName(); |
| 137 | + if (StringUtils.hasText(sortParameter)) { |
| 138 | + builder.addStatement("$L.with($L)", queryVariableName, sortParameter); |
| 139 | + } |
| 140 | + |
| 141 | + String limitParameter = metadata.getLimitParameterName(); |
| 142 | + if (StringUtils.hasText(limitParameter)) { |
| 143 | + builder.addStatement("$L.limit($L)", queryVariableName, limitParameter); |
| 144 | + } |
| 145 | + |
| 146 | + String pageableParameter = metadata.getPageableParameterName(); |
| 147 | + if (StringUtils.hasText(pageableParameter)) { |
| 148 | + builder.addStatement("$L.with($L)", queryVariableName, pageableParameter); |
| 149 | + } |
| 150 | + |
| 151 | + String hint = metadata.annotationValue(Hint.class, "value"); |
| 152 | + |
| 153 | + if (StringUtils.hasText(hint)) { |
| 154 | + builder.addStatement("$L.withHint($S)", queryVariableName, hint); |
| 155 | + } |
| 156 | + |
| 157 | + String readPreference = metadata.annotationValue(ReadPreference.class, "value"); |
| 158 | + if (StringUtils.hasText(readPreference)) { |
| 159 | + builder.addStatement("$L.withReadPreference($T.valueOf($S))", queryVariableName, |
| 160 | + com.mongodb.ReadPreference.class, readPreference); |
| 161 | + } |
| 162 | + |
| 163 | + // TODO: all the meta stuff |
| 164 | + |
| 165 | + return builder.build(); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments