Skip to content

Commit bfbc7ce

Browse files
committed
Simplify the Union Builders
1 parent e966810 commit bfbc7ce

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class KotlinQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
5050

5151
fun allRows() = this
5252

53-
fun union(union: KotlinUnionBuilder.() -> QueryExpressionDSL<SelectModel>) =
53+
fun union(union: KotlinUnionBuilder.() -> Unit) =
5454
apply {
55-
union(KotlinUnionBuilder(dsl, dsl.union()))
55+
union(KotlinUnionBuilder(dsl.union()))
5656
}
5757

58-
fun unionAll(unionAll: KotlinUnionBuilder.() -> QueryExpressionDSL<SelectModel>) =
58+
fun unionAll(unionAll: KotlinUnionBuilder.() -> Unit) =
5959
apply {
60-
unionAll(KotlinUnionBuilder(dsl, dsl.unionAll()))
60+
unionAll(KotlinUnionBuilder(dsl.unionAll()))
6161
}
6262

6363
override fun build(): SelectModel = dsl.build()

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,26 @@ import org.mybatis.dynamic.sql.SqlTable
2020
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
2121
import org.mybatis.dynamic.sql.select.SelectModel
2222

23-
class KotlinUnionBuilder(
24-
private val outerDsl: QueryExpressionDSL<SelectModel>,
25-
private val unionBuilder: QueryExpressionDSL<SelectModel>.UnionBuilder
26-
) {
23+
class KotlinUnionBuilder(private val unionBuilder: QueryExpressionDSL<SelectModel>.UnionBuilder) {
2724
fun select(vararg selectList: BasicColumn) =
2825
select(selectList.toList())
2926

3027
fun select(selectList: List<BasicColumn>) =
31-
KotlinUnionFromGatherer(outerDsl, unionBuilder.select(selectList))
28+
KotlinUnionFromGatherer(unionBuilder.select(selectList))
3229

3330
fun selectDistinct(vararg selectList: BasicColumn) =
3431
selectDistinct(selectList.toList())
3532

3633
fun selectDistinct(selectList: List<BasicColumn>) =
37-
KotlinUnionFromGatherer(outerDsl, unionBuilder.selectDistinct(selectList))
34+
KotlinUnionFromGatherer(unionBuilder.selectDistinct(selectList))
3835
}
3936

40-
class KotlinUnionFromGatherer(
41-
private val outerDsl: QueryExpressionDSL<SelectModel>,
42-
private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>
43-
) {
44-
fun from(
45-
table: SqlTable,
46-
enhance: KotlinUnionQueryBuilder.() -> KotlinUnionQueryBuilder
47-
): QueryExpressionDSL<SelectModel> {
48-
val unionBuilder = KotlinUnionQueryBuilder(fromGatherer.from(table))
49-
enhance(unionBuilder)
50-
return outerDsl
51-
}
37+
class KotlinUnionFromGatherer(private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>) {
38+
fun from(table: SqlTable, enhance: KotlinUnionQueryBuilder.() -> Unit) =
39+
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table)))
5240

53-
fun from(
54-
table: SqlTable,
55-
alias: String,
56-
enhance: KotlinUnionQueryBuilder.() -> KotlinUnionQueryBuilder
57-
): QueryExpressionDSL<SelectModel> {
58-
val unionBuilder = KotlinUnionQueryBuilder(fromGatherer.from(table, alias))
59-
enhance(unionBuilder)
60-
return outerDsl
61-
}
41+
fun from(table: SqlTable, alias: String, enhance: KotlinUnionQueryBuilder.() -> Unit) =
42+
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table, alias)))
6243
}
6344

6445
class KotlinUnionQueryBuilder(private val unionDsl: QueryExpressionDSL<SelectModel>) :

0 commit comments

Comments
 (0)