Skip to content

Commit a11c121

Browse files
mp911deschauder
authored andcommitted
DATAJDBC-335 - Add DSL and renderer for Insert, Update, and Delete.
We now provide Statement builders for Insert, Update, and Delete statements. Table myTable = SQL.table("mytable"); Insert insert = StatementBuilder.insert().into(myTable).values(SQL.bindMarker()).build(); Update update = StatementBuilder.update(table).set(myTable.column("foo").set(SQL.bindMarker())).build(); Delete delete = StatementBuilder.delete().from(table).where(myTable.column("foo").isEqualTo(SQL.literal("bar"))).build(); Original pull request: #121.
1 parent 2aa44a3 commit a11c121

Some content is hidden

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

49 files changed

+3065
-99
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 java.util.HashSet;
19+
import java.util.Set;
20+
21+
/**
22+
* Validator for statements to import columns.
23+
*
24+
* @author Mark Paluch
25+
* @since 1.1
26+
*/
27+
abstract class AbstractImportValidator implements Visitor {
28+
29+
Set<Table> requiredByWhere = new HashSet<>();
30+
Set<Table> from = new HashSet<>();
31+
Visitable parent;
32+
33+
/*
34+
* (non-Javadoc)
35+
* @see org.springframework.data.relational.core.sql.Visitor#enter(org.springframework.data.relational.core.sql.Visitable)
36+
*/
37+
@Override
38+
public void enter(Visitable segment) {
39+
40+
if (segment instanceof Table && parent instanceof From) {
41+
from.add((Table) segment);
42+
}
43+
44+
if (segment instanceof Where) {
45+
46+
segment.visit(item -> {
47+
48+
if (item instanceof Table) {
49+
requiredByWhere.add((Table) item);
50+
}
51+
});
52+
}
53+
54+
if (segment instanceof Join || segment instanceof OrderByField || segment instanceof From
55+
|| segment instanceof Select || segment instanceof Where || segment instanceof SimpleFunction) {
56+
parent = segment;
57+
}
58+
}
59+
60+
/*
61+
* (non-Javadoc)
62+
* @see org.springframework.data.relational.core.sql.Visitor#leave(org.springframework.data.relational.core.sql.Visitable)
63+
*/
64+
@Override
65+
public void leave(Visitable segment) {}
66+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
* Assign a {@link Expression} to a {@link Column}.
22+
* <p/>
23+
* Results in a rendered assignment: {@code <column> = <value>} (e.g. {@code col = 'foo'}.
24+
*
25+
* @author Mark Paluch
26+
* @since 1.1
27+
*/
28+
public class AssignValue extends AbstractSegment implements Assignment {
29+
30+
private final Column column;
31+
private final Expression value;
32+
33+
private AssignValue(Column column, Expression value) {
34+
super(column, value);
35+
this.column = column;
36+
this.value = value;
37+
}
38+
39+
/**
40+
* Creates a {@link AssignValue value} assignment to a {@link Column} given an {@link Expression}.
41+
*
42+
* @param target target column, must not be {@literal null}.
43+
* @param value assignment value, must not be {@literal null}.
44+
* @return the {@link AssignValue}.
45+
*/
46+
public static AssignValue create(Column target, Expression value) {
47+
48+
Assert.notNull(target, "Target column must not be null!");
49+
Assert.notNull(value, "Value must not be null!");
50+
51+
return new AssignValue(target, value);
52+
}
53+
54+
/**
55+
* @return the target {@link Column}.
56+
*/
57+
public Column getColumn() {
58+
return column;
59+
}
60+
61+
/**
62+
* @return the value to assign.
63+
*/
64+
public Expression getValue() {
65+
return value;
66+
}
67+
68+
/*
69+
* (non-Javadoc)
70+
* @see java.lang.Object#toString()
71+
*/
72+
@Override
73+
public String toString() {
74+
75+
StringBuilder builder = new StringBuilder(32);
76+
return builder.append(this.column).append(" = ").append(this.value).toString();
77+
}
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
/**
19+
* Update assignment to a {@link Column}.
20+
*
21+
* @author Mark Paluch
22+
*/
23+
public interface Assignment extends Segment {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
/**
19+
* Factory for common {@link Assignment}s.
20+
*
21+
* @author Mark Paluch
22+
* @since 1.1
23+
* @see SQL
24+
* @see Expressions
25+
* @see Functions
26+
*/
27+
public abstract class Assignments {
28+
29+
/**
30+
* Creates a {@link AssignValue value} assignment to a {@link Column} given an {@link Expression}.
31+
*
32+
* @param target target column, must not be {@literal null}.
33+
* @param value assignment value, must not be {@literal null}.
34+
* @return the {@link AssignValue}.
35+
*/
36+
public static AssignValue value(Column target, Expression value) {
37+
return AssignValue.create(target, value);
38+
}
39+
40+
// Utility constructor.
41+
private Assignments() {}
42+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ public Condition isNotNull() {
200200
return isNull().not();
201201
}
202202

203+
// -------------------------------------------------------------------------
204+
// Methods for Assignment creation.
205+
// -------------------------------------------------------------------------
206+
207+
/**
208+
* Creates a value {@link AssignValue assignment}.
209+
*
210+
* @param value the value to assign.
211+
* @return the {@link AssignValue} assignment.
212+
*/
213+
public AssignValue set(Expression value) {
214+
return Assignments.value(this, value);
215+
}
216+
203217
/*
204218
* (non-Javadoc)
205219
* @see org.springframework.data.relational.core.sql.Named#getName()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.lang.Nullable;
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* Default {@link Delete} implementation.
23+
*
24+
* @author Mark Paluch
25+
* @since 1.1
26+
*/
27+
class DefaultDelete implements Delete {
28+
29+
private final From from;
30+
private final @Nullable Where where;
31+
32+
DefaultDelete(Table table, @Nullable Condition where) {
33+
34+
this.from = new From(table);
35+
this.where = where != null ? new Where(where) : null;
36+
}
37+
38+
/*
39+
* (non-Javadoc)
40+
* @see org.springframework.data.relational.core.sql.Visitable#visit(org.springframework.data.relational.core.sql.Visitor)
41+
*/
42+
@Override
43+
public void visit(Visitor visitor) {
44+
45+
Assert.notNull(visitor, "Visitor must not be null!");
46+
47+
visitor.enter(this);
48+
49+
from.visit(visitor);
50+
51+
if (where != null) {
52+
where.visit(visitor);
53+
}
54+
55+
visitor.leave(this);
56+
}
57+
58+
/*
59+
* (non-Javadoc)
60+
* @see java.lang.Object#toString()
61+
*/
62+
@Override
63+
public String toString() {
64+
65+
StringBuilder builder = new StringBuilder();
66+
67+
builder.append("DELETE ").append(this.from);
68+
69+
if (this.where != null) {
70+
builder.append(' ').append(this.where);
71+
}
72+
73+
return builder.toString();
74+
}
75+
}

0 commit comments

Comments
 (0)