Skip to content

Commit 37aaa0d

Browse files
authored
Merge pull request #215 from jeffgbutler/general-insert
Implement a General Insert Statement
2 parents 6469607 + cb6a435 commit 37aaa0d

File tree

60 files changed

+1503
-178
lines changed

Some content is hidden

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

60 files changed

+1503
-178
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages.
44

5+
## Release 1.1.5 - Unreleased
6+
7+
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.5+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.5+)
8+
9+
### Added
10+
11+
- Added a general insert statement that does not require a separate record class to hold values for the insert. ([#201](https://github.com/mybatis/mybatis-dynamic-sql/issues/201))
12+
513
## Release 1.1.4 - November 23, 2019
614

715
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.4+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.4+)

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -17,11 +17,13 @@
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;
2527
import org.mybatis.dynamic.sql.insert.InsertDSL;
2628
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
2729
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
@@ -133,8 +135,8 @@ static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(Collection<T> record
133135
return MultiRowInsertDSL.insert(records);
134136
}
135137

136-
static InsertSelectDSL.InsertColumnGatherer insertInto(SqlTable table) {
137-
return InsertSelectDSL.insertInto(table);
138+
static InsertIntoNextStep insertInto(SqlTable table) {
139+
return new InsertIntoNextStep(table);
138140
}
139141

140142
static FromGatherer<SelectModel> select(BasicColumn...selectList) {
@@ -636,4 +638,28 @@ static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(Colle
636638
static SortSpecification sortColumn(String name) {
637639
return SimpleSortSpecification.of(name);
638640
}
641+
642+
class InsertIntoNextStep {
643+
644+
private SqlTable table;
645+
646+
private InsertIntoNextStep(SqlTable table) {
647+
this.table = Objects.requireNonNull(table);
648+
}
649+
650+
public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
651+
return InsertSelectDSL.insertInto(table)
652+
.withSelectStatement(selectModelBuilder);
653+
}
654+
655+
public InsertSelectDSL.SelectGatherer withColumnList(SqlColumn<?>...columns) {
656+
return InsertSelectDSL.insertInto(table)
657+
.withColumnList(columns);
658+
}
659+
660+
public <T> GeneralInsertDSL.SetClauseFinisher<T> set(SqlColumn<T> column) {
661+
return GeneralInsertDSL.insertInto(table)
662+
.set(column);
663+
}
664+
}
639665
}

src/main/java/org/mybatis/dynamic/sql/insert/AbstractMultiRowInsertModel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424
import java.util.stream.Stream;
2525

2626
import org.mybatis.dynamic.sql.SqlTable;
27-
import org.mybatis.dynamic.sql.util.InsertMapping;
27+
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
2828

2929
public abstract class AbstractMultiRowInsertModel<T> {
3030
private SqlTable table;
3131
private List<T> records;
32-
private List<InsertMapping> columnMappings;
32+
private List<AbstractColumnMapping> columnMappings;
3333

3434
protected AbstractMultiRowInsertModel(AbstractBuilder<T, ?> builder) {
3535
table = Objects.requireNonNull(builder.table);
3636
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
3737
columnMappings = Objects.requireNonNull(builder.columnMappings);
3838
}
3939

40-
public <R> Stream<R> mapColumnMappings(Function<InsertMapping, R> mapper) {
40+
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
4141
return columnMappings.stream().map(mapper);
4242
}
4343

@@ -56,7 +56,7 @@ public int recordCount() {
5656
public abstract static class AbstractBuilder<T, S extends AbstractBuilder<T, S>> {
5757
private SqlTable table;
5858
private List<T> records = new ArrayList<>();
59-
private List<InsertMapping> columnMappings = new ArrayList<>();
59+
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
6060

6161
public S withTable(SqlTable table) {
6262
this.table = table;
@@ -68,7 +68,7 @@ public S withRecords(Collection<T> records) {
6868
return getThis();
6969
}
7070

71-
public S withColumnMappings(List<InsertMapping> columnMappings) {
71+
public S withColumnMappings(List<AbstractColumnMapping> columnMappings) {
7272
this.columnMappings.addAll(columnMappings);
7373
return getThis();
7474
}

src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertDSL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import org.mybatis.dynamic.sql.SqlColumn;
2424
import org.mybatis.dynamic.sql.SqlTable;
25+
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
2526
import org.mybatis.dynamic.sql.util.ConstantMapping;
26-
import org.mybatis.dynamic.sql.util.InsertMapping;
2727
import org.mybatis.dynamic.sql.util.NullMapping;
2828
import org.mybatis.dynamic.sql.util.PropertyMapping;
2929
import org.mybatis.dynamic.sql.util.StringConstantMapping;
@@ -32,7 +32,7 @@ public class BatchInsertDSL<T> {
3232

3333
private Collection<T> records;
3434
private SqlTable table;
35-
private List<InsertMapping> columnMappings = new ArrayList<>();
35+
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
3636

3737
private BatchInsertDSL(Collection<T> records, SqlTable table) {
3838
this.records = records;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2016-2020 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.util.AbstractColumnMapping;
26+
import org.mybatis.dynamic.sql.util.ConstantMapping;
27+
import org.mybatis.dynamic.sql.util.NullMapping;
28+
import org.mybatis.dynamic.sql.util.StringConstantMapping;
29+
import org.mybatis.dynamic.sql.util.ValueMapping;
30+
31+
public class GeneralInsertDSL {
32+
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();
33+
private SqlTable table;
34+
35+
private GeneralInsertDSL(SqlTable table) {
36+
this.table = Objects.requireNonNull(table);
37+
}
38+
39+
public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
40+
return new SetClauseFinisher<>(column);
41+
}
42+
43+
public GeneralInsertModel build() {
44+
return new GeneralInsertModel.Builder()
45+
.withTable(table)
46+
.withInsertMappings(insertMappings)
47+
.build();
48+
}
49+
50+
public static GeneralInsertDSL insertInto(SqlTable table) {
51+
return new GeneralInsertDSL(table);
52+
}
53+
54+
public class SetClauseFinisher<T> {
55+
56+
private SqlColumn<T> column;
57+
58+
public SetClauseFinisher(SqlColumn<T> column) {
59+
this.column = column;
60+
}
61+
62+
public GeneralInsertDSL toNull() {
63+
insertMappings.add(NullMapping.of(column));
64+
return GeneralInsertDSL.this;
65+
}
66+
67+
public GeneralInsertDSL toConstant(String constant) {
68+
insertMappings.add(ConstantMapping.of(column, constant));
69+
return GeneralInsertDSL.this;
70+
}
71+
72+
public GeneralInsertDSL toStringConstant(String constant) {
73+
insertMappings.add(StringConstantMapping.of(column, constant));
74+
return GeneralInsertDSL.this;
75+
}
76+
77+
public GeneralInsertDSL toValue(T value) {
78+
return toValue(() -> value);
79+
}
80+
81+
public GeneralInsertDSL toValue(Supplier<T> valueSupplier) {
82+
insertMappings.add(ValueMapping.of(column, valueSupplier));
83+
return GeneralInsertDSL.this;
84+
}
85+
86+
public GeneralInsertDSL toValueWhenPresent(T value) {
87+
return toValueWhenPresent(() -> value);
88+
}
89+
90+
public GeneralInsertDSL toValueWhenPresent(Supplier<T> valueSupplier) {
91+
if (valueSupplier.get() != null) {
92+
insertMappings.add(ValueMapping.of(column, valueSupplier));
93+
}
94+
return GeneralInsertDSL.this;
95+
}
96+
}
97+
}
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.AbstractColumnMapping;
29+
30+
public class GeneralInsertModel {
31+
32+
private SqlTable table;
33+
private List<AbstractColumnMapping> 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<AbstractColumnMapping, 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<AbstractColumnMapping> insertMappings = new ArrayList<>();
58+
59+
public Builder withTable(SqlTable table) {
60+
this.table = table;
61+
return this;
62+
}
63+
64+
public Builder withInsertMappings(List<AbstractColumnMapping> insertMappings) {
65+
this.insertMappings.addAll(insertMappings);
66+
return this;
67+
}
68+
69+
public GeneralInsertModel build() {
70+
return new GeneralInsertModel(this);
71+
}
72+
}
73+
}

src/main/java/org/mybatis/dynamic/sql/insert/InsertDSL.java

Lines changed: 3 additions & 3 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.
@@ -21,8 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.SqlColumn;
2323
import org.mybatis.dynamic.sql.SqlTable;
24+
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
2425
import org.mybatis.dynamic.sql.util.ConstantMapping;
25-
import org.mybatis.dynamic.sql.util.InsertMapping;
2626
import org.mybatis.dynamic.sql.util.NullMapping;
2727
import org.mybatis.dynamic.sql.util.PropertyMapping;
2828
import org.mybatis.dynamic.sql.util.StringConstantMapping;
@@ -31,7 +31,7 @@ public class InsertDSL<T> {
3131

3232
private T record;
3333
private SqlTable table;
34-
private List<InsertMapping> columnMappings = new ArrayList<>();
34+
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
3535

3636
private InsertDSL(T record, SqlTable table) {
3737
this.record = record;

src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java

Lines changed: 6 additions & 6 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.
@@ -25,20 +25,20 @@
2525
import org.mybatis.dynamic.sql.insert.render.InsertRenderer;
2626
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2727
import org.mybatis.dynamic.sql.render.RenderingStrategy;
28-
import org.mybatis.dynamic.sql.util.InsertMapping;
28+
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
2929

3030
public class InsertModel<T> {
3131
private SqlTable table;
3232
private T record;
33-
private List<InsertMapping> columnMappings;
33+
private List<AbstractColumnMapping> columnMappings;
3434

3535
private InsertModel(Builder<T> builder) {
3636
table = Objects.requireNonNull(builder.table);
3737
record = Objects.requireNonNull(builder.record);
3838
columnMappings = Objects.requireNonNull(builder.columnMappings);
3939
}
4040

41-
public <R> Stream<R> mapColumnMappings(Function<InsertMapping, R> mapper) {
41+
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
4242
return columnMappings.stream().map(mapper);
4343
}
4444

@@ -64,7 +64,7 @@ public static <T> Builder<T> withRecord(T record) {
6464
public static class Builder<T> {
6565
private SqlTable table;
6666
private T record;
67-
private List<InsertMapping> columnMappings = new ArrayList<>();
67+
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
6868

6969
public Builder<T> withTable(SqlTable table) {
7070
this.table = table;
@@ -76,7 +76,7 @@ public Builder<T> withRecord(T record) {
7676
return this;
7777
}
7878

79-
public Builder<T> withColumnMappings(List<InsertMapping> columnMappings) {
79+
public Builder<T> withColumnMappings(List<AbstractColumnMapping> columnMappings) {
8080
this.columnMappings.addAll(columnMappings);
8181
return this;
8282
}

0 commit comments

Comments
 (0)