Skip to content

Commit 221773b

Browse files
committed
DATAJDBC-309 - Polishing.
Extend In to multi-expression argument. Introduce helper methods in Table to create multiple columns. Introduce factory method on StatementBuilder to create a new builder given a collection of expressions.
1 parent edec1ce commit 221773b

File tree

5 files changed

+151
-9
lines changed

5 files changed

+151
-9
lines changed

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

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

18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
1822
import org.springframework.util.Assert;
1923

2024
/**
@@ -69,11 +73,41 @@ public static Equals isEqual(Expression leftColumnOrExpression, Expression right
6973
public static Condition in(Expression columnOrExpression, Expression arg) {
7074

7175
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
72-
Assert.notNull(columnOrExpression, "Expression argument must not be null");
76+
Assert.notNull(arg, "Expression argument must not be null");
7377

7478
return In.create(columnOrExpression, arg);
7579
}
7680

81+
/**
82+
* Creates a new {@link In} {@link Condition} given left and right {@link Expression}s.
83+
*
84+
* @param columnOrExpression left hand side of the {@link Condition} must not be {@literal null}.
85+
* @param expressions right hand side (collection {@link Expression}) must not be {@literal null}.
86+
* @return the {@link In} {@link Condition}.
87+
*/
88+
public static Condition in(Expression columnOrExpression, Collection<? extends Expression> expressions) {
89+
90+
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
91+
Assert.notNull(expressions, "Expression argument must not be null");
92+
93+
return In.create(columnOrExpression, new ArrayList<>(expressions));
94+
}
95+
96+
/**
97+
* Creates a new {@link In} {@link Condition} given left and right {@link Expression}s.
98+
*
99+
* @param columnOrExpression left hand side of the {@link Condition} must not be {@literal null}.
100+
* @param expressions right hand side (collection {@link Expression}) must not be {@literal null}.
101+
* @return the {@link In} {@link Condition}.
102+
*/
103+
public static Condition in(Expression columnOrExpression, Expression... expressions) {
104+
105+
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
106+
Assert.notNull(expressions, "Expression argument must not be null");
107+
108+
return In.create(columnOrExpression, Arrays.asList(expressions));
109+
}
110+
77111
/**
78112
* Creates a {@code IN} {@link Condition clause} for a {@link Select subselect}.
79113
*

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

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,90 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.StringUtils;
25+
1826
/**
1927
* {@code IN} {@link Condition} clause.
2028
*
21-
* @author Jens Schauder TODO: Accept multiple Expressions.
29+
* @author Jens Schauder
30+
* @author Mark Paluch
2231
*/
2332
public class In extends AbstractSegment implements Condition {
2433

2534
private final Expression left;
26-
private final Expression right;
35+
private final Collection<Expression> expressions;
2736

28-
private In(Expression left, Expression right) {
37+
private In(Expression left, Collection<Expression> expressions) {
2938

30-
super(left, right);
39+
super(toArray(left, expressions));
3140

3241
this.left = left;
33-
this.right = right;
42+
this.expressions = expressions;
3443
}
3544

45+
private static Segment[] toArray(Expression expression, Collection<Expression> expressions) {
46+
47+
Segment[] segments = new Segment[1 + expressions.size()];
48+
segments[0] = expression;
49+
50+
int index = 1;
51+
52+
for (Expression e : expressions) {
53+
segments[index++] = e;
54+
}
55+
56+
return segments;
57+
}
58+
59+
/**
60+
* Creates a new {@link In} {@link Condition} given left and right {@link Expression}s.
61+
*
62+
* @param columnOrExpression left hand side of the {@link Condition} must not be {@literal null}.
63+
* @param arg right hand side (collection {@link Expression}) must not be {@literal null}.
64+
* @return the {@link In} {@link Condition}.
65+
*/
3666
public static Condition create(Expression columnOrExpression, Expression arg) {
37-
return new In(columnOrExpression, arg);
67+
68+
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
69+
Assert.notNull(arg, "Expression argument must not be null");
70+
71+
return new In(columnOrExpression, Collections.singletonList(arg));
72+
}
73+
74+
/**
75+
* Creates a new {@link In} {@link Condition} given left and right {@link Expression}s.
76+
*
77+
* @param columnOrExpression left hand side of the {@link Condition} must not be {@literal null}.
78+
* @param expressions right hand side (collection {@link Expression}) must not be {@literal null}.
79+
* @return the {@link In} {@link Condition}.
80+
*/
81+
public static Condition create(Expression columnOrExpression, Collection<? extends Expression> expressions) {
82+
83+
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
84+
Assert.notNull(expressions, "Expression argument must not be null");
85+
86+
return new In(columnOrExpression, new ArrayList<>(expressions));
87+
}
88+
89+
/**
90+
* Creates a new {@link In} {@link Condition} given left and right {@link Expression}s.
91+
*
92+
* @param columnOrExpression left hand side of the {@link Condition} must not be {@literal null}.
93+
* @param expressions right hand side (collection {@link Expression}) must not be {@literal null}.
94+
* @return the {@link In} {@link Condition}.
95+
*/
96+
public static Condition create(Expression columnOrExpression, Expression... expressions) {
97+
98+
Assert.notNull(columnOrExpression, "Comparison column or expression must not be null");
99+
Assert.notNull(expressions, "Expression argument must not be null");
100+
101+
return new In(columnOrExpression, Arrays.asList(expressions));
38102
}
39103

40104
/*
@@ -43,6 +107,6 @@ public static Condition create(Expression columnOrExpression, Expression arg) {
43107
*/
44108
@Override
45109
public String toString() {
46-
return left + " IN " + right;
110+
return left + " IN (" + StringUtils.collectionToDelimitedString(expressions, ", ") + ")";
47111
}
48112
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18+
import java.util.Collection;
19+
1820
import org.springframework.data.relational.core.sql.SelectBuilder.SelectAndFrom;
1921

2022
/**
@@ -50,6 +52,17 @@ public static SelectAndFrom select(Expression... expressions) {
5052
return Select.builder().select(expressions);
5153
}
5254

55+
/**
56+
* Include one or more {@link Expression}s in the select list.
57+
*
58+
* @param expressions the expressions to include.
59+
* @return {@code this} builder.
60+
* @see Table#columns(String...)
61+
*/
62+
public static SelectAndFrom select(Collection<? extends Expression> expressions) {
63+
return Select.builder().select(expressions);
64+
}
65+
5366
/**
5467
* Creates a new {@link SelectBuilder}.
5568
*

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.springframework.data.relational.core.sql;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.List;
2022

2123
import org.springframework.util.Assert;
@@ -107,6 +109,22 @@ public List<Column> columns(String... names) {
107109

108110
Assert.notNull(names, "Names must not be null");
109111

112+
return columns(Arrays.asList(names));
113+
}
114+
115+
/**
116+
* Creates a {@link List} of {@link Column}s associated with this {@link Table}.
117+
* <p/>
118+
* Note: This {@link Table} does not track column creation and there is no possibility to enumerate all
119+
* {@link Column}s that were created for this table.
120+
*
121+
* @param names column names, must not be {@literal null} or empty.
122+
* @return a new {@link List} of {@link Column}s associated with this {@link Table}.
123+
*/
124+
public List<Column> columns(Collection<String> names) {
125+
126+
Assert.notNull(names, "Names must not be null");
127+
110128
List<Column> columns = new ArrayList<>();
111129
for (String name : names) {
112130
columns.add(column(name));
@@ -139,7 +157,7 @@ public String getName() {
139157

140158
/**
141159
* @return the table name as it is used in references. This can be the actual {@link #getName() name} or an
142-
* {@link Aliased#getAlias() alias}.
160+
* {@link Aliased#getAlias() alias}.
143161
*/
144162
public String getReferenceName() {
145163
return name;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ public void shouldInWithNamedParameter() {
217217
assertThat(NaiveSqlRenderer.render(select)).isEqualTo("SELECT foo.bar FROM foo WHERE foo.bar IN (:name)");
218218
}
219219

220+
@Test // DATAJDBC-309
221+
public void shouldInWithNamedParameters() {
222+
223+
Table table = SQL.table("foo");
224+
Column bar = table.column("bar");
225+
226+
Select select = Select.builder().select(bar).from(table).where(
227+
Conditions.in(bar, SQL.bindMarker(":name"), SQL.bindMarker(":name2"))
228+
).build();
229+
230+
assertThat(NaiveSqlRenderer.render(select)).isEqualTo("SELECT foo.bar FROM foo WHERE foo.bar IN (:name, :name2)");
231+
}
232+
220233
@Test // DATAJDBC-309
221234
public void shouldRenderInSubselect() {
222235

0 commit comments

Comments
 (0)