|
|
@ -1,20 +1,26 @@ |
|
|
|
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.room.migration.Migration |
|
|
|
import androidx.room.migration.Migration |
|
|
|
import androidx.sqlite.db.SupportSQLiteDatabase |
|
|
|
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( |
|
|
|
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, |
|
|
|
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, |
|
|
|
SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, |
|
|
|
SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, |
|
|
|
RssArticle::class], |
|
|
|
RssArticle::class], |
|
|
|
version = 1, |
|
|
|
version = 2, |
|
|
|
exportSchema = true |
|
|
|
exportSchema = true |
|
|
|
) |
|
|
|
) |
|
|
|
abstract class AppDatabase : RoomDatabase() { |
|
|
|
abstract class AppDatabase : RoomDatabase() { |
|
|
@ -22,31 +28,45 @@ abstract class AppDatabase : RoomDatabase() { |
|
|
|
companion object { |
|
|
|
companion object { |
|
|
|
private const val DATABASE_NAME = "legado.db" |
|
|
|
private const val DATABASE_NAME = "legado.db" |
|
|
|
|
|
|
|
|
|
|
|
private val MIGRATION_1_2: Migration = object : Migration(1, 2) { |
|
|
|
private val MIGRATION_1_N: Migration = object : Migration(1, 2) { |
|
|
|
override fun migrate(database: SupportSQLiteDatabase) { |
|
|
|
override fun migrate(database: SupportSQLiteDatabase) { |
|
|
|
database.run { |
|
|
|
backup(database) |
|
|
|
// execSQL("ALTER TABLE parsers ADD COLUMN fulltextScript TEXT") |
|
|
|
|
|
|
|
// execSQL("ALTER TABLE feeds ADD COLUMN lastUpdateTime INTEGER NOT NULL DEFAULT 0") |
|
|
|
|
|
|
|
// execSQL("DELETE FROM entries WHERE rowid NOT IN (SELECT MIN(rowid) FROM entries GROUP BY link)") |
|
|
|
|
|
|
|
// execSQL("CREATE UNIQUE INDEX index_entries_link ON entries(link)") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun createDatabase(context: Context): AppDatabase { |
|
|
|
fun createDatabase(context: Context): AppDatabase { |
|
|
|
return Room.databaseBuilder( |
|
|
|
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) |
|
|
|
context.applicationContext, |
|
|
|
// .fallbackToDestructiveMigration() |
|
|
|
AppDatabase::class.java, |
|
|
|
.addMigrations(MIGRATION_1_N) |
|
|
|
DATABASE_NAME |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
// .addMigrations(MIGRATION_1_2) |
|
|
|
|
|
|
|
// .addMigrations(MIGRATION_2_3) |
|
|
|
|
|
|
|
// .addMigrations(MIGRATION_3_4) |
|
|
|
|
|
|
|
// .addMigrations(MIGRATION_4_5) |
|
|
|
|
|
|
|
// .addMigrations(MIGRATION_5_6) |
|
|
|
|
|
|
|
.build() |
|
|
|
.build() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun backup(database: SupportSQLiteDatabase) { |
|
|
|
|
|
|
|
val forms = arrayOf("books") |
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
abstract fun bookDao(): BookDao |
|
|
|
abstract fun bookDao(): BookDao |
|
|
|