Skip to content

Commit 3fe0bce

Browse files
committed
DATAJDBC-309 - Add condition creation methods to Column.
Column objects can now create conditions for a fluent DSL. .where(left.isGreater(right) as in StatementBuilder.select(left).from(table).where(left.isGreater(right)).build().
1 parent be87845 commit 3fe0bce

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,108 @@ public Column from(Table table) {
9797
return new Column(name, table);
9898
}
9999

100+
// -------------------------------------------------------------------------
101+
// Methods for Condition creation.
102+
// -------------------------------------------------------------------------
103+
104+
/**
105+
* Creates a {@code =} (equals) {@link Condition}.
106+
*
107+
* @param expression right side of the comparison.
108+
* @return the {@link Comparison} condition.
109+
*/
110+
public Comparison isEqualTo(Expression expression) {
111+
return Conditions.isEqual(this, expression);
112+
}
113+
114+
/**
115+
* Creates a {@code !=} (not equals) {@link Condition}.
116+
*
117+
* @param expression right side of the comparison.
118+
* @return the {@link Comparison} condition.
119+
*/
120+
public Comparison isNotEqualTo(Expression expression) {
121+
return Conditions.isNotEqual(this, expression);
122+
}
123+
124+
/**
125+
* Creates a {@code <} (less) {@link Condition} {@link Condition}.
126+
*
127+
* @param expression right side of the comparison.
128+
* @return the {@link Comparison} condition.
129+
*/
130+
public Comparison isLess(Expression expression) {
131+
return Conditions.isLess(this, expression);
132+
}
133+
134+
/**
135+
* CCreates a {@code <=} (greater ) {@link Condition} {@link Condition}.
136+
*
137+
* @param expression right side of the comparison.
138+
* @return the {@link Comparison} condition.
139+
*/
140+
public Comparison isLessOrEqualTo(Expression expression) {
141+
return Conditions.isLessOrEqualTo(this, expression);
142+
}
143+
144+
/**
145+
* Creates a {@code !=} (not equals) {@link Condition}.
146+
*
147+
* @param expression right side of the comparison.
148+
* @return the {@link Comparison} condition.
149+
*/
150+
public Comparison isGreater(Expression expression) {
151+
return Conditions.isGreater(this, expression);
152+
}
153+
154+
/**
155+
* Creates a {@code <=} (greater or equal to) {@link Condition} {@link Condition}.
156+
*
157+
* @param expression right side of the comparison.
158+
* @return the {@link Comparison} condition.
159+
*/
160+
public Comparison isGreaterOrEqualTo(Expression expression) {
161+
return Conditions.isGreaterOrEqualTo(this, expression);
162+
}
163+
164+
/**
165+
* Creates a {@code LIKE} {@link Condition}.
166+
*
167+
* @param expression right side of the comparison.
168+
* @return the {@link Like} condition.
169+
*/
170+
public Like like(Expression expression) {
171+
return Conditions.like(this, expression);
172+
}
173+
174+
/**
175+
* Creates a new {@link In} {@link Condition} given right {@link Expression}s.
176+
*
177+
* @param expression right side of the comparison.
178+
* @return the {@link In} condition.
179+
*/
180+
public In in(Expression... expression) {
181+
return Conditions.in(this, expression);
182+
}
183+
184+
/**
185+
* Creates a {@code IS NULL} condition.
186+
*
187+
* @return the {@link IsNull} condition.
188+
*/
189+
public IsNull isNull() {
190+
return Conditions.isNull(this);
191+
}
192+
193+
/**
194+
* Creates a {@code IS NOT NULL} condition.
195+
*
196+
* @return the {@link Condition} condition.
197+
*/
198+
public Condition isNotNull() {
199+
return isNull().not();
200+
}
201+
100202
/*
101203
* (non-Javadoc)
102204
* @see org.springframework.data.relational.core.sql.Named#getName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.relational.core.sql.render;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.data.relational.core.sql.Column;
22+
import org.springframework.data.relational.core.sql.StatementBuilder;
23+
import org.springframework.data.relational.core.sql.Table;
24+
25+
/**
26+
* Unit tests for rendered {@link org.springframework.data.relational.core.sql.Conditions}.
27+
*
28+
* @author Mark Paluch
29+
*/
30+
public class ConditionRendererUnitTests {
31+
32+
Table table = Table.create("my_table");
33+
Column left = table.column("left");
34+
Column right = table.column("right");
35+
36+
@Test // DATAJDBC-309
37+
public void shouldRenderEquals() {
38+
39+
String sql = NaiveSqlRenderer
40+
.render(StatementBuilder.select(left).from(table).where(left.isEqualTo(right)).build());
41+
42+
assertThat(sql).endsWith("WHERE my_table.left = my_table.right");
43+
}
44+
45+
@Test // DATAJDBC-309
46+
public void shouldRenderNotEquals() {
47+
48+
String sql = NaiveSqlRenderer
49+
.render(StatementBuilder.select(left).from(table).where(left.isNotEqualTo(right)).build());
50+
51+
assertThat(sql).endsWith("WHERE my_table.left != my_table.right");
52+
53+
sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isEqualTo(right).not()).build());
54+
55+
assertThat(sql).endsWith("WHERE my_table.left != my_table.right");
56+
}
57+
58+
@Test // DATAJDBC-309
59+
public void shouldRenderIsLess() {
60+
61+
String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isLess(right)).build());
62+
63+
assertThat(sql).endsWith("WHERE my_table.left < my_table.right");
64+
}
65+
66+
@Test // DATAJDBC-309
67+
public void shouldRenderIsLessOrEqualTo() {
68+
69+
String sql = NaiveSqlRenderer
70+
.render(StatementBuilder.select(left).from(table).where(left.isLessOrEqualTo(right)).build());
71+
72+
assertThat(sql).endsWith("WHERE my_table.left <= my_table.right");
73+
}
74+
75+
@Test // DATAJDBC-309
76+
public void shouldRenderIsGreater() {
77+
78+
String sql = NaiveSqlRenderer
79+
.render(StatementBuilder.select(left).from(table).where(left.isGreater(right)).build());
80+
81+
assertThat(sql).endsWith("WHERE my_table.left > my_table.right");
82+
}
83+
84+
@Test // DATAJDBC-309
85+
public void shouldRenderIsGreaterOrEqualTo() {
86+
87+
String sql = NaiveSqlRenderer
88+
.render(StatementBuilder.select(left).from(table).where(left.isGreaterOrEqualTo(right)).build());
89+
90+
assertThat(sql).endsWith("WHERE my_table.left >= my_table.right");
91+
}
92+
93+
@Test // DATAJDBC-309
94+
public void shouldRenderIn() {
95+
96+
String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.in(right)).build());
97+
98+
assertThat(sql).endsWith("WHERE my_table.left IN (my_table.right)");
99+
}
100+
101+
@Test // DATAJDBC-309
102+
public void shouldRenderLike() {
103+
104+
String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.like(right)).build());
105+
106+
assertThat(sql).endsWith("WHERE my_table.left LIKE my_table.right");
107+
}
108+
109+
@Test // DATAJDBC-309
110+
public void shouldRenderIsNull() {
111+
112+
String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNull()).build());
113+
114+
assertThat(sql).endsWith("WHERE my_table.left IS NULL");
115+
}
116+
117+
@Test // DATAJDBC-309
118+
public void shouldRenderIsNotNull() {
119+
120+
String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNotNull()).build());
121+
122+
assertThat(sql).endsWith("WHERE my_table.left IS NOT NULL");
123+
124+
sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNull().not()).build());
125+
126+
assertThat(sql).endsWith("WHERE my_table.left IS NOT NULL");
127+
}
128+
}

0 commit comments

Comments
 (0)