2018-01-15 21:30:52 +01:00
|
|
|
package org.schabi.newpipe.database.playlist.dao;
|
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.room.Dao;
|
|
|
|
import androidx.room.Query;
|
2022-02-19 17:13:57 +01:00
|
|
|
import androidx.room.RewriteQueriesToDropUnusedColumns;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.room.Transaction;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.database.BasicDAO;
|
|
|
|
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
|
2018-01-28 07:14:38 +01:00
|
|
|
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
|
2018-01-15 21:30:52 +01:00
|
|
|
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
import static org.schabi.newpipe.database.playlist.PlaylistMetadataEntry.PLAYLIST_STREAM_COUNT;
|
2020-03-31 19:20:15 +02:00
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_ID;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_THUMBNAIL_URL;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_INDEX;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_PLAYLIST_ID;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_STREAM_ID;
|
|
|
|
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.PLAYLIST_STREAM_JOIN_TABLE;
|
|
|
|
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;
|
2021-06-07 09:35:40 +02:00
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS;
|
2020-11-15 14:08:41 +01:00
|
|
|
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@Dao
|
2021-01-20 01:59:50 +01:00
|
|
|
public interface PlaylistStreamDAO extends BasicDAO<PlaylistStreamEntity> {
|
2018-01-15 21:30:52 +01:00
|
|
|
@Override
|
|
|
|
@Query("SELECT * FROM " + PLAYLIST_STREAM_JOIN_TABLE)
|
2021-01-20 01:59:50 +01:00
|
|
|
Flowable<List<PlaylistStreamEntity>> getAll();
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@Query("DELETE FROM " + PLAYLIST_STREAM_JOIN_TABLE)
|
2021-01-20 01:59:50 +01:00
|
|
|
int deleteAll();
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@Override
|
2021-01-20 01:59:50 +01:00
|
|
|
default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId) {
|
2018-01-15 21:30:52 +01:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("DELETE FROM " + PLAYLIST_STREAM_JOIN_TABLE
|
|
|
|
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")
|
2021-01-20 01:59:50 +01:00
|
|
|
void deleteBatch(long playlistId);
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT COALESCE(MAX(" + JOIN_INDEX + "), -1)"
|
|
|
|
+ " FROM " + PLAYLIST_STREAM_JOIN_TABLE
|
|
|
|
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")
|
2021-01-20 01:59:50 +01:00
|
|
|
Flowable<Integer> getMaximumIndexOf(long playlistId);
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2022-02-19 17:13:57 +01:00
|
|
|
@RewriteQueriesToDropUnusedColumns
|
2018-01-15 21:30:52 +01:00
|
|
|
@Transaction
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT * FROM " + STREAM_TABLE + " INNER JOIN "
|
2018-01-15 21:30:52 +01:00
|
|
|
// get ids of streams of the given playlist
|
2020-03-31 19:20:15 +02:00
|
|
|
+ "(SELECT " + JOIN_STREAM_ID + "," + JOIN_INDEX
|
|
|
|
+ " FROM " + PLAYLIST_STREAM_JOIN_TABLE
|
|
|
|
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId)"
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
// then merge with the stream metadata
|
2020-03-31 19:20:15 +02:00
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
|
2020-11-15 14:08:41 +01:00
|
|
|
|
|
|
|
+ " LEFT JOIN "
|
|
|
|
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
|
2021-06-07 09:35:40 +02:00
|
|
|
+ STREAM_PROGRESS_MILLIS
|
2020-11-15 14:08:41 +01:00
|
|
|
+ " FROM " + STREAM_STATE_TABLE + " )"
|
|
|
|
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
+ " ORDER BY " + JOIN_INDEX + " ASC")
|
2021-01-20 01:59:50 +01:00
|
|
|
Flowable<List<PlaylistStreamEntry>> getOrderedStreamsOf(long playlistId);
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@Transaction
|
2020-03-31 19:20:15 +02:00
|
|
|
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ", " + PLAYLIST_THUMBNAIL_URL + ", "
|
|
|
|
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
+ " FROM " + PLAYLIST_TABLE
|
|
|
|
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
|
|
|
|
+ " ON " + PLAYLIST_ID + " = " + JOIN_PLAYLIST_ID
|
|
|
|
+ " GROUP BY " + JOIN_PLAYLIST_ID
|
|
|
|
+ " ORDER BY " + PLAYLIST_NAME + " COLLATE NOCASE ASC")
|
2021-01-20 01:59:50 +01:00
|
|
|
Flowable<List<PlaylistMetadataEntry>> getPlaylistMetadata();
|
2018-01-15 21:30:52 +01:00
|
|
|
}
|