Update history custom put resolver

This commit is contained in:
len 2016-07-23 15:41:47 +02:00
parent 6999fa858e
commit 8f83f497d5
2 changed files with 28 additions and 16 deletions

View file

@ -22,7 +22,7 @@ class HistoryTypeMapping : SQLiteTypeMapping<History>(
HistoryDeleteResolver() HistoryDeleteResolver()
) )
class HistoryPutResolver : DefaultPutResolver<History>() { open class HistoryPutResolver : DefaultPutResolver<History>() {
override fun mapToInsertQuery(obj: History) = InsertQuery.builder() override fun mapToInsertQuery(obj: History) = InsertQuery.builder()
.table(TABLE) .table(TABLE)

View file

@ -3,49 +3,61 @@ package eu.kanade.tachiyomi.data.database.resolvers
import android.content.ContentValues import android.content.ContentValues
import android.support.annotation.NonNull import android.support.annotation.NonNull
import com.pushtorefresh.storio.sqlite.StorIOSQLite import com.pushtorefresh.storio.sqlite.StorIOSQLite
import com.pushtorefresh.storio.sqlite.operations.put.PutResolver
import com.pushtorefresh.storio.sqlite.operations.put.PutResult import com.pushtorefresh.storio.sqlite.operations.put.PutResult
import com.pushtorefresh.storio.sqlite.queries.Query
import com.pushtorefresh.storio.sqlite.queries.UpdateQuery import com.pushtorefresh.storio.sqlite.queries.UpdateQuery
import eu.kanade.tachiyomi.data.database.inTransactionReturn import eu.kanade.tachiyomi.data.database.inTransactionReturn
import eu.kanade.tachiyomi.data.database.mappers.HistoryPutResolver
import eu.kanade.tachiyomi.data.database.models.History import eu.kanade.tachiyomi.data.database.models.History
import eu.kanade.tachiyomi.data.database.tables.HistoryTable import eu.kanade.tachiyomi.data.database.tables.HistoryTable
class HistoryLastReadPutResolver : PutResolver<History>() { class HistoryLastReadPutResolver : HistoryPutResolver() {
/** /**
* Updates last_read time of chapter * Updates last_read time of chapter
*/ */
override fun performPut(@NonNull db: StorIOSQLite, @NonNull history: History): PutResult = db.inTransactionReturn { override fun performPut(@NonNull db: StorIOSQLite, @NonNull history: History): PutResult = db.inTransactionReturn {
// Create put query
val updateQuery = mapToUpdateQuery(history) val updateQuery = mapToUpdateQuery(history)
val contentValues = mapToContentValues(history)
// Execute query val cursor = db.lowLevel().query(Query.builder()
val numberOfRowsUpdated = db.lowLevel().update(updateQuery, contentValues) .table(updateQuery.table())
.where(updateQuery.where())
.whereArgs(updateQuery.whereArgs())
.build())
// If chapter not found in history insert into database val putResult: PutResult
if (numberOfRowsUpdated == 0) {
db.put().`object`(history).prepare().executeAsBlocking() try {
if (cursor.count == 0) {
val insertQuery = mapToInsertQuery(history)
val insertedId = db.lowLevel().insert(insertQuery, mapToContentValues(history))
putResult = PutResult.newInsertResult(insertedId, insertQuery.table())
} else {
val numberOfRowsUpdated = db.lowLevel().update(updateQuery, mapToUpdateContentValues(history))
putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table())
}
} finally {
cursor.close()
} }
// Update result
PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table()) putResult
} }
/** /**
* Creates update query * Creates update query
* @param history object * @param obj history object
*/ */
fun mapToUpdateQuery(history: History) = UpdateQuery.builder() override fun mapToUpdateQuery(obj: History) = UpdateQuery.builder()
.table(HistoryTable.TABLE) .table(HistoryTable.TABLE)
.where("${HistoryTable.COL_CHAPTER_ID} = ?") .where("${HistoryTable.COL_CHAPTER_ID} = ?")
.whereArgs(history.chapter_id) .whereArgs(obj.chapter_id)
.build() .build()
/** /**
* Create content query * Create content query
* @param history object * @param history object
*/ */
fun mapToContentValues(history: History) = ContentValues(1).apply { fun mapToUpdateContentValues(history: History) = ContentValues(1).apply {
put(HistoryTable.COL_LAST_READ, history.last_read) put(HistoryTable.COL_LAST_READ, history.last_read)
} }