2018-01-27 06:34:17 +01:00
|
|
|
package org.schabi.newpipe.database.history.dao;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.annotation.Nullable;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.room.Dao;
|
|
|
|
import androidx.room.Query;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
import org.schabi.newpipe.database.history.model.StreamHistoryEntity;
|
2018-01-27 06:34:17 +01:00
|
|
|
import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
|
2018-01-15 21:30:52 +01:00
|
|
|
import org.schabi.newpipe.database.stream.StreamStatisticsEntry;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.core.Flowable;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID;
|
|
|
|
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE;
|
|
|
|
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE;
|
2018-01-29 03:26:19 +01:00
|
|
|
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_REPEAT_COUNT;
|
2018-01-15 21:30:52 +01:00
|
|
|
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_LATEST_DATE;
|
|
|
|
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_WATCH_COUNT;
|
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID;
|
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE;
|
2020-11-15 14:08:41 +01:00
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS;
|
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_TIME;
|
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@Dao
|
2018-01-27 06:45:48 +01:00
|
|
|
public abstract class StreamHistoryDAO implements HistoryDAO<StreamHistoryEntity> {
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT * FROM " + STREAM_HISTORY_TABLE
|
|
|
|
+ " WHERE " + STREAM_ACCESS_DATE + " = "
|
|
|
|
+ "(SELECT MAX(" + STREAM_ACCESS_DATE + ") FROM " + STREAM_HISTORY_TABLE + ")")
|
2018-01-27 06:45:48 +01:00
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public abstract StreamHistoryEntity getLatestEntry();
|
|
|
|
|
2018-01-15 21:30:52 +01:00
|
|
|
@Override
|
|
|
|
@Query("SELECT * FROM " + STREAM_HISTORY_TABLE)
|
|
|
|
public abstract Flowable<List<StreamHistoryEntity>> getAll();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Query("DELETE FROM " + STREAM_HISTORY_TABLE)
|
|
|
|
public abstract int deleteAll();
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public Flowable<List<StreamHistoryEntity>> listByService(final int serviceId) {
|
2018-01-15 21:30:52 +01:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT * FROM " + STREAM_TABLE
|
|
|
|
+ " INNER JOIN " + STREAM_HISTORY_TABLE
|
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
|
|
|
|
+ " ORDER BY " + STREAM_ACCESS_DATE + " DESC")
|
2018-01-26 07:24:59 +01:00
|
|
|
public abstract Flowable<List<StreamHistoryEntry>> getHistory();
|
|
|
|
|
2020-04-03 18:58:47 +02:00
|
|
|
|
|
|
|
@Query("SELECT * FROM " + STREAM_TABLE
|
|
|
|
+ " INNER JOIN " + STREAM_HISTORY_TABLE
|
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
|
|
|
|
+ " ORDER BY " + STREAM_ID + " ASC")
|
|
|
|
public abstract Flowable<List<StreamHistoryEntry>> getHistorySortedById();
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT * FROM " + STREAM_HISTORY_TABLE + " WHERE " + JOIN_STREAM_ID
|
|
|
|
+ " = :streamId ORDER BY " + STREAM_ACCESS_DATE + " DESC LIMIT 1")
|
2019-04-13 09:31:32 +02:00
|
|
|
@Nullable
|
2020-03-31 19:20:15 +02:00
|
|
|
public abstract StreamHistoryEntity getLatestEntry(long streamId);
|
2019-04-13 09:31:32 +02:00
|
|
|
|
2018-01-15 21:30:52 +01:00
|
|
|
@Query("DELETE FROM " + STREAM_HISTORY_TABLE + " WHERE " + JOIN_STREAM_ID + " = :streamId")
|
2020-03-31 19:20:15 +02:00
|
|
|
public abstract int deleteStreamHistory(long streamId);
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT * FROM " + STREAM_TABLE
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
// Select the latest entry and watch count for each stream id on history table
|
2020-03-31 19:20:15 +02:00
|
|
|
+ " INNER JOIN "
|
|
|
|
+ "(SELECT " + JOIN_STREAM_ID + ", "
|
|
|
|
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
|
|
|
|
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
|
|
|
|
+ " FROM " + STREAM_HISTORY_TABLE + " GROUP BY " + JOIN_STREAM_ID + ")"
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-11-15 14:08:41 +01:00
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
|
|
|
|
|
|
|
|
+ " LEFT JOIN "
|
|
|
|
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
|
|
|
|
+ STREAM_PROGRESS_TIME
|
|
|
|
+ " FROM " + STREAM_STATE_TABLE + " )"
|
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS)
|
2018-01-15 21:30:52 +01:00
|
|
|
public abstract Flowable<List<StreamStatisticsEntry>> getStatistics();
|
|
|
|
}
|