15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
18
- import static org .springframework .data .jpa .repository .query .QueryTokens .TOKEN_ASC ;
19
- import static org .springframework .data .jpa .repository .query .QueryTokens .TOKEN_DESC ;
18
+ import static org .springframework .data .jpa .repository .query .QueryTokens .*;
20
19
21
20
import java .util .ArrayList ;
22
21
import java .util .Arrays ;
@@ -192,7 +191,7 @@ public static Expression expression(PathAndOrigin pas) {
192
191
}
193
192
194
193
/**
195
- * Create a simple expression from a string as is.
194
+ * Create a simple expression from a string as- is.
196
195
*
197
196
* @param expression
198
197
* @return
@@ -204,21 +203,56 @@ public static Expression expression(String expression) {
204
203
return new LiteralExpression (expression );
205
204
}
206
205
207
- public static Expression stringLiteral (String literal ) {
206
+ /**
207
+ * Create a simple numeric literal.
208
+ *
209
+ * @param literal
210
+ * @return
211
+ */
212
+ public static Expression literal (Number literal ) {
213
+ return new LiteralExpression (literal .toString ());
214
+ }
215
+
216
+ /**
217
+ * Create a simple literal from a string by quoting it.
218
+ *
219
+ * @param literal
220
+ * @return
221
+ */
222
+ public static Expression literal (String literal ) {
208
223
return new StringLiteralExpression (literal );
209
224
}
210
225
226
+ /**
227
+ * A parameter placeholder.
228
+ *
229
+ * @param parameter
230
+ * @return
231
+ */
211
232
public static Expression parameter (String parameter ) {
212
233
213
234
Assert .hasText (parameter , "Parameter must not be empty or null" );
214
235
215
236
return new ParameterExpression (new ParameterPlaceholder (parameter ));
216
237
}
217
238
239
+ /**
240
+ * A parameter placeholder.
241
+ *
242
+ * @param placeholder the placeholder to use.
243
+ * @return
244
+ */
218
245
public static Expression parameter (ParameterPlaceholder placeholder ) {
219
246
return new ParameterExpression (placeholder );
220
247
}
221
248
249
+ /**
250
+ * Create a new ordering expression.
251
+ *
252
+ * @param sortExpression
253
+ * @param order
254
+ * @return
255
+ */
222
256
public static Expression orderBy (Expression sortExpression , Sort .Order order ) {
223
257
return new OrderExpression (sortExpression , order );
224
258
}
@@ -318,16 +352,6 @@ public Predicate notIn(Expression value) {
318
352
return new InPredicate (rhs , "NOT IN" , value );
319
353
}
320
354
321
- @ Override
322
- public Predicate inMultivalued (Expression value ) {
323
- return new MemberOfPredicate (rhs , "IN" , value ); // TODO: that does not line up in my head - ahahah
324
- }
325
-
326
- @ Override
327
- public Predicate notInMultivalued (Expression value ) {
328
- return new MemberOfPredicate (rhs , "NOT IN" , value );
329
- }
330
-
331
355
@ Override
332
356
public Predicate memberOf (Expression value ) {
333
357
return new MemberOfPredicate (rhs , "MEMBER OF" , value );
@@ -479,30 +503,30 @@ public String toString() {
479
503
480
504
static PathAndOrigin path (Origin origin , String path ) {
481
505
482
- if (origin instanceof Entity entity ) {
506
+ if (origin instanceof Entity entity ) {
483
507
484
- try {
508
+ try {
485
509
PropertyPath from = PropertyPath .from (path , ClassUtils .forName (entity .entity , Entity .class .getClassLoader ()));
486
510
return new PathAndOrigin (from , entity , false );
487
511
} catch (ClassNotFoundException e ) {
488
- throw new RuntimeException (e );
489
- }
490
- }
491
- if (origin instanceof Join join ) {
512
+ throw new RuntimeException (e );
513
+ }
514
+ }
515
+ if (origin instanceof Join join ) {
492
516
493
517
Origin parent = join .source ;
494
518
List <String > segments = new ArrayList <>();
495
519
segments .add (join .path );
496
- while (!(parent instanceof Entity )) {
497
- if (parent instanceof Join pj ) {
520
+ while (!(parent instanceof Entity )) {
521
+ if (parent instanceof Join pj ) {
498
522
parent = pj .source ;
499
523
segments .add (pj .path );
500
524
} else {
501
525
parent = null ;
502
526
}
503
527
}
504
528
505
- if (parent instanceof Entity entity ) {
529
+ if (parent instanceof Entity entity ) {
506
530
Collections .reverse (segments );
507
531
segments .add (path );
508
532
PathAndOrigin path1 = path (parent , StringUtils .collectionToDelimitedString (segments , "." ));
@@ -561,7 +585,6 @@ record ConstructorExpression(String resultType, Multiselect multiselect) impleme
561
585
@ Override
562
586
public String render (RenderContext context ) {
563
587
564
-
565
588
return "new %s(%s)" .formatted (resultType , multiselect .render (new ConstructorContext (context )));
566
589
}
567
590
@@ -591,7 +614,7 @@ public String render(RenderContext context) {
591
614
}
592
615
593
616
builder .append (PathExpression .render (path , context ));
594
- if (!context .isConstructorContext ()) {
617
+ if (!context .isConstructorContext ()) {
595
618
builder .append (" " ).append (path .path ().getSegment ());
596
619
}
597
620
}
@@ -917,17 +940,6 @@ public String render(RenderContext context) {
917
940
*/
918
941
public interface WhereStep {
919
942
920
- /**
921
- * Create a {@code BETWEEN … AND …} predicate.
922
- *
923
- * @param lower lower boundary.
924
- * @param upper upper boundary.
925
- * @return
926
- */
927
- default Predicate between (String lower , String upper ) {
928
- return between (expression (lower ), expression (upper ));
929
- }
930
-
931
943
/**
932
944
* Create a {@code BETWEEN … AND …} predicate.
933
945
*
@@ -943,142 +955,140 @@ default Predicate between(String lower, String upper) {
943
955
* @param value the comparison value.
944
956
* @return
945
957
*/
946
- default Predicate gt (String value ) {
947
- return gt (expression (value ));
948
- }
958
+ Predicate gt (Expression value );
949
959
950
960
/**
951
- * Create a greater {@code > …} predicate.
961
+ * Create a greater-or-equals {@code >= …} predicate.
952
962
*
953
963
* @param value the comparison value.
954
964
* @return
955
965
*/
956
- Predicate gt (Expression value );
966
+ Predicate gte (Expression value );
957
967
958
968
/**
959
- * Create a greater-or-equals {@code >= …} predicate.
969
+ * Create a less {@code < …} predicate.
960
970
*
961
971
* @param value the comparison value.
962
972
* @return
963
973
*/
964
- default Predicate gte (String value ) {
965
- return gte (expression (value ));
966
- }
974
+ Predicate lt (Expression value );
967
975
968
976
/**
969
- * Create a greater -or-equals {@code > = …} predicate.
977
+ * Create a less -or-equals {@code < = …} predicate.
970
978
*
971
979
* @param value the comparison value.
972
980
* @return
973
981
*/
974
- Predicate gte (Expression value );
982
+ Predicate lte (Expression value );
975
983
976
984
/**
977
- * Create a less {@code < … } predicate.
985
+ * Create a {@code IS NULL } predicate.
978
986
*
979
- * @param value the comparison value.
980
987
* @return
981
988
*/
982
- default Predicate lt (String value ) {
983
- return lt (expression (value ));
984
- }
989
+ Predicate isNull ();
985
990
986
991
/**
987
- * Create a less {@code < … } predicate.
992
+ * Create a {@code IS NOT NULL } predicate.
988
993
*
989
- * @param value the comparison value.
990
994
* @return
991
995
*/
992
- Predicate lt ( Expression value );
996
+ Predicate isNotNull ( );
993
997
994
998
/**
995
- * Create a less-or-equals {@code <= … } predicate.
999
+ * Create a {@code IS TRUE } predicate.
996
1000
*
997
- * @param value the comparison value.
998
1001
* @return
999
1002
*/
1000
- default Predicate lte (String value ) {
1001
- return lte (expression (value ));
1002
- }
1003
+ Predicate isTrue ();
1003
1004
1004
1005
/**
1005
- * Create a less-or-equals {@code <= … } predicate.
1006
+ * Create a {@code IS FALSE } predicate.
1006
1007
*
1007
- * @param value the comparison value.
1008
1008
* @return
1009
1009
*/
1010
- Predicate lte (Expression value );
1011
-
1012
- Predicate isNull ();
1013
-
1014
- Predicate isNotNull ();
1015
-
1016
- Predicate isTrue ();
1017
-
1018
1010
Predicate isFalse ();
1019
1011
1012
+ /**
1013
+ * Create a {@code IS EMPTY} predicate.
1014
+ *
1015
+ * @return
1016
+ */
1020
1017
Predicate isEmpty ();
1021
1018
1019
+ /**
1020
+ * Create a {@code IS NOT EMPTY} predicate.
1021
+ *
1022
+ * @return
1023
+ */
1022
1024
Predicate isNotEmpty ();
1023
1025
1024
- default Predicate in (String value ) {
1025
- return in (expression (value ));
1026
- }
1027
-
1026
+ /**
1027
+ * Create a {@code IN} predicate.
1028
+ *
1029
+ * @param value
1030
+ * @return
1031
+ */
1028
1032
Predicate in (Expression value );
1029
1033
1030
- default Predicate notIn (String value ) {
1031
- return notIn (expression (value ));
1032
- }
1033
-
1034
+ /**
1035
+ * Create a {@code NOT IN} predicate.
1036
+ *
1037
+ * @param value
1038
+ * @return
1039
+ */
1034
1040
Predicate notIn (Expression value );
1035
1041
1036
- default Predicate inMultivalued (String value ) {
1037
- return inMultivalued (expression (value ));
1038
- }
1039
-
1040
- Predicate inMultivalued (Expression value );
1041
-
1042
- default Predicate notInMultivalued (String value ) {
1043
- return notInMultivalued (expression (value ));
1044
- }
1045
-
1046
- Predicate notInMultivalued (Expression value );
1047
-
1048
- default Predicate memberOf (String value ) {
1049
- return memberOf (expression (value ));
1050
- }
1051
-
1042
+ /**
1043
+ * Create a {@code MEMBER OF <collection>} predicate.
1044
+ *
1045
+ * @param value
1046
+ * @return
1047
+ */
1052
1048
Predicate memberOf (Expression value );
1053
1049
1054
- default Predicate notMemberOf (String value ) {
1055
- return notMemberOf (expression (value ));
1056
- }
1057
-
1050
+ /**
1051
+ * Create a {@code NOT MEMBER OF <collection>} predicate.
1052
+ *
1053
+ * @param value
1054
+ * @return
1055
+ */
1058
1056
Predicate notMemberOf (Expression value );
1059
1057
1060
1058
default Predicate like (String value , String escape ) {
1061
1059
return like (expression (value ), escape );
1062
1060
}
1063
1061
1062
+ /**
1063
+ * Create a {@code LIKE … ESCAPE} predicate.
1064
+ *
1065
+ * @param value
1066
+ * @return
1067
+ */
1064
1068
Predicate like (Expression value , String escape );
1065
1069
1066
- default Predicate notLike (String value , String escape ) {
1067
- return notLike (expression (value ), escape );
1068
- }
1069
-
1070
+ /**
1071
+ * Create a {@code NOT LIKE … ESCAPE} predicate.
1072
+ *
1073
+ * @param value
1074
+ * @return
1075
+ */
1070
1076
Predicate notLike (Expression value , String escape );
1071
1077
1072
- default Predicate eq (String value ) {
1073
- return eq (expression (value ));
1074
- }
1075
-
1078
+ /**
1079
+ * Create a {@code =} (equals) predicate.
1080
+ *
1081
+ * @param value
1082
+ * @return
1083
+ */
1076
1084
Predicate eq (Expression value );
1077
1085
1078
- default Predicate neq (String value ) {
1079
- return neq (expression (value ));
1080
- }
1081
-
1086
+ /**
1087
+ * Create a {@code <>} (not equals) predicate.
1088
+ *
1089
+ * @param value
1090
+ * @return
1091
+ */
1082
1092
Predicate neq (Expression value );
1083
1093
}
1084
1094
@@ -1243,7 +1253,7 @@ record InPredicate(Expression path, String operator, Expression predicate) imple
1243
1253
@ Override
1244
1254
public String render (RenderContext context ) {
1245
1255
1246
- //TODO: should we rather wrap it with nested or check if its a nested predicate before we call render
1256
+ // TODO: should we rather wrap it with nested or check if its a nested predicate before we call render
1247
1257
return "%s %s (%s)" .formatted (path .render (context ), operator , predicate .render (context ));
1248
1258
}
1249
1259
0 commit comments