|
31 | 31 | import org.springframework.data.domain.Sort.Order;
|
32 | 32 | import org.springframework.data.geo.Box;
|
33 | 33 | import org.springframework.data.geo.Circle;
|
| 34 | +import org.springframework.data.geo.Distance; |
34 | 35 | import org.springframework.data.geo.Polygon;
|
35 | 36 | import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
|
36 | 37 | import org.springframework.data.mongodb.core.ExecutableRemoveOperation.ExecutableRemove;
|
|
46 | 47 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
47 | 48 | import org.springframework.data.mongodb.core.query.BasicUpdate;
|
48 | 49 | import org.springframework.data.mongodb.core.query.Collation;
|
| 50 | +import org.springframework.data.mongodb.core.query.NearQuery; |
49 | 51 | import org.springframework.data.mongodb.repository.Hint;
|
50 | 52 | import org.springframework.data.mongodb.repository.Meta;
|
51 | 53 | import org.springframework.data.mongodb.repository.ReadPreference;
|
@@ -151,6 +153,12 @@ static AggregationCodeBlockBuilder aggregationBlockBuilder(AotQueryMethodGenerat
|
151 | 153 | return new AggregationCodeBlockBuilder(context, queryMethod);
|
152 | 154 | }
|
153 | 155 |
|
| 156 | + static GeoNearCodeBlockBuilder geoNearBlockBuilder(AotQueryMethodGenerationContext context, |
| 157 | + MongoQueryMethod queryMethod) { |
| 158 | + |
| 159 | + return new GeoNearCodeBlockBuilder(context, queryMethod); |
| 160 | + } |
| 161 | + |
154 | 162 | /**
|
155 | 163 | * Builder for generating aggregation execution {@link CodeBlock}.
|
156 | 164 | *
|
@@ -467,14 +475,77 @@ CodeBlock build() {
|
467 | 475 | }
|
468 | 476 | }
|
469 | 477 |
|
| 478 | + static class GeoNearCodeBlockBuilder { |
| 479 | + |
| 480 | + private final AotQueryMethodGenerationContext context; |
| 481 | + private final MongoQueryMethod queryMethod; |
| 482 | + private final List<CodeBlock> arguments; |
| 483 | + |
| 484 | + private String variableName; |
| 485 | + |
| 486 | + GeoNearCodeBlockBuilder(AotQueryMethodGenerationContext context, MongoQueryMethod queryMethod) { |
| 487 | + |
| 488 | + this.context = context; |
| 489 | + this.arguments = context.getBindableParameterNames().stream().map(CodeBlock::of).collect(Collectors.toList()); |
| 490 | + this.queryMethod = queryMethod; |
| 491 | + } |
| 492 | + |
| 493 | + CodeBlock build() { |
| 494 | + |
| 495 | + CodeBlock.Builder builder = CodeBlock.builder(); |
| 496 | + builder.add("\n"); |
| 497 | + |
| 498 | + String locationParameterName = context.getParameterName(queryMethod.getParameters().getNearIndex()); |
| 499 | + |
| 500 | + builder.addStatement("$1T $2L = $1T.near($3L)", NearQuery.class, variableName, locationParameterName); |
| 501 | + |
| 502 | + if (queryMethod.getParameters().getRangeIndex() != -1) { |
| 503 | + |
| 504 | + String rangeParametername = context.getParameterName(queryMethod.getParameters().getRangeIndex()); |
| 505 | + String minVarName = context.localVariable("min"); |
| 506 | + String maxVarName = context.localVariable("max"); |
| 507 | + |
| 508 | + builder.beginControlFlow("if($L.getLowerBound().isPresent())", rangeParametername); |
| 509 | + builder.addStatement("$1T $2L = $3L.getLowerBound().get()", Distance.class, minVarName, rangeParametername); |
| 510 | + builder.addStatement("$1L.minDistance($2L.getValue()).in($2L.getMetric())", variableName, minVarName); |
| 511 | + builder.endControlFlow(); |
| 512 | + |
| 513 | + builder.beginControlFlow("if($L.getUpperBound().isPresent())", rangeParametername); |
| 514 | + builder.addStatement("$1T $2L = $3L.getUpperBound().get()", Distance.class, maxVarName, rangeParametername); |
| 515 | + builder.addStatement("$1L.maxDistance($2L.getValue()).in($2L.getMetric())", variableName, maxVarName); |
| 516 | + builder.endControlFlow(); |
| 517 | + } else { |
| 518 | + |
| 519 | + String distanceParametername = context.getParameterName(queryMethod.getParameters().getMaxDistanceIndex()); |
| 520 | + builder.addStatement("$1L.maxDistance($2L.getValue()).in($2L.getMetric())", variableName, |
| 521 | + distanceParametername); |
| 522 | + } |
| 523 | + |
| 524 | + if (context.getPageableParameterName() != null) { |
| 525 | + builder.addStatement("$L.with($L)", variableName, context.getPageableParameterName()); |
| 526 | + } |
| 527 | + |
| 528 | + builder.add("\n"); |
| 529 | + builder.addStatement("return $L.query($T.class).near($L).all()", context.fieldNameOf(MongoOperations.class), |
| 530 | + context.getRepositoryInformation().getDomainType(), variableName); |
| 531 | + return builder.build(); |
| 532 | + } |
| 533 | + |
| 534 | + public GeoNearCodeBlockBuilder usingQueryVariableName(String variableName) { |
| 535 | + this.variableName = variableName; |
| 536 | + return this; |
| 537 | + } |
| 538 | + } |
| 539 | + |
470 | 540 | @NullUnmarked
|
471 | 541 | static class AggregationCodeBlockBuilder {
|
472 | 542 |
|
473 | 543 | private final AotQueryMethodGenerationContext context;
|
474 | 544 | private final MongoQueryMethod queryMethod;
|
| 545 | + private final List<CodeBlock> arguments; |
475 | 546 |
|
476 | 547 | private AggregationInteraction source;
|
477 |
| - private final List<CodeBlock> arguments; |
| 548 | + |
478 | 549 | private String aggregationVariableName;
|
479 | 550 | private boolean pipelineOnly;
|
480 | 551 |
|
|
0 commit comments