Skip to content

Commit c7b629a

Browse files
committed
Deprecate defunct SimpleCondition.
SimpleCondition was defunct and is now deprecated. An equivalent convenience function is added to Comparison. Closes #1034
1 parent aa51d63 commit c7b629a

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Comparison.java

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Results in a rendered condition: {@code <left> <comparator> <right>} (e.g. {@code col = 'predicate'}.
2424
*
2525
* @author Mark Paluch
26+
* @author Jens Schauder
2627
* @since 1.1
2728
*/
2829
public class Comparison extends AbstractSegment implements Condition {
@@ -58,6 +59,24 @@ public static Comparison create(Expression leftColumnOrExpression, String compar
5859
return new Comparison(leftColumnOrExpression, comparator, rightColumnOrExpression);
5960
}
6061

62+
/**
63+
* Creates a new {@link Comparison} from simple {@literal StringP} arguments
64+
* @param unqualifiedColumnName gets turned in a {@link Expressions#just(String)} and is expected to be an unqualified unique column name but also could be an verbatim expression. Must not be {@literal null}.
65+
* @param comparator must not be {@literal null}.
66+
* @param rightValue is considered a {@link Literal}. Must not be {@literal null}.
67+
* @return a new {@literal Comparison} of the first with the third argument using the second argument as comparison operator. Guaranteed to be not {@literal null}.
68+
*
69+
* @since 2.3
70+
*/
71+
public static Comparison create(String unqualifiedColumnName, String comparator, Object rightValue) {
72+
73+
Assert.notNull(unqualifiedColumnName, "UnqualifiedColumnName must not be null.");
74+
Assert.notNull(comparator, "Comparator must not be null.");
75+
Assert.notNull(rightValue, "RightValue must not be null.");
76+
77+
return new Comparison(Expressions.just(unqualifiedColumnName), comparator, SQL.literalOf(rightValue));
78+
}
79+
6180
@Override
6281
public Condition not() {
6382

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SimpleCondition.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*
2121
* @author Mark Paluch
2222
* @since 1.1
23+
* @deprecated since 2.2.5 use {@link Comparison} instead.
2324
*/
25+
@Deprecated
2426
public class SimpleCondition extends AbstractSegment implements Condition {
2527

2628
private final Expression expression;
@@ -40,11 +42,6 @@ public class SimpleCondition extends AbstractSegment implements Condition {
4042

4143
/**
4244
* Creates a simple {@link Condition} given {@code column}, {@code comparator} and {@code predicate}.
43-
*
44-
* @param column
45-
* @param comparator
46-
* @param predicate
47-
* @return
4845
*/
4946
public static SimpleCondition create(String column, String comparator, String predicate) {
5047
return new SimpleCondition(new Column(column, null), comparator, predicate);

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.data.relational.core.sql.Column;
2020
import org.springframework.data.relational.core.sql.Condition;
2121
import org.springframework.data.relational.core.sql.Expression;
22-
import org.springframework.data.relational.core.sql.Literal;
2322
import org.springframework.data.relational.core.sql.Named;
2423
import org.springframework.data.relational.core.sql.SimpleFunction;
2524
import org.springframework.data.relational.core.sql.SubselectExpression;
@@ -79,7 +78,7 @@ Delegation enterMatched(Expression segment) {
7978
} else {
8079
value = segment.toString();
8180
}
82-
} else if (segment instanceof Literal) {
81+
} else { // works for Literal and SimpleExpression and possibly more
8382
value = segment.toString();
8483
}
8584

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/DeleteValidatorUnitTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Unit tests for {@link DeleteValidator}.
2424
*
2525
* @author Mark Paluch
26+
* @author Jens Schauder
2627
*/
2728
public class DeleteValidatorUnitTests {
2829

@@ -35,7 +36,7 @@ public void shouldReportMissingTableForDeleteViaWhere() {
3536
assertThatThrownBy(() -> {
3637
StatementBuilder.delete() //
3738
.from(bar) //
38-
.where(new SimpleCondition(column, "=", "foo")) //
39+
.where(column.isEqualTo(SQL.literalOf("foo"))) //
3940
.build();
4041
}).isInstanceOf(IllegalStateException.class)
4142
.hasMessageContaining("Required table [table] by a WHERE predicate not imported by FROM [bar]");

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/SelectValidatorUnitTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Unit tests for {@link SelectValidator}.
2424
*
2525
* @author Mark Paluch
26+
* @author Jens Schauder
2627
*/
2728
public class SelectValidatorUnitTests {
2829

@@ -83,7 +84,7 @@ public void shouldReportMissingTableViaWhere() {
8384
assertThatThrownBy(() -> {
8485
StatementBuilder.select(bar.column("foo")) //
8586
.from(bar) //
86-
.where(new SimpleCondition(column, "=", "foo")) //
87+
.where(column.isEqualTo(SQL.literalOf("foo"))) //
8788
.build();
8889
}).isInstanceOf(IllegalStateException.class)
8990
.hasMessageContaining("Required table [table] by a WHERE predicate not imported by FROM [bar] or JOIN []");

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

+30-9
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,7 @@
2121

2222
import org.springframework.data.relational.core.dialect.PostgresDialect;
2323
import org.springframework.data.relational.core.dialect.RenderContextFactory;
24-
import org.springframework.data.relational.core.sql.Column;
25-
import org.springframework.data.relational.core.sql.Conditions;
26-
import org.springframework.data.relational.core.sql.Expressions;
27-
import org.springframework.data.relational.core.sql.Functions;
28-
import org.springframework.data.relational.core.sql.OrderByField;
29-
import org.springframework.data.relational.core.sql.SQL;
30-
import org.springframework.data.relational.core.sql.Select;
31-
import org.springframework.data.relational.core.sql.SqlIdentifier;
32-
import org.springframework.data.relational.core.sql.Table;
24+
import org.springframework.data.relational.core.sql.*;
3325
import org.springframework.util.StringUtils;
3426

3527
/**
@@ -349,4 +341,33 @@ public void shouldRenderWithRenderContext() {
349341
assertThat(rendered).isEqualTo(
350342
"SELECT COUNT(\"my_table\".*) AS counter, \"my_table\".\"reserved_keyword\" FROM \"my_table\" JOIN \"join_table\" ON \"my_table\".source = \"join_table\".target");
351343
}
344+
345+
346+
@Test // GH-1034
347+
void simpleComparisonWithStringArguments() {
348+
349+
Table table_user = SQL.table("User");
350+
Select select = StatementBuilder
351+
.select(table_user.column("name"),table_user.column("age"))
352+
.from(table_user)
353+
.where(Comparison.create("age",">",20))
354+
.build();
355+
356+
final String rendered = SqlRenderer.toString(select);
357+
assertThat(rendered).isEqualTo("SELECT User.name, User.age FROM User WHERE age > 20");
358+
}
359+
360+
@Test // GH-1034
361+
void simpleComparison() {
362+
363+
Table table_user = SQL.table("User");
364+
Select select = StatementBuilder
365+
.select(table_user.column("name"),table_user.column("age"))
366+
.from(table_user)
367+
.where(Comparison.create(table_user.column("age"),">",SQL.literalOf(20)))
368+
.build();
369+
370+
final String rendered = SqlRenderer.toString(select);
371+
assertThat(rendered).isEqualTo("SELECT User.name, User.age FROM User WHERE User.age > 20");
372+
}
352373
}

0 commit comments

Comments
 (0)