diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 48770cefe..7d0a81128 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -6,6 +6,7 @@ import androidx.room.Room import androidx.room.RoomDatabase import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase +import io.legado.app.App import io.legado.app.data.dao.* import io.legado.app.data.entities.* @@ -15,7 +16,7 @@ import io.legado.app.data.entities.* ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class], - version = 18, + version = 19, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -34,7 +35,8 @@ abstract class AppDatabase : RoomDatabase() { migration_13_14, migration_14_15, migration_15_17, - migration_17_18 + migration_17_18, + migration_18_19 ) .allowMainThreadQueries() .build() @@ -101,6 +103,15 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))") } } + + private val migration_18_19 = object : Migration(18, 19) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE IF NOT EXISTS `readRecordNew` (`androidId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`androidId`, `bookName`))") + database.execSQL("INSERT INTO readRecordNew(androidId, bookName, readTime) select '${App.androidId}' as androidId, bookName, readTime from readRecord") + database.execSQL("DROP TABLE readRecord") + database.execSQL("ALTER TABLE readRecordNew RENAME TO readRecord") + } + } } abstract fun bookDao(): BookDao diff --git a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt index 38eb5ead1..d7619e9a8 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReadRecordDao.kt @@ -12,7 +12,7 @@ interface ReadRecordDao { @get:Query("select sum(readTime) from readRecord") val allTime: Long - @Query("select readTime from readRecord where bookName = :bookName") + @Query("select sum(readTime) from readRecord where bookName = :bookName") fun getReadTime(bookName: String): Long? @Insert(onConflict = OnConflictStrategy.REPLACE) diff --git a/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt b/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt index 488dc0205..60f577477 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReadRecord.kt @@ -1,11 +1,10 @@ package io.legado.app.data.entities import androidx.room.Entity -import androidx.room.PrimaryKey -@Entity(tableName = "readRecord") +@Entity(tableName = "readRecord", primaryKeys = ["androidId", "bookName"]) data class ReadRecord( - @PrimaryKey + var androidId: String = "", var bookName: String = "", var readTime: Long = 0L ) \ No newline at end of file