This library is a simple enhancement on top of sfm-jooq
to provide additional kotlin support. Currently, support has
only been added for data classes. This repo is brand new and will be constantly evolving, so I am not providing any
backwards compatibility guarantees between releases for the time being.
newMapper
extension function forSelectQueryMapperFactory
which accepts aKClass<*>
and instantiates custom handlers for the construction of the class by the mapper if the class is a data class. If the passed inKClass
is not a data class, we revert to the default behavior- Custom
BiInstantiator
,InstantiatorFactory
,ReflectionService
, andInstantiatorDefinition
to support the functionality
Note: For general usage of the SelectQueryMapper
, see
the SimpleFlatMapper docs
First, add the dependency to your project from maven central or jitpack
build.gradle
repositories {
mavenCentral()
maven { url 'https://jitpack.io' } // only required if pulling from jitpack
}
dependencies {
implementation 'dev.aga:sfm-jooq-kotlin:0.0.2'
}
build.gradle.kts
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") } // only required if pulling from jitpack
}
dependencies {
implementation("dev.aga:sfm-jooq-kotlin:0.0.2")
}
// note: each property of the class must have a default value
// OR you must be sure to always include values for the
// properties without default values in your queries
data class Person(
val id: Long? = null,
val firstName: String? = null,
val lastName: String? = null,
val address: Address? = null,
)
data class Address(
val id: Long? = null,
val streetName: String? = null,
val streetNumber: String? = null,
val zipCode: String? = null,
val city: String? = null,
val state: String? = null,
)
import dev.aga.sfm.newMapper
// other imports
class MyService(private val dsl: DSLContext) {
private val mapper =
SelectQueryMapperFactory
.newInstance()
.ignorePropertyNotFound()
.newMapper(Person::class) // note this takes a KClass
fun getPersons(): List<Person> {
val ppl = Persons.PERSONS
val addr = Addresses.ADDRESSES
val query = dsl.select(
ppl.ID,
ppl.FIRST_NAME,
ppl.LAST_NAME,
addr.ID,
addr.STREET_NAME,
addr.STREET_NUMBER,
addr.ZIP_CODE,
addr.CITY,
addr.STATE
).from(ppl)
.join(addr).on(ppl.ADDRESS_ID.eq(addr.ID))
return mapper.asList(query)
}
}