historyView.sq 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. CREATE VIEW historyView AS
  2. SELECT
  3. history._id AS id,
  4. mangas._id AS mangaId,
  5. chapters._id AS chapterId,
  6. mangas.title,
  7. mangas.thumbnail_url AS thumbnailUrl,
  8. mangas.source,
  9. mangas.favorite,
  10. mangas.cover_last_modified,
  11. chapters.chapter_number AS chapterNumber,
  12. history.last_read AS readAt,
  13. history.time_read AS readDuration,
  14. max_last_read.last_read AS maxReadAt,
  15. max_last_read.chapter_id AS maxReadAtChapterId
  16. FROM mangas
  17. JOIN chapters
  18. ON mangas._id = chapters.manga_id
  19. JOIN history
  20. ON chapters._id = history.chapter_id
  21. JOIN (
  22. SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
  23. FROM chapters JOIN history
  24. ON chapters._id = history.chapter_id
  25. GROUP BY chapters.manga_id
  26. ) AS max_last_read
  27. ON chapters.manga_id = max_last_read.manga_id;
  28. countHistory:
  29. SELECT count(*)
  30. FROM historyView
  31. WHERE historyView.readAt > 0
  32. AND maxReadAtChapterId = historyView.chapterId
  33. AND lower(historyView.title) LIKE ('%' || :query || '%');
  34. history:
  35. SELECT
  36. id,
  37. mangaId,
  38. chapterId,
  39. title,
  40. thumbnailUrl,
  41. source,
  42. favorite,
  43. cover_last_modified,
  44. chapterNumber,
  45. readAt,
  46. readDuration
  47. FROM historyView
  48. WHERE historyView.readAt > 0
  49. AND maxReadAtChapterId = historyView.chapterId
  50. AND lower(historyView.title) LIKE ('%' || :query || '%')
  51. ORDER BY readAt DESC
  52. LIMIT :limit OFFSET :offset;
  53. getLatestHistory:
  54. SELECT
  55. id,
  56. mangaId,
  57. chapterId,
  58. title,
  59. thumbnailUrl,
  60. source,
  61. favorite,
  62. cover_last_modified,
  63. chapterNumber,
  64. readAt,
  65. readDuration
  66. FROM historyView
  67. WHERE historyView.readAt > 0
  68. ORDER BY readAt DESC
  69. LIMIT 1;