Skip to content

Commit b23d9a3

Browse files
authored
Merge pull request #154 from jeffgbutler/fix-kotlin
Improve Kotlin Support
2 parents 2955dcc + 3fa8862 commit b23d9a3

23 files changed

+425
-265
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
99
### Added
1010

1111
- Added support for reusing WHERE clauses among count, delete, select, and update statements ([#152](https://github.com/mybatis/mybatis-dynamic-sql/pull/152))
12-
12+
- Improved Kotlin support. Previously, several overloaded methods could collide causing queries to be fragile and very dependent on having the correct imports in a Kotlin file. With this improved support there is no longer any ambiguity. ([#154](https://github.com/mybatis/mybatis-dynamic-sql/pull/154))
1313

1414
### Bugs Fixed
1515

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<sonar.sources>pom.xml,src/main/java,src/main/kotlin</sonar.sources>
4444
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
4545
<jacoco.version>0.8.4</jacoco.version>
46+
<kotlin.code.style>official</kotlin.code.style>
4647
</properties>
4748

4849
<build>

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition
5151
return whereBuilder;
5252
}
5353

54-
@SuppressWarnings("unchecked")
5554
public DeleteWhereBuilder applyWhere(WhereApplier whereApplier) {
56-
return (DeleteWhereBuilder) whereApplier.apply(whereBuilder);
55+
return whereBuilder.applyWhere(whereApplier);
5756
}
5857

5958
/**
@@ -99,7 +98,7 @@ public class DeleteWhereBuilder extends AbstractWhereDSL<DeleteWhereBuilder> imp
9998
private DeleteWhereBuilder() {
10099
super();
101100
}
102-
101+
103102
@Override
104103
public R build() {
105104
return DeleteDSL.this.build();

src/main/java/org/mybatis/dynamic/sql/select/CountDSL.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ public <T> CountWhereBuilder where(BindableColumn<T> column, VisitableCondition<
5757
return whereBuilder;
5858
}
5959

60-
@SuppressWarnings("unchecked")
6160
public CountWhereBuilder applyWhere(WhereApplier whereApplier) {
62-
return (CountWhereBuilder) whereApplier.apply(whereBuilder);
61+
return whereBuilder.applyWhere(whereApplier);
6362
}
6463

6564
@Override

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
6969
return whereBuilder;
7070
}
7171

72-
@SuppressWarnings("unchecked")
7372
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
74-
return (QueryExpressionWhereBuilder) whereApplier.apply(whereBuilder);
73+
return whereBuilder.applyWhere(whereApplier);
7574
}
7675

7776
@Override

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public <T> UpdateWhereBuilder where(BindableColumn<T> column, VisitableCondition
6969
return whereBuilder;
7070
}
7171

72-
@SuppressWarnings("unchecked")
7372
public UpdateWhereBuilder applyWhere(WhereApplier whereApplier) {
74-
return (UpdateWhereBuilder) whereApplier.apply(whereBuilder);
73+
return whereBuilder.applyWhere(whereApplier);
7574
}
7675

7776
/**

src/main/java/org/mybatis/dynamic/sql/where/AbstractWhereDSL.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public <S> T where(BindableColumn<S> column, VisitableCondition<S> condition, Li
4444
addCriterion(column, condition, subCriteria);
4545
return getThis();
4646
}
47-
47+
48+
@SuppressWarnings("unchecked")
49+
public T applyWhere(WhereApplier whereApplier) {
50+
return (T) whereApplier.apply(this);
51+
}
52+
4853
public <S> T and(BindableColumn<S> column, VisitableCondition<S> condition) {
4954
addCriterion("and", column, condition); //$NON-NLS-1$
5055
return getThis();

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

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.util.kotlin
17+
18+
import org.mybatis.dynamic.sql.BindableColumn
19+
import org.mybatis.dynamic.sql.SqlTable
20+
import org.mybatis.dynamic.sql.VisitableCondition
21+
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
22+
import org.mybatis.dynamic.sql.select.SelectModel
23+
import org.mybatis.dynamic.sql.util.Buildable
24+
import org.mybatis.dynamic.sql.where.AbstractWhereDSL
25+
26+
abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<M, W, B>> : Buildable<M> {
27+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
28+
getWhere().where(column, condition)
29+
return getThis()
30+
}
31+
32+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
33+
getWhere().where(column, condition, collect)
34+
return getThis()
35+
}
36+
37+
fun applyWhere(whereApplier: WhereApplier): B {
38+
getWhere().applyWhere(whereApplier)
39+
return getThis()
40+
}
41+
42+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
43+
getWhere().and(column, condition)
44+
return getThis()
45+
}
46+
47+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
48+
getWhere().and(column, condition, collect)
49+
return getThis()
50+
}
51+
52+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
53+
getWhere().or(column, condition)
54+
return getThis()
55+
}
56+
57+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
58+
getWhere().or(column, condition, collect)
59+
return getThis()
60+
}
61+
62+
protected abstract fun getWhere(): W
63+
protected abstract fun getThis(): B
64+
}
65+
66+
abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<T, SelectModel>, W: AbstractWhereDSL<W>, B : KotlinBaseJoiningBuilder<T, W, B>>(private val dsl: AbstractQueryExpressionDSL<T, SelectModel>) :
67+
KotlinBaseBuilder<SelectModel, W, B>() {
68+
69+
fun join(table: SqlTable, receiver: JoinReceiver) =
70+
apply {
71+
dsl.join(table, receiver)
72+
}
73+
74+
fun join(table: SqlTable, alias: String, receiver: JoinReceiver) =
75+
apply {
76+
dsl.join(table, alias, receiver)
77+
}
78+
79+
fun fullJoin(table: SqlTable, receiver: JoinReceiver) =
80+
apply {
81+
dsl.fullJoin(table, receiver)
82+
}
83+
84+
fun fullJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
85+
apply {
86+
dsl.fullJoin(table, alias, receiver)
87+
}
88+
89+
fun leftJoin(table: SqlTable, receiver: JoinReceiver) =
90+
apply {
91+
dsl.leftJoin(table, receiver)
92+
}
93+
94+
fun leftJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
95+
apply {
96+
dsl.leftJoin(table, alias, receiver)
97+
}
98+
99+
fun rightJoin(table: SqlTable, receiver: JoinReceiver) =
100+
apply {
101+
dsl.rightJoin(table, receiver)
102+
}
103+
104+
fun rightJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
105+
apply {
106+
dsl.rightJoin(table, alias, receiver)
107+
}
108+
}

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