24
24
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .COLLECTION_MUTATION ;
25
25
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .COLLECTION_NAME ;
26
26
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .FIELD_PATH ;
27
+ import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .FIELD_PATHS ;
27
28
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .FIELD_VALUE ;
28
29
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .FILTER ;
29
30
import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .PROJECT_STAGE_SPECIFICATIONS ;
30
- import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .SORT_FIELD ;
31
+ import static com .mongodb .hibernate .internal .translate .AstVisitorValueDescriptor .SORT_FIELDS ;
31
32
import static com .mongodb .hibernate .internal .translate .mongoast .command .aggregate .AstSortOrder .ASC ;
32
33
import static com .mongodb .hibernate .internal .translate .mongoast .command .aggregate .AstSortOrder .DESC ;
33
34
import static com .mongodb .hibernate .internal .translate .mongoast .filter .AstComparisonFilterOperator .EQ ;
40
41
import static com .mongodb .hibernate .internal .translate .mongoast .filter .AstLogicalFilterOperator .NOR ;
41
42
import static com .mongodb .hibernate .internal .translate .mongoast .filter .AstLogicalFilterOperator .OR ;
42
43
import static java .lang .String .format ;
44
+ import static java .util .Collections .singletonList ;
43
45
44
46
import com .mongodb .hibernate .internal .FeatureNotSupportedException ;
45
47
import com .mongodb .hibernate .internal .extension .service .StandardServiceRegistryScopedState ;
@@ -378,14 +380,10 @@ public void visitQuerySpec(QuerySpec querySpec) {
378
380
var stages = new ArrayList <AstStage >(3 );
379
381
380
382
// $match stage
381
- var whereClauseRestrictions = querySpec .getWhereClauseRestrictions ();
382
- if (whereClauseRestrictions != null && !whereClauseRestrictions .isEmpty ()) {
383
- var filter = acceptAndYield (whereClauseRestrictions , FILTER );
384
- stages .add (new AstMatchStage (filter ));
385
- }
383
+ getAstMatchStage (querySpec ).ifPresent (stages ::add );
386
384
387
385
// $sort stage
388
- getOptionalAstSortStage (querySpec ).ifPresent (stages ::add );
386
+ getAstSortStage (querySpec ).ifPresent (stages ::add );
389
387
390
388
// $project stage
391
389
var projectStageSpecifications = acceptAndYield (querySpec .getSelectClause (), PROJECT_STAGE_SPECIFICATIONS );
@@ -394,26 +392,22 @@ public void visitQuerySpec(QuerySpec querySpec) {
394
392
astVisitorValueHolder .yield (COLLECTION_AGGREGATE , new AstAggregateCommand (collection , stages ));
395
393
}
396
394
397
- private Optional <AstSortStage > getOptionalAstSortStage (QuerySpec querySpec ) {
395
+ private Optional <AstMatchStage > getAstMatchStage (QuerySpec querySpec ) {
396
+ var whereClauseRestrictions = querySpec .getWhereClauseRestrictions ();
397
+ if (whereClauseRestrictions != null && !whereClauseRestrictions .isEmpty ()) {
398
+ var filter = acceptAndYield (whereClauseRestrictions , FILTER );
399
+ return Optional .of (new AstMatchStage (filter ));
400
+ } else {
401
+ return Optional .empty ();
402
+ }
403
+ }
404
+
405
+ private Optional <AstSortStage > getAstSortStage (QuerySpec querySpec ) {
398
406
if (querySpec .hasSortSpecifications ()) {
399
407
var sortFields = new ArrayList <AstSortField >(
400
408
querySpec .getSortSpecifications ().size ());
401
- for (SortSpecification sortSpecification : querySpec .getSortSpecifications ()) {
402
- if (!isFieldPathExpression (sortSpecification .getSortExpression ())) {
403
- throw new FeatureNotSupportedException (
404
- format ("%s does not support sort key not of field path type" , MONGO_DBMS_NAME ));
405
- }
406
- if (sortSpecification .getNullPrecedence () != null
407
- && sortSpecification .getNullPrecedence () != NullPrecedence .NONE ) {
408
- throw new FeatureNotSupportedException (format (
409
- "%s does not support nulls precedence: NULLS %s" ,
410
- MONGO_DBMS_NAME , sortSpecification .getNullPrecedence ()));
411
- }
412
- if (sortSpecification .isIgnoreCase ()) {
413
- throw new FeatureNotSupportedException (
414
- format ("%s does not support case-insensitive sort key" , MONGO_DBMS_NAME ));
415
- }
416
- sortFields .add (acceptAndYield (sortSpecification , SORT_FIELD ));
409
+ for (var sortSpecification : querySpec .getSortSpecifications ()) {
410
+ sortFields .addAll (acceptAndYield (sortSpecification , SORT_FIELDS ));
417
411
}
418
412
return Optional .of (new AstSortStage (sortFields ));
419
413
}
@@ -541,12 +535,57 @@ public void visitSqlSelectionExpression(SqlSelectionExpression sqlSelectionExpre
541
535
sqlSelectionExpression .getSelection ().getExpression ().accept (this );
542
536
}
543
537
538
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539
+ // ORDER BY clause
540
+
544
541
@ Override
545
542
public void visitSortSpecification (SortSpecification sortSpecification ) {
546
- var sortFieldPath = acceptAndYield (sortSpecification .getSortExpression (), FIELD_PATH );
547
- var astSortField = new AstSortField (
548
- sortFieldPath , sortSpecification .getSortOrder () == SortDirection .ASCENDING ? ASC : DESC );
549
- astVisitorValueHolder .yield (SORT_FIELD , astSortField );
543
+ if (sortSpecification .getNullPrecedence () != null
544
+ && sortSpecification .getNullPrecedence () != NullPrecedence .NONE ) {
545
+ throw new FeatureNotSupportedException (format (
546
+ "%s does not support nulls precedence: NULLS %s" ,
547
+ MONGO_DBMS_NAME , sortSpecification .getNullPrecedence ()));
548
+ }
549
+ if (sortSpecification .isIgnoreCase ()) {
550
+ throw new FeatureNotSupportedException (
551
+ format ("%s does not support case-insensitive sort key [%s]" , MONGO_DBMS_NAME , sortSpecification ));
552
+ }
553
+ var sortExpression = sortSpecification .getSortExpression ();
554
+ final List <String > fieldPaths ;
555
+ if (sortExpression instanceof SqlTuple sqlTuple ) {
556
+ fieldPaths = acceptAndYield (sqlTuple , FIELD_PATHS );
557
+ } else {
558
+ if (!isFieldPathExpression (sortExpression )) {
559
+ throw new FeatureNotSupportedException (
560
+ format ("%s does not support sort key not of field path type" , MONGO_DBMS_NAME ));
561
+ }
562
+ var sortFieldPath = acceptAndYield (sortExpression , FIELD_PATH );
563
+ fieldPaths = singletonList (sortFieldPath );
564
+ }
565
+
566
+ var astSortFields = new ArrayList <AstSortField >(fieldPaths .size ());
567
+ for (var fieldPath : fieldPaths ) {
568
+ var astSortField = new AstSortField (
569
+ fieldPath , sortSpecification .getSortOrder () == SortDirection .ASCENDING ? ASC : DESC );
570
+ astSortFields .add (astSortField );
571
+ }
572
+ astVisitorValueHolder .yield (SORT_FIELDS , astSortFields );
573
+ }
574
+
575
+ @ Override
576
+ public void visitTuple (SqlTuple sqlTuple ) {
577
+ List <String > fieldPaths = new ArrayList <>(sqlTuple .getExpressions ().size ());
578
+ for (var expression : sqlTuple .getExpressions ()) {
579
+ if (expression instanceof SqlTuple ) {
580
+ fieldPaths .addAll (acceptAndYield (expression , FIELD_PATHS ));
581
+ } else {
582
+ if (!isFieldPathExpression (expression )) {
583
+ throw new FeatureNotSupportedException ();
584
+ }
585
+ fieldPaths .add (acceptAndYield (expression , FIELD_PATH ));
586
+ }
587
+ }
588
+ astVisitorValueHolder .yield (FIELD_PATHS , fieldPaths );
550
589
}
551
590
552
591
@ Override
@@ -709,11 +748,6 @@ public void visitEmbeddableTypeLiteral(EmbeddableTypeLiteral embeddableTypeLiter
709
748
throw new FeatureNotSupportedException ();
710
749
}
711
750
712
- @ Override
713
- public void visitTuple (SqlTuple sqlTuple ) {
714
- throw new FeatureNotSupportedException ();
715
- }
716
-
717
751
@ Override
718
752
public void visitCollation (Collation collation ) {
719
753
throw new FeatureNotSupportedException ();
0 commit comments