To use the library, you must define a metamodel using AlisableSqlTable. This involves creating an object that holds column definitions and a nested class representing the table structure.
Using Parameter Type Converters
You can use parameterTypeConverter on columns to map Kotlin types to database-specific formats. For example, mapping a Kotlin Boolean to a database string like "Yes" or "No".
Important: Type converters are used in general inserts, updates, and where clauses, but not in single-row insert statements that map fields directly to properties in a data class. For those, you must provide a property in your data class that performs the conversion.
import org.mybatis.dynamic.sql.AlisableSqlTable
import org.mybatis.dynamic.sql.util.kotlin.elements.column
import java.util.Date
object PersonDynamicSqlSupport {
val person = Person()
val id = person.id
val firstName = person.firstName
val lastName = person.lastName
val birthDate = person.birthDate
val employed = person.employed
val occupation = person.occupation
val addressId = person.addressId
class Person : AlisableSqlTable<Person>("Person", ::Person) {
val id = column<Int>(name = "id")
val firstName = column<String>(name = "first_name")
val lastName = column(
name = "last_name",
parameterTypeConverter = lastNameConverter
)
val birthDate = column<Date>(name = "birth_date")
val employed = column(
name = "employed",
parameterTypeConverter = booleanToStringConverter
)
val occupation = column<String>(name = "occupation")
val addressId = column<Int>(name = "address_id")
}
}
// Example converter
val booleanToStringConverter: (Boolean?) -> String = { it?.let { if (it) "Yes" else "No" } ?: "No" }
// Data class for single-row inserts
data class PersonRecord(
var id: Int? = null,
var firstName: String? = null,
var lastName: String? = null,
var birthDate: Date? = null,
var employed: Boolean? = null,
var occupation: String? = null,
var addressId: Int? = null
) {
val employedAsString: String
get() = booleanToStringConverter(employed)
}