2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.table
|
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.database.sqlite.SQLiteDatabase
|
|
|
|
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.App1
|
|
|
|
import jp.juggler.subwaytooter.util.LogCategory
|
2018-01-21 13:46:36 +01:00
|
|
|
import jp.juggler.subwaytooter.util.toJsonObject
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
object ClientInfo :TableCompanion {
|
2018-01-04 19:52:25 +01:00
|
|
|
private val log = LogCategory("ClientInfo")
|
|
|
|
|
|
|
|
const val table = "client_info2"
|
|
|
|
private const val COL_HOST = "h"
|
|
|
|
private const val COL_CLIENT_NAME = "cn"
|
|
|
|
private const val COL_RESULT = "r"
|
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBCreate(db : SQLiteDatabase) {
|
2018-01-04 19:52:25 +01:00
|
|
|
db.execSQL(
|
|
|
|
"create table if not exists " + table
|
|
|
|
+ "(_id INTEGER PRIMARY KEY"
|
|
|
|
+ ",h text not null"
|
|
|
|
+ ",cn text not null"
|
|
|
|
+ ",r text not null"
|
|
|
|
+ ")"
|
|
|
|
)
|
|
|
|
db.execSQL(
|
|
|
|
"create unique index if not exists " + table + "_host_client_name on " + table + "(h,cn)"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) {
|
2018-01-04 19:52:25 +01:00
|
|
|
if(oldVersion <= 18 && newVersion >= 19) {
|
|
|
|
onDBCreate(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun load(instance : String, client_name : String) : JSONObject? {
|
|
|
|
try {
|
|
|
|
App1.database.query(table, null, "h=? and cn=?", arrayOf(instance, client_name), null, null, null)
|
|
|
|
.use { cursor ->
|
|
|
|
if(cursor.moveToFirst()) {
|
2018-01-21 13:46:36 +01:00
|
|
|
return cursor.getString(cursor.getColumnIndex(COL_RESULT)).toJsonObject()
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "load failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun save(instance : String, client_name : String, json : String) {
|
|
|
|
try {
|
|
|
|
val cv = ContentValues()
|
|
|
|
cv.put(COL_HOST, instance)
|
|
|
|
cv.put(COL_CLIENT_NAME, client_name)
|
|
|
|
cv.put(COL_RESULT, json)
|
|
|
|
App1.database.replace(table, null, cv)
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "save failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-01-12 10:01:25 +01:00
|
|
|
|
|
|
|
// 単体テスト用。インスタンス名を指定して削除する
|
2018-05-11 15:42:54 +02:00
|
|
|
fun delete(instance : String,client_name : String ) {
|
2018-01-12 10:01:25 +01:00
|
|
|
try {
|
2018-05-11 15:42:54 +02:00
|
|
|
App1.database.delete(table, "$COL_HOST=? and $COL_CLIENT_NAME=?", arrayOf(instance, client_name))
|
2018-01-12 10:01:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "delete failed.")
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|