Improved last_read sorting (#576)

This commit is contained in:
Bram van de Kerkhof
2016-12-13 21:36:26 +01:00
committed by inorichi
parent 32511149d1
commit 79ab9d80f2
4 changed files with 44 additions and 35 deletions

View File

@@ -40,16 +40,6 @@ interface HistoryQueries : DbProvider {
.build())
.prepare()
fun getLastHistoryByMangaId(mangaId: Long) = db.get()
.`object`(History::class.java)
.withQuery(RawQuery.builder()
.query(getLastHistoryByMangaId())
.args(mangaId)
.observesTables(HistoryTable.TABLE)
.build())
.prepare()
/**
* Updates the history last read.
* Inserts history object if not yet in database

View File

@@ -84,4 +84,11 @@ interface MangaQueries : DbProvider {
.build())
.prepare()
fun getLastReadManga() = db.get()
.listOfObjects(Manga::class.java)
.withQuery(RawQuery.builder()
.query(getLastReadMangaQuery())
.observesTables(MangaTable.TABLE)
.build())
.prepare()
}

View File

@@ -73,17 +73,16 @@ fun getHistoryByMangaId() = """
WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID}
"""
fun getLastHistoryByMangaId() = """
SELECT ${History.TABLE}.*
FROM ${History.TABLE}
fun getLastReadMangaQuery() = """
SELECT ${Manga.TABLE}.*, MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max
FROM ${Manga.TABLE}
JOIN ${Chapter.TABLE}
ON ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID}
LEFT JOIN (
SELECT MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max
FROM ${History.TABLE}
GROUP BY ${History.COL_LAST_READ}
) AS M
WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND M.max = ${History.TABLE}.${History.COL_LAST_READ}
ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID}
JOIN ${History.TABLE}
ON ${Chapter.TABLE}.${Chapter.COL_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID}
WHERE ${Manga.TABLE}.${Manga.COL_FAVORITE} = 1
GROUP BY ${Manga.TABLE}.${Manga.COL_ID}
ORDER BY max DESC
"""
/**