Skip to content

Commit 1c9fad8

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-309 - Reworked visitor structure towards a more modular design.
Also added support for conditions. Original pull request: #119.
1 parent 10b38c5 commit 1c9fad8

35 files changed

+924
-847
lines changed

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

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

18+
import org.springframework.util.Assert;
19+
1820
/**
1921
* Abstract implementation to support {@link Segment} implementations.
2022
*
2123
* @author Mark Paluch
2224
*/
2325
abstract class AbstractSegment implements Segment {
2426

27+
private final Segment[] children;
28+
29+
protected AbstractSegment(Segment ... children) {
30+
this.children = children;
31+
}
32+
33+
@Override
34+
public void visit(Visitor visitor) {
35+
36+
Assert.notNull(visitor, "Visitor must not be null!");
37+
38+
visitor.enter(this);
39+
for (Segment child : children) {
40+
child.visit(visitor);
41+
}
42+
visitor.leave(this);
43+
}
44+
2545
/*
2646
* (non-Javadoc)
2747
* @see java.lang.Object#hashCode()

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,30 @@
1616
package org.springframework.data.relational.core.sql;
1717

1818
/**
19-
* {@code TOP} clause for {@code SELECT TOP …}.
19+
* An expression with an alias.
2020
*
21-
* @author Mark Paluch
21+
* @author Jens Schauder
2222
*/
23-
public class SelectTop extends AbstractSegment implements Segment {
23+
public class AliasedExpression extends AbstractSegment implements Aliased, Expression {
2424

25-
private final int count;
25+
private final Expression expression;
26+
private final String alias;
2627

27-
private SelectTop(int count) {
28-
this.count = count;
29-
}
28+
public AliasedExpression(Expression expression, String alias) {
29+
30+
super(expression);
3031

31-
public static SelectTop create(int count) {
32-
return new SelectTop(count);
32+
this.expression = expression;
33+
this.alias = alias;
3334
}
3435

35-
/**
36-
* @return the count.
37-
*/
38-
public int getCount() {
39-
return count;
36+
@Override
37+
public String getAlias() {
38+
return alias;
4039
}
4140

42-
/*
43-
* (non-Javadoc)
44-
* @see java.lang.Object#toString()
45-
*/
4641
@Override
4742
public String toString() {
48-
return "TOP " + count;
43+
return expression.toString() + " AS " + alias;
4944
}
5045
}

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

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

18-
import org.springframework.util.Assert;
19-
2018
/**
2119
* {@link Condition} representing an {@code AND} relation between two {@link Condition}s.
2220
*
2321
* @author Mark Paluch
2422
* @see Condition#and(Condition)
2523
*/
26-
public class AndCondition implements Condition {
27-
28-
private final Condition left;
29-
private final Condition right;
30-
31-
AndCondition(Condition left, Condition right) {
32-
this.left = left;
33-
this.right = right;
34-
}
35-
36-
/*
37-
* (non-Javadoc)
38-
* @see org.springframework.data.relational.core.sql.Visitable#visit(org.springframework.data.relational.core.sql.Visitor)
39-
*/
40-
@Override
41-
public void visit(Visitor visitor) {
42-
43-
Assert.notNull(visitor, "Visitor must not be null!");
24+
public class AndCondition extends MultipleCondition {
4425

45-
visitor.enter(this);
46-
left.visit(visitor);
47-
right.visit(visitor);
48-
visitor.leave(this);
26+
AndCondition(Condition... conditions) {
27+
super(" AND ", conditions);
4928
}
5029

51-
/**
52-
* @return the left {@link Condition}.
53-
*/
54-
public Condition getLeft() {
55-
return left;
56-
}
57-
58-
/**
59-
* @return the right {@link Condition}.
60-
*/
61-
public Condition getRight() {
62-
return right;
63-
}
64-
65-
/*
66-
* (non-Javadoc)
67-
* @see java.lang.Object#toString()
68-
*/
69-
@Override
70-
public String toString() {
71-
return left.toString() + " AND " + right.toString();
72-
}
7330
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,14 @@ public class AsteriskFromTable extends AbstractSegment implements Expression {
3030
private final Table table;
3131

3232
AsteriskFromTable(Table table) {
33+
super(table);
3334
this.table = table;
3435
}
3536

3637
public static AsteriskFromTable create(Table table) {
3738
return new AsteriskFromTable(table);
3839
}
3940

40-
@Override
41-
public void visit(Visitor visitor) {
42-
43-
Assert.notNull(visitor, "Visitor must not be null!");
44-
45-
visitor.enter(this);
46-
table.visit(visitor);
47-
visitor.leave(this);
48-
}
49-
5041
/**
5142
* @return the associated {@link Table}.
5243
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author Mark Paluch
2424
*/
25-
public class BindMarker implements Segment {
25+
public class BindMarker extends AbstractSegment implements Expression {
2626

2727
/*
2828
* (non-Javadoc)

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,17 @@
2828
public class Column extends AbstractSegment implements Expression, Named {
2929

3030
private final String name;
31-
private final @Nullable Table table;
31+
private final Table table;
3232

33-
Column(String name, @Nullable Table table) {
33+
Column(String name, Table table) {
3434

35+
super(table);
3536
Assert.notNull(name, "Name must not be null");
3637

3738
this.name = name;
3839
this.table = table;
3940
}
4041

41-
/**
42-
* Creates a new {@link Column}.
43-
*
44-
* @param name column name, must not {@literal null} or empty.
45-
* @return the new {@link Column}.
46-
*/
47-
public static Column create(String name) {
48-
49-
Assert.hasText(name, "Name must not be null or empty");
50-
51-
return new Column(name, null);
52-
}
53-
54-
/**
55-
* Creates a new aliased {@link Column}.
56-
*
57-
* @param name column name, must not {@literal null} or empty.
58-
* @param alias alias name, must not {@literal null} or empty.
59-
* @return the new {@link Column}.
60-
*/
61-
public static Column aliased(String name, String alias) {
62-
63-
Assert.hasText(name, "Name must not be null or empty");
64-
Assert.hasText(alias, "Alias must not be null or empty");
65-
66-
return new AliasedColumn(name, null, alias);
67-
}
68-
6942
/**
7043
* Creates a new {@link Column} associated with a {@link Table}.
7144
*
@@ -124,23 +97,6 @@ public Column from(Table table) {
12497
return new Column(name, table);
12598
}
12699

127-
/*
128-
* (non-Javadoc)
129-
* @see org.springframework.data.relational.core.sql.Visitable#visit(org.springframework.data.relational.core.sql.Visitor)
130-
*/
131-
@Override
132-
public void visit(Visitor visitor) {
133-
134-
Assert.notNull(visitor, "Visitor must not be null!");
135-
136-
visitor.enter(this);
137-
138-
if (table != null) {
139-
table.visit(visitor);
140-
}
141-
visitor.leave(this);
142-
}
143-
144100
/*
145101
* (non-Javadoc)
146102
* @see org.springframework.data.relational.core.sql.Named#getName()
@@ -192,7 +148,7 @@ static class AliasedColumn extends Column implements Aliased {
192148

193149
private final String alias;
194150

195-
private AliasedColumn(String name, @Nullable Table table, String alias) {
151+
private AliasedColumn(String name, Table table, String alias) {
196152
super(name, table);
197153
this.alias = alias;
198154
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ default Condition or(Condition other) {
4343
return new OrCondition(this, other);
4444
}
4545

46-
/**
47-
* Encapsulate this {@link Condition} in a group of parentheses.
48-
*
49-
* @return the grouped {@link Condition}.
50-
*/
51-
default Condition group() {
52-
return new ConditionGroup(this);
46+
default Condition not() {
47+
return new Not(this);
5348
}
5449
}

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public static Condition just(String sql) {
4646
private Conditions() {
4747
}
4848

49+
public static Condition isNull(Expression expression) {
50+
return new IsNull(expression);
51+
}
52+
53+
public static Condition isEqual(Column bar, Expression param) {
54+
return new Equals(bar, param);
55+
}
56+
57+
public static Condition in(Column bar, Expression subselectExpression) {
58+
return new In(bar, subselectExpression);
59+
}
60+
4961
static class ConstantCondition extends AbstractSegment implements Condition {
5062

5163
private final String condition;

0 commit comments

Comments
 (0)