Deprecate defunct SimpleCondition.

SimpleCondition was defunct and is now deprecated.
An equivalent convenience function is added to Comparison.

Closes #1034
This commit is contained in:
Jens Schauder
2021-08-26 08:28:43 +02:00
parent aa51d637fb
commit c7b629a5b0
6 changed files with 56 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.util.Assert;
* Results in a rendered condition: {@code <left> <comparator> <right>} (e.g. {@code col = 'predicate'}.
*
* @author Mark Paluch
* @author Jens Schauder
* @since 1.1
*/
public class Comparison extends AbstractSegment implements Condition {
@@ -58,6 +59,24 @@ public class Comparison extends AbstractSegment implements Condition {
return new Comparison(leftColumnOrExpression, comparator, rightColumnOrExpression);
}
/**
* Creates a new {@link Comparison} from simple {@literal StringP} arguments
* @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}.
* @param comparator must not be {@literal null}.
* @param rightValue is considered a {@link Literal}. Must not be {@literal null}.
* @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}.
*
* @since 2.3
*/
public static Comparison create(String unqualifiedColumnName, String comparator, Object rightValue) {
Assert.notNull(unqualifiedColumnName, "UnqualifiedColumnName must not be null.");
Assert.notNull(comparator, "Comparator must not be null.");
Assert.notNull(rightValue, "RightValue must not be null.");
return new Comparison(Expressions.just(unqualifiedColumnName), comparator, SQL.literalOf(rightValue));
}
@Override
public Condition not() {

View File

@@ -20,7 +20,9 @@ package org.springframework.data.relational.core.sql;
*
* @author Mark Paluch
* @since 1.1
* @deprecated since 2.2.5 use {@link Comparison} instead.
*/
@Deprecated
public class SimpleCondition extends AbstractSegment implements Condition {
private final Expression expression;
@@ -40,11 +42,6 @@ public class SimpleCondition extends AbstractSegment implements Condition {
/**
* Creates a simple {@link Condition} given {@code column}, {@code comparator} and {@code predicate}.
*
* @param column
* @param comparator
* @param predicate
* @return
*/
public static SimpleCondition create(String column, String comparator, String predicate) {
return new SimpleCondition(new Column(column, null), comparator, predicate);

View File

@@ -19,7 +19,6 @@ import org.springframework.data.relational.core.sql.BindMarker;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Literal;
import org.springframework.data.relational.core.sql.Named;
import org.springframework.data.relational.core.sql.SimpleFunction;
import org.springframework.data.relational.core.sql.SubselectExpression;
@@ -79,7 +78,7 @@ class ExpressionVisitor extends TypedSubtreeVisitor<Expression> implements PartR
} else {
value = segment.toString();
}
} else if (segment instanceof Literal) {
} else { // works for Literal and SimpleExpression and possibly more
value = segment.toString();
}

View File

@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
* Unit tests for {@link DeleteValidator}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class DeleteValidatorUnitTests {
@@ -35,7 +36,7 @@ public class DeleteValidatorUnitTests {
assertThatThrownBy(() -> {
StatementBuilder.delete() //
.from(bar) //
.where(new SimpleCondition(column, "=", "foo")) //
.where(column.isEqualTo(SQL.literalOf("foo"))) //
.build();
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Required table [table] by a WHERE predicate not imported by FROM [bar]");

View File

@@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.*;
* Unit tests for {@link SelectValidator}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class SelectValidatorUnitTests {
@@ -83,7 +84,7 @@ public class SelectValidatorUnitTests {
assertThatThrownBy(() -> {
StatementBuilder.select(bar.column("foo")) //
.from(bar) //
.where(new SimpleCondition(column, "=", "foo")) //
.where(column.isEqualTo(SQL.literalOf("foo"))) //
.build();
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Required table [table] by a WHERE predicate not imported by FROM [bar] or JOIN []");

View File

@@ -21,15 +21,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.data.relational.core.dialect.PostgresDialect;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.Expressions;
import org.springframework.data.relational.core.sql.Functions;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.Table;
import org.springframework.data.relational.core.sql.*;
import org.springframework.util.StringUtils;
/**
@@ -349,4 +341,33 @@ public class SelectRendererUnitTests {
assertThat(rendered).isEqualTo(
"SELECT COUNT(\"my_table\".*) AS counter, \"my_table\".\"reserved_keyword\" FROM \"my_table\" JOIN \"join_table\" ON \"my_table\".source = \"join_table\".target");
}
@Test // GH-1034
void simpleComparisonWithStringArguments() {
Table table_user = SQL.table("User");
Select select = StatementBuilder
.select(table_user.column("name"),table_user.column("age"))
.from(table_user)
.where(Comparison.create("age",">",20))
.build();
final String rendered = SqlRenderer.toString(select);
assertThat(rendered).isEqualTo("SELECT User.name, User.age FROM User WHERE age > 20");
}
@Test // GH-1034
void simpleComparison() {
Table table_user = SQL.table("User");
Select select = StatementBuilder
.select(table_user.column("name"),table_user.column("age"))
.from(table_user)
.where(Comparison.create(table_user.column("age"),">",SQL.literalOf(20)))
.build();
final String rendered = SqlRenderer.toString(select);
assertThat(rendered).isEqualTo("SELECT User.name, User.age FROM User WHERE User.age > 20");
}
}