Skip to content

Commit 6ee6b13

Browse files
committed
Add a few infix functions for a more natural feel
1 parent 007ab15 commit 6ee6b13

File tree

11 files changed

+133
-97
lines changed

11 files changed

+133
-97
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2016-2022 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.elements
17+
18+
import org.mybatis.dynamic.sql.DerivedColumn
19+
import org.mybatis.dynamic.sql.SqlColumn
20+
21+
infix fun <T> DerivedColumn<T>.`as`(alias: String): DerivedColumn<T> = this.`as`(alias)
22+
23+
infix fun <T> SqlColumn<T>.`as`(alias: String): SqlColumn<T> = this.`as`(alias)
24+
25+
infix fun <T> SqlColumn<T>.qualifiedWith(tableQualifier: String): SqlColumn<T> = this.qualifiedWith(tableQualifier)

src/site/markdown/docs/kotlinMyBatis3.md

+39-38
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object PersonDynamicSqlSupport {
6363
val birthDate = column<Date>(name = "birth_date", jdbcType = JDBCType.DATE)
6464
val employed = column(
6565
name = "employed",
66-
jdbcType = JDBCType.VARCHAR
66+
jdbcType = JDBCType.VARCHAR,
6767
typeHandler = "foo.bar.StringToBooleanTypeHandler"
6868
)
6969
val occupation = column<String>(name = "occupation", jdbcType = JDBCType.VARCHAR)
@@ -143,8 +143,9 @@ Then extensions could be added to make a shortcut method as follows:
143143
```kotlin
144144
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
145145
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.selectList
146+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
146147

147-
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
148+
private val columnList = listOf(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId)
148149

149150
fun PersonMapper.select(completer: SelectCompleter) =
150151
selectList(this::selectMany, columnList, Person, completer)
@@ -158,7 +159,7 @@ where clause and an order by clause as follows:
158159
val rows = mapper.select {
159160
where { id isLessThan 100 }
160161
or {
161-
employed.isTrue())
162+
employed.isTrue()
162163
and { occupation isEqualTo "Developer" }
163164
}
164165
orderBy(id)
@@ -193,7 +194,7 @@ interface PersonMapper : CommonCountMapper
193194
The mapper method can be used as follows:
194195

195196
```kotlin
196-
val countStatement = count(...) // not shown... see the overview page for examples
197+
val countStatement = count() // not shown... see the overview page for examples
197198
val mapper: PersonMapper = getMapper() // not shown
198199
val rows: Long = mapper.count(countStatement)
199200
```
@@ -267,7 +268,7 @@ interface PersonMapper : CommonDeleteMapper
267268
The mapper method can be used as follows:
268269

269270
```kotlin
270-
val deleteStatement = deleteFrom(...) // not shown... see the overview page for examples
271+
val deleteStatement = deleteFrom() // not shown... see the overview page for examples
271272
val mapper: PersonMapper = getMapper() // not shown
272273
val rows: Int = mapper.delete(deleteStatement)
273274
```
@@ -332,7 +333,7 @@ interface PersonMapper : CommonInsertMapper<T>
332333
The mapper method can be used as follows:
333334

334335
```kotlin
335-
val insertStatement = insert(...) // not shown, see overview page
336+
val insertStatement = insert() // not shown, see overview page
336337
val mapper: PersonMapper = getMapper() // not shown
337338
val rows: Int = mapper.insert(insertStatement)
338339
```
@@ -411,7 +412,7 @@ interface PersonMapper : CommonInsertMapper<T>
411412
The mapper method can be used as follows:
412413

413414
```kotlin
414-
val insertStatement = insertInto(...) // not shown, see overview page
415+
val insertStatement = insertInto() // not shown, see overview page
415416
val mapper: PersonMapper = getMapper() // not shown
416417
val rows: Int = mapper.generalInsert(insertStatement)
417418
```
@@ -512,7 +513,7 @@ interface PersonMapper : CommonInsertMapper<T>
512513
The mapper method can be used as follows:
513514

514515
```kotlin
515-
val insertStatement = insertMultiple(...) // not shown, see overview page
516+
val insertStatement = insertMultiple() // not shown, see overview page
516517
val mapper: PersonMapper = getMapper() // not shown
517518
val rows: Int = mapper.insertMultiple(insertStatement)
518519
```
@@ -600,7 +601,6 @@ batch such as update counts. The methods are coded as follows:
600601
import org.apache.ibatis.annotations.Flush
601602
import org.apache.ibatis.annotations.InsertProvider
602603
import org.apache.ibatis.executor.BatchResult
603-
...
604604

605605
@Mapper
606606
interface PersonMapper {
@@ -649,9 +649,9 @@ val sqlSessionFactory: SqlSessionFactory = getSessionFactory() // not shown
649649
val sqlSession: SqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)
650650
val mapper: PersonMapper = sqlSession.getMapper(PersonMapper::class.java)
651651

652-
val batchInsert = insertBatch(...) // not shown, see overview page
652+
val batchInsert = insertBatch() // not shown, see overview page
653653
batchInsert.execute(mapper) // see note below about return value
654-
val batchResults: mapper.flush()
654+
val batchResults = mapper.flush()
655655
```
656656

657657
Note the use of the extension function `BatchInsert.execute(mapper)`. This function simply loops over all
@@ -700,7 +700,7 @@ val sqlSessionFactory: SqlSessionFactory = getSessionFactory() // not shown
700700
val sqlSession: SqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)
701701
val mapper: PersonMapper = sqlSession.getMapper(PersonMapper::class.java)
702702
mapper.insertBatch(record1, record2)
703-
val batchResults: mapper.flush()
703+
val batchResults = mapper.flush()
704704
```
705705

706706
### Generated Key Support
@@ -748,7 +748,7 @@ interface PersonMapper : CommonInsertMapper<T>
748748
The mapper method can be used as follows:
749749

750750
```kotlin
751-
val insertStatement = insertSelect(...) // not shown, see overview page
751+
val insertStatement = insertSelect() // not shown, see overview page
752752
val mapper: PersonMapper = getMapper() // not shown
753753
val rows: Int = mapper.insertSelect(insertStatement)
754754
```
@@ -825,10 +825,10 @@ The methods can be used as follows:
825825
```kotlin
826826
val mapper: PersonMapper = getMapper() // not shown
827827

828-
val selectStatement = select(...) // not shown... see the overview page for examples
828+
val selectStatement = select() // not shown... see the overview page for examples
829829
val rows: List<PersonRecord> = mapper.selectMany(selectStatement)
830830

831-
val selectOneStatement = select(...) // not shown... see the overview page for examples
831+
val selectOneStatement = select() // not shown... see the overview page for examples
832832
val row: PersonRecord? = mapper.selectOne(selectStatement)
833833
```
834834

@@ -843,11 +843,12 @@ and selecting a single record:
843843

844844
```kotlin
845845
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
846+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
846847
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.selectDistinct
847848
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.selectList
848849
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.selectOne
849850

850-
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
851+
private val columnList = listOf(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId)
851852

852853
fun PersonMapper.selectOne(completer: SelectCompleter) =
853854
selectOne(this::selectOne, columnList, Person, completer)
@@ -917,7 +918,7 @@ and other parts of a select statement. For example, you could code a mapper exte
917918
```kotlin
918919
fun PersonWithAddressMapper.select(completer: SelectCompleter): List<PersonWithAddress> =
919920
select(
920-
id.`as`("A_ID"), firstName, lastName, birthDate,
921+
id `as` "A_ID", firstName, lastName, birthDate,
921922
employed, occupation, address.id, address.streetAddress, address.city, address.state
922923
) {
923924
from(person, "p")
@@ -967,7 +968,7 @@ interface PersonMapper : CommonUpdateMapper
967968
The mapper method can be used as follows:
968969

969970
```kotlin
970-
val updateStatement = update(...) // not shown... see the overview page for examples
971+
val updateStatement = update() // not shown... see the overview page for examples
971972
val mapper: PersonMapper = getMapper() // not shown
972973
val rows: Int = mapper.update(updateStatement)
973974
```
@@ -1005,7 +1006,7 @@ If you wish to update all rows in a table, you can simply omit the where clause
10051006
```kotlin
10061007
// update all rows...
10071008
val rows = mapper.update {
1008-
set(occupation).equalTo("Programmer")
1009+
set(occupation) equalTo "Programmer"
10091010
}
10101011
```
10111012

@@ -1014,13 +1015,13 @@ It is also possible to write utility methods that will set values. For example:
10141015
```kotlin
10151016
fun KotlinUpdateBuilder.updateSelectiveColumns(record: PersonRecord) =
10161017
apply {
1017-
set(id).equalToWhenPresent(record::id)
1018-
set(firstName).equalToWhenPresent(record::firstName)
1019-
set(lastName).equalToWhenPresent(record::lastName)
1020-
set(birthDate).equalToWhenPresent(record::birthDate)
1021-
set(employed).equalToWhenPresent(record::employed)
1022-
set(occupation).equalToWhenPresent(record::occupation)
1023-
set(addressId).equalToWhenPresent(record::addressId)
1018+
set(id) equalToWhenPresent record::id
1019+
set(firstName) equalToWhenPresent record::firstName
1020+
set(lastName) equalToWhenPresent record::lastName
1021+
set(birthDate) equalToWhenPresent record::birthDate
1022+
set(employed) equalToWhenPresent record::employed
1023+
set(occupation) equalToWhenPresent record::occupation
1024+
set(addressId) equalToWhenPresent record::addressId
10241025
}
10251026
```
10261027

@@ -1039,23 +1040,23 @@ If you wish to implement an "update by primary key" method, you can reuse the ex
10391040
```kotlin
10401041
fun PersonMapper.updateByPrimaryKey(record: PersonRecord) =
10411042
update {
1042-
set(firstName).equalToOrNull(record::firstName)
1043-
set(lastName).equalToOrNull(record::lastName)
1044-
set(birthDate).equalToOrNull(record::birthDate)
1045-
set(employed).equalToOrNull(record::employed)
1046-
set(occupation).equalToOrNull(record::occupation)
1047-
set(addressId).equalToOrNull(record::addressId)
1043+
set(firstName) equalToOrNull record::firstName
1044+
set(lastName) equalToOrNull record::lastName
1045+
set(birthDate) equalToOrNull record::birthDate
1046+
set(employed) equalToOrNull record::employed
1047+
set(occupation) equalToOrNull record::occupation
1048+
set(addressId) equalToOrNull record::addressId
10481049
where { id isEqualTo record.id!! }
10491050
}
10501051

10511052
fun PersonMapper.updateByPrimaryKeySelective(record: PersonRecord) =
10521053
update {
1053-
set(firstName).equalToWhenPresent(record::firstName)
1054-
set(lastName).equalToWhenPresent(record::lastName)
1055-
set(birthDate).equalToWhenPresent(record::birthDate)
1056-
set(employed).equalToWhenPresent(record::employed)
1057-
set(occupation).equalToWhenPresent(record::occupation)
1058-
set(addressId).equalToWhenPresent(record::addressId)
1054+
set(firstName) equalToWhenPresent record::firstName
1055+
set(lastName) equalToWhenPresent record::lastName
1056+
set(birthDate) equalToWhenPresent record::birthDate
1057+
set(employed) equalToWhenPresent record::employed
1058+
set(occupation) equalToWhenPresent record::occupation
1059+
set(addressId) equalToWhenPresent record::addressId
10591060
where { id isEqualTo record.id!! }
10601061
}
10611062
```

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperExtensions.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.mybatis.dynamic.sql.util.kotlin.InsertSelectCompleter
3131
import org.mybatis.dynamic.sql.util.kotlin.KotlinUpdateBuilder
3232
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
3333
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
34+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
3435
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.count
3536
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countDistinct
3637
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom
@@ -118,7 +119,7 @@ fun PersonMapper.insertSelective(record: PersonRecord) =
118119
map(addressId).toPropertyWhenPresent("addressId", record::addressId)
119120
}
120121

121-
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
122+
private val columnList = listOf(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId)
122123

123124
fun PersonMapper.selectOne(completer: SelectCompleter) =
124125
selectOne(this::selectOne, columnList, person, completer)

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonWithAddressMapperExtensions.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id
2424
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.lastName
2525
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.occupation
2626
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
27+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
2728
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
2829
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.selectDistinct
2930

30-
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate,
31+
private val columnList = listOf(id `as` "A_ID", firstName, lastName, birthDate,
3132
employed, occupation, address.id, address.streetAddress, address.city, address.state, address.addressType
3233
)
3334

src/test/kotlin/examples/kotlin/mybatis3/custom/render/KCustomRenderingTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.junit.jupiter.api.BeforeEach
3030
import org.junit.jupiter.api.Test
3131
import org.junit.jupiter.api.TestInstance
3232
import org.mybatis.dynamic.sql.SqlColumn
33+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
3334
import org.mybatis.dynamic.sql.util.kotlin.elements.insert
3435
import org.mybatis.dynamic.sql.util.kotlin.elements.insertMultiple
3536
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.deleteFrom
@@ -297,7 +298,7 @@ class KCustomRenderingTest {
297298
}
298299
val rows = mapper.insertMultiple(insertStatement)
299300
assertThat(rows).isEqualTo(2)
300-
val selectStatement = select(dereference(info, "firstName").`as`("firstname")) {
301+
val selectStatement = select(dereference(info, "firstName") `as` "firstname") {
301302
from(jsonTest)
302303
where { dereference(info, "age") isEqualTo "25" }
303304
}

src/test/kotlin/examples/kotlin/mybatis3/general/GeneralKotlinTest.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
4141
import org.assertj.core.api.Assertions.assertThat
4242
import org.assertj.core.api.Assertions.assertThatExceptionOfType
4343
import org.junit.jupiter.api.Test
44+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
4445
import org.mybatis.dynamic.sql.util.kotlin.elements.count
4546
import org.mybatis.dynamic.sql.util.kotlin.elements.insert
4647
import org.mybatis.dynamic.sql.util.kotlin.elements.insertMultiple
@@ -379,7 +380,7 @@ class GeneralKotlinTest {
379380
val mapper = session.getMapper(PersonMapper::class.java)
380381

381382
val selectStatement = selectDistinct(
382-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
383+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
383384
addressId
384385
) {
385386
from(person)
@@ -413,7 +414,7 @@ class GeneralKotlinTest {
413414
val mapper = session.getMapper(PersonMapper::class.java)
414415

415416
val selectStatement = select(
416-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
417+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
417418
addressId
418419
) {
419420
from(person)
@@ -447,7 +448,7 @@ class GeneralKotlinTest {
447448
val mapper = session.getMapper(PersonWithAddressMapper::class.java)
448449

449450
val selectStatement = select(
450-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
451+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
451452
address.id, address.streetAddress, address.city, address.state
452453
) {
453454
from(person)
@@ -483,7 +484,7 @@ class GeneralKotlinTest {
483484
val mapper = session.getMapper(PersonWithAddressMapper::class.java)
484485

485486
val selectStatement = select(
486-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
487+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
487488
address.id, address.streetAddress, address.city, address.state
488489
) {
489490
from(person)
@@ -524,7 +525,7 @@ class GeneralKotlinTest {
524525
val mapper = session.getMapper(PersonWithAddressMapper::class.java)
525526

526527
val selectStatement = select(
527-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
528+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
528529
address.id, address.streetAddress, address.city, address.state
529530
) {
530531
from(person)
@@ -567,7 +568,7 @@ class GeneralKotlinTest {
567568
val mapper = session.getMapper(PersonMapper::class.java)
568569

569570
val selectStatement = select(
570-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
571+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
571572
addressId
572573
) {
573574
from(person)
@@ -613,7 +614,7 @@ class GeneralKotlinTest {
613614
val mapper = session.getMapper(PersonMapper::class.java)
614615

615616
val selectStatement = select(
616-
id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
617+
id `as` "A_ID", firstName, lastName, birthDate, employed, occupation,
617618
addressId
618619
) {
619620
from(person)
@@ -656,7 +657,7 @@ class GeneralKotlinTest {
656657
@Test
657658
fun testRawSelectWithoutFrom() {
658659
assertThatExceptionOfType(UninitializedPropertyAccessException::class.java).isThrownBy {
659-
select(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId) {
660+
select(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId) {
660661
where { id isEqualTo 5 }
661662
or {
662663
id isEqualTo 4

0 commit comments

Comments
 (0)