historyView.sq 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. chapters.chapter_number AS chapterNumber,
  9. history.last_read AS readAt,
  10. history.time_read AS readDuration,
  11. max_last_read.last_read AS maxReadAt,
  12. max_last_read.chapter_id AS maxReadAtChapterId
  13. FROM mangas
  14. JOIN chapters
  15. ON mangas._id = chapters.manga_id
  16. JOIN history
  17. ON chapters._id = history.chapter_id
  18. JOIN (
  19. SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
  20. FROM chapters JOIN history
  21. ON chapters._id = history.chapter_id
  22. GROUP BY chapters.manga_id
  23. ) AS max_last_read
  24. ON chapters.manga_id = max_last_read.manga_id;
  25. countHistory:
  26. SELECT count(*)
  27. FROM historyView
  28. WHERE historyView.readAt > 0
  29. AND maxReadAtChapterId = historyView.chapterId
  30. AND lower(historyView.title) LIKE ('%' || :query || '%');
  31. history:
  32. SELECT
  33. id,
  34. mangaId,
  35. chapterId,
  36. title,
  37. thumbnailUrl,
  38. chapterNumber,
  39. readAt,
  40. readDuration
  41. FROM historyView
  42. WHERE historyView.readAt > 0
  43. AND maxReadAtChapterId = historyView.chapterId
  44. AND lower(historyView.title) LIKE ('%' || :query || '%')
  45. ORDER BY readAt DESC
  46. LIMIT :limit OFFSET :offset;
  47. getLatestHistory:
  48. SELECT
  49. id,
  50. mangaId,
  51. chapterId,
  52. title,
  53. thumbnailUrl,
  54. chapterNumber,
  55. readAt,
  56. readDuration
  57. FROM historyView
  58. WHERE historyView.readAt > 0
  59. ORDER BY readAt DESC
  60. LIMIT 1;