Skip to content

Commit e10414c

Browse files
authored
Merge pull request #30 from jeffgbutler/master
Add Ability to Modify SQL After Generation
2 parents d886a6b + 55f0d28 commit e10414c

19 files changed

+661
-315
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2016-2018 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 static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.Optional;
24+
25+
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
26+
27+
public class DefaultDeleteStatementProvider implements DeleteStatementProvider {
28+
private String tableName;
29+
private Optional<String> whereClause;
30+
private Map<String, Object> parameters;
31+
32+
private DefaultDeleteStatementProvider(Builder builder) {
33+
tableName = Objects.requireNonNull(builder.tableName);
34+
whereClause = Optional.ofNullable(builder.whereClause);
35+
parameters = Objects.requireNonNull(builder.parameters);
36+
}
37+
38+
@Override
39+
public Map<String, Object> getParameters() {
40+
return parameters;
41+
}
42+
43+
@Override
44+
public String getDeleteStatement() {
45+
return "delete from" //$NON-NLS-1$
46+
+ spaceBefore(tableName)
47+
+ spaceBefore(whereClause);
48+
}
49+
50+
public static Builder withTableName(String tableName) {
51+
return new Builder().withTableName(tableName);
52+
}
53+
54+
public static class Builder {
55+
private String tableName;
56+
private String whereClause;
57+
private Map<String, Object> parameters = new HashMap<>();
58+
59+
public Builder withTableName(String tableName) {
60+
this.tableName = tableName;
61+
return this;
62+
}
63+
64+
public Builder withWhereClause(Optional<WhereClauseProvider> whereClauseProvider) {
65+
whereClauseProvider.ifPresent(wcp -> {
66+
whereClause = wcp.getWhereClause();
67+
parameters.putAll(wcp.getParameters());
68+
});
69+
return this;
70+
}
71+
72+
public DefaultDeleteStatementProvider build() {
73+
return new DefaultDeleteStatementProvider(this);
74+
}
75+
}
76+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ private DeleteRenderer(Builder builder) {
3232
}
3333

3434
public DeleteStatementProvider render() {
35-
return DeleteStatementProvider.withTableName(deleteModel.table().name())
35+
return DefaultDeleteStatementProvider.withTableName(deleteModel.table().name())
3636
.withWhereClause(deleteModel.whereModel().map(this::renderWhereClause))
3737
.build();
3838
}

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

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,10 @@
1515
*/
1616
package org.mybatis.dynamic.sql.delete.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
20-
import java.util.HashMap;
2118
import java.util.Map;
22-
import java.util.Objects;
23-
import java.util.Optional;
24-
25-
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
26-
27-
public class DeleteStatementProvider {
28-
private String tableName;
29-
private Optional<String> whereClause;
30-
private Map<String, Object> parameters;
31-
32-
private DeleteStatementProvider(Builder builder) {
33-
tableName = Objects.requireNonNull(builder.tableName);
34-
whereClause = Optional.ofNullable(builder.whereClause);
35-
parameters = Objects.requireNonNull(builder.parameters);
36-
}
37-
38-
public Map<String, Object> getParameters() {
39-
return parameters;
40-
}
41-
42-
public String getDeleteStatement() {
43-
return "delete from" //$NON-NLS-1$
44-
+ spaceBefore(tableName)
45-
+ spaceBefore(whereClause);
46-
}
4719

48-
public static Builder withTableName(String tableName) {
49-
return new Builder().withTableName(tableName);
50-
}
20+
public interface DeleteStatementProvider {
21+
Map<String, Object> getParameters();
5122

52-
public static class Builder {
53-
private String tableName;
54-
private String whereClause;
55-
private Map<String, Object> parameters = new HashMap<>();
56-
57-
public Builder withTableName(String tableName) {
58-
this.tableName = tableName;
59-
return this;
60-
}
61-
62-
public Builder withWhereClause(Optional<WhereClauseProvider> whereClauseProvider) {
63-
whereClauseProvider.ifPresent(wcp -> {
64-
whereClause = wcp.getWhereClause();
65-
parameters.putAll(wcp.getParameters());
66-
});
67-
return this;
68-
}
69-
70-
public DeleteStatementProvider build() {
71-
return new DeleteStatementProvider(this);
72-
}
73-
}
23+
String getDeleteStatement();
7424
}

src/main/java/org/mybatis/dynamic/sql/insert/render/BatchInsert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public List<InsertStatementProvider<T>> insertStatements() {
4848
}
4949

5050
private InsertStatementProvider<T> toInsertStatement(T record) {
51-
return InsertStatementProvider.withRecord(record)
51+
return DefaultInsertStatementProvider.withRecord(record)
5252
.withTableName(tableName)
5353
.withColumnsPhrase(columnsPhrase)
5454
.withValuesPhrase(valuesPhrase)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2016-2018 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.render;
17+
18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.Optional;
24+
25+
public class DefaultInsertSelectStatementProvider implements InsertSelectStatementProvider {
26+
private String tableName;
27+
private Optional<String> columnsPhrase;
28+
private String selectStatement;
29+
private Map<String, Object> parameters;
30+
31+
private DefaultInsertSelectStatementProvider(Builder builder) {
32+
tableName = Objects.requireNonNull(builder.tableName);
33+
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
34+
selectStatement = Objects.requireNonNull(builder.selectStatement);
35+
parameters = Objects.requireNonNull(builder.parameters);
36+
}
37+
38+
@Override
39+
public String getInsertStatement() {
40+
return "insert into" //$NON-NLS-1$
41+
+ spaceBefore(tableName)
42+
+ spaceBefore(columnsPhrase)
43+
+ spaceBefore(selectStatement);
44+
}
45+
46+
@Override
47+
public Map<String, Object> getParameters() {
48+
return parameters;
49+
}
50+
51+
public static Builder withTableName(String tableName) {
52+
return new Builder().withTableName(tableName);
53+
}
54+
55+
public static class Builder {
56+
private String tableName;
57+
private Optional<String> columnsPhrase;
58+
private String selectStatement;
59+
private Map<String, Object> parameters = new HashMap<>();
60+
61+
public Builder withTableName(String tableName) {
62+
this.tableName = tableName;
63+
return this;
64+
}
65+
66+
public Builder withColumnsPhrase(Optional<String> columnsPhrase) {
67+
this.columnsPhrase = columnsPhrase;
68+
return this;
69+
}
70+
71+
public Builder withSelectStatement(String selectStatement) {
72+
this.selectStatement = selectStatement;
73+
return this;
74+
}
75+
76+
public Builder withParameters(Map<String, Object> parameters) {
77+
this.parameters.putAll(parameters);
78+
return this;
79+
}
80+
81+
public DefaultInsertSelectStatementProvider build() {
82+
return new DefaultInsertSelectStatementProvider(this);
83+
}
84+
}
85+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright 2016-2018 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.render;
17+
18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
20+
import java.util.Objects;
21+
22+
public class DefaultInsertStatementProvider<T> implements InsertStatementProvider<T> {
23+
private String tableName;
24+
private String columnsPhrase;
25+
private String valuesPhrase;
26+
private T record;
27+
28+
private DefaultInsertStatementProvider(Builder<T> builder) {
29+
tableName = Objects.requireNonNull(builder.tableName);
30+
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
31+
valuesPhrase = Objects.requireNonNull(builder.valuesPhrase);
32+
record = Objects.requireNonNull(builder.record);
33+
}
34+
35+
@Override
36+
public T getRecord() {
37+
return record;
38+
}
39+
40+
@Override
41+
public String getInsertStatement() {
42+
return "insert into" //$NON-NLS-1$
43+
+ spaceBefore(tableName)
44+
+ spaceBefore(columnsPhrase)
45+
+ spaceBefore(valuesPhrase);
46+
}
47+
48+
public static <T> Builder<T> withRecord(T record) {
49+
return new Builder<T>().withRecord(record);
50+
}
51+
52+
public static class Builder<T> {
53+
private String tableName;
54+
private String columnsPhrase;
55+
private String valuesPhrase;
56+
private T record;
57+
58+
public Builder<T> withTableName(String tableName) {
59+
this.tableName = tableName;
60+
return this;
61+
}
62+
63+
public Builder<T> withColumnsPhrase(String columnsPhrase) {
64+
this.columnsPhrase = columnsPhrase;
65+
return this;
66+
}
67+
68+
public Builder<T> withValuesPhrase(String valuesPhrase) {
69+
this.valuesPhrase = valuesPhrase;
70+
return this;
71+
}
72+
73+
public Builder<T> withRecord(T record) {
74+
this.record = record;
75+
return this;
76+
}
77+
78+
public DefaultInsertStatementProvider<T> build() {
79+
return new DefaultInsertStatementProvider<>(this);
80+
}
81+
}
82+
}

src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ public InsertStatementProvider<T> render() {
3636
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
3737
FieldAndValueCollector<T> collector = model.mapColumnMappings(toFieldAndValue(visitor))
3838
.collect(FieldAndValueCollector.collect());
39-
return InsertStatementProvider.withRecord(model.record())
39+
return DefaultInsertStatementProvider.withRecord(model.record())
4040
.withTableName(model.table().name())
4141
.withColumnsPhrase(collector.columnsPhrase())
4242
.withValuesPhrase(collector.valuesPhrase())

src/main/java/org/mybatis/dynamic/sql/insert/render/InsertSelectRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ private InsertSelectRenderer(Builder builder) {
3838
public InsertSelectStatementProvider render() {
3939
SelectStatementProvider selectStatement = model.selectModel().render(renderingStrategy);
4040

41-
return InsertSelectStatementProvider.withTableName(model.table().name())
41+
return DefaultInsertSelectStatementProvider.withTableName(model.table().name())
4242
.withColumnsPhrase(calculateColumnsPhrase())
4343
.withSelectStatement(selectStatement.getSelectStatement())
4444
.withParameters(selectStatement.getParameters())

0 commit comments

Comments
 (0)