Skip to content

Commit fa30902

Browse files
committed
Polishing.
Align test methods between EQL and JPQL. Refine identification variable syntax grouping. Align and simplify EQL and JPQL renderers. See #3902 Original pull request: #3903
1 parent d9c102e commit fa30902

File tree

6 files changed

+419
-84
lines changed

6 files changed

+419
-84
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,23 @@ from_clause
7272
identificationVariableDeclarationOrCollectionMemberDeclaration
7373
: identification_variable_declaration
7474
| collection_member_declaration
75-
| '(' subquery ')' identification_variable
75+
| '(' subquery ')' (AS? identification_variable)?
7676
;
7777

7878
identification_variable_declaration
7979
: range_variable_declaration (join | fetch_join)*
8080
;
8181

8282
range_variable_declaration
83-
: (entity_name|function_invocation) AS? identification_variable?
83+
: (entity_name|function_invocation) (AS? identification_variable)?
8484
;
8585

8686
join
87-
: join_spec join_association_path_expression AS? identification_variable? join_condition?
87+
: join_spec join_association_path_expression (AS? identification_variable)? join_condition?
8888
;
8989

9090
fetch_join
91-
: join_spec FETCH join_association_path_expression AS? identification_variable? join_condition?
91+
: join_spec FETCH join_association_path_expression (AS? identification_variable)? join_condition?
9292
;
9393

9494
join_spec
@@ -115,7 +115,7 @@ join_single_valued_path_expression
115115
;
116116

117117
collection_member_declaration
118-
: IN '(' collection_valued_path_expression ')' AS? identification_variable
118+
: IN '(' collection_valued_path_expression ')' (AS? identification_variable)?
119119
;
120120

121121
qualified_identification_variable
@@ -271,7 +271,7 @@ subquery_from_clause
271271

272272
subselect_identification_variable_declaration
273273
: identification_variable_declaration
274-
| derived_path_expression AS? identification_variable (join)*
274+
| derived_path_expression (AS? identification_variable)? (join)*
275275
| derived_collection_member_declaration
276276
;
277277

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Jpql.g4

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ from_clause
7373
identificationVariableDeclarationOrCollectionMemberDeclaration
7474
: identification_variable_declaration
7575
| collection_member_declaration
76-
| '(' subquery ')' identification_variable
76+
| '(' subquery ')' (AS? identification_variable)?
7777
;
7878

7979
identification_variable_declaration
8080
: range_variable_declaration (join | fetch_join)*
8181
;
8282

8383
range_variable_declaration
84-
: entity_name AS? identification_variable?
84+
: entity_name (AS? identification_variable)?
8585
;
8686

8787
join
88-
: join_spec join_association_path_expression AS? identification_variable? (join_condition)?
88+
: join_spec join_association_path_expression (AS? identification_variable)? (join_condition)?
8989
;
9090

9191
fetch_join
92-
: join_spec FETCH join_association_path_expression AS? identification_variable? join_condition?
92+
: join_spec FETCH join_association_path_expression (AS? identification_variable)? join_condition?
9393
;
9494

9595
join_spec
@@ -116,7 +116,7 @@ join_single_valued_path_expression
116116
;
117117

118118
collection_member_declaration
119-
: IN '(' collection_valued_path_expression ')' AS? identification_variable
119+
: IN '(' collection_valued_path_expression ')' (AS? identification_variable)?
120120
;
121121

122122
qualified_identification_variable
@@ -271,7 +271,7 @@ subquery_from_clause
271271

272272
subselect_identification_variable_declaration
273273
: identification_variable_declaration
274-
| derived_path_expression AS? identification_variable (join)*
274+
| derived_path_expression (AS? identification_variable)? (join)*
275275
| derived_collection_member_declaration
276276
;
277277

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ public QueryTokenStream visitIdentificationVariableDeclarationOrCollectionMember
213213

214214
QueryRendererBuilder builder = QueryRenderer.builder();
215215
builder.appendExpression(nested);
216+
217+
if (ctx.AS() != null) {
218+
builder.append(QueryTokens.expression(ctx.AS()));
219+
}
220+
216221
if (ctx.identification_variable() != null) {
217222
builder.appendExpression(visit(ctx.identification_variable()));
218223
}
@@ -406,12 +411,15 @@ public QueryTokenStream visitJoin_single_valued_path_expression(
406411
@Override
407412
public QueryTokenStream visitCollection_member_declaration(EqlParser.Collection_member_declarationContext ctx) {
408413

409-
QueryRendererBuilder builder = QueryRenderer.builder();
414+
QueryRendererBuilder nested = QueryRenderer.builder();
410415

411-
builder.append(QueryTokens.token(ctx.IN()));
412-
builder.append(TOKEN_OPEN_PAREN);
413-
builder.appendInline(visit(ctx.collection_valued_path_expression()));
414-
builder.append(TOKEN_CLOSE_PAREN);
416+
nested.append(QueryTokens.token(ctx.IN()));
417+
nested.append(TOKEN_OPEN_PAREN);
418+
nested.appendInline(visit(ctx.collection_valued_path_expression()));
419+
nested.append(TOKEN_CLOSE_PAREN);
420+
421+
QueryRendererBuilder builder = QueryRenderer.builder();
422+
builder.appendExpression(nested);
415423

416424
if (ctx.AS() != null) {
417425
builder.append(QueryTokens.expression(ctx.AS()));
@@ -496,34 +504,36 @@ public QueryTokenStream visitGeneral_identification_variable(EqlParser.General_i
496504
QueryRendererBuilder builder = QueryRenderer.builder();
497505

498506
if (ctx.identification_variable() != null) {
499-
builder.append(visit(ctx.identification_variable()));
507+
return visit(ctx.identification_variable());
500508
} else if (ctx.map_field_identification_variable() != null) {
501-
builder.append(visit(ctx.map_field_identification_variable()));
509+
return visit(ctx.map_field_identification_variable());
502510
}
503511

504-
return builder;
512+
return QueryTokenStream.empty();
505513
}
506514

507515
@Override
508516
public QueryTokenStream visitGeneral_subpath(EqlParser.General_subpathContext ctx) {
509517

510-
QueryRendererBuilder builder = QueryRenderer.builder();
511-
512518
if (ctx.simple_subpath() != null) {
513-
builder.appendInline(visit(ctx.simple_subpath()));
519+
return visit(ctx.simple_subpath());
514520
} else if (ctx.treated_subpath() != null) {
515521

516-
builder.appendInline(visit(ctx.treated_subpath()));
517-
builder.appendInline(QueryTokenStream.concat(ctx.single_valued_object_field(), this::visit, TOKEN_DOT));
522+
List<ParseTree> items = new ArrayList<>(1 + ctx.single_valued_object_field().size());
523+
524+
items.add(ctx.treated_subpath());
525+
items.addAll(ctx.single_valued_object_field());
526+
return QueryTokenStream.concat(items, this::visit, TOKEN_DOT);
518527
}
519528

520-
return builder;
529+
return QueryTokenStream.empty();
521530
}
522531

523532
@Override
524533
public QueryTokenStream visitSimple_subpath(EqlParser.Simple_subpathContext ctx) {
525534

526535
List<ParseTree> items = new ArrayList<>(1 + ctx.single_valued_object_field().size());
536+
527537
items.add(ctx.general_identification_variable());
528538
items.addAll(ctx.single_valued_object_field());
529539

@@ -566,12 +576,12 @@ public QueryTokenStream visitState_valued_path_expression(EqlParser.State_valued
566576
QueryRendererBuilder builder = QueryRenderer.builder();
567577

568578
if (ctx.state_field_path_expression() != null) {
569-
builder.append(visit(ctx.state_field_path_expression()));
579+
return visit(ctx.state_field_path_expression());
570580
} else if (ctx.general_identification_variable() != null) {
571-
builder.append(visit(ctx.general_identification_variable()));
581+
return visit(ctx.general_identification_variable());
572582
}
573583

574-
return builder;
584+
return QueryTokenStream.empty();
575585
}
576586

577587
@Override
@@ -617,7 +627,7 @@ public QueryTokenStream visitUpdate_clause(EqlParser.Update_clauseContext ctx) {
617627
}
618628

619629
builder.append(QueryTokens.expression(ctx.SET()));
620-
builder.appendExpression(QueryTokenStream.concat(ctx.update_item(), this::visit, TOKEN_COMMA));
630+
builder.append(QueryTokenStream.concat(ctx.update_item(), this::visit, TOKEN_COMMA));
621631

622632
return builder;
623633
}
@@ -628,6 +638,7 @@ public QueryTokenStream visitUpdate_item(EqlParser.Update_itemContext ctx) {
628638
QueryRendererBuilder builder = QueryRenderer.builder();
629639

630640
List<ParseTree> items = new ArrayList<>(3 + ctx.single_valued_embeddable_object_field().size());
641+
631642
if (ctx.identification_variable() != null) {
632643
items.add(ctx.identification_variable());
633644
}
@@ -657,7 +668,7 @@ public QueryTokenStream visitNew_value(EqlParser.New_valueContext ctx) {
657668
} else if (ctx.NULL() != null) {
658669
return QueryTokenStream.ofToken(ctx.NULL());
659670
} else {
660-
return QueryRenderer.builder();
671+
return QueryTokenStream.empty();
661672
}
662673
}
663674

@@ -748,7 +759,7 @@ public QueryTokenStream visitSelect_expression(EqlParser.Select_expressionContex
748759
} else if (ctx.constructor_expression() != null) {
749760
return visit(ctx.constructor_expression());
750761
} else {
751-
return QueryRenderer.builder();
762+
return QueryTokenStream.empty();
752763
}
753764
}
754765

@@ -860,17 +871,15 @@ public QueryTokenStream visitGroupby_clause(EqlParser.Groupby_clauseContext ctx)
860871
@Override
861872
public QueryTokenStream visitGroupby_item(EqlParser.Groupby_itemContext ctx) {
862873

863-
QueryRendererBuilder builder = QueryRenderer.builder();
864-
865874
if (ctx.single_valued_path_expression() != null) {
866-
builder.append(visit(ctx.single_valued_path_expression()));
875+
return visit(ctx.single_valued_path_expression());
867876
} else if (ctx.identification_variable() != null) {
868-
builder.append(visit(ctx.identification_variable()));
877+
return visit(ctx.identification_variable());
869878
} else if (ctx.scalar_expression() != null) {
870-
builder.append(visit(ctx.scalar_expression()));
879+
return visit(ctx.scalar_expression());
871880
}
872881

873-
return builder;
882+
return QueryTokenStream.empty();
874883
}
875884

876885
@Override
@@ -1061,19 +1070,17 @@ public QueryTokenStream visitSimple_select_clause(EqlParser.Simple_select_clause
10611070
@Override
10621071
public QueryTokenStream visitSimple_select_expression(EqlParser.Simple_select_expressionContext ctx) {
10631072

1064-
QueryRendererBuilder builder = QueryRenderer.builder();
1065-
10661073
if (ctx.single_valued_path_expression() != null) {
1067-
builder.append(visit(ctx.single_valued_path_expression()));
1074+
return visit(ctx.single_valued_path_expression());
10681075
} else if (ctx.scalar_expression() != null) {
1069-
builder.append(visit(ctx.scalar_expression()));
1076+
return visit(ctx.scalar_expression());
10701077
} else if (ctx.aggregate_expression() != null) {
1071-
builder.append(visit(ctx.aggregate_expression()));
1078+
return visit(ctx.aggregate_expression());
10721079
} else if (ctx.identification_variable() != null) {
1073-
builder.append(visit(ctx.identification_variable()));
1080+
return visit(ctx.identification_variable());
10741081
}
10751082

1076-
return builder;
1083+
return QueryTokenStream.empty();
10771084
}
10781085

10791086
@Override
@@ -2083,9 +2090,11 @@ public QueryTokenStream visitType_cast_function(EqlParser.Type_cast_functionCont
20832090
builder.append(QueryTokens.token(ctx.CAST()));
20842091
builder.append(TOKEN_OPEN_PAREN);
20852092
builder.appendExpression(visit(ctx.scalar_expression()));
2093+
20862094
if (ctx.AS() != null) {
20872095
builder.append(QueryTokens.expression(ctx.AS()));
20882096
}
2097+
20892098
builder.appendInline(visit(ctx.identification_variable()));
20902099

20912100
if (!CollectionUtils.isEmpty(ctx.numeric_literal())) {
@@ -2388,12 +2397,7 @@ public QueryTokenStream visitInput_parameter(EqlParser.Input_parameterContext ct
23882397

23892398
@Override
23902399
public QueryTokenStream visitPattern_value(EqlParser.Pattern_valueContext ctx) {
2391-
2392-
QueryRendererBuilder builder = QueryRenderer.builder();
2393-
2394-
builder.append(visit(ctx.string_expression()));
2395-
2396-
return builder;
2400+
return visit(ctx.string_expression());
23972401
}
23982402

23992403
@Override

0 commit comments

Comments
 (0)