Skip to content

Commit 03e15af

Browse files
committed
Refinements.
Introduce type context, apply type conversions for bindings. Simplify criteria sources.
1 parent cadd092 commit 03e15af

File tree

12 files changed

+460
-190
lines changed

12 files changed

+460
-190
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Bindings.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package org.springframework.data.relational.core.query;
1717

18+
import org.springframework.data.relational.core.sql.BindMarker;
19+
1820
/**
1921
* @author Mark Paluch
2022
*/
2123
public interface Bindings {
2224

23-
String bind(Object values);
25+
void bind(BindMarker bindMarker, Object value);
26+
27+
void bind(BindMarker bindMarker, Object value, QueryExpression.ExpressionTypeContext typeContext);
2428

2529
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Criteria.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static CriteriaStep where(String column) {
169169

170170
Assert.hasText(column, "Column name must not be null or empty");
171171

172-
return new DefaultCriteriaStep(CriteriaSource.ofColumn(column), SqlIdentifier.unquoted(column));
172+
return new DefaultCriteriaStep(QueryExpression.column(column), SqlIdentifier.unquoted(column));
173173
}
174174

175175
/**
@@ -183,7 +183,7 @@ public CriteriaStep and(String column) {
183183
Assert.hasText(column, "Column name must not be null or empty");
184184

185185
SqlIdentifier identifier = SqlIdentifier.unquoted(column);
186-
CriteriaSource lhs = CriteriaSource.ofColumn(column);
186+
QueryExpression lhs = QueryExpression.column(column);
187187
return new DefaultCriteriaStep(lhs, identifier) {
188188
@Override
189189
protected Criteria createCriteria(Comparator comparator, @Nullable Object value) {
@@ -250,7 +250,7 @@ public CriteriaStep or(String column) {
250250
Assert.hasText(column, "Column name must not be null or empty");
251251

252252
SqlIdentifier identifier = SqlIdentifier.unquoted(column);
253-
CriteriaSource lhs = CriteriaSource.ofColumn(column);
253+
QueryExpression lhs = QueryExpression.column(column);
254254
return new DefaultCriteriaStep(lhs, identifier) {
255255
@Override
256256
protected Criteria createCriteria(Comparator comparator, @Nullable Object value) {

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/CriteriaContext.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/CriteriaSources.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,51 @@
1919
import org.springframework.data.relational.core.sql.SqlIdentifier;
2020

2121
/**
22+
* Utility class providing implementations of {@link CriteriaSource} for common sources such as columns and SQL
23+
* identifiers.
24+
*
2225
* @author Mark Paluch
26+
* @since 4.0
2327
*/
2428
class CriteriaSources {
25-
public static final record Column(String name) implements CriteriaSource {
29+
30+
/**
31+
* A column name (or property path) reference.
32+
*
33+
* @param name
34+
*/
35+
public record Column(String name) implements QueryExpression {
2636

2737
@Override
28-
public QueryRenderContext contextualize(QueryRenderContext context) {
29-
return context.withProperty(name);
38+
public ExpressionTypeContext getType(EvaluationContext context) {
39+
return context.getColumn(name);
3040
}
3141

3242
@Override
33-
public Expression render(QueryRenderContext context) {
34-
return context.getColumnName(name);
43+
public Expression evaluate(EvaluationContext context) {
44+
return context.getColumn(name).toExpression();
3545
}
46+
3647
}
3748

38-
public static final record SqlIdentifierSource(SqlIdentifier identifier) implements CriteriaSource {
49+
/**
50+
* A column or alias name.
51+
*
52+
* @param identifier
53+
*/
54+
public record SqlIdentifierSource(SqlIdentifier identifier) implements QueryExpression {
3955

4056
@Override
41-
public QueryRenderContext contextualize(QueryRenderContext context) {
42-
return context.withProperty(identifier);
57+
public ExpressionTypeContext getType(EvaluationContext context) {
58+
return context.getColumn(identifier);
4359
}
4460

61+
4562
@Override
46-
public Expression render(QueryRenderContext context) {
47-
return context.getColumnName(identifier);
63+
public Expression evaluate(EvaluationContext context) {
64+
return context.getColumn(identifier).toExpression();
4865
}
66+
4967
}
68+
5069
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/NestedQueryExpression.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.springframework.data.relational.core.sql.Expressions;
2020

2121
/**
22+
* Query expression that nests another {@link QueryExpression}. This is used to ensure that the nested expression
23+
*
2224
* @author Mark Paluch
2325
*/
2426
class NestedQueryExpression implements QueryExpression {
@@ -35,13 +37,13 @@ public QueryExpression nest() {
3537
}
3638

3739
@Override
38-
public QueryRenderContext contextualize(QueryRenderContext context) {
39-
return expression.contextualize(context);
40+
public ExpressionTypeContext getType(EvaluationContext context) {
41+
return expression.getType(context);
4042
}
4143

4244
@Override
43-
public Expression render(QueryRenderContext context) {
44-
return Expressions.nest(expression.render(context));
45+
public Expression evaluate(EvaluationContext context) {
46+
return Expressions.nest(expression.evaluate(context));
4547
}
4648

4749
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/PgSql.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public PgCriteria.PostgresCriteriaStep and(String column) {
165165

166166
Assert.hasText(column, "Column name must not be null or empty");
167167

168-
return and(CriteriaSource.ofColumn(column));
168+
return and(QueryExpression.column(column));
169169
}
170170

171171
@Override
@@ -181,7 +181,7 @@ public PgCriteria.PostgresCriteriaStep or(String column) {
181181

182182
Assert.hasText(column, "Column name must not be null or empty");
183183

184-
return or(CriteriaSource.ofColumn(column));
184+
return or(QueryExpression.column(column));
185185
}
186186

187187
@Override
@@ -251,7 +251,7 @@ public interface PostgresArrayCriteriaStep {
251251
* @property
252252
*/
253253
default PgCriteria contains(String column) {
254-
return contains(CriteriaSource.ofColumn(column));
254+
return contains(QueryExpression.column(column));
255255
}
256256

257257
/**
@@ -280,7 +280,7 @@ default PgCriteria contains(Object... values) {
280280
* @return
281281
*/
282282
default PgCriteria overlaps(String column) {
283-
return overlaps(CriteriaSource.ofColumn(column));
283+
return overlaps(QueryExpression.column(column));
284284
}
285285

286286
/**
@@ -301,7 +301,7 @@ default PgCriteria overlaps(Object... values) {
301301
public interface PostgresJsonCriteriaStep {
302302

303303
default PgCriteria exists(String column) {
304-
return exists(CriteriaSource.ofColumn(column));
304+
return exists(QueryExpression.column(column));
305305
}
306306

307307
/**
@@ -323,7 +323,7 @@ default PgCriteria exists(Object value) {
323323
}
324324

325325
default PgCriteria contains(String field) {
326-
return contains(CriteriaSource.ofColumn(field));
326+
return contains(QueryExpression.column(field));
327327
}
328328

329329
PgCriteria contains(Object value);
@@ -518,7 +518,7 @@ public interface ArrayOperators {
518518
* @property
519519
*/
520520
default PostgresQueryExpression contains(String column) {
521-
return contains(CriteriaSource.ofColumn(column));
521+
return contains(QueryExpression.column(column));
522522
}
523523

524524
/**
@@ -547,7 +547,7 @@ default PostgresQueryExpression contains(Object... values) {
547547
* @return
548548
*/
549549
default PostgresQueryExpression overlaps(String column) {
550-
return overlaps(CriteriaSource.ofColumn(column));
550+
return overlaps(QueryExpression.column(column));
551551
}
552552

553553
/**
@@ -577,7 +577,7 @@ default PostgresQueryExpression overlaps(Object... values) {
577577
* @return
578578
*/
579579
default PostgresQueryExpression concatWith(String column) {
580-
return concatWith(CriteriaSource.ofColumn(column));
580+
return concatWith(QueryExpression.column(column));
581581
}
582582

583583
/**
@@ -604,7 +604,7 @@ public interface PostgresQueryExpression extends QueryExpression {
604604
* @return
605605
*/
606606
default PostgresQueryExpression as(String type) {
607-
return new PgSqlImpl.AppendingPostgresExpression(this, "::" + type);
607+
return PgSqlImpl.PgCastExpression.create(this, type);
608608
}
609609

610610
/**

0 commit comments

Comments
 (0)