pull/49/head
kunfei 5 years ago
parent 52153e825d
commit d24cbdf3b0
  1. 49
      app/src/main/java/io/legado/app/data/AppDatabase.kt

@ -1,18 +1,11 @@
package io.legado.app.data package io.legado.app.data
import android.content.Context import android.content.Context
import android.database.Cursor
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.legado.app.data.dao.* import io.legado.app.data.dao.*
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
import io.legado.app.help.FileHelp
import io.legado.app.utils.GSON
import java.io.File
@Database( @Database(
@ -29,50 +22,8 @@ abstract class AppDatabase : RoomDatabase() {
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addCallback(callback)
.build() .build()
} }
val callback = object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
}
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
}
}
private fun backup(database: SupportSQLiteDatabase) {
val forms = arrayOf(
"books", "book_groups", "bookmarks", "book_sources", "cookies",
"replace_rules", "rssSources", "search_keywords", "rssArticles"
)
forms.forEach { form ->
database.query("select * from $form").let {
val ja = JsonArray()
while (it.moveToNext()) {
val jo = JsonObject()
for (i in 0 until it.columnCount) {
if (!it.isNull(i)) {
when (it.getType(i)) {
Cursor.FIELD_TYPE_FLOAT ->
jo.addProperty(it.getColumnName(i), it.getFloat(i))
Cursor.FIELD_TYPE_INTEGER ->
jo.addProperty(it.getColumnName(i), it.getInt(i))
else -> jo.addProperty(it.getColumnName(i), it.getString(i))
}
}
}
ja.add(jo)
}
it.close()
FileHelp.getFile(FileHelp.getCachePath() + File.separator + "db" + File.separator + form + ".json")
.writeText(GSON.toJson(ja))
}
database.execSQL("drop table $form")
}
}
} }
abstract fun bookDao(): BookDao abstract fun bookDao(): BookDao

Loading…
Cancel
Save