3 ms·
I really think trying to create ORMs is a misguided endavour. Instead what we really need is a way to map a row from an sql query to a struct (or similar). Lu
by hasenj 9y ago
I really think trying to create ORMs is a misguided endavour.
Instead what we really need is a way to map a row from an sql query to a struct (or similar).
Luckily, the SQLite helpers from Anko do provide this ability, and it's pretty much the only part I use.
data class UserRow(val id: Long, val name: String);
// .... open db .. etc
var users = db.query<UserRow>("select id, name from users where ....."); // where clause content omitted
// now users is a list of structs (as close to structs as you can get in Kotlin).
Where I have an extension method `query`:
inline fun <reified T : Any> SQLiteDatabase.query(sql: String, vararg args: String): List<T> {
this.rawQuery(sql, args).parseList(classParser<T>())
}