KQLite Adapter
An adapter responsible for converting between a database row and a Kotlin object of type T.
Example
object TblContact : KQLiteTable("contacts"), KQLiteAdapter<Contact> {
val id = intColumn("id").notNull().primaryKey().autoIncrement()
val firstName = textColumn("first_name").notNull()
val lastName = textColumn("last_name")
val phone = jsonArrayColumn("phone").notNull()
val birthDate = dateColumn("birth_date")
val email = textColumn("email")
val image = blobColumn("image")
val type = enumColumn("type", ContactType.entries).notNull()
val deleted = booleanColumn("deleted").notNull().default(false)
override fun binder(
bind: Bind,
item: Contact
) {
bind.apply {
firstName.bind(item.firstName)
lastName.bind(item.lastName)
phone.bind(JSON_ARRAY(*item.phone.toTypedArray()))
if (item.birthDate != null) {
birthDate.bind(DATE(item.birthDate.toDateString()))
} else {
birthDate.bind(null)
}
email.bind(item.email)
image.bind(item.image)
type.bind(item.type)
}
}
override fun mapper(cursor: KQLiteCursor): Contact {
return Contact(
id = cursor[id],
firstName = cursor[firstName],
lastName = cursor[lastName],
phone = Json.decodeFromString(cursor[phone].getText()),
birthDate = cursor[birthDate]?.getText()?.toInstant(),
email = cursor[email],
image = cursor[image],
type = cursor[type],
deleted = cursor[deleted]
)
}
}Content copied to clipboard
Author
MOHAMMAD AZIM ANSARI
Type Parameters
T
The type of the object being mapped to and from the database.