Skip to content

Commit 12972ab

Browse files
committed
Move all insert rendering into the insert renderers
1 parent e6f6a35 commit 12972ab

File tree

6 files changed

+59
-102
lines changed

6 files changed

+59
-102
lines changed

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -15,24 +15,18 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.ArrayList;
2119
import java.util.Collections;
2220
import java.util.List;
2321
import java.util.Objects;
2422
import java.util.stream.Collectors;
2523

2624
public class BatchInsert<T> {
27-
private String tableName;
28-
private String columnsPhrase;
29-
private String valuesPhrase;
25+
private String insertStatement;
3026
private List<T> records;
3127

3228
private BatchInsert(Builder<T> builder) {
33-
tableName = Objects.requireNonNull(builder.tableName);
34-
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
35-
valuesPhrase = Objects.requireNonNull(builder.valuesPhrase);
29+
insertStatement = Objects.requireNonNull(builder.insertStatement);
3630
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
3731
}
3832

@@ -49,9 +43,7 @@ public List<InsertStatementProvider<T>> insertStatements() {
4943

5044
private InsertStatementProvider<T> toInsertStatement(T record) {
5145
return DefaultInsertStatementProvider.withRecord(record)
52-
.withTableName(tableName)
53-
.withColumnsPhrase(columnsPhrase)
54-
.withValuesPhrase(valuesPhrase)
46+
.withInsertStatement(insertStatement)
5547
.build();
5648
}
5749

@@ -61,37 +53,22 @@ private InsertStatementProvider<T> toInsertStatement(T record) {
6153
* @return the generated INSERT statement
6254
*/
6355
public String getInsertStatementSQL() {
64-
return "insert into" //$NON-NLS-1$
65-
+ spaceBefore(tableName)
66-
+ spaceBefore(columnsPhrase)
67-
+ spaceBefore(valuesPhrase);
56+
return insertStatement;
6857
}
6958

7059
public static <T> Builder<T> withRecords(List<T> records) {
7160
return new Builder<T>().withRecords(records);
7261
}
7362

7463
public static class Builder<T> {
75-
private String tableName;
76-
private String columnsPhrase;
77-
private String valuesPhrase;
64+
private String insertStatement;
7865
private List<T> records = new ArrayList<>();
7966

80-
public Builder<T> withTableName(String tableName) {
81-
this.tableName = tableName;
67+
public Builder<T> withInsertStatement(String insertStatement) {
68+
this.insertStatement = insertStatement;
8269
return this;
8370
}
8471

85-
public Builder<T> withColumnsPhrase(String columnsPhrase) {
86-
this.columnsPhrase = columnsPhrase;
87-
return this;
88-
}
89-
90-
public Builder<T> withValuesPhrase(String valuesPhrase) {
91-
this.valuesPhrase = valuesPhrase;
92-
return this;
93-
}
94-
9572
public Builder<T> withRecords(List<T> records) {
9673
this.records.addAll(records);
9774
return this;

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

Lines changed: 12 additions & 4 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-2019 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
1820
import java.util.Objects;
1921
import java.util.function.Function;
2022

@@ -36,10 +38,9 @@ public BatchInsert<T> render() {
3638
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
3739
FieldAndValueCollector<T> collector = model.mapColumnMappings(toFieldAndValue(visitor))
3840
.collect(FieldAndValueCollector.collect());
41+
3942
return BatchInsert.withRecords(model.records())
40-
.withTableName(model.table().name())
41-
.withColumnsPhrase(collector.columnsPhrase())
42-
.withValuesPhrase(collector.valuesPhrase())
43+
.withInsertStatement(calculateInsertStatement(collector))
4344
.build();
4445
}
4546

@@ -50,6 +51,13 @@ private Function<InsertMapping, FieldAndValue> toFieldAndValue(ValuePhraseVisito
5051
private FieldAndValue toFieldAndValue(ValuePhraseVisitor visitor, InsertMapping insertMapping) {
5152
return insertMapping.accept(visitor);
5253
}
54+
55+
private String calculateInsertStatement(FieldAndValueCollector<T> collector) {
56+
return "insert into" //$NON-NLS-1$
57+
+ spaceBefore(model.table().name())
58+
+ spaceBefore(collector.columnsPhrase())
59+
+ spaceBefore(collector.valuesPhrase());
60+
}
5361

5462
public static <T> Builder<T> withBatchInsertModel(BatchInsertModel<T> model) {
5563
return new Builder<T>().withBatchInsertModel(model);

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -15,64 +15,42 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.HashMap;
2119
import java.util.Map;
2220
import java.util.Objects;
23-
import java.util.Optional;
2421

2522
public class DefaultInsertSelectStatementProvider implements InsertSelectStatementProvider {
26-
private String tableName;
27-
private Optional<String> columnsPhrase;
28-
private String selectStatement;
23+
private String insertStatement;
2924
private Map<String, Object> parameters;
3025

3126
private DefaultInsertSelectStatementProvider(Builder builder) {
32-
tableName = Objects.requireNonNull(builder.tableName);
33-
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
34-
selectStatement = Objects.requireNonNull(builder.selectStatement);
27+
insertStatement = Objects.requireNonNull(builder.insertStatement);
3528
parameters = Objects.requireNonNull(builder.parameters);
3629
}
3730

3831
@Override
3932
public String getInsertStatement() {
40-
return "insert into" //$NON-NLS-1$
41-
+ spaceBefore(tableName)
42-
+ spaceBefore(columnsPhrase)
43-
+ spaceBefore(selectStatement);
33+
return insertStatement;
4434
}
4535

4636
@Override
4737
public Map<String, Object> getParameters() {
4838
return parameters;
4939
}
5040

51-
public static Builder withTableName(String tableName) {
52-
return new Builder().withTableName(tableName);
41+
public static Builder withInsertStatement(String insertStatement) {
42+
return new Builder().withInsertStatement(insertStatement);
5343
}
5444

5545
public static class Builder {
56-
private String tableName;
57-
private Optional<String> columnsPhrase;
58-
private String selectStatement;
46+
private String insertStatement;
5947
private Map<String, Object> parameters = new HashMap<>();
6048

61-
public Builder withTableName(String tableName) {
62-
this.tableName = tableName;
49+
public Builder withInsertStatement(String insertStatement) {
50+
this.insertStatement = insertStatement;
6351
return this;
6452
}
6553

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-
7654
public Builder withParameters(Map<String, Object> parameters) {
7755
this.parameters.putAll(parameters);
7856
return this;

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

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -15,20 +15,14 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.Objects;
2119

2220
public class DefaultInsertStatementProvider<T> implements InsertStatementProvider<T> {
23-
private String tableName;
24-
private String columnsPhrase;
25-
private String valuesPhrase;
21+
private String insertStatement;
2622
private T record;
2723

2824
private DefaultInsertStatementProvider(Builder<T> builder) {
29-
tableName = Objects.requireNonNull(builder.tableName);
30-
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
31-
valuesPhrase = Objects.requireNonNull(builder.valuesPhrase);
25+
insertStatement = Objects.requireNonNull(builder.insertStatement);
3226
record = Objects.requireNonNull(builder.record);
3327
}
3428

@@ -39,37 +33,22 @@ public T getRecord() {
3933

4034
@Override
4135
public String getInsertStatement() {
42-
return "insert into" //$NON-NLS-1$
43-
+ spaceBefore(tableName)
44-
+ spaceBefore(columnsPhrase)
45-
+ spaceBefore(valuesPhrase);
36+
return insertStatement;
4637
}
4738

4839
public static <T> Builder<T> withRecord(T record) {
4940
return new Builder<T>().withRecord(record);
5041
}
5142

5243
public static class Builder<T> {
53-
private String tableName;
54-
private String columnsPhrase;
55-
private String valuesPhrase;
44+
private String insertStatement;
5645
private T record;
5746

58-
public Builder<T> withTableName(String tableName) {
59-
this.tableName = tableName;
47+
public Builder<T> withInsertStatement(String insertStatement) {
48+
this.insertStatement = insertStatement;
6049
return this;
6150
}
6251

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-
7352
public Builder<T> withRecord(T record) {
7453
this.record = record;
7554
return this;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
1820
import java.util.Objects;
1921
import java.util.function.Function;
2022

@@ -36,13 +38,19 @@ public InsertStatementProvider<T> render() {
3638
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
3739
FieldAndValueCollector<T> collector = model.mapColumnMappings(toFieldAndValue(visitor))
3840
.collect(FieldAndValueCollector.collect());
41+
3942
return DefaultInsertStatementProvider.withRecord(model.record())
40-
.withTableName(model.table().name())
41-
.withColumnsPhrase(collector.columnsPhrase())
42-
.withValuesPhrase(collector.valuesPhrase())
43+
.withInsertStatement(calculateInsertStatement(collector))
4344
.build();
4445
}
4546

47+
private String calculateInsertStatement(FieldAndValueCollector<T> collector) {
48+
return "insert into" //$NON-NLS-1$
49+
+ spaceBefore(model.table().name())
50+
+ spaceBefore(collector.columnsPhrase())
51+
+ spaceBefore(collector.valuesPhrase());
52+
}
53+
4654
private Function<InsertMapping, FieldAndValue> toFieldAndValue(ValuePhraseVisitor visitor) {
4755
return insertMapping -> toFieldAndValue(visitor, insertMapping);
4856
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
1820
import java.util.Objects;
1921
import java.util.Optional;
2022
import java.util.stream.Collectors;
@@ -38,13 +40,18 @@ private InsertSelectRenderer(Builder builder) {
3840
public InsertSelectStatementProvider render() {
3941
SelectStatementProvider selectStatement = model.selectModel().render(renderingStrategy);
4042

41-
return DefaultInsertSelectStatementProvider.withTableName(model.table().name())
42-
.withColumnsPhrase(calculateColumnsPhrase())
43-
.withSelectStatement(selectStatement.getSelectStatement())
43+
return DefaultInsertSelectStatementProvider.withInsertStatement(calculateInsertStatement(selectStatement))
4444
.withParameters(selectStatement.getParameters())
4545
.build();
4646
}
4747

48+
private String calculateInsertStatement(SelectStatementProvider selectStatement) {
49+
return "insert into" //$NON-NLS-1$
50+
+ spaceBefore(model.table().name())
51+
+ spaceBefore(calculateColumnsPhrase())
52+
+ spaceBefore(selectStatement.getSelectStatement());
53+
}
54+
4855
private Optional<String> calculateColumnsPhrase() {
4956
return model.columnList()
5057
.map(this::calculateColumnsPhrase);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy