|
1 | 1 | package org.schabi.newpipe.database.history.dao; |
2 | 2 |
|
| 3 | +import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID; |
| 4 | +import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE; |
| 5 | +import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE; |
| 6 | +import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_REPEAT_COUNT; |
| 7 | +import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_LATEST_DATE; |
| 8 | +import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_WATCH_COUNT; |
| 9 | +import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID; |
| 10 | +import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE; |
| 11 | +import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS; |
| 12 | +import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS; |
| 13 | +import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE; |
| 14 | + |
3 | 15 | import androidx.annotation.Nullable; |
| 16 | +import androidx.paging.PagingSource; |
4 | 17 | import androidx.room.Dao; |
5 | 18 | import androidx.room.Query; |
6 | 19 | import androidx.room.RewriteQueriesToDropUnusedColumns; |
|
13 | 26 |
|
14 | 27 | import io.reactivex.rxjava3.core.Flowable; |
15 | 28 |
|
16 | | -import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID; |
17 | | -import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE; |
18 | | -import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE; |
19 | | -import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_REPEAT_COUNT; |
20 | | -import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_LATEST_DATE; |
21 | | -import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_WATCH_COUNT; |
22 | | -import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID; |
23 | | -import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE; |
24 | | -import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS; |
25 | | -import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS; |
26 | | -import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE; |
27 | | - |
28 | 29 | @Dao |
29 | 30 | public abstract class StreamHistoryDAO implements HistoryDAO<StreamHistoryEntity> { |
30 | 31 | @Query("SELECT * FROM " + STREAM_HISTORY_TABLE |
@@ -70,20 +71,45 @@ public Flowable<List<StreamHistoryEntity>> listByService(final int serviceId) { |
70 | 71 |
|
71 | 72 | @RewriteQueriesToDropUnusedColumns |
72 | 73 | @Query("SELECT * FROM " + STREAM_TABLE |
| 74 | + // Select the latest entry and watch count for each stream id on history table |
| 75 | + + " INNER JOIN " |
| 76 | + + "(SELECT " + JOIN_STREAM_ID + ", " |
| 77 | + + " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", " |
| 78 | + + " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT |
| 79 | + + " FROM " + STREAM_HISTORY_TABLE |
| 80 | + + " GROUP BY " + JOIN_STREAM_ID + ")" |
| 81 | + |
| 82 | + + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID |
| 83 | + |
| 84 | + + " LEFT JOIN " |
| 85 | + + "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", " |
| 86 | + + STREAM_PROGRESS_MILLIS |
| 87 | + + " FROM " + STREAM_STATE_TABLE + " )" |
| 88 | + + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS |
| 89 | + |
| 90 | + + " ORDER BY " + STREAM_LATEST_DATE + " DESC" |
| 91 | + ) |
| 92 | + public abstract PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByLastWatched(); |
73 | 93 |
|
| 94 | + @RewriteQueriesToDropUnusedColumns |
| 95 | + @Query("SELECT * FROM " + STREAM_TABLE |
74 | 96 | // Select the latest entry and watch count for each stream id on history table |
75 | 97 | + " INNER JOIN " |
76 | 98 | + "(SELECT " + JOIN_STREAM_ID + ", " |
77 | 99 | + " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", " |
78 | 100 | + " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT |
79 | | - + " FROM " + STREAM_HISTORY_TABLE + " GROUP BY " + JOIN_STREAM_ID + ")" |
| 101 | + + " FROM " + STREAM_HISTORY_TABLE |
| 102 | + + " GROUP BY " + JOIN_STREAM_ID + ")" |
80 | 103 |
|
81 | 104 | + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID |
82 | 105 |
|
83 | 106 | + " LEFT JOIN " |
84 | 107 | + "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", " |
85 | 108 | + STREAM_PROGRESS_MILLIS |
86 | 109 | + " FROM " + STREAM_STATE_TABLE + " )" |
87 | | - + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS) |
88 | | - public abstract Flowable<List<StreamStatisticsEntry>> getStatistics(); |
| 110 | + + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS |
| 111 | + |
| 112 | + + " ORDER BY " + STREAM_WATCH_COUNT + " DESC" |
| 113 | + ) |
| 114 | + public abstract PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByViewCount(); |
89 | 115 | } |
0 commit comments