Skip to content

Commit 70b2833

Browse files
committed
Support for basic union queries in Kotlin
1 parent ca7d55b commit 70b2833

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed

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

Lines changed: 1 addition & 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.

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

Lines changed: 1 addition & 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.

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

Lines changed: 1 addition & 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.

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

Lines changed: 6 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.
@@ -50,6 +50,11 @@ class KotlinQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
5050

5151
fun allRows() = this
5252

53+
fun union(union: KotlinUnionBuilder.() -> QueryExpressionDSL<SelectModel>) =
54+
apply {
55+
union(KotlinUnionBuilder(dsl))
56+
}
57+
5358
override fun build(): SelectModel = dsl.build()
5459

5560
override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = dsl.where()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2016-2020 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.BasicColumn
19+
import org.mybatis.dynamic.sql.SqlTable
20+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
21+
import org.mybatis.dynamic.sql.select.SelectModel
22+
23+
class KotlinUnionBuilder(private val dsl: QueryExpressionDSL<SelectModel>) {
24+
fun select(vararg selectList: BasicColumn) =
25+
select(listOf(*selectList))
26+
27+
fun select(selectList: List<BasicColumn>) =
28+
KotlinUnionFromGatherer(dsl, selectList)
29+
}
30+
31+
class KotlinUnionFromGatherer(
32+
private val dsl: QueryExpressionDSL<SelectModel>,
33+
private val selectList: List<BasicColumn>
34+
) {
35+
fun from(
36+
table: SqlTable,
37+
enhance: KotlinUnionQueryBuilder.() -> KotlinUnionQueryBuilder
38+
): QueryExpressionDSL<SelectModel> {
39+
val unionBuilder = KotlinUnionQueryBuilder(dsl.union().select(selectList).from(table))
40+
enhance(unionBuilder)
41+
return dsl
42+
}
43+
}
44+
45+
class KotlinUnionQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
46+
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder,
47+
KotlinUnionQueryBuilder>(dsl) {
48+
override fun self() = this
49+
50+
override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = dsl.where()
51+
}

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTest.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,69 @@ class CanonicalSpringKotlinTest {
360360
}
361361
}
362362

363+
@Test
364+
fun testRawSelectWithUnion() {
365+
val selectStatement = select(
366+
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
367+
addressId
368+
).from(Person) {
369+
where(id, isEqualTo(1))
370+
union {
371+
select(
372+
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
373+
addressId
374+
).from(Person) {
375+
where(id, isEqualTo(2))
376+
}
377+
}
378+
union {
379+
select(
380+
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
381+
addressId
382+
).from(Person) {
383+
where(id, isEqualTo(3))
384+
}
385+
}
386+
}
387+
388+
val expected = "select id as A_ID, first_name, last_name, birth_date, employed, occupation, address_id " +
389+
"from Person " +
390+
"where id = :p1 " +
391+
"union " +
392+
"select id as A_ID, first_name, last_name, birth_date, employed, occupation, address_id " +
393+
"from Person " +
394+
"where id = :p2 " +
395+
"union " +
396+
"select id as A_ID, first_name, last_name, birth_date, employed, occupation, address_id " +
397+
"from Person " +
398+
"where id = :p3"
399+
400+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
401+
402+
val records = template.selectList(selectStatement) { rs, _ ->
403+
val record = PersonRecord()
404+
record.id = rs.getInt(1)
405+
record.firstName = rs.getString(2)
406+
record.lastName = rs.getString(3)
407+
record.birthDate = rs.getTimestamp(4)
408+
record.employed = rs.getString(5)
409+
record.occupation = rs.getString(6)
410+
record.addressId = rs.getInt(7)
411+
record
412+
}
413+
414+
assertThat(records).hasSize(3)
415+
with(records[0]!!) {
416+
assertThat(id).isEqualTo(1)
417+
assertThat(firstName).isEqualTo("Fred")
418+
assertThat(lastName).isEqualTo("Flintstone")
419+
assertThat(birthDate).isNotNull()
420+
assertThat(employed).isEqualTo("Yes")
421+
assertThat(occupation).isEqualTo("Brontosaurus Operator")
422+
assertThat(addressId).isEqualTo(1)
423+
}
424+
}
425+
363426
@Test
364427
fun testRawSelectWithJoin() {
365428
val selectStatement = select(

0 commit comments

Comments
 (0)