Skip to content

Commit 7c63417

Browse files
committed
Make Delete, Insert, Where support model driven
This allows pluggable renderers
1 parent 32889fb commit 7c63417

Some content is hidden

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

44 files changed

+795
-275
lines changed

src/main/java/org/mybatis/dynamic/sql/MyBatis3Column.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.sql.JDBCType;
19-
import java.util.Optional;
2019

2120
/**
2221
*
@@ -25,21 +24,16 @@
2524
*/
2625
public class MyBatis3Column<T> extends SqlColumn<T> {
2726

28-
protected String typeHandler;
2927

3028
protected MyBatis3Column(MyBatis3Column<?> myBatis3Column) {
3129
super(myBatis3Column);
32-
this.typeHandler = myBatis3Column.typeHandler;
3330
}
3431

3532
protected MyBatis3Column(String name, JDBCType jdbcType) {
3633
super(name, jdbcType);
3734
}
3835

39-
public Optional<String> typeHandler() {
40-
return Optional.ofNullable(typeHandler);
41-
}
42-
36+
@Override
4337
public <S> MyBatis3Column<S> withTypeHandler(String typeHandler) {
4438
MyBatis3Column<S> column = new MyBatis3Column<>(this);
4539
column.typeHandler = typeHandler;

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
import org.mybatis.dynamic.sql.delete.DeleteSupportBuilder;
19-
import org.mybatis.dynamic.sql.insert.InsertSupportBuilder;
18+
import org.mybatis.dynamic.sql.delete.DeleteModelBuilder;
19+
import org.mybatis.dynamic.sql.insert.InsertModelBuilder;
2020
import org.mybatis.dynamic.sql.select.SelectCountOrDistinctBuilder;
2121
import org.mybatis.dynamic.sql.select.SelectSupportBuilder;
2222
import org.mybatis.dynamic.sql.update.UpdateSupportBuilder;
2323

2424
public interface SqlBuilder {
2525

26-
public static DeleteSupportBuilder deleteFrom(SqlTable table) {
27-
return DeleteSupportBuilder.of(table);
26+
public static DeleteModelBuilder deleteFrom(SqlTable table) {
27+
return DeleteModelBuilder.of(table);
2828
}
2929

30-
public static <T> InsertSupportBuilder<T> insert(T record) {
31-
return InsertSupportBuilder.of(record);
30+
public static <T> InsertModelBuilder<T> insert(T record) {
31+
return InsertModelBuilder.insert(record);
3232
}
3333

3434
public static SelectSupportBuilder select(SqlColumn<?>...columns) {

src/main/java/org/mybatis/dynamic/sql/SqlColumn.java

+12
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public class SqlColumn<T> extends AbstractColumn<T> {
3232
protected JDBCType jdbcType;
3333
protected String sortOrder = ASCENDING;
3434
protected String alias;
35+
protected String typeHandler;
3536

3637
protected SqlColumn(SqlColumn<?> sqlColumn) {
3738
super(sqlColumn.name);
3839
this.table = sqlColumn.table;
3940
this.jdbcType = sqlColumn.jdbcType;
4041
this.sortOrder = sqlColumn.sortOrder;
4142
this.alias = sqlColumn.alias;
43+
this.typeHandler = sqlColumn.typeHandler;
4244
}
4345

4446
protected SqlColumn(String name, JDBCType jdbcType) {
@@ -71,6 +73,10 @@ public Optional<String> columnAlias() {
7173
return Optional.ofNullable(alias);
7274
}
7375

76+
public Optional<String> typeHandler() {
77+
return Optional.ofNullable(typeHandler);
78+
}
79+
7480
public <S> SqlColumn<S> inTable(SqlTable table) {
7581
SqlColumn<S> column = new SqlColumn<>(this);
7682
column.table = table;
@@ -89,6 +95,12 @@ public <S> SqlColumn<S> withAlias(String alias) {
8995
return column;
9096
}
9197

98+
public <S> SqlColumn<S> withTypeHandler(String typeHandler) {
99+
SqlColumn<S> column = new SqlColumn<>(this);
100+
column.typeHandler = typeHandler;
101+
return column;
102+
}
103+
92104
public String getFormattedJdbcPlaceholder(String prefix, String parameterName) {
93105
return "{" + prefix + "." + parameterName + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
94106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2016-2017 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.mybatis.dynamic.sql.delete;
17+
18+
import java.util.Optional;
19+
20+
import org.mybatis.dynamic.sql.SqlTable;
21+
import org.mybatis.dynamic.sql.where.WhereModel;
22+
23+
public class DeleteModel {
24+
private SqlTable table;
25+
private WhereModel whereModel;
26+
27+
private DeleteModel() {
28+
super();
29+
}
30+
31+
public SqlTable table() {
32+
return table;
33+
}
34+
35+
public Optional<WhereModel> whereModel() {
36+
return Optional.ofNullable(whereModel);
37+
}
38+
39+
public static DeleteModel of(SqlTable table) {
40+
return of(table, null);
41+
}
42+
43+
public static DeleteModel of(SqlTable table, WhereModel whereModel) {
44+
DeleteModel deleteModel = new DeleteModel();
45+
deleteModel.table = table;
46+
deleteModel.whereModel = whereModel;
47+
return deleteModel;
48+
}
49+
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteSupportBuilder.java src/main/java/org/mybatis/dynamic/sql/delete/DeleteModelBuilder.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import org.mybatis.dynamic.sql.SqlColumn;
2020
import org.mybatis.dynamic.sql.SqlCriterion;
2121
import org.mybatis.dynamic.sql.SqlTable;
22-
import org.mybatis.dynamic.sql.where.AbstractWhereBuilder;
23-
import org.mybatis.dynamic.sql.where.WhereSupport;
22+
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
23+
import org.mybatis.dynamic.sql.delete.render.DeleteSupport;
24+
import org.mybatis.dynamic.sql.where.AbstractWhereModelBuilder;
2425

25-
public class DeleteSupportBuilder {
26+
public class DeleteModelBuilder {
2627

2728
private SqlTable table;
2829

29-
private DeleteSupportBuilder(SqlTable table) {
30+
private DeleteModelBuilder(SqlTable table) {
3031
this.table = table;
3132
}
3233

@@ -35,28 +36,35 @@ public <T> DeleteSupportWhereBuilder where(SqlColumn<T> column, Condition<T> con
3536
}
3637

3738
/**
38-
* WARNING! Calling this method will result in an delete statement that deletes
39+
* WARNING! Calling this method could result in an delete statement that deletes
3940
* all rows in a table.
4041
*
4142
* @return
4243
*/
43-
public DeleteSupport build() {
44-
return DeleteSupport.of(table);
44+
public DeleteModel build() {
45+
return DeleteModel.of(table);
4546
}
4647

47-
public static DeleteSupportBuilder of(SqlTable table) {
48-
return new DeleteSupportBuilder(table);
48+
public DeleteSupport buildAndRender() {
49+
return DeleteRenderer.of(build()).render();
4950
}
5051

51-
public class DeleteSupportWhereBuilder extends AbstractWhereBuilder<DeleteSupportWhereBuilder> {
52+
public static DeleteModelBuilder of(SqlTable table) {
53+
return new DeleteModelBuilder(table);
54+
}
55+
56+
public class DeleteSupportWhereBuilder extends AbstractWhereModelBuilder<DeleteSupportWhereBuilder> {
5257

5358
private <T> DeleteSupportWhereBuilder(SqlColumn<T> column, Condition<T> condition, SqlCriterion<?>...subCriteria) {
5459
super(column, condition, subCriteria);
5560
}
5661

57-
public DeleteSupport build() {
58-
WhereSupport whereSupport = renderCriteriaIgnoringTableAlias();
59-
return DeleteSupport.of(whereSupport.getWhereClause(), whereSupport.getParameters(), table);
62+
public DeleteModel build() {
63+
return DeleteModel.of(table, buildWhereModel());
64+
}
65+
66+
public DeleteSupport buildAndRender() {
67+
return DeleteRenderer.of(build()).render();
6068
}
6169

6270
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2016-2017 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.mybatis.dynamic.sql.delete.render;
17+
18+
import org.mybatis.dynamic.sql.delete.DeleteModel;
19+
import org.mybatis.dynamic.sql.where.WhereModel;
20+
import org.mybatis.dynamic.sql.where.render.WhereRenderer;
21+
import org.mybatis.dynamic.sql.where.render.WhereSupport;
22+
23+
public class DeleteRenderer {
24+
private DeleteModel deleteModel;
25+
26+
private DeleteRenderer(DeleteModel deleteModel) {
27+
this.deleteModel = deleteModel;
28+
}
29+
30+
public DeleteSupport render() {
31+
return deleteModel.whereModel().map(this::renderWithWhereClause)
32+
.orElse(DeleteSupport.of(deleteModel.table()));
33+
}
34+
35+
private DeleteSupport renderWithWhereClause(WhereModel whereModel) {
36+
WhereRenderer whereRenderer = WhereRenderer.of(whereModel);
37+
WhereSupport whereSupport = whereRenderer.renderCriteriaIgnoringTableAlias();
38+
return DeleteSupport.of(whereSupport.getWhereClause(), whereSupport.getParameters(), deleteModel.table());
39+
}
40+
41+
public static DeleteRenderer of(DeleteModel deleteModel) {
42+
return new DeleteRenderer(deleteModel);
43+
}
44+
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteSupport.java src/main/java/org/mybatis/dynamic/sql/delete/render/DeleteSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.delete;
16+
package org.mybatis.dynamic.sql.delete.render;
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2017 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.mybatis.dynamic.sql.insert;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
20+
public abstract class AbstractColumnMapping {
21+
protected SqlColumn<?> column;
22+
23+
protected AbstractColumnMapping(SqlColumn<?> column) {
24+
this.column = column;
25+
}
26+
27+
public SqlColumn<?> column() {
28+
return column;
29+
}
30+
31+
public abstract <R> R accept(ColumnMappingVisitor<R> visitor);
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2016-2017 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.mybatis.dynamic.sql.insert;
17+
18+
public interface ColumnMappingVisitor<T> {
19+
T visit(NullMapping mapping);
20+
T visit(ConstantMapping mapping);
21+
T visit(PropertyMapping mapping);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2016-2017 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.mybatis.dynamic.sql.insert;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
20+
public class ConstantMapping extends AbstractColumnMapping {
21+
private String constant;
22+
23+
private ConstantMapping(SqlColumn<?> column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
public <S> S accept(ColumnMappingVisitor<S> visitor) {
29+
return visitor.visit(this);
30+
}
31+
32+
public String constant() {
33+
return constant;
34+
}
35+
36+
public static ConstantMapping of(SqlColumn<?> column, String constant) {
37+
ConstantMapping mapping = new ConstantMapping(column);
38+
mapping.constant = constant;
39+
return mapping;
40+
}
41+
}

0 commit comments

Comments
 (0)