Skip to content

Commit 8969f54

Browse files
committed
Polishing.
Remove method overloads accepting pure strings. Use switch-expressions.
1 parent f86c19c commit 8969f54

File tree

4 files changed

+198
-198
lines changed

4 files changed

+198
-198
lines changed

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

+121-111
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

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.*;
2019

2120
import java.util.ArrayList;
2221
import java.util.Arrays;
@@ -192,7 +191,7 @@ public static Expression expression(PathAndOrigin pas) {
192191
}
193192

194193
/**
195-
* Create a simple expression from a string as is.
194+
* Create a simple expression from a string as-is.
196195
*
197196
* @param expression
198197
* @return
@@ -204,21 +203,56 @@ public static Expression expression(String expression) {
204203
return new LiteralExpression(expression);
205204
}
206205

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) {
208223
return new StringLiteralExpression(literal);
209224
}
210225

226+
/**
227+
* A parameter placeholder.
228+
*
229+
* @param parameter
230+
* @return
231+
*/
211232
public static Expression parameter(String parameter) {
212233

213234
Assert.hasText(parameter, "Parameter must not be empty or null");
214235

215236
return new ParameterExpression(new ParameterPlaceholder(parameter));
216237
}
217238

239+
/**
240+
* A parameter placeholder.
241+
*
242+
* @param placeholder the placeholder to use.
243+
* @return
244+
*/
218245
public static Expression parameter(ParameterPlaceholder placeholder) {
219246
return new ParameterExpression(placeholder);
220247
}
221248

249+
/**
250+
* Create a new ordering expression.
251+
*
252+
* @param sortExpression
253+
* @param order
254+
* @return
255+
*/
222256
public static Expression orderBy(Expression sortExpression, Sort.Order order) {
223257
return new OrderExpression(sortExpression, order);
224258
}
@@ -318,16 +352,6 @@ public Predicate notIn(Expression value) {
318352
return new InPredicate(rhs, "NOT IN", value);
319353
}
320354

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-
331355
@Override
332356
public Predicate memberOf(Expression value) {
333357
return new MemberOfPredicate(rhs, "MEMBER OF", value);
@@ -479,30 +503,30 @@ public String toString() {
479503

480504
static PathAndOrigin path(Origin origin, String path) {
481505

482-
if(origin instanceof Entity entity) {
506+
if (origin instanceof Entity entity) {
483507

484-
try {
508+
try {
485509
PropertyPath from = PropertyPath.from(path, ClassUtils.forName(entity.entity, Entity.class.getClassLoader()));
486510
return new PathAndOrigin(from, entity, false);
487511
} 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) {
492516

493517
Origin parent = join.source;
494518
List<String> segments = new ArrayList<>();
495519
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) {
498522
parent = pj.source;
499523
segments.add(pj.path);
500524
} else {
501525
parent = null;
502526
}
503527
}
504528

505-
if(parent instanceof Entity entity) {
529+
if (parent instanceof Entity entity) {
506530
Collections.reverse(segments);
507531
segments.add(path);
508532
PathAndOrigin path1 = path(parent, StringUtils.collectionToDelimitedString(segments, "."));
@@ -561,7 +585,6 @@ record ConstructorExpression(String resultType, Multiselect multiselect) impleme
561585
@Override
562586
public String render(RenderContext context) {
563587

564-
565588
return "new %s(%s)".formatted(resultType, multiselect.render(new ConstructorContext(context)));
566589
}
567590

@@ -591,7 +614,7 @@ public String render(RenderContext context) {
591614
}
592615

593616
builder.append(PathExpression.render(path, context));
594-
if(!context.isConstructorContext()) {
617+
if (!context.isConstructorContext()) {
595618
builder.append(" ").append(path.path().getSegment());
596619
}
597620
}
@@ -917,17 +940,6 @@ public String render(RenderContext context) {
917940
*/
918941
public interface WhereStep {
919942

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-
931943
/**
932944
* Create a {@code BETWEEN … AND …} predicate.
933945
*
@@ -943,142 +955,140 @@ default Predicate between(String lower, String upper) {
943955
* @param value the comparison value.
944956
* @return
945957
*/
946-
default Predicate gt(String value) {
947-
return gt(expression(value));
948-
}
958+
Predicate gt(Expression value);
949959

950960
/**
951-
* Create a greater {@code > …} predicate.
961+
* Create a greater-or-equals {@code >= …} predicate.
952962
*
953963
* @param value the comparison value.
954964
* @return
955965
*/
956-
Predicate gt(Expression value);
966+
Predicate gte(Expression value);
957967

958968
/**
959-
* Create a greater-or-equals {@code >= …} predicate.
969+
* Create a less {@code < …} predicate.
960970
*
961971
* @param value the comparison value.
962972
* @return
963973
*/
964-
default Predicate gte(String value) {
965-
return gte(expression(value));
966-
}
974+
Predicate lt(Expression value);
967975

968976
/**
969-
* Create a greater-or-equals {@code >= …} predicate.
977+
* Create a less-or-equals {@code <= …} predicate.
970978
*
971979
* @param value the comparison value.
972980
* @return
973981
*/
974-
Predicate gte(Expression value);
982+
Predicate lte(Expression value);
975983

976984
/**
977-
* Create a less {@code < …} predicate.
985+
* Create a {@code IS NULL} predicate.
978986
*
979-
* @param value the comparison value.
980987
* @return
981988
*/
982-
default Predicate lt(String value) {
983-
return lt(expression(value));
984-
}
989+
Predicate isNull();
985990

986991
/**
987-
* Create a less {@code < …} predicate.
992+
* Create a {@code IS NOT NULL} predicate.
988993
*
989-
* @param value the comparison value.
990994
* @return
991995
*/
992-
Predicate lt(Expression value);
996+
Predicate isNotNull();
993997

994998
/**
995-
* Create a less-or-equals {@code <= …} predicate.
999+
* Create a {@code IS TRUE} predicate.
9961000
*
997-
* @param value the comparison value.
9981001
* @return
9991002
*/
1000-
default Predicate lte(String value) {
1001-
return lte(expression(value));
1002-
}
1003+
Predicate isTrue();
10031004

10041005
/**
1005-
* Create a less-or-equals {@code <= …} predicate.
1006+
* Create a {@code IS FALSE} predicate.
10061007
*
1007-
* @param value the comparison value.
10081008
* @return
10091009
*/
1010-
Predicate lte(Expression value);
1011-
1012-
Predicate isNull();
1013-
1014-
Predicate isNotNull();
1015-
1016-
Predicate isTrue();
1017-
10181010
Predicate isFalse();
10191011

1012+
/**
1013+
* Create a {@code IS EMPTY} predicate.
1014+
*
1015+
* @return
1016+
*/
10201017
Predicate isEmpty();
10211018

1019+
/**
1020+
* Create a {@code IS NOT EMPTY} predicate.
1021+
*
1022+
* @return
1023+
*/
10221024
Predicate isNotEmpty();
10231025

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+
*/
10281032
Predicate in(Expression value);
10291033

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+
*/
10341040
Predicate notIn(Expression value);
10351041

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 &lt;collection&gt;} predicate.
1044+
*
1045+
* @param value
1046+
* @return
1047+
*/
10521048
Predicate memberOf(Expression value);
10531049

1054-
default Predicate notMemberOf(String value) {
1055-
return notMemberOf(expression(value));
1056-
}
1057-
1050+
/**
1051+
* Create a {@code NOT MEMBER OF &lt;collection&gt;} predicate.
1052+
*
1053+
* @param value
1054+
* @return
1055+
*/
10581056
Predicate notMemberOf(Expression value);
10591057

10601058
default Predicate like(String value, String escape) {
10611059
return like(expression(value), escape);
10621060
}
10631061

1062+
/**
1063+
* Create a {@code LIKE … ESCAPE} predicate.
1064+
*
1065+
* @param value
1066+
* @return
1067+
*/
10641068
Predicate like(Expression value, String escape);
10651069

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+
*/
10701076
Predicate notLike(Expression value, String escape);
10711077

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+
*/
10761084
Predicate eq(Expression value);
10771085

1078-
default Predicate neq(String value) {
1079-
return neq(expression(value));
1080-
}
1081-
1086+
/**
1087+
* Create a {@code &lt;&gt;} (not equals) predicate.
1088+
*
1089+
* @param value
1090+
* @return
1091+
*/
10821092
Predicate neq(Expression value);
10831093
}
10841094

@@ -1243,7 +1253,7 @@ record InPredicate(Expression path, String operator, Expression predicate) imple
12431253
@Override
12441254
public String render(RenderContext context) {
12451255

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
12471257
return "%s %s (%s)".formatted(path.render(context), operator, predicate.render(context));
12481258
}
12491259

0 commit comments

Comments
 (0)