Skip to content

Commit cbc8869

Browse files
committed
Initial work on general insert
1 parent 01c4e8e commit cbc8869

21 files changed

+763
-20
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
import java.util.Arrays;
1919
import java.util.Collection;
20+
import java.util.Objects;
2021
import java.util.function.Supplier;
2122

2223
import org.mybatis.dynamic.sql.delete.DeleteDSL;
2324
import org.mybatis.dynamic.sql.delete.DeleteModel;
2425
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
26+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
27+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL.SetClauseFinisher;
2528
import org.mybatis.dynamic.sql.insert.InsertDSL;
2629
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
2730
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
31+
import org.mybatis.dynamic.sql.insert.InsertSelectDSL.SelectGatherer;
2832
import org.mybatis.dynamic.sql.select.CountDSL;
2933
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
3034
import org.mybatis.dynamic.sql.select.SelectDSL;
@@ -133,8 +137,8 @@ static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(Collection<T> record
133137
return MultiRowInsertDSL.insert(records);
134138
}
135139

136-
static InsertSelectDSL.InsertColumnGatherer insertInto(SqlTable table) {
137-
return InsertSelectDSL.insertInto(table);
140+
static InsertIntoNextStep insertInto(SqlTable table) {
141+
return new InsertIntoNextStep(table);
138142
}
139143

140144
static FromGatherer<SelectModel> select(BasicColumn...selectList) {
@@ -636,4 +640,28 @@ static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(Colle
636640
static SortSpecification sortColumn(String name) {
637641
return SimpleSortSpecification.of(name);
638642
}
643+
644+
public static class InsertIntoNextStep {
645+
646+
private SqlTable table;
647+
648+
private InsertIntoNextStep(SqlTable table) {
649+
this.table = Objects.requireNonNull(table);
650+
}
651+
652+
public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
653+
return InsertSelectDSL.insertInto(table)
654+
.withSelectStatement(selectModelBuilder);
655+
}
656+
657+
public SelectGatherer withColumnList(SqlColumn<?>...columns) {
658+
return InsertSelectDSL.insertInto(table)
659+
.withColumnList(columns);
660+
}
661+
662+
public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
663+
return GeneralInsertDSL.insertInto(table)
664+
.set(column);
665+
}
666+
}
639667
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright 2016-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.mybatis.dynamic.sql.insert;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Objects;
21+
import java.util.function.Supplier;
22+
23+
import org.mybatis.dynamic.sql.SqlColumn;
24+
import org.mybatis.dynamic.sql.SqlTable;
25+
import org.mybatis.dynamic.sql.select.SelectModel;
26+
import org.mybatis.dynamic.sql.util.Buildable;
27+
import org.mybatis.dynamic.sql.util.ConstantMapping;
28+
import org.mybatis.dynamic.sql.util.GeneralInsertMapping;
29+
import org.mybatis.dynamic.sql.util.NullMapping;
30+
import org.mybatis.dynamic.sql.util.SelectMapping;
31+
import org.mybatis.dynamic.sql.util.StringConstantMapping;
32+
import org.mybatis.dynamic.sql.util.ValueMapping;
33+
34+
public class GeneralInsertDSL {
35+
private List<GeneralInsertMapping> insertMappings = new ArrayList<>();
36+
private SqlTable table;
37+
38+
private GeneralInsertDSL(SqlTable table) {
39+
this.table = Objects.requireNonNull(table);
40+
}
41+
42+
public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
43+
return new SetClauseFinisher<>(column);
44+
}
45+
46+
public GeneralInsertModel build() {
47+
return new GeneralInsertModel.Builder()
48+
.withTable(table)
49+
.withInsertMappings(insertMappings)
50+
.build();
51+
}
52+
53+
public static GeneralInsertDSL insertInto(SqlTable table) {
54+
return new GeneralInsertDSL(table);
55+
}
56+
57+
public class SetClauseFinisher<T> {
58+
59+
private SqlColumn<T> column;
60+
61+
public SetClauseFinisher(SqlColumn<T> column) {
62+
this.column = column;
63+
}
64+
65+
public GeneralInsertDSL equalToNull() {
66+
insertMappings.add(NullMapping.of(column));
67+
return GeneralInsertDSL.this;
68+
}
69+
70+
public GeneralInsertDSL equalToConstant(String constant) {
71+
insertMappings.add(ConstantMapping.of(column, constant));
72+
return GeneralInsertDSL.this;
73+
}
74+
75+
public GeneralInsertDSL equalToStringConstant(String constant) {
76+
insertMappings.add(StringConstantMapping.of(column, constant));
77+
return GeneralInsertDSL.this;
78+
}
79+
80+
public GeneralInsertDSL equalTo(T value) {
81+
return equalTo(() -> value);
82+
}
83+
84+
public GeneralInsertDSL equalTo(Supplier<T> valueSupplier) {
85+
insertMappings.add(ValueMapping.of(column, valueSupplier));
86+
return GeneralInsertDSL.this;
87+
}
88+
89+
public GeneralInsertDSL equalTo(Buildable<SelectModel> buildable) {
90+
insertMappings.add(SelectMapping.of(column, buildable));
91+
return GeneralInsertDSL.this;
92+
}
93+
94+
public GeneralInsertDSL equalToWhenPresent(T value) {
95+
return equalToWhenPresent(() -> value);
96+
}
97+
98+
public GeneralInsertDSL equalToWhenPresent(Supplier<T> valueSupplier) {
99+
if (valueSupplier.get() != null) {
100+
insertMappings.add(ValueMapping.of(column, valueSupplier));
101+
}
102+
return GeneralInsertDSL.this;
103+
}
104+
}
105+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2016-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.mybatis.dynamic.sql.insert;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Objects;
21+
import java.util.function.Function;
22+
import java.util.stream.Stream;
23+
24+
import org.mybatis.dynamic.sql.SqlTable;
25+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
26+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
27+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
28+
import org.mybatis.dynamic.sql.util.GeneralInsertMapping;
29+
30+
public class GeneralInsertModel {
31+
32+
private SqlTable table;
33+
private List<GeneralInsertMapping> insertMappings;
34+
35+
private GeneralInsertModel(Builder builder) {
36+
table = Objects.requireNonNull(builder.table);
37+
insertMappings = builder.insertMappings;
38+
}
39+
40+
public <R> Stream<R> mapColumnMappings(Function<GeneralInsertMapping, R> mapper) {
41+
return insertMappings.stream().map(mapper);
42+
}
43+
44+
public SqlTable table() {
45+
return table;
46+
}
47+
48+
public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
49+
return GeneralInsertRenderer.withInsertModel(this)
50+
.withRenderingStrategy(renderingStrategy)
51+
.build()
52+
.render();
53+
}
54+
55+
public static class Builder {
56+
private SqlTable table;
57+
private List<GeneralInsertMapping> insertMappings = new ArrayList<>();
58+
59+
public Builder withTable(SqlTable table) {
60+
this.table = table;
61+
return this;
62+
}
63+
64+
public Builder withInsertMappings(List<GeneralInsertMapping> insertMappings) {
65+
this.insertMappings.addAll(insertMappings);
66+
return this;
67+
}
68+
69+
public GeneralInsertModel build() {
70+
return new GeneralInsertModel(this);
71+
}
72+
}
73+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2016-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.mybatis.dynamic.sql.insert.render;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
public class DefaultGeneralInsertStatementProvider implements GeneralInsertStatementProvider {
23+
private String insertStatement;
24+
private Map<String, Object> parameters = new HashMap<>();
25+
26+
private DefaultGeneralInsertStatementProvider(Builder builder) {
27+
insertStatement = Objects.requireNonNull(builder.insertStatement);
28+
parameters.putAll(builder.parameters);
29+
}
30+
31+
@Override
32+
public Map<String, Object> getParameters() {
33+
return parameters;
34+
}
35+
36+
@Override
37+
public String getInsertStatement() {
38+
return insertStatement;
39+
}
40+
41+
public static Builder withInsertStatement(String insertStatement) {
42+
return new Builder().withInsertStatement(insertStatement);
43+
}
44+
45+
public static class Builder {
46+
private String insertStatement;
47+
private Map<String, Object> parameters = new HashMap<>();
48+
49+
public Builder withInsertStatement(String insertStatement) {
50+
this.insertStatement = insertStatement;
51+
return this;
52+
}
53+
54+
public Builder withParameters(Map<String, Object> parameters) {
55+
this.parameters.putAll(parameters);
56+
return this;
57+
}
58+
59+
public DefaultGeneralInsertStatementProvider build() {
60+
return new DefaultGeneralInsertStatementProvider(this);
61+
}
62+
}
63+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright 2016-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.mybatis.dynamic.sql.insert.render;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
public class FieldAndValueAndParameters {
23+
private String fieldName;
24+
private String valuePhrase;
25+
private Map<String, Object> parameters;
26+
27+
private FieldAndValueAndParameters(Builder builder) {
28+
fieldName = Objects.requireNonNull(builder.fieldName);
29+
valuePhrase = Objects.requireNonNull(builder.valuePhrase);
30+
parameters = builder.parameters;
31+
}
32+
33+
public String fieldName() {
34+
return fieldName;
35+
}
36+
37+
public String valuePhrase() {
38+
return valuePhrase;
39+
}
40+
41+
public String valuePhrase(int row) {
42+
return String.format(valuePhrase, row);
43+
}
44+
45+
public Map<String, Object> parameters() {
46+
return parameters;
47+
}
48+
49+
public static Builder withFieldName(String fieldName) {
50+
return new Builder().withFieldName(fieldName);
51+
}
52+
53+
public static class Builder {
54+
private String fieldName;
55+
private String valuePhrase;
56+
private Map<String, Object> parameters = new HashMap<>();
57+
58+
public Builder withFieldName(String fieldName) {
59+
this.fieldName = fieldName;
60+
return this;
61+
}
62+
63+
public Builder withValuePhrase(String valuePhrase) {
64+
this.valuePhrase = valuePhrase;
65+
return this;
66+
}
67+
68+
public Builder withParameter(String key, Object value) {
69+
parameters.put(key, value);
70+
return this;
71+
}
72+
73+
public Builder withParameters(Map<String, Object> parameters) {
74+
this.parameters.putAll(parameters);
75+
return this;
76+
}
77+
78+
public FieldAndValueAndParameters build() {
79+
return new FieldAndValueAndParameters(this);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)