Skip to content

Commit 7403779

Browse files
committed
Kotlin Documentaion Updates
1 parent e6c892b commit 7403779

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

src/site/markdown/docs/kotlin.md

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ And then extensions could be added to make a shortcut method as follows:
5757
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
5858

5959
fun PersonMapper.select(completer: SelectCompleter) =
60-
selectList(this::selectMany, columnList, Person, completer)
60+
selectList(this::selectMany, columnList, Person, completer)
6161
```
6262

6363
The extension method shows the use of the `SelectCompleter` type alias. This is a DSL extension supplied with the library. We will detail its use below. For now see that the extension method can be used in client code as follows:
@@ -94,10 +94,10 @@ This is a standard method for MyBatis Dynamic SQL that executes a query and retu
9494

9595
```kotlin
9696
fun PersonMapper.count(completer: CountCompleter) =
97-
MyBatis3Utils.count(this::count, Person, completer)
97+
count(this::count, Person, completer)
9898
```
9999

100-
This method shows the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. Clients can use the method as follows:
100+
This method shows the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `count` method which is supplied by the library. That method will build and execute the select count statement with the supplied where clause. Clients can use the method as follows:
101101

102102
```kotlin
103103
val rows = mapper.count {
@@ -131,10 +131,10 @@ This is a standard method for MyBatis Dynamic SQL that executes a delete and ret
131131

132132
```kotlin
133133
fun PersonMapper.delete(completer: DeleteCompleter) =
134-
MyBatis3Utils.deleteFrom(this::delete, Person, completer)
134+
deleteFrom(this::delete, Person, completer)
135135
```
136136

137-
This method shows the use of `DeleteCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. Clients can use the method as follows:
137+
This method shows the use of `DeleteCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `deleteFrom` method which is supplied by the library. That method will build and execute the delete statement with the supplied where clause. Clients can use the method as follows:
138138

139139
```kotlin
140140
val rows = mapper.delete {
@@ -169,43 +169,43 @@ These methods can be used to implement simplified insert methods with Kotlin ext
169169

170170
```kotlin
171171
fun PersonMapper.insert(record: PersonRecord) =
172-
insert(this::insert, record, Person) {
173-
map(id).toProperty("id")
174-
map(firstName).toProperty("firstName")
175-
map(lastName).toProperty("lastName")
176-
map(birthDate).toProperty("birthDate")
177-
map(employed).toProperty("employed")
178-
map(occupation).toProperty("occupation")
179-
map(addressId).toProperty("addressId")
180-
}
172+
insert(this::insert, record, Person) {
173+
map(id).toProperty("id")
174+
map(firstName).toProperty("firstName")
175+
map(lastName).toProperty("lastName")
176+
map(birthDate).toProperty("birthDate")
177+
map(employed).toProperty("employed")
178+
map(occupation).toProperty("occupation")
179+
map(addressId).toProperty("addressId")
180+
}
181181

182182
fun PersonMapper.insertMultiple(vararg records: PersonRecord) =
183-
insertMultiple(records.toList())
183+
insertMultiple(records.toList())
184184

185185
fun PersonMapper.insertMultiple(records: Collection<PersonRecord>) =
186-
insertMultiple(this::insertMultiple, records, Person) {
187-
map(id).toProperty("id")
188-
map(firstName).toProperty("firstName")
189-
map(lastName).toProperty("lastName")
190-
map(birthDate).toProperty("birthDate")
191-
map(employed).toProperty("employed")
192-
map(occupation).toProperty("occupation")
193-
map(addressId).toProperty("addressId")
194-
}
186+
insertMultiple(this::insertMultiple, records, Person) {
187+
map(id).toProperty("id")
188+
map(firstName).toProperty("firstName")
189+
map(lastName).toProperty("lastName")
190+
map(birthDate).toProperty("birthDate")
191+
map(employed).toProperty("employed")
192+
map(occupation).toProperty("occupation")
193+
map(addressId).toProperty("addressId")
194+
}
195195

196196
fun PersonMapper.insertSelective(record: PersonRecord) =
197-
insert(this::insert, record, Person) {
198-
map(id).toPropertyWhenPresent("id", record::id)
199-
map(firstName).toPropertyWhenPresent("firstName", record::firstName)
200-
map(lastName).toPropertyWhenPresent("lastName", record::lastName)
201-
map(birthDate).toPropertyWhenPresent("birthDate", record::birthDate)
202-
map(employed).toPropertyWhenPresent("employed", record::employed)
203-
map(occupation).toPropertyWhenPresent("occupation", record::occupation)
204-
map(addressId).toPropertyWhenPresent("addressId", record::addressId)
205-
}
197+
insert(this::insert, record, Person) {
198+
map(id).toPropertyWhenPresent("id", record::id)
199+
map(firstName).toPropertyWhenPresent("firstName", record::firstName)
200+
map(lastName).toPropertyWhenPresent("lastName", record::lastName)
201+
map(birthDate).toPropertyWhenPresent("birthDate", record::birthDate)
202+
map(employed).toPropertyWhenPresent("employed", record::employed)
203+
map(occupation).toPropertyWhenPresent("occupation", record::occupation)
204+
map(addressId).toPropertyWhenPresent("addressId", record::addressId)
205+
}
206206
```
207207

208-
Note these methods use Kotlin utility methods named `insert` and `insertMultiple`. Both methods accept a function with a receiver that will allow column mappings.
208+
Note these methods use Kotlin utility methods named `insert` and `insertMultiple`. Both methods accept a function with a receiver that will allow column mappings. The methods will build and execute insert statements.= with the supplied column mappings.
209209

210210
Clients use these methods as follows:
211211

@@ -254,24 +254,24 @@ These methods can be used to create simplified select methods with Kotlin extens
254254
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
255255

256256
fun PersonMapper.selectOne(completer: SelectCompleter) =
257-
MyBatis3Utils.selectOne(this::selectOne, columnList, Person, completer)
257+
selectOne(this::selectOne, columnList, Person, completer)
258258

259259
fun PersonMapper.select(completer: SelectCompleter) =
260-
selectList(this::selectMany, columnList, Person, completer)
260+
selectList(this::selectMany, columnList, Person, completer)
261261

262262
fun PersonMapper.selectDistinct(completer: SelectCompleter) =
263-
selectDistinct(this::selectMany, columnList, Person, completer)
263+
selectDistinct(this::selectMany, columnList, Person, completer)
264264
```
265265

266-
These methods show the use of `SelectCompleter` which is a which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. The `selectMany` method can be used to implement generalized select methods where a user can specify a where clause and/or an order by clause. Typically we recommend two of these methods - for select, and select distinct. The `selectOne` method is used to create a generalized select method where a user can specify a where clause. These methods also show the use of the built in Kotlin functions `selectDistinct` and `selectList`. These functions help to avoid platform type issues in Kotlin and enable the Kotlin compiler to correctly infer the result type (`List<PersonRecord>` in this case).
266+
These methods show the use of `SelectCompleter` which is a which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. The `selectMany` method can be used to implement generalized select methods where a user can specify a where clause and/or an order by clause. Typically we recommend two of these methods - for select, and select distinct. The `selectOne` method is used to create a generalized select method where a user can specify a where clause. These methods also show the use of the built in Kotlin functions `selectDistinct`, `selectList`, and `selectOne`. These functions build and execute select statements, and help to avoid platform type issues in Kotlin. They enable the Kotlin compiler to correctly infer the result type (either `PersonRecord?` or `List<PersonRecord>` in this case).
267267

268268
The general `selectOne` method can also be used to implement a `selectByPrimaryKey` method:
269269

270270
```kotlin
271271
fun PersonMapper.selectByPrimaryKey(id_: Int) =
272-
selectOne {
273-
where(id, isEqualTo(id_))
274-
}
272+
selectOne {
273+
where(id, isEqualTo(id_))
274+
}
275275
```
276276

277277
Clients can use the methods as follows:
@@ -317,10 +317,10 @@ This is a standard method for MyBatis Dynamic SQL that executes an update and re
317317

318318
```kotlin
319319
fun PersonMapper.update(completer: UpdateCompleter) =
320-
MyBatis3Utils.update(this::update, Person, completer)
320+
update(this::update, Person, completer)
321321
```
322322

323-
This extension method shows the use of `UpdateCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply values and a where clause. Clients can use the method as follows:
323+
This extension method shows the use of `UpdateCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply values and a where clause. This also shows use of the Kotlin `update` method which is supplied by the library. That method will build and execute the update statement with the supplied values and where clause. Clients can use the method as follows:
324324

325325
```kotlin
326326
val rows = mapper.update {
@@ -341,15 +341,15 @@ It is also possible to write a utility method that will set values. For example:
341341

342342
```kotlin
343343
fun UpdateDSL<UpdateModel>.updateSelectiveColumns(record: PersonRecord) =
344-
apply {
345-
set(id).equalToWhenPresent(record::id)
346-
set(firstName).equalToWhenPresent(record::firstName)
347-
set(lastName).equalToWhenPresent(record::lastName)
348-
set(birthDate).equalToWhenPresent(record::birthDate)
349-
set(employed).equalToWhenPresent(record::employed)
350-
set(occupation).equalToWhenPresent(record::occupation)
351-
set(addressId).equalToWhenPresent(record::addressId)
352-
}
344+
apply {
345+
set(id).equalToWhenPresent(record::id)
346+
set(firstName).equalToWhenPresent(record::firstName)
347+
set(lastName).equalToWhenPresent(record::lastName)
348+
set(birthDate).equalToWhenPresent(record::birthDate)
349+
set(employed).equalToWhenPresent(record::employed)
350+
set(occupation).equalToWhenPresent(record::occupation)
351+
set(addressId).equalToWhenPresent(record::addressId)
352+
}
353353
```
354354

355355
This method will selectively set values if corresponding fields in a record are non null. This method can be used as follows:
@@ -360,3 +360,26 @@ val rows = mapper.update {
360360
where(id, isEqualTo(100))
361361
}
362362
```
363+
## Join Support
364+
365+
There are extension functions that support building a reusable select method based on a join. In this way, you can create the start of the select statement (the column list and join specifications) and allow the user to supply where clauses and other parts of a select statement. For example, you could code a mapper extension method like this:
366+
367+
```kotlin
368+
fun PersonWithAddressMapper.select(completer: SelectCompleter): List<PersonWithAddress> {
369+
val start = select(columnList).from(Person, "p") {
370+
join(Address, "a") {
371+
on(Person.addressId, equalTo(Address.id))
372+
}
373+
}
374+
return selectList(this::selectMany, start, completer)
375+
}
376+
```
377+
378+
This method creates the start of a select statement with a join, and accepts user input to complete the statement. This shows use of and overloaded `selectList` method that accepts the start of a select statement and a completer. Like other select methods, this method can be used as follows:
379+
380+
```kotlin
381+
val records = mapper.select {
382+
where(id, isLessThan(100))
383+
limit(5)
384+
}
385+
```

0 commit comments

Comments
 (0)