Skip to content

Commit a035e8a

Browse files
committed
Complete work on a general insert statement and test everything
1 parent be24b8c commit a035e8a

File tree

17 files changed

+265
-83
lines changed

17 files changed

+265
-83
lines changed

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

Lines changed: 6 additions & 8 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.
@@ -24,11 +24,9 @@
2424
import org.mybatis.dynamic.sql.delete.DeleteModel;
2525
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
2626
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
27-
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL.SetClauseFinisher;
2827
import org.mybatis.dynamic.sql.insert.InsertDSL;
2928
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
3029
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
31-
import org.mybatis.dynamic.sql.insert.InsertSelectDSL.SelectGatherer;
3230
import org.mybatis.dynamic.sql.select.CountDSL;
3331
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
3432
import org.mybatis.dynamic.sql.select.SelectDSL;
@@ -641,25 +639,25 @@ static SortSpecification sortColumn(String name) {
641639
return SimpleSortSpecification.of(name);
642640
}
643641

644-
public static class InsertIntoNextStep {
642+
class InsertIntoNextStep {
645643

646644
private SqlTable table;
647-
645+
648646
private InsertIntoNextStep(SqlTable table) {
649647
this.table = Objects.requireNonNull(table);
650648
}
651-
649+
652650
public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
653651
return InsertSelectDSL.insertInto(table)
654652
.withSelectStatement(selectModelBuilder);
655653
}
656654

657-
public SelectGatherer withColumnList(SqlColumn<?>...columns) {
655+
public InsertSelectDSL.SelectGatherer withColumnList(SqlColumn<?>...columns) {
658656
return InsertSelectDSL.insertInto(table)
659657
.withColumnList(columns);
660658
}
661659

662-
public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
660+
public <T> GeneralInsertDSL.SetClauseFinisher<T> set(SqlColumn<T> column) {
663661
return GeneralInsertDSL.insertInto(table)
664662
.set(column);
665663
}

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

Lines changed: 1 addition & 9 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.
@@ -22,12 +22,9 @@
2222

2323
import org.mybatis.dynamic.sql.SqlColumn;
2424
import org.mybatis.dynamic.sql.SqlTable;
25-
import org.mybatis.dynamic.sql.select.SelectModel;
2625
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
27-
import org.mybatis.dynamic.sql.util.Buildable;
2826
import org.mybatis.dynamic.sql.util.ConstantMapping;
2927
import org.mybatis.dynamic.sql.util.NullMapping;
30-
import org.mybatis.dynamic.sql.util.SelectMapping;
3128
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3229
import org.mybatis.dynamic.sql.util.ValueMapping;
3330

@@ -86,11 +83,6 @@ public GeneralInsertDSL equalTo(Supplier<T> valueSupplier) {
8683
return GeneralInsertDSL.this;
8784
}
8885

89-
public GeneralInsertDSL equalTo(Buildable<SelectModel> buildable) {
90-
insertMappings.add(SelectMapping.of(column, buildable));
91-
return GeneralInsertDSL.this;
92-
}
93-
9486
public GeneralInsertDSL equalToWhenPresent(T value) {
9587
return equalToWhenPresent(() -> value);
9688
}

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

Lines changed: 1 addition & 6 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.
@@ -66,11 +66,6 @@ public Builder withParameter(String key, Object value) {
6666
return this;
6767
}
6868

69-
public Builder withParameters(Map<String, Object> parameters) {
70-
this.parameters.putAll(parameters);
71-
return this;
72-
}
73-
7469
public FieldAndValueAndParameters build() {
7570
return new FieldAndValueAndParameters(this);
7671
}

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

Lines changed: 1 addition & 23 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.
@@ -20,12 +20,9 @@
2020

2121
import org.mybatis.dynamic.sql.SqlColumn;
2222
import org.mybatis.dynamic.sql.render.RenderingStrategy;
23-
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
24-
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2523
import org.mybatis.dynamic.sql.util.ColumnMappingVisitor;
2624
import org.mybatis.dynamic.sql.util.ConstantMapping;
2725
import org.mybatis.dynamic.sql.util.NullMapping;
28-
import org.mybatis.dynamic.sql.util.SelectMapping;
2926
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3027
import org.mybatis.dynamic.sql.util.ValueMapping;
3128

@@ -71,25 +68,6 @@ public <R> FieldAndValueAndParameters visit(ValueMapping<R> mapping) {
7168
.build();
7269
}
7370

74-
@Override
75-
public FieldAndValueAndParameters visit(SelectMapping mapping) {
76-
SelectStatementProvider selectStatement = SelectRenderer.withSelectModel(mapping.selectModel())
77-
.withRenderingStrategy(renderingStrategy)
78-
.withSequence(sequence)
79-
.build()
80-
.render();
81-
82-
String fragment = mapping.mapColumn(SqlColumn::name)
83-
+ " = (" //$NON-NLS-1$
84-
+ selectStatement.getSelectStatement()
85-
+ ")"; //$NON-NLS-1$
86-
87-
return FieldAndValueAndParameters.withFieldName(mapping.mapColumn(SqlColumn::name))
88-
.withValuePhrase(fragment)
89-
.withParameters(selectStatement.getParameters())
90-
.build();
91-
}
92-
9371
private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
9472
return column -> renderingStrategy
9573
.getFormattedJdbcPlaceholder(column, RenderingStrategy.DEFAULT_PARAMETER_PREFIX, parameterName);

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

Lines changed: 15 additions & 1 deletion
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.
@@ -27,8 +27,10 @@
2727
import org.mybatis.dynamic.sql.SqlTable;
2828
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
2929
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
30+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
3031
import org.mybatis.dynamic.sql.insert.InsertDSL;
3132
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
33+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
3234
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
3335
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
3436
import org.mybatis.dynamic.sql.render.RenderingStrategies;
@@ -93,6 +95,18 @@ public static <R> int insert(ToIntFunction<InsertStatementProvider<R>> mapper, R
9395
return mapper.applyAsInt(insert(record, table, completer));
9496
}
9597

98+
public static GeneralInsertStatementProvider insert(SqlTable table,
99+
UnaryOperator<GeneralInsertDSL> completer) {
100+
return completer.apply(GeneralInsertDSL.insertInto(table))
101+
.build()
102+
.render(RenderingStrategies.MYBATIS3);
103+
}
104+
105+
public static int insert(ToIntFunction<GeneralInsertStatementProvider> mapper,
106+
SqlTable table, UnaryOperator<GeneralInsertDSL> completer) {
107+
return mapper.applyAsInt(insert(table, completer));
108+
}
109+
96110
public static <R> MultiRowInsertStatementProvider<R> insertMultiple(Collection<R> records, SqlTable table,
97111
UnaryOperator<MultiRowInsertDSL<R>> completer) {
98112
return completer.apply(SqlBuilder.insertMultiple(records).into(table))

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinUpdateBuilder.kt

Lines changed: 5 additions & 2 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.
@@ -16,21 +16,24 @@
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

1818
import org.mybatis.dynamic.sql.SqlColumn
19+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL
1920
import org.mybatis.dynamic.sql.insert.InsertDSL
2021
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2122
import org.mybatis.dynamic.sql.update.UpdateDSL
2223
import org.mybatis.dynamic.sql.update.UpdateModel
2324
import org.mybatis.dynamic.sql.util.Buildable
2425

2526
// insert completers are here because sonar doesn't see them as covered if they are in a file by themselves
27+
typealias GeneralInsertCompleter = GeneralInsertDSL.() -> GeneralInsertDSL
28+
2629
typealias InsertCompleter<T> = InsertDSL<T>.() -> InsertDSL<T>
2730

2831
typealias MultiRowInsertCompleter<T> = MultiRowInsertDSL<T>.() -> MultiRowInsertDSL<T>
2932

3033
typealias UpdateCompleter = KotlinUpdateBuilder.() -> Buildable<UpdateModel>
3134

3235
class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
33-
KotlinBaseBuilder<UpdateModel, UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>() {
36+
KotlinBaseBuilder<UpdateModel, UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>() {
3437

3538
fun <T> set(column: SqlColumn<T>): UpdateDSL<UpdateModel>.SetClauseFinisher<T> = dsl.set(column)
3639

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt

Lines changed: 39 additions & 7 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.
@@ -19,6 +19,7 @@ import org.mybatis.dynamic.sql.BasicColumn
1919
import org.mybatis.dynamic.sql.SqlBuilder
2020
import org.mybatis.dynamic.sql.SqlTable
2121
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider
22+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2223
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
2324
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider
2425
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
@@ -36,22 +37,53 @@ fun deleteFrom(mapper: (DeleteStatementProvider) -> Int, table: SqlTable, comple
3637
fun <T> insert(mapper: (InsertStatementProvider<T>) -> Int, record: T, table: SqlTable, completer: InsertCompleter<T>) =
3738
mapper(SqlBuilder.insert(record).into(table, completer))
3839

39-
fun <T> insertMultiple(mapper: (MultiRowInsertStatementProvider<T>) -> Int, records: Collection<T>, table: SqlTable, completer: MultiRowInsertCompleter<T>) =
40+
fun insertInto(mapper: (GeneralInsertStatementProvider) -> Int, table: SqlTable, completer: GeneralInsertCompleter) =
41+
mapper(insertInto(table, completer))
42+
43+
fun <T> insertMultiple(
44+
mapper: (MultiRowInsertStatementProvider<T>) -> Int,
45+
records: Collection<T>,
46+
table: SqlTable,
47+
completer: MultiRowInsertCompleter<T>
48+
) =
4049
mapper(SqlBuilder.insertMultiple(records).into(table, completer))
4150

42-
fun <T> selectDistinct(mapper: (SelectStatementProvider) -> List<T>, selectList: List<BasicColumn>, table: SqlTable, completer: SelectCompleter) =
51+
fun <T> selectDistinct(
52+
mapper: (SelectStatementProvider) -> List<T>,
53+
selectList: List<BasicColumn>,
54+
table: SqlTable,
55+
completer: SelectCompleter
56+
) =
4357
mapper(SqlBuilder.selectDistinct(selectList).from(table, completer))
4458

45-
fun <T> selectList(mapper: (SelectStatementProvider) -> List<T>, selectList: List<BasicColumn>, table: SqlTable, completer: SelectCompleter) =
59+
fun <T> selectList(
60+
mapper: (SelectStatementProvider) -> List<T>,
61+
selectList: List<BasicColumn>,
62+
table: SqlTable,
63+
completer: SelectCompleter
64+
) =
4665
mapper(SqlBuilder.select(selectList).from(table, completer))
4766

48-
fun <T> selectList(mapper: (SelectStatementProvider) -> List<T>, start: QueryExpressionDSL<SelectModel>, completer: SelectCompleter) =
67+
fun <T> selectList(
68+
mapper: (SelectStatementProvider) -> List<T>,
69+
start: QueryExpressionDSL<SelectModel>,
70+
completer: SelectCompleter
71+
) =
4972
mapper(select(start, completer))
5073

51-
fun <T> selectOne(mapper: (SelectStatementProvider) -> T?, selectList: List<BasicColumn>, table: SqlTable, completer: SelectCompleter) =
74+
fun <T> selectOne(
75+
mapper: (SelectStatementProvider) -> T?,
76+
selectList: List<BasicColumn>,
77+
table: SqlTable,
78+
completer: SelectCompleter
79+
) =
5280
mapper(SqlBuilder.select(selectList).from(table, completer))
5381

54-
fun <T> selectOne(mapper: (SelectStatementProvider) -> T?, start: QueryExpressionDSL<SelectModel>, completer: SelectCompleter) =
82+
fun <T> selectOne(
83+
mapper: (SelectStatementProvider) -> T?,
84+
start: QueryExpressionDSL<SelectModel>,
85+
completer: SelectCompleter
86+
) =
5587
mapper(select(start, completer))
5688

5789
fun update(mapper: (UpdateStatementProvider) -> Int, table: SqlTable, completer: UpdateCompleter) =

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt

Lines changed: 21 additions & 6 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.
@@ -18,8 +18,10 @@ package org.mybatis.dynamic.sql.util.kotlin.mybatis3
1818
import org.mybatis.dynamic.sql.SqlBuilder
1919
import org.mybatis.dynamic.sql.SqlTable
2020
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider
21+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL
2122
import org.mybatis.dynamic.sql.insert.InsertDSL
2223
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
24+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2325
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
2426
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider
2527
import org.mybatis.dynamic.sql.render.RenderingStrategies
@@ -42,18 +44,28 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProv
4244
}
4345

4446
fun <T> InsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: InsertCompleter<T>): InsertStatementProvider<T> =
45-
completer(into(table)).build().render(RenderingStrategies.MYBATIS3)
47+
completer(into(table)).build().render(RenderingStrategies.MYBATIS3)
4648

47-
fun <T> MultiRowInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: MultiRowInsertCompleter<T>): MultiRowInsertStatementProvider<T> =
48-
completer(into(table)).build().render(RenderingStrategies.MYBATIS3)
49+
fun <T> MultiRowInsertDSL.IntoGatherer<T>.into(
50+
table: SqlTable,
51+
completer: MultiRowInsertCompleter<T>
52+
): MultiRowInsertStatementProvider<T> =
53+
completer(into(table)).build().render(RenderingStrategies.MYBATIS3)
4954

50-
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(table: SqlTable, completer: SelectCompleter): SelectStatementProvider {
55+
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(
56+
table: SqlTable,
57+
completer: SelectCompleter
58+
): SelectStatementProvider {
5159
val builder = KotlinQueryBuilder(from(table))
5260
completer(builder)
5361
return builder.build().render(RenderingStrategies.MYBATIS3)
5462
}
5563

56-
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(table: SqlTable, alias: String, completer: SelectCompleter): SelectStatementProvider {
64+
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(
65+
table: SqlTable,
66+
alias: String,
67+
completer: SelectCompleter
68+
): SelectStatementProvider {
5769
val builder = KotlinQueryBuilder(from(table, alias))
5870
completer(builder)
5971
return builder.build().render(RenderingStrategies.MYBATIS3)
@@ -70,3 +82,6 @@ fun update(table: SqlTable, completer: UpdateCompleter): UpdateStatementProvider
7082
completer(builder)
7183
return builder.build().render(RenderingStrategies.MYBATIS3)
7284
}
85+
86+
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertStatementProvider =
87+
completer(GeneralInsertDSL.insertInto(table)).build().render(RenderingStrategies.MYBATIS3)

0 commit comments

Comments
 (0)