DATAJDBC-490 - Incorporate review feedback.
Rename ConditionGroup to GroupedCondition. Introduce Conditions.group(…) factory method instead of applying grouping via `condition.group()`. Introduce getter to introspect conditions. Original pull request: #193.
This commit is contained in:
committed by
Jens Schauder
parent
c4f024bb50
commit
a4bf09e8e8
@@ -53,14 +53,4 @@ public interface Condition extends Segment {
|
||||
default Condition not() {
|
||||
return new Not(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a {@link Condition} into a condition group that groups the nested {@link Condition} using parentheses.
|
||||
*
|
||||
* @return the grouped {@link Condition}.
|
||||
* @since 2.0
|
||||
*/
|
||||
default Condition group() {
|
||||
return new ConditionGroup(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,18 @@ public abstract class Conditions {
|
||||
return new ConstantCondition(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a grouped {@link Condition} that is enclosed with parentheses. Useful to combine {@code AND} and {@code OR}
|
||||
* statements.
|
||||
*
|
||||
* @param condition the nested condition to be grouped.
|
||||
* @return a {@link GroupedCondition}.
|
||||
* @since 2.0
|
||||
*/
|
||||
public static Condition group(Condition condition) {
|
||||
return new GroupedCondition(condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code IS NULL} condition.
|
||||
*
|
||||
|
||||
@@ -21,9 +21,9 @@ package org.springframework.data.relational.core.sql;
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ConditionGroup extends MultipleCondition implements Condition {
|
||||
public class GroupedCondition extends MultipleCondition implements Condition {
|
||||
|
||||
ConditionGroup(Condition condition) {
|
||||
GroupedCondition(Condition condition) {
|
||||
super("", condition);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ public abstract class MultipleCondition extends AbstractSegment implements Condi
|
||||
this.conditions = Arrays.asList(conditions);
|
||||
}
|
||||
|
||||
public List<Condition> getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.data.relational.core.sql.render;
|
||||
import org.springframework.data.relational.core.sql.AndCondition;
|
||||
import org.springframework.data.relational.core.sql.Comparison;
|
||||
import org.springframework.data.relational.core.sql.Condition;
|
||||
import org.springframework.data.relational.core.sql.ConditionGroup;
|
||||
import org.springframework.data.relational.core.sql.GroupedCondition;
|
||||
import org.springframework.data.relational.core.sql.In;
|
||||
import org.springframework.data.relational.core.sql.IsNull;
|
||||
import org.springframework.data.relational.core.sql.Like;
|
||||
@@ -87,8 +87,8 @@ class ConditionVisitor extends TypedSubtreeVisitor<Condition> implements PartRen
|
||||
return new InVisitor(context, builder::append);
|
||||
}
|
||||
|
||||
if (segment instanceof ConditionGroup) {
|
||||
return new ConditionGroupVisitor(context, builder::append);
|
||||
if (segment instanceof GroupedCondition) {
|
||||
return new GroupedConditionVisitor(context, builder::append);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -16,24 +16,24 @@
|
||||
package org.springframework.data.relational.core.sql.render;
|
||||
|
||||
import org.springframework.data.relational.core.sql.Condition;
|
||||
import org.springframework.data.relational.core.sql.ConditionGroup;
|
||||
import org.springframework.data.relational.core.sql.GroupedCondition;
|
||||
import org.springframework.data.relational.core.sql.Visitable;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Renderer for {@link ConditionGroup}. Uses a {@link RenderTarget} to call back for render results.
|
||||
* Renderer for {@link GroupedCondition}. Uses a {@link RenderTarget} to call back for render results.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
class ConditionGroupVisitor extends TypedSubtreeVisitor<ConditionGroup> {
|
||||
class GroupedConditionVisitor extends TypedSubtreeVisitor<GroupedCondition> {
|
||||
|
||||
private final RenderContext context;
|
||||
private final RenderTarget target;
|
||||
|
||||
private @Nullable ConditionVisitor conditionVisitor;
|
||||
|
||||
ConditionGroupVisitor(RenderContext context, RenderTarget target) {
|
||||
GroupedConditionVisitor(RenderContext context, RenderTarget target) {
|
||||
this.context = context;
|
||||
this.target = target;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.data.relational.core.sql.Column;
|
||||
import org.springframework.data.relational.core.sql.Conditions;
|
||||
import org.springframework.data.relational.core.sql.StatementBuilder;
|
||||
import org.springframework.data.relational.core.sql.Table;
|
||||
|
||||
@@ -46,7 +47,7 @@ public class ConditionRendererUnitTests {
|
||||
public void shouldRenderEqualsGroup() {
|
||||
|
||||
String sql = SqlRenderer
|
||||
.toString(StatementBuilder.select(left).from(table).where(left.isEqualTo(right).group()).build());
|
||||
.toString(StatementBuilder.select(left).from(table).where(Conditions.group(left.isEqualTo(right))).build());
|
||||
|
||||
assertThat(sql).endsWith("WHERE (my_table.left = my_table.right)");
|
||||
}
|
||||
@@ -55,7 +56,7 @@ public class ConditionRendererUnitTests {
|
||||
public void shouldRenderAndGroup() {
|
||||
|
||||
String sql = SqlRenderer.toString(StatementBuilder.select(left).from(table)
|
||||
.where(left.isEqualTo(right).and(left.isGreater(right)).group()).build());
|
||||
.where(Conditions.group(left.isEqualTo(right).and(left.isGreater(right)))).build());
|
||||
|
||||
assertThat(sql).endsWith("WHERE (my_table.left = my_table.right AND my_table.left > my_table.right)");
|
||||
}
|
||||
@@ -64,21 +65,22 @@ public class ConditionRendererUnitTests {
|
||||
public void shouldRenderAndGroupOr() {
|
||||
|
||||
String sql = SqlRenderer.toString(StatementBuilder.select(left).from(table)
|
||||
.where(left.isEqualTo(right).and(left.isGreater(right)).group().or(left.like(right))).build());
|
||||
.where(Conditions.group(left.isEqualTo(right).and(left.isGreater(right))).or(left.like(right))).build());
|
||||
|
||||
assertThat(sql).endsWith(
|
||||
"WHERE (my_table.left = my_table.right AND my_table.left > my_table.right) OR my_table.left LIKE my_table.right");
|
||||
}
|
||||
|
||||
|
||||
@Test // DATAJDBC-490
|
||||
public void shouldRenderAndGroupOrAandGroup() {
|
||||
public void shouldRenderAndGroupOrAndGroup() {
|
||||
|
||||
String sql = SqlRenderer.toString(StatementBuilder.select(left).from(table)
|
||||
.where(left.isEqualTo(right).and(left.isGreater(right)).group().or(left.like(right).and(right.like(left)).group())).build());
|
||||
.where(Conditions.group(left.isEqualTo(right).and(left.isGreater(right)))
|
||||
.or(Conditions.group(left.like(right).and(right.like(left)))))
|
||||
.build());
|
||||
|
||||
assertThat(sql).endsWith(
|
||||
"WHERE (my_table.left = my_table.right AND my_table.left > my_table.right) OR (my_table.left LIKE my_table.right AND my_table.right LIKE my_table.left");
|
||||
"WHERE (my_table.left = my_table.right AND my_table.left > my_table.right) OR (my_table.left LIKE my_table.right AND my_table.right LIKE my_table.left)");
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-309
|
||||
|
||||
Reference in New Issue
Block a user