|
|
|
@ -28,8 +28,7 @@ import org.springframework.data.mongodb.repository.Hint; |
|
|
|
import org.springframework.data.mongodb.repository.ReadPreference; |
|
|
|
import org.springframework.data.mongodb.repository.ReadPreference; |
|
|
|
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagedExecution; |
|
|
|
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagedExecution; |
|
|
|
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.SlicedExecution; |
|
|
|
import org.springframework.data.mongodb.repository.query.MongoQueryExecution.SlicedExecution; |
|
|
|
import org.springframework.data.repository.aot.generate.AotRepositoryMethodBuilder.MethodGenerationMetadata; |
|
|
|
import org.springframework.data.repository.aot.generate.AotRepositoryMethodGenerationContext; |
|
|
|
import org.springframework.data.repository.core.RepositoryInformation; |
|
|
|
|
|
|
|
import org.springframework.javapoet.CodeBlock; |
|
|
|
import org.springframework.javapoet.CodeBlock; |
|
|
|
import org.springframework.javapoet.CodeBlock.Builder; |
|
|
|
import org.springframework.javapoet.CodeBlock.Builder; |
|
|
|
import org.springframework.javapoet.TypeName; |
|
|
|
import org.springframework.javapoet.TypeName; |
|
|
|
@ -41,65 +40,63 @@ import org.springframework.util.StringUtils; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MongoBlocks { |
|
|
|
public class MongoBlocks { |
|
|
|
|
|
|
|
|
|
|
|
public static QueryBlockBuilder queryBlockBuilder(RepositoryInformation repositoryInformation, |
|
|
|
public static QueryBlockBuilder queryBlockBuilder(AotRepositoryMethodGenerationContext context) { |
|
|
|
MethodGenerationMetadata metadata) { |
|
|
|
return new QueryBlockBuilder(context); |
|
|
|
return new QueryBlockBuilder(repositoryInformation, metadata); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static QueryExecutionBlockBuilder queryExecutionBlockBuilder(RepositoryInformation repositoryInformation, |
|
|
|
public static QueryExecutionBlockBuilder queryExecutionBlockBuilder(AotRepositoryMethodGenerationContext context) { |
|
|
|
MethodGenerationMetadata metadata) { |
|
|
|
return new QueryExecutionBlockBuilder(context); |
|
|
|
return new QueryExecutionBlockBuilder(repositoryInformation, metadata); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class QueryExecutionBlockBuilder { |
|
|
|
static class QueryExecutionBlockBuilder { |
|
|
|
|
|
|
|
|
|
|
|
RepositoryInformation repositoryInformation; |
|
|
|
AotRepositoryMethodGenerationContext context; |
|
|
|
MethodGenerationMetadata metadata; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public QueryExecutionBlockBuilder(RepositoryInformation repositoryInformation, MethodGenerationMetadata metadata) { |
|
|
|
public QueryExecutionBlockBuilder(AotRepositoryMethodGenerationContext context) { |
|
|
|
this.repositoryInformation = repositoryInformation; |
|
|
|
this.context = context; |
|
|
|
this.metadata = metadata; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CodeBlock build(String queryVariableName) { |
|
|
|
CodeBlock build(String queryVariableName) { |
|
|
|
|
|
|
|
|
|
|
|
String mongoOpsRef = metadata.fieldNameOf(MongoOperations.class); |
|
|
|
String mongoOpsRef = context.fieldNameOf(MongoOperations.class); |
|
|
|
|
|
|
|
|
|
|
|
Builder builder = CodeBlock.builder(); |
|
|
|
Builder builder = CodeBlock.builder(); |
|
|
|
|
|
|
|
|
|
|
|
boolean isProjecting = metadata.getActualReturnType() != null && !ObjectUtils |
|
|
|
boolean isProjecting = context.getActualReturnType() != null |
|
|
|
.nullSafeEquals(TypeName.get(repositoryInformation.getDomainType()), metadata.getActualReturnType()); |
|
|
|
&& !ObjectUtils.nullSafeEquals(TypeName.get(context.getRepositoryInformation().getDomainType()), |
|
|
|
Object actualReturnType = isProjecting ? metadata.getActualReturnType() : repositoryInformation.getDomainType(); |
|
|
|
context.getActualReturnType()); |
|
|
|
|
|
|
|
Object actualReturnType = isProjecting ? context.getActualReturnType() |
|
|
|
|
|
|
|
: context.getRepositoryInformation().getDomainType(); |
|
|
|
|
|
|
|
|
|
|
|
if (isProjecting) { |
|
|
|
if (isProjecting) { |
|
|
|
// builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class).matching($L)", TerminatingFind.class,
|
|
|
|
// builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class).matching($L)", TerminatingFind.class,
|
|
|
|
// actualReturnType, mongoOpsRef, repositoryInformation.getDomainType(), actualReturnType, queryVariableName);
|
|
|
|
// actualReturnType, mongoOpsRef, repositoryInformation.getDomainType(), actualReturnType, queryVariableName);
|
|
|
|
builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class)", FindWithQuery.class, actualReturnType, |
|
|
|
builder.addStatement("$T<$T> finder = $L.query($T.class).as($T.class)", FindWithQuery.class, actualReturnType, |
|
|
|
mongoOpsRef, repositoryInformation.getDomainType(), actualReturnType); |
|
|
|
mongoOpsRef, context.getRepositoryInformation().getDomainType(), actualReturnType); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// builder.addStatement("$T<$T> finder = $L.query($T.class).matching($L)", TerminatingFind.class,
|
|
|
|
// builder.addStatement("$T<$T> finder = $L.query($T.class).matching($L)", TerminatingFind.class,
|
|
|
|
// actualReturnType,
|
|
|
|
// actualReturnType,
|
|
|
|
// mongoOpsRef, repositoryInformation.getDomainType(), queryVariableName);
|
|
|
|
// mongoOpsRef, repositoryInformation.getDomainType(), queryVariableName);
|
|
|
|
builder.addStatement("$T<$T> finder = $L.query($T.class)", FindWithQuery.class, actualReturnType, mongoOpsRef, |
|
|
|
builder.addStatement("$T<$T> finder = $L.query($T.class)", FindWithQuery.class, actualReturnType, mongoOpsRef, |
|
|
|
repositoryInformation.getDomainType()); |
|
|
|
context.getRepositoryInformation().getDomainType()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String terminatingMethod = "all()"; |
|
|
|
String terminatingMethod = "all()"; |
|
|
|
if (metadata.returnsSingleValue()) { |
|
|
|
if (context.returnsSingleValue()) { |
|
|
|
if (metadata.returnsOptionalValue()) { |
|
|
|
if (context.returnsOptionalValue()) { |
|
|
|
terminatingMethod = "one()"; |
|
|
|
terminatingMethod = "one()"; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
terminatingMethod = "oneValue()"; |
|
|
|
terminatingMethod = "oneValue()"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (metadata.returnsPage()) { |
|
|
|
if (context.returnsPage()) { |
|
|
|
// builder.addStatement("return finder.$L", terminatingMethod);
|
|
|
|
// builder.addStatement("return finder.$L", terminatingMethod);
|
|
|
|
builder.addStatement("return new $T(finder, $L).execute($L)", PagedExecution.class, |
|
|
|
builder.addStatement("return new $T(finder, $L).execute($L)", PagedExecution.class, |
|
|
|
metadata.getPageableParameterName(), queryVariableName); |
|
|
|
context.getPageableParameterName(), queryVariableName); |
|
|
|
} else if (metadata.returnsSlice()) { |
|
|
|
} else if (context.returnsSlice()) { |
|
|
|
builder.addStatement("return new $T(finder, $L).execute($L)", SlicedExecution.class, |
|
|
|
builder.addStatement("return new $T(finder, $L).execute($L)", SlicedExecution.class, |
|
|
|
metadata.getPageableParameterName(), queryVariableName); |
|
|
|
context.getPageableParameterName(), queryVariableName); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
builder.addStatement("return finder.matching($L).$L", queryVariableName, terminatingMethod); |
|
|
|
builder.addStatement("return finder.matching($L).$L", queryVariableName, terminatingMethod); |
|
|
|
// builder.addStatement("return $T.getPage(finder.$L, $L, () -> finder.count())", PageableExecutionUtils.class,
|
|
|
|
// builder.addStatement("return $T.getPage(finder.$L, $L, () -> finder.count())", PageableExecutionUtils.class,
|
|
|
|
@ -117,14 +114,14 @@ public class MongoBlocks { |
|
|
|
|
|
|
|
|
|
|
|
static class QueryBlockBuilder { |
|
|
|
static class QueryBlockBuilder { |
|
|
|
|
|
|
|
|
|
|
|
MethodGenerationMetadata metadata; |
|
|
|
AotRepositoryMethodGenerationContext context; |
|
|
|
String queryString; |
|
|
|
String queryString; |
|
|
|
List<String> arguments; |
|
|
|
List<String> arguments; |
|
|
|
// MongoParameters argumentSource;
|
|
|
|
// MongoParameters argumentSource;
|
|
|
|
|
|
|
|
|
|
|
|
public QueryBlockBuilder(RepositoryInformation repositoryInformation, MethodGenerationMetadata metadata) { |
|
|
|
public QueryBlockBuilder(AotRepositoryMethodGenerationContext context) { |
|
|
|
this.metadata = metadata; |
|
|
|
this.context = context; |
|
|
|
this.arguments = Arrays.stream(metadata.getRepositoryMethod().getParameters()).map(Parameter::getName) |
|
|
|
this.arguments = Arrays.stream(context.getMethod().getParameters()).map(Parameter::getName) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
// ParametersSource parametersSource = ParametersSource.of(repositoryInformation, metadata.getRepositoryMethod());
|
|
|
|
// ParametersSource parametersSource = ParametersSource.of(repositoryInformation, metadata.getRepositoryMethod());
|
|
|
|
@ -139,7 +136,7 @@ public class MongoBlocks { |
|
|
|
|
|
|
|
|
|
|
|
CodeBlock build(String queryVariableName) { |
|
|
|
CodeBlock build(String queryVariableName) { |
|
|
|
|
|
|
|
|
|
|
|
String mongoOpsRef = metadata.fieldNameOf(MongoOperations.class); |
|
|
|
String mongoOpsRef = context.fieldNameOf(MongoOperations.class); |
|
|
|
|
|
|
|
|
|
|
|
CodeBlock.Builder builder = CodeBlock.builder(); |
|
|
|
CodeBlock.Builder builder = CodeBlock.builder(); |
|
|
|
|
|
|
|
|
|
|
|
@ -149,28 +146,28 @@ public class MongoBlocks { |
|
|
|
builder.addStatement("$T $L = new $T(filter.toDocument())", |
|
|
|
builder.addStatement("$T $L = new $T(filter.toDocument())", |
|
|
|
org.springframework.data.mongodb.core.query.Query.class, queryVariableName, BasicQuery.class); |
|
|
|
org.springframework.data.mongodb.core.query.Query.class, queryVariableName, BasicQuery.class); |
|
|
|
|
|
|
|
|
|
|
|
String sortParameter = metadata.getSortParameterName(); |
|
|
|
String sortParameter = context.getSortParameterName(); |
|
|
|
if (StringUtils.hasText(sortParameter)) { |
|
|
|
if (StringUtils.hasText(sortParameter)) { |
|
|
|
builder.addStatement("$L.with($L)", queryVariableName, sortParameter); |
|
|
|
builder.addStatement("$L.with($L)", queryVariableName, sortParameter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String limitParameter = metadata.getLimitParameterName(); |
|
|
|
String limitParameter = context.getLimitParameterName(); |
|
|
|
if (StringUtils.hasText(limitParameter)) { |
|
|
|
if (StringUtils.hasText(limitParameter)) { |
|
|
|
builder.addStatement("$L.limit($L)", queryVariableName, limitParameter); |
|
|
|
builder.addStatement("$L.limit($L)", queryVariableName, limitParameter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String pageableParameter = metadata.getPageableParameterName(); |
|
|
|
String pageableParameter = context.getPageableParameterName(); |
|
|
|
if (StringUtils.hasText(pageableParameter)) { |
|
|
|
if (StringUtils.hasText(pageableParameter)) { |
|
|
|
builder.addStatement("$L.with($L)", queryVariableName, pageableParameter); |
|
|
|
builder.addStatement("$L.with($L)", queryVariableName, pageableParameter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String hint = metadata.annotationValue(Hint.class, "value"); |
|
|
|
String hint = context.annotationValue(Hint.class, "value"); |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(hint)) { |
|
|
|
if (StringUtils.hasText(hint)) { |
|
|
|
builder.addStatement("$L.withHint($S)", queryVariableName, hint); |
|
|
|
builder.addStatement("$L.withHint($S)", queryVariableName, hint); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String readPreference = metadata.annotationValue(ReadPreference.class, "value"); |
|
|
|
String readPreference = context.annotationValue(ReadPreference.class, "value"); |
|
|
|
if (StringUtils.hasText(readPreference)) { |
|
|
|
if (StringUtils.hasText(readPreference)) { |
|
|
|
builder.addStatement("$L.withReadPreference($T.valueOf($S))", queryVariableName, |
|
|
|
builder.addStatement("$L.withReadPreference($T.valueOf($S))", queryVariableName, |
|
|
|
com.mongodb.ReadPreference.class, readPreference); |
|
|
|
com.mongodb.ReadPreference.class, readPreference); |
|
|
|
|