Skip to content

Commit c48a1d2

Browse files
committed
Optimizing
1 parent bf5ac7d commit c48a1d2

File tree

14 files changed

+172
-248
lines changed

14 files changed

+172
-248
lines changed

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();
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+
}

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

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,92 +15,20 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

18-
import org.mybatis.dynamic.sql.BindableColumn
19-
import org.mybatis.dynamic.sql.SqlTable
20-
import org.mybatis.dynamic.sql.VisitableCondition
2118
import org.mybatis.dynamic.sql.select.CountDSL
2219
import org.mybatis.dynamic.sql.select.SelectModel
2320
import org.mybatis.dynamic.sql.util.Buildable
2421

2522
typealias CountCompleter = KotlinCountBuilder.() -> Buildable<SelectModel>
2623

27-
class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>): Buildable<SelectModel> {
28-
fun join(table: SqlTable, receiver: JoinReceiver) =
29-
apply {
30-
dsl.join(table, receiver)
31-
}
24+
class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>) :
25+
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl) {
3226

33-
fun join(table: SqlTable, alias: String, receiver: JoinReceiver) =
34-
apply {
35-
dsl.join(table, alias, receiver)
36-
}
37-
38-
fun fullJoin(table: SqlTable, receiver: JoinReceiver) =
39-
apply {
40-
dsl.fullJoin(table, receiver)
41-
}
42-
43-
fun fullJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
44-
apply {
45-
dsl.fullJoin(table, alias, receiver)
46-
}
47-
48-
fun leftJoin(table: SqlTable, receiver: JoinReceiver) =
49-
apply {
50-
dsl.leftJoin(table, receiver)
51-
}
52-
53-
fun leftJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
54-
apply {
55-
dsl.leftJoin(table, alias, receiver)
56-
}
57-
58-
fun rightJoin(table: SqlTable, receiver: JoinReceiver) =
59-
apply {
60-
dsl.rightJoin(table, receiver)
61-
}
62-
63-
fun rightJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
64-
apply {
65-
dsl.rightJoin(table, alias, receiver)
66-
}
67-
68-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>) =
69-
apply {
70-
dsl.where(column, condition)
71-
}
72-
73-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
74-
apply {
75-
dsl.where().where(column, condition, collect)
76-
}
77-
78-
fun applyWhere(whereApplier: WhereApplier) =
79-
apply {
80-
dsl.applyWhere(whereApplier)
81-
}
82-
83-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>) =
84-
apply {
85-
dsl.where().and(column, condition)
86-
}
87-
88-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
89-
apply {
90-
dsl.where().and(column, condition, collect)
91-
}
92-
93-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>) =
94-
apply {
95-
dsl.where().or(column, condition)
96-
}
27+
fun allRows() = this
9728

98-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
99-
apply {
100-
dsl.where().or(column, condition, collect)
101-
}
29+
override fun build(): SelectModel = dsl.build()
10230

103-
fun allRows() = this
31+
override fun getWhere(): CountDSL<SelectModel>.CountWhereBuilder = dsl.where()
10432

105-
override fun build(): SelectModel = dsl.build()
33+
override fun getThis() = this
10634
}

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

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,20 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

18-
import org.mybatis.dynamic.sql.BindableColumn
19-
import org.mybatis.dynamic.sql.VisitableCondition
2018
import org.mybatis.dynamic.sql.delete.DeleteDSL
2119
import org.mybatis.dynamic.sql.delete.DeleteModel
2220
import org.mybatis.dynamic.sql.util.Buildable
2321

2422
typealias DeleteCompleter = KotlinDeleteBuilder.() -> Buildable<DeleteModel>
2523

26-
class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>): Buildable<DeleteModel> {
27-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>) =
28-
apply {
29-
dsl.where(column, condition)
30-
}
31-
32-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
33-
apply {
34-
dsl.where().where(column, condition, collect)
35-
}
36-
37-
fun applyWhere(whereApplier: WhereApplier) =
38-
apply {
39-
dsl.applyWhere(whereApplier)
40-
}
41-
42-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>) =
43-
apply {
44-
dsl.where().and(column, condition)
45-
}
46-
47-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
48-
apply {
49-
dsl.where().and(column, condition, collect)
50-
}
51-
52-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>) =
53-
apply {
54-
dsl.where().or(column, condition)
55-
}
56-
57-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
58-
apply {
59-
dsl.where().or(column, condition, collect)
60-
}
24+
class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
25+
KotlinBaseBuilder<DeleteModel, DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>() {
6126

6227
fun allRows() = this
6328

6429
override fun build(): DeleteModel = dsl.build()
30+
31+
override fun getWhere(): DeleteDSL<DeleteModel>.DeleteWhereBuilder = dsl.where()
32+
33+
override fun getThis(): KotlinDeleteBuilder = this
6534
}

0 commit comments

Comments
 (0)