|
18 | 18 | import androidx.room.Delete; |
19 | 19 | import androidx.room.Insert; |
20 | 20 | import androidx.room.Query; |
21 | | -import androidx.room.RewriteQueriesToDropUnusedColumns; |
| 21 | +import androidx.room.RawQuery; |
| 22 | +import androidx.sqlite.db.SimpleSQLiteQuery; |
| 23 | +import androidx.sqlite.db.SupportSQLiteQuery; |
22 | 24 |
|
23 | 25 | import org.schabi.newpipe.database.history.model.StreamHistoryEntity; |
24 | 26 | import org.schabi.newpipe.database.history.model.StreamHistoryEntry; |
25 | 27 | import org.schabi.newpipe.database.stream.StreamStatisticsEntry; |
| 28 | +import org.schabi.newpipe.database.stream.model.StreamEntity; |
| 29 | +import org.schabi.newpipe.local.history.SortKey; |
26 | 30 |
|
27 | 31 | import java.util.List; |
28 | 32 |
|
|
31 | 35 |
|
32 | 36 | @Dao |
33 | 37 | public interface StreamHistoryDAO { |
| 38 | + String ORDERED_HISTORY_QUERY = "SELECT * FROM " + STREAM_TABLE |
| 39 | + + " INNER JOIN " |
| 40 | + + "(SELECT " + JOIN_STREAM_ID + ", " |
| 41 | + + " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", " |
| 42 | + + " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT |
| 43 | + + " FROM " + STREAM_HISTORY_TABLE |
| 44 | + + " GROUP BY " + JOIN_STREAM_ID + ")" |
| 45 | + + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID |
| 46 | + + " LEFT JOIN " |
| 47 | + + "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", " |
| 48 | + + STREAM_PROGRESS_MILLIS |
| 49 | + + " FROM " + STREAM_STATE_TABLE + " )" |
| 50 | + + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS; |
| 51 | + |
34 | 52 | @Insert |
35 | 53 | long insert(StreamHistoryEntity entity); |
36 | 54 |
|
@@ -60,47 +78,16 @@ public interface StreamHistoryDAO { |
60 | 78 | + " ORDER BY " + STREAM_ACCESS_DATE + " DESC") |
61 | 79 | Flowable<List<StreamHistoryEntry>> getHistory(); |
62 | 80 |
|
63 | | - @RewriteQueriesToDropUnusedColumns |
64 | | - @Query("SELECT * FROM " + STREAM_TABLE |
65 | | - // Select the latest entry and watch count for each stream id on history table |
66 | | - + " INNER JOIN " |
67 | | - + "(SELECT " + JOIN_STREAM_ID + ", " |
68 | | - + " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", " |
69 | | - + " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT |
70 | | - + " FROM " + STREAM_HISTORY_TABLE |
71 | | - + " GROUP BY " + JOIN_STREAM_ID + ")" |
72 | | - |
73 | | - + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID |
74 | | - |
75 | | - + " LEFT JOIN " |
76 | | - + "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", " |
77 | | - + STREAM_PROGRESS_MILLIS |
78 | | - + " FROM " + STREAM_STATE_TABLE + " )" |
79 | | - + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS |
80 | | - |
81 | | - + " ORDER BY " + STREAM_LATEST_DATE + " DESC" |
82 | | - ) |
83 | | - PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByLastWatched(); |
84 | | - |
85 | | - @RewriteQueriesToDropUnusedColumns |
86 | | - @Query("SELECT * FROM " + STREAM_TABLE |
87 | | - // Select the latest entry and watch count for each stream id on history table |
88 | | - + " INNER JOIN " |
89 | | - + "(SELECT " + JOIN_STREAM_ID + ", " |
90 | | - + " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", " |
91 | | - + " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT |
92 | | - + " FROM " + STREAM_HISTORY_TABLE |
93 | | - + " GROUP BY " + JOIN_STREAM_ID + ")" |
94 | | - |
95 | | - + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID |
96 | | - |
97 | | - + " LEFT JOIN " |
98 | | - + "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", " |
99 | | - + STREAM_PROGRESS_MILLIS |
100 | | - + " FROM " + STREAM_STATE_TABLE + " )" |
101 | | - + " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS |
102 | | - |
103 | | - + " ORDER BY " + STREAM_WATCH_COUNT + " DESC" |
104 | | - ) |
105 | | - PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByViewCount(); |
| 81 | + @RawQuery(observedEntities = {StreamStatisticsEntry.class, StreamEntity.class, |
| 82 | + StreamHistoryEntity.class}) |
| 83 | + PagingSource<Integer, StreamStatisticsEntry> getOrderedHistoryByRaw(SupportSQLiteQuery query); |
| 84 | + |
| 85 | + default PagingSource<Integer, StreamStatisticsEntry> getOrderedHistory(SortKey key) { |
| 86 | + final String orderBy = switch (key) { |
| 87 | + case LAST_PLAYED -> STREAM_LATEST_DATE; |
| 88 | + case MOST_PLAYED -> STREAM_WATCH_COUNT; |
| 89 | + }; |
| 90 | + return getOrderedHistoryByRaw(new SimpleSQLiteQuery(ORDERED_HISTORY_QUERY + " ORDER BY " |
| 91 | + + orderBy + " DESC")); |
| 92 | + } |
106 | 93 | } |
0 commit comments