KQLiteAdapter

interface KQLiteAdapter<T>

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]
)
}
}

Author

MOHAMMAD AZIM ANSARI

Type Parameters

T

The type of the object being mapped to and from the database.

See also

Functions

Link copied to clipboard
abstract fun binder(bind: Bind, item: T)

Binds the properties of the given item to the provided bind instance.

Link copied to clipboard
abstract fun mapper(cursor: KQLiteCursor): T

Maps the current row of the cursor to an object of type T.