more clean up

This commit is contained in:
tateisu 2021-06-28 04:01:33 +09:00
parent 92b39a464a
commit e94389c4d2
6 changed files with 121 additions and 105 deletions

View File

@ -3,6 +3,7 @@ package jp.juggler.subwaytooter.table
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.provider.BaseColumns
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.BackgroundColorSpan
@ -75,16 +76,39 @@ class AcctColor {
private val log = LogCategory("AcctColor")
const val table = "acct_color"
private const val COL_TIME_SAVE = "time_save"
private const val COL_ACCT = "ac" //@who@host ascii文字の大文字小文字は(sqliteにより)同一視される
private const val COL_COLOR_FG = "cf" // 未設定なら0、それ以外は色
private const val COL_COLOR_BG = "cb" // 未設定なら0、それ以外は色
private const val COL_NICKNAME = "nick" // 未設定ならnullか空文字列
private const val COL_NOTIFICATION_SOUND = "notification_sound" // 未設定ならnullか空文字列
val columnList: ColumnMeta.List = ColumnMeta.List(table, 9).apply {
// not used, but must be defined
ColumnMeta(this, 0, BaseColumns._ID, "INTEGER PRIMARY KEY", primary = true)
createExtra = {
arrayOf(
"create unique index if not exists ${table}_acct on $table($COL_ACCT)",
"create index if not exists ${table}_time on $table($COL_TIME_SAVE)",
)
}
}
private val COL_TIME_SAVE = ColumnMeta(columnList, 0, "time_save", "integer not null")
//@who@host ascii文字の大文字小文字は(sqliteにより)同一視される
private val COL_ACCT = ColumnMeta(columnList, 0, "ac", "text not null")
// 未設定なら0、それ以外は色
private val COL_COLOR_FG = ColumnMeta(columnList, 0, "cf", "integer")
// 未設定なら0、それ以外は色
private val COL_COLOR_BG = ColumnMeta(columnList, 0, "cb", "integer")
// 未設定ならnullか空文字列
private val COL_NICKNAME = ColumnMeta(columnList, 0, "nick", "text")
// 未設定ならnullか空文字列
private val COL_NOTIFICATION_SOUND = ColumnMeta(columnList, 17, "notification_sound", "text default ''")
private const val CHAR_REPLACE: Char = 0x328A.toChar()
private const val load_where = "$COL_ACCT=?"
private val load_where = "$COL_ACCT=?"
private val load_where_arg = object : ThreadLocal<Array<String?>>() {
override fun initialValue(): Array<String?> {
@ -94,37 +118,11 @@ class AcctColor {
private val mMemoryCache = LruCache<String, AcctColor>(2048)
override fun onDBCreate(db: SQLiteDatabase) {
log.d("onDBCreate!")
db.execSQL(
"""create table if not exists $table
(_id INTEGER PRIMARY KEY
,$COL_TIME_SAVE integer not null
,$COL_ACCT text not null
,$COL_COLOR_FG integer
,$COL_COLOR_BG integer
,$COL_NICKNAME text
,$COL_NOTIFICATION_SOUND text default ''
)""".trimIndent()
)
db.execSQL("create unique index if not exists ${table}_acct on $table($COL_ACCT)")
db.execSQL("create index if not exists ${table}_time on $table($COL_TIME_SAVE)")
}
override fun onDBCreate(db: SQLiteDatabase) =
columnList.onDBCreate(db)
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion < 9 && newVersion >= 9) {
onDBCreate(db)
return
}
if (oldVersion < 17 && newVersion >= 17) {
try {
db.execSQL("alter table $table add column $COL_NOTIFICATION_SOUND text default ''")
} catch (ex: Throwable) {
log.trace(ex)
}
}
}
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) =
columnList.onDBUpgrade(db, oldVersion, newVersion)
fun load(a: SavedAccount, who: TootAccount) = load(a.getFullAcct(who))
fun load(a: SavedAccount) = load(a.acct)

View File

@ -2,22 +2,37 @@ package jp.juggler.subwaytooter.table
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.provider.BaseColumns
import java.util.ArrayList
import jp.juggler.subwaytooter.App1
import jp.juggler.util.ColumnMeta
import jp.juggler.util.LogCategory
import jp.juggler.util.TableCompanion
import jp.juggler.util.put
object AcctSet : TableCompanion {
private val log = LogCategory("AcctSet")
private const val table = "acct_set"
private const val COL_TIME_SAVE = "time_save"
private const val COL_ACCT = "acct" //@who@host ascii文字の大文字小文字は(sqliteにより)同一視される
val columnList: ColumnMeta.List = ColumnMeta.List("", 7).apply {
ColumnMeta(this, 0, BaseColumns._ID, "INTEGER PRIMARY KEY", primary = true)
private const val prefix_search_where = "$COL_ACCT like ? escape '$'"
createExtra = {
arrayOf(
"create unique index if not exists ${table}_acct on $table($COL_ACCT)",
"create index if not exists ${table}_time on $table($COL_TIME_SAVE)",
)
}
}
private val COL_TIME_SAVE = ColumnMeta(columnList, 0, "time_save", "integer not null")
//@who@host ascii文字の大文字小文字は(sqliteにより)同一視される
private val COL_ACCT = ColumnMeta(columnList, 0, "acct", "text not null")
private val prefix_search_where = "$COL_ACCT like ? escape '$'"
private val prefix_search_where_arg = object : ThreadLocal<Array<String?>>() {
override fun initialValue(): Array<String?> {
@ -25,24 +40,11 @@ object AcctSet : TableCompanion {
}
}
override fun onDBCreate(db: SQLiteDatabase) {
log.d("onDBCreate!")
db.execSQL(
"""create table if not exists $table
(_id INTEGER PRIMARY KEY
,$COL_TIME_SAVE integer not null
,$COL_ACCT text not null
)""".trimIndent()
)
db.execSQL("create unique index if not exists ${table}_acct on $table($COL_ACCT)")
db.execSQL("create index if not exists ${table}_time on $table($COL_TIME_SAVE)")
}
override fun onDBCreate(db: SQLiteDatabase) =
columnList.onDBCreate(db)
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion < 7 && newVersion >= 7) {
onDBCreate(db)
}
}
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) =
columnList.onDBUpgrade(db, oldVersion, newVersion)
fun deleteOld(now: Long) {
try {
@ -115,15 +117,14 @@ object AcctSet : TableCompanion {
null,
null,
"$COL_ACCT asc limit $limit"
)
.use { cursor ->
val dst = ArrayList<CharSequence>(cursor.count)
val idx_acct = cursor.getColumnIndex(COL_ACCT)
while (cursor.moveToNext()) {
dst.add(cursor.getString(idx_acct))
}
return dst
).use { cursor ->
val dst = ArrayList<CharSequence>(cursor.count)
val idx_acct = COL_ACCT.getIndex(cursor)
while (cursor.moveToNext()) {
dst.add(cursor.getString(idx_acct))
}
return dst
}
} catch (ex: Throwable) {
log.trace(ex)
log.e(ex, "searchPrefix failed.")

View File

@ -2,6 +2,7 @@ package jp.juggler.subwaytooter.table
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.provider.BaseColumns
import jp.juggler.subwaytooter.App1
import jp.juggler.util.*
@ -9,27 +10,23 @@ object ClientInfo : TableCompanion {
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"
override fun onDBCreate(db: SQLiteDatabase) {
db.execSQL(
"""create table if not exists $table
(_id INTEGER PRIMARY KEY
,$COL_HOST text not null
,$COL_CLIENT_NAME text not null
,$COL_RESULT text not null
)""".trimIndent()
)
db.execSQL("create unique index if not exists ${table}_host_client_name on $table($COL_HOST,$COL_CLIENT_NAME)")
}
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion <= 18 && newVersion >= 19) {
onDBCreate(db)
val columnList: ColumnMeta.List = ColumnMeta.List(table, 19).apply {
ColumnMeta(this, 0, BaseColumns._ID, "INTEGER PRIMARY KEY", primary = true)
createExtra = {
arrayOf(
"create unique index if not exists ${table}_host_client_name on $table($COL_HOST,$COL_CLIENT_NAME)"
)
}
}
private val COL_HOST = ColumnMeta(columnList, 0, "h", "text not null")
private val COL_CLIENT_NAME = ColumnMeta(columnList, 0, "cn", "text not null")
private val COL_RESULT = ColumnMeta(columnList, 0, "r", "text not null")
override fun onDBCreate(db: SQLiteDatabase) =
columnList.onDBCreate(db)
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) =
columnList.onDBUpgrade(db, oldVersion, newVersion)
fun load(instance: String, clientName: String): JsonObject? {
try {

View File

@ -347,7 +347,14 @@ class SavedAccount(
const val table = "access_info"
val columnList = ColumnMeta.List(table)
val columnList = ColumnMeta.List(table, 0).apply {
createExtra = {
arrayOf(
"create index if not exists ${table}_user on $table(u)",
"create index if not exists ${table}_host on $table(h,u)"
)
}
}
private val COL_ID = ColumnMeta(columnList, 0, BaseColumns._ID, "INTEGER PRIMARY KEY", primary = true)
private val COL_HOST = ColumnMeta(columnList, 0, "h", "text not null")
@ -429,15 +436,11 @@ class SavedAccount(
}
}
override fun onDBCreate(db: SQLiteDatabase) {
db.execSQL("create table if not exists $table (${columnList.createParams()})")
db.execSQL("create index if not exists ${table}_user on $table(u)")
db.execSQL("create index if not exists ${table}_host on $table(h,u)")
}
override fun onDBCreate(db: SQLiteDatabase) =
columnList.onDBCreate(db)
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
columnList.addColumns(db, oldVersion, newVersion)
}
override fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) =
columnList.onDBUpgrade(db, oldVersion, newVersion)
val defaultResizeConfig = ResizeConfig(ResizeType.LongSide, 1280)

View File

@ -3,6 +3,7 @@ package jp.juggler.util
import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import jp.juggler.subwaytooter.table.SavedAccount
/////////////////////////////////////////////////////////////
// SQLite にBooleanをそのまま保存することはできないのでInt型との変換が必要になる
@ -71,24 +72,40 @@ class ColumnMeta(
const val TS_TRUE = "integer default 1"
}
class List(val table: String) : ArrayList<ColumnMeta>() {
fun createParams(): String =
sorted().joinToString(",") { "${it.name} ${it.typeSpec}" }
class List(
val table: String,
val initialVersion: Int,
var createExtra: () -> Array<String> = { emptyArray() },
) : ArrayList<ColumnMeta>() {
val maxVersion: Int
get() = this.maxOfOrNull{ it.version} ?: 0
get() = this.maxOfOrNull { it.version } ?: 0
fun createTableSql() =
listOf(
"create table if not exists ${SavedAccount.table} (${sorted().joinToString(",") { "${it.name} ${it.typeSpec}" }})",
*(createExtra())
)
fun addColumnsSql(oldVersion: Int, newVersion: Int) =
sorted()
.filter { oldVersion < it.version && newVersion >= it.version }
.map { "alter table $table add column ${it.name} ${it.typeSpec}" }
fun addColumns(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
for (sql in addColumnsSql(oldVersion, newVersion)) {
fun onDBCreate(db: SQLiteDatabase) {
log.d("onDBCreate table=$table")
createTableSql().forEach { db.execSQL(it) }
}
fun onDBUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion < initialVersion && newVersion >= initialVersion) {
onDBCreate(db)
return
}
addColumnsSql(oldVersion, newVersion).forEach {
try {
db.execSQL(sql)
db.execSQL(it)
} catch (ex: Throwable) {
log.trace(ex, "addColumns failed. $sql")
log.trace(ex, "execSQL failed. $it")
}
}
}
@ -99,7 +116,7 @@ class ColumnMeta(
// プライマリキーを先頭にする
val ia = if (this.primary) -1 else 0
val ib = if (other.primary) -1 else 0
ia.compareTo(ib).notZero()?.let{ return it}
ia.compareTo(ib).notZero()?.let { return it }
// 残りはカラム名順
return name.compareTo(other.name)

View File

@ -9,9 +9,9 @@ class TestColumnMeta {
@Test
fun test1() {
val columnList = SavedAccount.columnList
val actual = columnList.createParams()
val actual = columnList.createTableSql().joinToString(";")
val expect =
"_id INTEGER PRIMARY KEY,a text not null,confirm_boost integer default 1,confirm_favourite integer default 1,confirm_follow integer default 1,confirm_follow_locked integer default 1,confirm_post integer default 1,confirm_reaction integer default 1,confirm_unboost integer default 1,confirm_unfavourite integer default 1,confirm_unfollow integer default 1,d text,default_sensitive integer default 0,default_text text default '',dont_hide_nsfw integer default 0,dont_show_timeout integer default 0,expand_cw integer default 0,h text not null,image_max_megabytes text default null,image_resize text default null,is_misskey integer default 0,last_notification_error text,last_push_endpoint text,last_subscription_error text,max_toot_chars integer default 0,movie_max_megabytes text default null,notification_boost integer default 1,notification_favourite integer default 1,notification_follow integer default 1,notification_follow_request integer default 1,notification_mention integer default 1,notification_post integer default 1,notification_reaction integer default 1,notification_server text default '',notification_vote integer default 1,push_policy text default null,register_key text default '',register_time integer default 0,sound_uri text default '',t text not null,u text not null,visibility text"
"create table if not exists access_info (_id INTEGER PRIMARY KEY,a text not null,confirm_boost integer default 1,confirm_favourite integer default 1,confirm_follow integer default 1,confirm_follow_locked integer default 1,confirm_post integer default 1,confirm_reaction integer default 1,confirm_unboost integer default 1,confirm_unfavourite integer default 1,confirm_unfollow integer default 1,d text,default_sensitive integer default 0,default_text text default '',dont_hide_nsfw integer default 0,dont_show_timeout integer default 0,expand_cw integer default 0,h text not null,image_max_megabytes text default null,image_resize text default null,is_misskey integer default 0,last_notification_error text,last_push_endpoint text,last_subscription_error text,max_toot_chars integer default 0,movie_max_megabytes text default null,notification_boost integer default 1,notification_favourite integer default 1,notification_follow integer default 1,notification_follow_request integer default 1,notification_mention integer default 1,notification_post integer default 1,notification_reaction integer default 1,notification_server text default '',notification_vote integer default 1,push_policy text default null,register_key text default '',register_time integer default 0,sound_uri text default '',t text not null,u text not null,visibility text);create index if not exists access_info_user on access_info(u);create index if not exists access_info_host on access_info(h,u)"
assertEquals("SavedAccount createParams()", expect, actual)
}