Last active
August 25, 2017 00:45
-
-
Save nosix/517369c4a7ddbd07196a7713116e4c4c to your computer and use it in GitHub Desktop.
Usage of SQLite for Android (API Level 21) in Kotlin 1.0.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.content.Context | |
import xxx.Database | |
import xxx.SampleDB | |
class Client(context: Context) { | |
private val db = Database(context, SampleDB()) | |
fun save(time: Int, type: Int) { | |
db.update { | |
val seqId = it.sampleTable.nextSeqId() | |
it.sampleTable.insert(seqId, time, type) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.content.Context | |
import android.database.sqlite.SQLiteDatabase | |
import android.database.sqlite.SQLiteOpenHelper | |
class Database<out D : Database.Dao>(context: Context, val def: Definition<D>) { | |
interface Dao { | |
fun onCreate() | |
} | |
abstract class Definition<out D : Dao> { | |
abstract val name: String | |
abstract val version: Int | |
val cursorFactory: SQLiteDatabase.CursorFactory? = null | |
abstract fun create(db: SQLiteDatabase): D | |
} | |
private val helper = object : SQLiteOpenHelper(context, def.name, def.cursorFactory, def.version) { | |
override fun onCreate(db: SQLiteDatabase) { | |
def.create(db).onCreate() | |
} | |
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {} | |
} | |
fun <R> query(procedure: (D) -> R): R { | |
helper.readableDatabase.use { | |
return procedure(def.create(it)) | |
} | |
} | |
fun <R> update(procedure: (D) -> R): R { | |
helper.writableDatabase.use { | |
it.beginTransaction() | |
try { | |
val result = procedure(def.create(it)) | |
it.setTransactionSuccessful() | |
return result | |
} | |
finally { | |
it.endTransaction() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.database.sqlite.SQLiteDatabase | |
import xxx.dao.SampleTableDao | |
class SampleDB : Database.Definition<SampleDB.Dao>() { | |
override val name = "sample.db" | |
override val version = 1 | |
class Dao(db: SQLiteDatabase) : Database.Dao { | |
val sampleTable = SampleTableDao(db) | |
override fun onCreate() { | |
sampleTable.createTable() | |
} | |
} | |
override fun create(db: SQLiteDatabase) = SampleDB.Dao(db) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx.dao | |
import android.content.ContentValues | |
import android.database.sqlite.SQLiteDatabase | |
import xxx.insert | |
import xxx.query | |
class SampleTableDao(val db: SQLiteDatabase) { | |
companion object { | |
private val TABLE_NAME = "sample_table" | |
} | |
fun createTable() { | |
db.execSQL(""" | |
CREATE TABLE $TABLE_NAME( | |
seq_id INTEGER, | |
time INTEGER, | |
type INTEGER | |
PRIMARY KEY(seq_id, time) | |
) | |
""") | |
} | |
fun nextSeqId(): Int { | |
db.query(TABLE_NAME, arrayOf("max(seq_id)")).use { | |
return 1 + if (it.moveToNext()) it.getInt(0) else 0 | |
} | |
} | |
fun insert(seqId: Int, time: Int, type: Int) { | |
val values = ContentValues().apply { | |
put("seq_id", seqId) | |
put("time", time) | |
put("type", type) | |
} | |
db.insert(TABLE_NAME, values) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.content.ContentValues | |
import android.database.sqlite.SQLiteDatabase | |
fun SQLiteDatabase.query( | |
table: String, | |
columns: Array<out String>, | |
selection: String? = null, | |
selectionArgs: Array<out String>? = null, | |
groupBy: String? = null, | |
having: String? = null, | |
orderBy: String? = null, | |
limit: String? = null, | |
distinct: Boolean = false) = | |
query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit) | |
fun SQLiteDatabase.insert( | |
table: String, | |
values: ContentValues, | |
nullColumnHack: String? = null) = | |
insert(table, nullColumnHack, values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great Example, many thanks is just what i needed.