Skip to content

Commit 307fe51

Browse files
committed
DATAJDBC-309 - Polishing.
Javadoc, static factory methods, typos. Refactor SQL rendering from a shared stack-based implementation to independent delegating visitors. Introduce DelegatingVisitor and TypedSubtreeVisitor base classes. Introduce SelectList container. Extract nested renderes to top-level types. Move SQL renderer to renderer package. 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. Add support for comparison conditions and LIKE and equal/not equal/less with equals to/greater with equals to conditions. Add condition creation methods to Column so Column objects can now create conditions for a fluent DSL as in (.where(left.isGreater(right)). StatementBuilder.select(left).from(table).where(left.isGreater(right)).build(). Introduce RenderContext and RenderNamingStrategy. Add since tags. Improve Javadoc. Original pull request: #119.
1 parent 1c9fad8 commit 307fe51

File tree

74 files changed

+3569
-901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3569
-901
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@
2121
* Abstract implementation to support {@link Segment} implementations.
2222
*
2323
* @author Mark Paluch
24+
* @since 1.1
2425
*/
2526
abstract class AbstractSegment implements Segment {
2627

2728
private final Segment[] children;
2829

29-
protected AbstractSegment(Segment ... children) {
30+
protected AbstractSegment(Segment... children) {
3031
this.children = children;
3132
}
3233

34+
/*
35+
* (non-Javadoc)
36+
* @see org.springframework.data.relational.core.sql.Visitable#visit(org.springframework.data.relational.core.sql.Visitor)
37+
*/
3338
@Override
3439
public void visit(Visitor visitor) {
3540

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Aliased element exposing an {@link #getAlias() alias}.
2020
*
2121
* @author Mark Paluch
22+
* @since 1.1
2223
*/
2324
public interface Aliased {
2425

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
* An expression with an alias.
2020
*
2121
* @author Jens Schauder
22+
* @since 1.1
2223
*/
23-
public class AliasedExpression extends AbstractSegment implements Aliased, Expression {
24+
class AliasedExpression extends AbstractSegment implements Aliased, Expression {
2425

2526
private final Expression expression;
2627
private final String alias;
@@ -33,11 +34,19 @@ public AliasedExpression(Expression expression, String alias) {
3334
this.alias = alias;
3435
}
3536

37+
/*
38+
* (non-Javadoc)
39+
* @see org.springframework.data.relational.core.sql.Aliased#getAlias()
40+
*/
3641
@Override
3742
public String getAlias() {
3843
return alias;
3944
}
4045

46+
/*
47+
* (non-Javadoc)
48+
* @see java.lang.Object#toString()
49+
*/
4150
@Override
4251
public String toString() {
4352
return expression.toString() + " AS " + alias;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
* {@link Condition} representing an {@code AND} relation between two {@link Condition}s.
2020
*
2121
* @author Mark Paluch
22+
* @since 1.1
2223
* @see Condition#and(Condition)
2324
*/
2425
public class AndCondition extends MultipleCondition {
2526

2627
AndCondition(Condition... conditions) {
2728
super(" AND ", conditions);
2829
}
29-
3030
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18-
import org.springframework.util.Assert;
19-
2018
/**
2119
* {@link Segment} to select all columns from a {@link Table}.
2220
* <p/>
23-
* * Renders to: {@code <table>.*} as in {@code SELECT <table>.* FROM …}.
21+
* * Renders to: {@code
22+
*
23+
<table>
24+
* .*} as in {@code SELECT
25+
*
26+
<table>
27+
* .* FROM …}.
2428
*
2529
* @author Mark Paluch
30+
* @since 1.1
2631
* @see Table#asterisk()
2732
*/
2833
public class AsteriskFromTable extends AbstractSegment implements Expression {
@@ -45,6 +50,10 @@ public Table getTable() {
4550
return table;
4651
}
4752

53+
/*
54+
* (non-Javadoc)
55+
* @see java.lang.Object#toString()
56+
*/
4857
@Override
4958
public String toString() {
5059

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Bind marker/parameter placeholder used to construct prepared statements with parameter substitution.
2222
*
2323
* @author Mark Paluch
24+
* @since 1.1
2425
*/
2526
public class BindMarker extends AbstractSegment implements Expression {
2627

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

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Renders to: {@code <name>} or {@code <table(alias)>.<name>}.
2525
*
2626
* @author Mark Paluch
27+
* @since 1.1
2728
*/
2829
public class Column extends AbstractSegment implements Expression, Named {
2930

@@ -72,7 +73,7 @@ public static Column aliased(String name, Table table, String alias) {
7273
}
7374

7475
/**
75-
* Create a new aliased {@link Column}.
76+
* Creates a new aliased {@link Column}.
7677
*
7778
* @param alias column alias name, must not {@literal null} or empty.
7879
* @return the aliased {@link Column}.
@@ -85,7 +86,7 @@ public Column as(String alias) {
8586
}
8687

8788
/**
88-
* Create a new {@link Column} associated with a {@link Table}.
89+
* Creates a new {@link Column} associated with a {@link Table}.
8990
*
9091
* @param table the table, must not be {@literal null}.
9192
* @return a new {@link Column} associated with {@link Table}.
@@ -97,6 +98,108 @@ public Column from(Table table) {
9798
return new Column(name, table);
9899
}
99100

101+
// -------------------------------------------------------------------------
102+
// Methods for Condition creation.
103+
// -------------------------------------------------------------------------
104+
105+
/**
106+
* Creates a {@code =} (equals) {@link Condition}.
107+
*
108+
* @param expression right side of the comparison.
109+
* @return the {@link Comparison} condition.
110+
*/
111+
public Comparison isEqualTo(Expression expression) {
112+
return Conditions.isEqual(this, expression);
113+
}
114+
115+
/**
116+
* Creates a {@code !=} (not equals) {@link Condition}.
117+
*
118+
* @param expression right side of the comparison.
119+
* @return the {@link Comparison} condition.
120+
*/
121+
public Comparison isNotEqualTo(Expression expression) {
122+
return Conditions.isNotEqual(this, expression);
123+
}
124+
125+
/**
126+
* Creates a {@code <} (less) {@link Condition} {@link Condition}.
127+
*
128+
* @param expression right side of the comparison.
129+
* @return the {@link Comparison} condition.
130+
*/
131+
public Comparison isLess(Expression expression) {
132+
return Conditions.isLess(this, expression);
133+
}
134+
135+
/**
136+
* CCreates a {@code <=} (greater ) {@link Condition} {@link Condition}.
137+
*
138+
* @param expression right side of the comparison.
139+
* @return the {@link Comparison} condition.
140+
*/
141+
public Comparison isLessOrEqualTo(Expression expression) {
142+
return Conditions.isLessOrEqualTo(this, expression);
143+
}
144+
145+
/**
146+
* Creates a {@code !=} (not equals) {@link Condition}.
147+
*
148+
* @param expression right side of the comparison.
149+
* @return the {@link Comparison} condition.
150+
*/
151+
public Comparison isGreater(Expression expression) {
152+
return Conditions.isGreater(this, expression);
153+
}
154+
155+
/**
156+
* Creates a {@code <=} (greater or equal to) {@link Condition} {@link Condition}.
157+
*
158+
* @param expression right side of the comparison.
159+
* @return the {@link Comparison} condition.
160+
*/
161+
public Comparison isGreaterOrEqualTo(Expression expression) {
162+
return Conditions.isGreaterOrEqualTo(this, expression);
163+
}
164+
165+
/**
166+
* Creates a {@code LIKE} {@link Condition}.
167+
*
168+
* @param expression right side of the comparison.
169+
* @return the {@link Like} condition.
170+
*/
171+
public Like like(Expression expression) {
172+
return Conditions.like(this, expression);
173+
}
174+
175+
/**
176+
* Creates a new {@link In} {@link Condition} given right {@link Expression}s.
177+
*
178+
* @param expression right side of the comparison.
179+
* @return the {@link In} condition.
180+
*/
181+
public In in(Expression... expression) {
182+
return Conditions.in(this, expression);
183+
}
184+
185+
/**
186+
* Creates a {@code IS NULL} condition.
187+
*
188+
* @return the {@link IsNull} condition.
189+
*/
190+
public IsNull isNull() {
191+
return Conditions.isNull(this);
192+
}
193+
194+
/**
195+
* Creates a {@code IS NOT NULL} condition.
196+
*
197+
* @return the {@link Condition} condition.
198+
*/
199+
public Condition isNotNull() {
200+
return isNull().not();
201+
}
202+
100203
/*
101204
* (non-Javadoc)
102205
* @see org.springframework.data.relational.core.sql.Named#getName()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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;
17+
18+
import org.springframework.util.Assert;
19+
20+
/**
21+
* Comparing {@link Condition} comparing two {@link Expression}s.
22+
* <p/>
23+
* Results in a rendered condition: {@code <left> <comparator> <right>} (e.g. {@code col = 'predicate'}.
24+
*
25+
* @author Mark Paluch
26+
* @since 1.1
27+
*/
28+
public class Comparison extends AbstractSegment implements Condition {
29+
30+
private final Expression left;
31+
private final String comparator;
32+
private final Expression right;
33+
34+
private Comparison(Expression left, String comparator, Expression right) {
35+
36+
super(left, right);
37+
38+
this.left = left;
39+
this.comparator = comparator;
40+
this.right = right;
41+
}
42+
43+
/**
44+
* Creates a new {@link Comparison} {@link Condition} given two {@link Expression}s.
45+
*
46+
* @param leftColumnOrExpression the left {@link Expression}.
47+
* @param comparator the comparator.
48+
* @param rightColumnOrExpression the right {@link Expression}.
49+
* @return the {@link Comparison} condition.
50+
*/
51+
public static Comparison create(Expression leftColumnOrExpression, String comparator,
52+
Expression rightColumnOrExpression) {
53+
54+
Assert.notNull(leftColumnOrExpression, "Left expression must not be null!");
55+
Assert.notNull(comparator, "Comparator must not be null!");
56+
Assert.notNull(rightColumnOrExpression, "Right expression must not be null!");
57+
58+
return new Comparison(leftColumnOrExpression, comparator, rightColumnOrExpression);
59+
}
60+
61+
@Override
62+
public Condition not() {
63+
64+
if ("=".equals(comparator)) {
65+
return new Comparison(left, "!=", right);
66+
}
67+
68+
if ("!=".equals(comparator)) {
69+
return new Comparison(left, "=", right);
70+
}
71+
72+
return new Not(this);
73+
}
74+
75+
/**
76+
* @return the left {@link Expression}.
77+
*/
78+
public Expression getLeft() {
79+
return left;
80+
}
81+
82+
/**
83+
* @return the comparator.
84+
*/
85+
public String getComparator() {
86+
return comparator;
87+
}
88+
89+
/**
90+
* @return the right {@link Expression}.
91+
*/
92+
public Expression getRight() {
93+
return right;
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return left.toString() + " " + comparator + " " + right.toString();
99+
}
100+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* AST {@link Segment} for a condition.
2020
*
2121
* @author Mark Paluch
22+
* @author Jens Schauder
23+
* @since 1.1
2224
* @see Conditions
2325
*/
2426
public interface Condition extends Segment {
@@ -43,6 +45,11 @@ default Condition or(Condition other) {
4345
return new OrCondition(this, other);
4446
}
4547

48+
/**
49+
* Creates a {@link Condition} that negates this {@link Condition}.
50+
*
51+
* @return the negated {@link Condition}.
52+
*/
4653
default Condition not() {
4754
return new Not(this);
4855
}

0 commit comments

Comments
 (0)