Skip to content

Commit a3a9cf7

Browse files
committed
[Kotlin] Add an operator overload to apply a qualifier to a column.
This gets us close to natural SQL syntax for qualifiers.
1 parent 7e90820 commit a3a9cf7

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/ColumnExtensions.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ infix fun <T> DerivedColumn<T>.`as`(alias: String): DerivedColumn<T> = this.`as`
2222

2323
infix fun <T> SqlColumn<T>.`as`(alias: String): SqlColumn<T> = this.`as`(alias)
2424

25-
infix fun <T> SqlColumn<T>.qualifiedWith(tableQualifier: String): SqlColumn<T> = this.qualifiedWith(tableQualifier)
25+
/**
26+
* Adds a qualifier to a column for use with table aliases (typically in joins or sub queries).
27+
* This is as close to natural SQL syntax as we can get in Kotlin. Natural SQL would look like
28+
* "qualifier.column". With this function we can say "qualifier(column)".
29+
*/
30+
operator fun <T> String.invoke(column: SqlColumn<T>): SqlColumn<T> = column.qualifiedWith(this)

src/site/markdown/docs/subQueries.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ The Kotlin select build supports subqueries in joins as follows:
358358

359359
```kotlin
360360
val selectStatement = select(OrderLine.orderId, OrderLine.quantity,
361-
ItemMaster.itemId.qualifiedWith("im"), ItemMaster.description) {
361+
"im"(ItemMaster.itemId), ItemMaster.description) {
362362
from(OrderMaster, "om")
363363
join(OrderLine, "ol") {
364364
on(OrderMaster.orderId) equalTo OrderLine.orderId
@@ -371,7 +371,7 @@ val selectStatement = select(OrderLine.orderId, OrderLine.quantity,
371371
+ "im"
372372
}
373373
) {
374-
on(OrderLine.itemId) equalTo (ItemMaster.itemId qualifiedWith "im")
374+
on(OrderLine.itemId) equalTo "im"(ItemMaster.itemId)
375375
}
376376
orderBy(OrderLine.orderId, ItemMaster.itemId)
377377
}
@@ -386,6 +386,7 @@ left join (select * from ItemMaster) im on ol.item_id = im.item_id
386386
order by order_id, item_id
387387
```
388388

389-
Notice again that subquery qualifiers must be specified when needed. Also note that the Kotlin join methods accept
390-
two lambda functions - one for the subquery and one for the join specification. Only the join specification can
391-
be outside the parenthesis of the join methods.
389+
Notice again that sub query qualifiers must be specified when needed. In this case we use a Kotlin trick - an invoke
390+
operator function that gets close to natural SQL syntax (```"im"(ItemMaster.itemId)```). Also note that the Kotlin
391+
join methods accept two lambda functions - one for the sub query and one for the join specification. Only the join
392+
specification can be outside the parenthesis of the join methods.

src/test/kotlin/examples/kotlin/mybatis3/joins/ExistsTest.kt

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder
2626
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
2727
import org.assertj.core.api.Assertions.assertThat
2828
import org.junit.jupiter.api.Test
29-
import org.mybatis.dynamic.sql.util.kotlin.elements.qualifiedWith
29+
import org.mybatis.dynamic.sql.util.kotlin.elements.invoke
3030
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
3131
import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper
3232
import java.io.InputStreamReader
@@ -62,7 +62,7 @@ class ExistsTest {
6262
exists {
6363
select(orderLine.allColumns()) {
6464
from(orderLine, "ol")
65-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
65+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
6666
}
6767
}
6868
}
@@ -149,7 +149,7 @@ class ExistsTest {
149149
exists {
150150
select(orderLine.allColumns()) {
151151
from(orderLine, "ol")
152-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
152+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
153153
}
154154
}
155155
}
@@ -184,7 +184,7 @@ class ExistsTest {
184184
exists {
185185
select(orderLine.allColumns()) {
186186
from(orderLine, "ol")
187-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
187+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
188188
}
189189
}
190190
}
@@ -254,7 +254,7 @@ class ExistsTest {
254254
exists {
255255
select(orderLine.allColumns()) {
256256
from(orderLine, "ol")
257-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
257+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
258258
}
259259
}
260260
}
@@ -290,7 +290,7 @@ class ExistsTest {
290290
exists {
291291
select(orderLine.allColumns()) {
292292
from(orderLine, "ol")
293-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
293+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
294294
}
295295
}
296296
and { itemMaster.itemId isGreaterThan 2 }
@@ -328,7 +328,7 @@ class ExistsTest {
328328
exists {
329329
select(orderLine.allColumns()) {
330330
from(orderLine, "ol")
331-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
331+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
332332
}
333333
}
334334
}
@@ -367,7 +367,7 @@ class ExistsTest {
367367
exists {
368368
select(orderLine.allColumns()) {
369369
from(orderLine, "ol")
370-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
370+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
371371
}
372372
}
373373
and { itemMaster.itemId isGreaterThan 2 }
@@ -405,7 +405,7 @@ class ExistsTest {
405405
exists {
406406
select(orderLine.allColumns()) {
407407
from(orderLine, "ol")
408-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
408+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
409409
}
410410
}
411411
}
@@ -450,7 +450,7 @@ class ExistsTest {
450450
exists {
451451
select(orderLine.allColumns()) {
452452
from(orderLine, "ol")
453-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
453+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
454454
}
455455
}
456456
and { itemMaster.itemId isGreaterThan 2 }
@@ -498,7 +498,7 @@ class ExistsTest {
498498
exists {
499499
select(orderLine.allColumns()) {
500500
from(orderLine, "ol")
501-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
501+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
502502
}
503503
}
504504
}
@@ -547,7 +547,7 @@ class ExistsTest {
547547
exists {
548548
select(orderLine.allColumns()) {
549549
from(orderLine, "ol")
550-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
550+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
551551
}
552552
}
553553
and { itemMaster.itemId isGreaterThan 2 }
@@ -594,7 +594,7 @@ class ExistsTest {
594594
exists {
595595
select(orderLine.allColumns()) {
596596
from(orderLine, "ol")
597-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
597+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
598598
}
599599
}
600600
or { itemMaster.itemId isEqualTo 22 }
@@ -639,7 +639,7 @@ class ExistsTest {
639639
exists {
640640
select(orderLine.allColumns()) {
641641
from(orderLine, "ol")
642-
where { orderLine.itemId isEqualTo (itemMaster.itemId qualifiedWith "im") }
642+
where { orderLine.itemId isEqualTo "im"(itemMaster.itemId) }
643643
}
644644
}
645645
and { itemMaster.itemId isEqualTo 22 }

src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapperTest.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import org.assertj.core.api.Assertions.entry
3333
import org.junit.jupiter.api.Test
3434
import org.mybatis.dynamic.sql.util.kotlin.KInvalidSQLException
3535
import org.mybatis.dynamic.sql.util.kotlin.elements.equalTo
36+
import org.mybatis.dynamic.sql.util.kotlin.elements.invoke
3637
import org.mybatis.dynamic.sql.util.kotlin.elements.max
37-
import org.mybatis.dynamic.sql.util.kotlin.elements.qualifiedWith
3838
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
3939
import java.io.InputStreamReader
4040
import java.sql.DriverManager
@@ -264,7 +264,7 @@ class JoinMapperTest {
264264
val mapper = session.getMapper(JoinMapper::class.java)
265265

266266
val selectStatement = select(
267-
orderLine.orderId qualifiedWith "ol", orderLine.quantity, itemMaster.itemId qualifiedWith "im",
267+
"ol"(orderLine.orderId), orderLine.quantity, "im"(itemMaster.itemId),
268268
itemMaster.description
269269
) {
270270
from {
@@ -281,8 +281,8 @@ class JoinMapperTest {
281281
+ "ol"
282282
},
283283
joinCriteria = {
284-
on(orderMaster.orderId qualifiedWith "om") equalTo
285-
(orderLine.orderId qualifiedWith "ol")
284+
on("om"(orderMaster.orderId)) equalTo
285+
"ol"(orderLine.orderId)
286286
}
287287
)
288288
fullJoin(
@@ -293,8 +293,8 @@ class JoinMapperTest {
293293
+ "im"
294294
}
295295
) {
296-
on(orderLine.itemId qualifiedWith "ol") equalTo
297-
(itemMaster.itemId qualifiedWith "im")
296+
on("ol"(orderLine.itemId)) equalTo
297+
"im"(itemMaster.itemId)
298298
}
299299
orderBy(orderLine.orderId, itemMaster.itemId)
300300
}
@@ -440,7 +440,7 @@ class JoinMapperTest {
440440
val mapper = session.getMapper(JoinMapper::class.java)
441441

442442
val selectStatement = select(
443-
orderLine.orderId, orderLine.quantity, itemMaster.itemId qualifiedWith "im",
443+
orderLine.orderId, orderLine.quantity, "im"(itemMaster.itemId),
444444
itemMaster.description
445445
) {
446446
from(orderMaster, "om")
@@ -455,7 +455,7 @@ class JoinMapperTest {
455455
+ "im"
456456
}
457457
) {
458-
on(orderLine.itemId) equalTo (itemMaster.itemId qualifiedWith "im")
458+
on(orderLine.itemId) equalTo "im"(itemMaster.itemId)
459459
}
460460
orderBy(orderLine.orderId, itemMaster.itemId)
461461
}
@@ -578,7 +578,7 @@ class JoinMapperTest {
578578

579579
val selectStatement = select(
580580
orderLine.orderId, orderLine.quantity,
581-
itemMaster.itemId qualifiedWith "im", itemMaster.description
581+
"im"(itemMaster.itemId), itemMaster.description
582582
) {
583583
from(orderMaster, "om")
584584
join(orderLine, "ol") {
@@ -592,7 +592,7 @@ class JoinMapperTest {
592592
+ "im"
593593
}
594594
) {
595-
on(orderLine.itemId) equalTo (itemMaster.itemId qualifiedWith "im")
595+
on(orderLine.itemId) equalTo "im"(itemMaster.itemId)
596596
}
597597
orderBy(orderLine.orderId, itemMaster.itemId)
598598
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.assertj.core.api.Assertions.assertThat
2222
import org.junit.jupiter.api.Test
2323
import org.mybatis.dynamic.sql.DerivedColumn
2424
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
25-
import org.mybatis.dynamic.sql.util.kotlin.elements.qualifiedWith
25+
import org.mybatis.dynamic.sql.util.kotlin.elements.invoke
2626
import org.mybatis.dynamic.sql.util.kotlin.spring.select
2727
import org.mybatis.dynamic.sql.util.kotlin.spring.selectList
2828
import org.springframework.beans.factory.annotation.Autowired
@@ -106,7 +106,7 @@ open class SpringKotlinSubQueryTest {
106106
@Test
107107
fun testBasicSubQueryWithAliases() {
108108
val rowNum = DerivedColumn.of<Int>("rownum()") `as` "myRows"
109-
val outerFirstName = firstName qualifiedWith "b"
109+
val outerFirstName = "b"(firstName)
110110
val personId = DerivedColumn.of<Int>("personId", "b")
111111

112112
val selectStatement =

0 commit comments

Comments
 (0)