Skip to content

Commit d023ee8

Browse files
authored
Major Kotlin DSL Improvement (#353)
* Update and extend the Kotlin DSL - Remove dependency on org.mybatis.dynamic.sql.SqlBuilder as this causes name collisions in some cases - Explicitly denote the nullability aspects of the API - Specify all return types per Kotlin Library coding conventions * Use new Kotlin IR compiler * Improved Kotlin specific column builder * Remove the supplier based methods - they cause weird overload issues and the don't make much sense anymore as the underlying Java code doesn't use them * New meta-model pattern * Add support for batch inserts with MyBatis * Kotlin documentation updates * Add flush capability to CommonInsertMapper * Add support for generated keys with insert select in Spring
1 parent 3962a17 commit d023ee8

File tree

70 files changed

+5845
-1578
lines changed

Some content is hidden

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

70 files changed

+5845
-1578
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The major themes of this release include the following:
1919
1. Remove deprecated code from prior releases.
2020

2121
### Built-In Condition Refactoring
22-
All built-in conditions have been rafactored. The changes should have no impact for the vast majority of users.
22+
All built-in conditions have been refactored. The changes should have no impact for the vast majority of users.
2323
However, there are some changes in behavior and one breaking change.
2424

2525
1. Internally, the conditions no longer hold value Suppliers, they now hold the values themselves. The SqlBuilder
@@ -100,6 +100,7 @@ Kotlin DSL.
100100
- Added a mapping for general insert and update statements that will render null values as "null" in the SQL ([#343](https://github.com/mybatis/mybatis-dynamic-sql/pull/343))
101101
- Allow the "in when present" conditions to accept a null Collection as a parameter ([#346](https://github.com/mybatis/mybatis-dynamic-sql/pull/346))
102102
- Add Better Support for MyBatis Multi-Row Inserts that Return Generated Keys ([#349](https://github.com/mybatis/mybatis-dynamic-sql/pull/349))
103+
- Major improvement to the Kotlin DSL ([#353](https://github.com/mybatis/mybatis-dynamic-sql/pull/353))
103104

104105
## Release 1.2.1 - September 29, 2020
105106

pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
<spring.batch.version>4.3.2</spring.batch.version>
3939
<clirr.comparisonVersion>1.2.0</clirr.comparisonVersion>
4040
<module.name>org.mybatis.dynamic.sql</module.name>
41-
<kotlin.version>1.5.0</kotlin.version>
41+
<!-- Kotlin 1.5.0/IR Compiler must wait for Jacoco 0.8.7 -->
42+
<kotlin.version>1.4.32</kotlin.version>
4243
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
4344
<sonar.sources>pom.xml,src/main/java,src/main/kotlin</sonar.sources>
4445
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
@@ -64,6 +65,11 @@
6465
<groupId>org.jetbrains.kotlin</groupId>
6566
<artifactId>kotlin-maven-plugin</artifactId>
6667
<version>${kotlin.version}</version>
68+
<configuration>
69+
<args>
70+
<arg>-Xuse-ir</arg>
71+
</args>
72+
</configuration>
6773
<executions>
6874
<execution>
6975
<id>compile</id>

src/main/java/org/mybatis/dynamic/sql/select/function/Concatenate.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -37,4 +37,9 @@ public static <T> Concatenate<T> concatenate(BindableColumn<T> firstColumn, Basi
3737
BasicColumn... subsequentColumns) {
3838
return new Concatenate<>(firstColumn, secondColumn, Arrays.asList(subsequentColumns));
3939
}
40+
41+
public static <T> Concatenate<T> of(BindableColumn<T> firstColumn, BasicColumn secondColumn,
42+
List<BasicColumn> subsequentColumns) {
43+
return new Concatenate<>(firstColumn, secondColumn, subsequentColumns);
44+
}
4045
}

src/main/java/org/mybatis/dynamic/sql/util/SqlProviderAdapter.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util;
1717

18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.stream.Collectors;
21+
1822
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
1923
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2024
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
@@ -23,10 +27,6 @@
2327
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2428
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
2529

26-
import java.util.List;
27-
import java.util.Map;
28-
import java.util.stream.Collectors;
29-
3030
/**
3131
* Adapter for use with MyBatis SQL provider annotations.
3232
*
@@ -52,8 +52,17 @@ public String insertMultiple(MultiRowInsertStatementProvider<?> insertStatement)
5252
}
5353

5454
/**
55-
* This adapter method is intended for use with MyBatis' @InsertProvider annotation when there are generated
56-
* values expected from executing the insert statement.
55+
* This adapter method is intended for use with MyBatis' &#064;InsertProvider annotation when there are generated
56+
* values expected from executing the insert statement. The canonical method signature for using this
57+
* adapter method is as follows:
58+
*
59+
* <pre>
60+
* public interface FooMapper {
61+
* &#064;InsertProvider(type=SqlProviderAdapter.class, method="insertMultipleWithGeneratedKeys")
62+
* &#064;Options(useGeneratedKeys=true, keyProperty="records.id")
63+
* int insertMultiple(String insertStatement, &#064;Param("records") List&lt;Foo&gt; records)
64+
* }
65+
* </pre>
5766
*
5867
* @param parameterMap The parameter map is automatically created by MyBatis when there are multiple
5968
* parameters in the insert method.
@@ -63,8 +72,9 @@ public String insertMultiple(MultiRowInsertStatementProvider<?> insertStatement)
6372
public String insertMultipleWithGeneratedKeys(Map<String, Object> parameterMap) {
6473
List<String> entries = parameterMap.entrySet().stream()
6574
.filter(e -> e.getKey().startsWith("param")) //$NON-NLS-1$
66-
.filter(e -> String.class.isAssignableFrom(e.getValue().getClass()))
67-
.map(e -> (String) e.getValue())
75+
.map(Map.Entry::getValue)
76+
.filter(String.class::isInstance)
77+
.map(String.class::cast)
6878
.collect(Collectors.toList());
6979

7080
if (entries.size() == 1) {

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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,15 +15,20 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.mybatis3;
1717

18+
import java.util.List;
19+
20+
import org.apache.ibatis.annotations.Flush;
1821
import org.apache.ibatis.annotations.InsertProvider;
22+
import org.apache.ibatis.executor.BatchResult;
1923
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2024
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
2125
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2226
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2327
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
2428

2529
/**
26-
* This is a general purpose mapper for executing various types of insert statement.
30+
* This is a general purpose mapper for executing various types of insert statements.
31+
* This mapper is appropriate for insert statements that do NOT expect generated keys.
2732
*
2833
* @param <T> the type of record associated with this mapper
2934
*/
@@ -64,4 +69,13 @@ public interface CommonInsertMapper<T> {
6469
*/
6570
@InsertProvider(type = SqlProviderAdapter.class, method = "insertMultiple")
6671
int insertMultiple(MultiRowInsertStatementProvider<T> insertStatement);
72+
73+
/**
74+
* Flush batched insert statements and return details of the current batch.
75+
* This is useful when there is no direct access to the @link({@link org.apache.ibatis.session.SqlSession}.
76+
*
77+
* @return details about the current batch including update counts, etc.
78+
*/
79+
@Flush
80+
List<BatchResult> flush();
6781
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -28,7 +28,7 @@ typealias CriteriaReceiver = CriteriaCollector.() -> Unit
2828
class CriteriaCollector {
2929
val criteria = mutableListOf<SqlCriterion>()
3030

31-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>) =
31+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): CriteriaCollector =
3232
apply {
3333
criteria.add(
3434
ColumnAndConditionCriterion.withColumn(column)
@@ -42,7 +42,7 @@ class CriteriaCollector {
4242
column: BindableColumn<T>,
4343
condition: VisitableCondition<T>,
4444
criteriaReceiver: CriteriaReceiver
45-
) =
45+
): CriteriaCollector =
4646
apply {
4747
criteria.add(
4848
ColumnAndConditionCriterion.withColumn(column)
@@ -53,7 +53,7 @@ class CriteriaCollector {
5353
)
5454
}
5555

56-
fun and(existsPredicate: ExistsPredicate) =
56+
fun and(existsPredicate: ExistsPredicate): CriteriaCollector =
5757
apply {
5858
criteria.add(
5959
ExistsCriterion.Builder()
@@ -63,7 +63,7 @@ class CriteriaCollector {
6363
)
6464
}
6565

66-
fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver) =
66+
fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver): CriteriaCollector =
6767
apply {
6868
criteria.add(
6969
ExistsCriterion.Builder()
@@ -74,7 +74,7 @@ class CriteriaCollector {
7474
)
7575
}
7676

77-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>) =
77+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): CriteriaCollector =
7878
apply {
7979
criteria.add(
8080
ColumnAndConditionCriterion.withColumn(column)
@@ -88,7 +88,7 @@ class CriteriaCollector {
8888
column: BindableColumn<T>,
8989
condition: VisitableCondition<T>,
9090
criteriaReceiver: CriteriaReceiver
91-
) =
91+
): CriteriaCollector =
9292
apply {
9393
criteria.add(
9494
ColumnAndConditionCriterion.withColumn(column)
@@ -99,7 +99,7 @@ class CriteriaCollector {
9999
)
100100
}
101101

102-
fun or(existsPredicate: ExistsPredicate) =
102+
fun or(existsPredicate: ExistsPredicate): CriteriaCollector =
103103
apply {
104104
criteria.add(
105105
ExistsCriterion.Builder()
@@ -109,7 +109,7 @@ class CriteriaCollector {
109109
)
110110
}
111111

112-
fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver) =
112+
fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver): CriteriaCollector =
113113
apply {
114114
criteria.add(
115115
ExistsCriterion.Builder()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class JoinCollector {
2727
val andJoinCriteria = mutableListOf<JoinCriterion>()
2828
private lateinit var internalOnCriterion: JoinCriterion
2929

30-
fun on(column: BasicColumn, condition: JoinCondition) =
30+
fun on(column: BasicColumn, condition: JoinCondition): JoinCollector =
3131
apply {
3232
internalOnCriterion = JoinCriterion.Builder()
3333
.withConnector("on")
@@ -36,7 +36,7 @@ class JoinCollector {
3636
.build()
3737
}
3838

39-
fun and(column: BasicColumn, condition: JoinCondition) =
39+
fun and(column: BasicColumn, condition: JoinCondition): JoinCollector =
4040
apply {
4141
andJoinCriteria.add(
4242
JoinCriterion.Builder()

0 commit comments

Comments
 (0)