Skip to content

Commit ac2e6dc

Browse files
committed
Add Kotlin Support for MappedColumns with inserts
1 parent 3897fba commit ac2e6dc

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.insert.BatchInsertDSL
2121
import org.mybatis.dynamic.sql.insert.BatchInsertModel
2222
import org.mybatis.dynamic.sql.util.AbstractColumnMapping
2323
import org.mybatis.dynamic.sql.util.Buildable
24+
import org.mybatis.dynamic.sql.util.MappedColumnMapping
2425

2526
typealias KotlinBatchInsertCompleter<T> = KotlinBatchInsertBuilder<T>.() -> Unit
2627

@@ -37,6 +38,10 @@ class KotlinBatchInsertBuilder<T : Any> (private val rows: Collection<T>): Build
3738
columnMappings.add(it)
3839
}
3940

41+
fun <C : Any> withMappedColumn(column: SqlColumn<C>) {
42+
columnMappings.add(MappedColumnMapping.of(column))
43+
}
44+
4045
override fun build(): BatchInsertModel<T> {
4146
assertNotNull(table, "ERROR.23") //$NON-NLS-1$
4247
return with(BatchInsertDSL.Builder<T>()) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import org.mybatis.dynamic.sql.insert.InsertDSL
2121
import org.mybatis.dynamic.sql.insert.InsertModel
2222
import org.mybatis.dynamic.sql.util.AbstractColumnMapping
2323
import org.mybatis.dynamic.sql.util.Buildable
24+
import org.mybatis.dynamic.sql.util.MappedColumnMapping
25+
import org.mybatis.dynamic.sql.util.MappedColumnWhenPresentMapping
2426

2527
typealias KotlinInsertCompleter<T> = KotlinInsertBuilder<T>.() -> Unit
2628

@@ -37,6 +39,14 @@ class KotlinInsertBuilder<T : Any> (private val row: T): Buildable<InsertModel<T
3739
columnMappings.add(it)
3840
}
3941

42+
fun <C : Any> withMappedColumn(column: SqlColumn<C>) {
43+
columnMappings.add(MappedColumnMapping.of(column))
44+
}
45+
46+
fun <C : Any> withMappedColumnWhenPresent(column: SqlColumn<C>, valueSupplier: () -> Any?) {
47+
columnMappings.add(MappedColumnWhenPresentMapping.of(column, valueSupplier))
48+
}
49+
4050
override fun build(): InsertModel<T> {
4151
assertNotNull(table, "ERROR.25") //$NON-NLS-1$
4252
return with(InsertDSL.Builder<T>()) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2121
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel
2222
import org.mybatis.dynamic.sql.util.AbstractColumnMapping
2323
import org.mybatis.dynamic.sql.util.Buildable
24+
import org.mybatis.dynamic.sql.util.MappedColumnMapping
2425

2526
typealias KotlinMultiRowInsertCompleter<T> = KotlinMultiRowInsertBuilder<T>.() -> Unit
2627

@@ -37,6 +38,10 @@ class KotlinMultiRowInsertBuilder<T : Any> (private val rows: Collection<T>): Bu
3738
columnMappings.add(it)
3839
}
3940

41+
fun <C : Any> withMappedColumn(column: SqlColumn<C>) {
42+
columnMappings.add(MappedColumnMapping.of(column))
43+
}
44+
4045
override fun build(): MultiRowInsertModel<T> {
4146
assertNotNull(table, "ERROR.26") //$NON-NLS-1$
4247
return with(MultiRowInsertDSL.Builder<T>()) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ fun <T : Any> SqlTable.column(
3434
typeHandler: String? = null,
3535
renderingStrategy: RenderingStrategy? = null,
3636
parameterTypeConverter: ((T?) -> Any?) = { it },
37-
javaType: KClass<T>? = null
37+
javaType: KClass<T>? = null,
38+
javaProperty: String? = null,
3839
): SqlColumn<T> = SqlColumn.Builder<T>().run {
3940
withTable(this@column)
4041
withName(name)
@@ -43,5 +44,6 @@ fun <T : Any> SqlTable.column(
4344
withRenderingStrategy(renderingStrategy)
4445
withParameterTypeConverter(parameterTypeConverter)
4546
withJavaType(javaType?.java)
47+
withJavaProperty(javaProperty)
4648
build()
4749
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ object PersonDynamicSqlSupport {
3131
val addressId = person.addressId
3232

3333
class Person : SqlTable("Person") {
34-
val id = column<Int>(name = "id", jdbcType = JDBCType.INTEGER)
35-
val firstName = column<String>(name = "first_name", jdbcType = JDBCType.VARCHAR)
34+
val id = column<Int>(name = "id", jdbcType = JDBCType.INTEGER, javaProperty = "id")
35+
val firstName = column<String>(name = "first_name", jdbcType = JDBCType.VARCHAR, javaProperty = "firstName")
3636
val lastName = column<LastName>(
3737
name = "last_name",
3838
jdbcType = JDBCType.VARCHAR,
39-
typeHandler = "examples.kotlin.mybatis3.canonical.LastNameTypeHandler"
39+
typeHandler = "examples.kotlin.mybatis3.canonical.LastNameTypeHandler",
40+
javaProperty = "lastName"
4041
)
41-
val birthDate = column<Date>(name = "birth_date", jdbcType = JDBCType.DATE)
42+
val birthDate = column<Date>(name = "birth_date", jdbcType = JDBCType.DATE, javaProperty = "birthDate")
4243
val employed = column<Boolean>(
4344
name = "employed",
4445
JDBCType.VARCHAR,
45-
typeHandler = "examples.kotlin.mybatis3.canonical.YesNoTypeHandler"
46+
typeHandler = "examples.kotlin.mybatis3.canonical.YesNoTypeHandler",
47+
javaProperty = "employed"
4648
)
47-
val occupation = column<String>(name = "occupation", jdbcType = JDBCType.VARCHAR)
48-
val addressId = column<Int>(name = "address_id", jdbcType = JDBCType.INTEGER)
49+
val occupation = column<String>(name = "occupation", jdbcType = JDBCType.VARCHAR, javaProperty = "occupation")
50+
val addressId = column<Int>(name = "address_id", jdbcType = JDBCType.INTEGER, javaProperty = "addressId")
4951
}
5052
}

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ fun PersonMapper.deleteByPrimaryKey(id_: Int) =
6565

6666
fun PersonMapper.insert(record: PersonRecord) =
6767
insert(this::insert, record, person) {
68-
map(id) toProperty "id"
69-
map(firstName) toProperty "firstName"
70-
map(lastName) toProperty "lastName"
71-
map(birthDate) toProperty "birthDate"
72-
map(employed) toProperty "employed"
73-
map(occupation) toProperty "occupation"
74-
map(addressId) toProperty "addressId"
68+
withMappedColumn(id)
69+
withMappedColumn(firstName)
70+
withMappedColumn(lastName)
71+
withMappedColumn(birthDate)
72+
withMappedColumn(employed)
73+
withMappedColumn(occupation)
74+
withMappedColumn(addressId)
7575
}
7676

7777
fun PersonMapper.generalInsert(completer: GeneralInsertCompleter) =
@@ -85,38 +85,38 @@ fun PersonMapper.insertBatch(vararg records: PersonRecord): List<Int> =
8585

8686
fun PersonMapper.insertBatch(records: Collection<PersonRecord>): List<Int> =
8787
insertBatch(this::insert, records, person) {
88-
map(id) toProperty "id"
89-
map(firstName) toProperty "firstName"
90-
map(lastName) toProperty "lastName"
91-
map(birthDate) toProperty "birthDate"
92-
map(employed) toProperty "employed"
93-
map(occupation) toProperty "occupation"
94-
map(addressId) toProperty "addressId"
88+
withMappedColumn(id)
89+
withMappedColumn(firstName)
90+
withMappedColumn(lastName)
91+
withMappedColumn(birthDate)
92+
withMappedColumn(employed)
93+
withMappedColumn(occupation)
94+
withMappedColumn(addressId)
9595
}
9696

9797
fun PersonMapper.insertMultiple(vararg records: PersonRecord) =
9898
insertMultiple(records.toList())
9999

100100
fun PersonMapper.insertMultiple(records: Collection<PersonRecord>) =
101101
insertMultiple(this::insertMultiple, records, person) {
102-
map(id) toProperty "id"
103-
map(firstName) toProperty "firstName"
104-
map(lastName) toProperty "lastName"
105-
map(birthDate) toProperty "birthDate"
106-
map(employed) toProperty "employed"
107-
map(occupation) toProperty "occupation"
108-
map(addressId) toProperty "addressId"
102+
withMappedColumn(id)
103+
withMappedColumn(firstName)
104+
withMappedColumn(lastName)
105+
withMappedColumn(birthDate)
106+
withMappedColumn(employed)
107+
withMappedColumn(occupation)
108+
withMappedColumn(addressId)
109109
}
110110

111111
fun PersonMapper.insertSelective(record: PersonRecord) =
112112
insert(this::insert, record, person) {
113-
map(id).toPropertyWhenPresent("id", record::id)
114-
map(firstName).toPropertyWhenPresent("firstName", record::firstName)
115-
map(lastName).toPropertyWhenPresent("lastName", record::lastName)
116-
map(birthDate).toPropertyWhenPresent("birthDate", record::birthDate)
117-
map(employed).toPropertyWhenPresent("employed", record::employed)
118-
map(occupation).toPropertyWhenPresent("occupation", record::occupation)
119-
map(addressId).toPropertyWhenPresent("addressId", record::addressId)
113+
withMappedColumnWhenPresent(id, record::id)
114+
withMappedColumnWhenPresent(firstName, record::firstName)
115+
withMappedColumnWhenPresent(lastName, record::lastName)
116+
withMappedColumnWhenPresent(birthDate, record::birthDate)
117+
withMappedColumnWhenPresent(employed, record::employed)
118+
withMappedColumnWhenPresent(occupation, record::occupation)
119+
withMappedColumnWhenPresent(addressId, record::addressId)
120120
}
121121

122122
private val columnList = listOf(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId)

0 commit comments

Comments
 (0)