Skip to content

Commit

Permalink
refactor: more queries
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Feb 16, 2025
1 parent 86ccef8 commit 8d90131
Show file tree
Hide file tree
Showing 77 changed files with 949 additions and 1,064 deletions.
24 changes: 15 additions & 9 deletions app/src/main/java/io/github/zyrouge/symphony/services/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.content.Context
import android.net.Uri
import androidx.core.content.edit
import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.groove.repositories.AlbumArtistRepository
import io.github.zyrouge.symphony.services.groove.repositories.AlbumRepository
import io.github.zyrouge.symphony.services.groove.repositories.ArtistRepository
import io.github.zyrouge.symphony.services.groove.repositories.GenreRepository
import io.github.zyrouge.symphony.services.groove.repositories.MediaTreeRepository
import io.github.zyrouge.symphony.services.groove.repositories.PlaylistRepository
import io.github.zyrouge.symphony.services.groove.repositories.SongRepository
import io.github.zyrouge.symphony.services.radio.RadioQueue
Expand Down Expand Up @@ -145,8 +145,8 @@ class Settings(private val symphony: Symphony) {
)
val lastUsedAlbumArtistsSortBy = EnumEntry(
"last_used_album_artists_sort_by",
enumEntries<AlbumArtistRepository.SortBy>(),
AlbumArtistRepository.SortBy.ARTIST_NAME,
enumEntries<ArtistRepository.SortBy>(),
ArtistRepository.SortBy.ARTIST_NAME,
)
val lastUsedAlbumArtistsSortReverse =
BooleanEntry("last_used_album_artists_sort_reverse", false)
Expand Down Expand Up @@ -199,6 +199,12 @@ class Settings(private val symphony: Symphony) {
PlaylistRepository.SortBy.CUSTOM,
)
val lastUsedPlaylistsSortReverse = BooleanEntry("last_used_playlists_sort_reverse", false)
val lastUsedFoldersSortBy = EnumEntry(
"last_used_folders_sort_by",
enumEntries<MediaTreeRepository.SortBy>(),
MediaTreeRepository.SortBy.TITLE,
)
val lastUsedFoldersSortReverse = BooleanEntry("last_used_folders_sort_reverse", false)
val lastUsedPlaylistsHorizontalGridColumns = IntEntry(
"last_used_playlists_horizontal_grid_columns",
ResponsiveGridColumns.DEFAULT_HORIZONTAL_COLUMNS,
Expand All @@ -220,18 +226,18 @@ class Settings(private val symphony: Symphony) {
SongRepository.SortBy.TRACK_NUMBER,
)
val lastUsedAlbumSongsSortReverse = BooleanEntry("last_used_album_songs_sort_reverse", false)
val lastUsedArtistSongsSortBy = EnumEntry(
"last_used_artist_songs_sort_by",
enumEntries<SongRepository.SortBy>(),
SongRepository.SortBy.YEAR,
)
val lastUsedArtistSongsSortReverse = BooleanEntry("last_used_artist_songs_sort_reverse", false)
val lastUsedTreePathSortBy = EnumEntry(
"last_used_tree_path_sort_by",
enumEntries<StringListUtils.SortBy>(),
StringListUtils.SortBy.NAME,
)
val lastUsedTreePathSortReverse = BooleanEntry("last_used_tree_path_sort_reverse", false)
val lastUsedFoldersSortBy = EnumEntry(
"last_used_folders_sort_by",
enumEntries<StringListUtils.SortBy>(),
StringListUtils.SortBy.NAME,
)
val lastUsedFoldersSortReverse = BooleanEntry("last_used_folders_sort_reverse", false)
val lastUsedFoldersHorizontalGridColumns = IntEntry(
"last_used_folders_horizontal_grid_columns",
ResponsiveGridColumns.DEFAULT_HORIZONTAL_COLUMNS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,10 @@ package io.github.zyrouge.symphony.services.database.store
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.RawQuery
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import io.github.zyrouge.symphony.services.groove.entities.AlbumArtistMapping
import io.github.zyrouge.symphony.services.groove.entities.Artist
import io.github.zyrouge.symphony.services.groove.entities.ArtistSongMapping
import io.github.zyrouge.symphony.services.groove.repositories.AlbumArtistRepository
import kotlinx.coroutines.flow.Flow

@Dao
interface AlbumArtistMappingStore {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(vararg entities: AlbumArtistMapping)

@RawQuery(observedEntities = [AlbumArtistMapping::class, Artist::class, ArtistSongMapping::class])
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<Artist.AlongAttributes>>
}

fun AlbumArtistMappingStore.valuesAsFlow(
sortBy: AlbumArtistRepository.SortBy,
sortReverse: Boolean,
): Flow<List<Artist.AlongAttributes>> {
val orderBy = when (sortBy) {
AlbumArtistRepository.SortBy.CUSTOM -> "${Artist.TABLE}.${Artist.COLUMN_ID}"
AlbumArtistRepository.SortBy.ARTIST_NAME -> "${Artist.TABLE}.${Artist.COLUMN_NAME}"
AlbumArtistRepository.SortBy.TRACKS_COUNT -> Artist.AlongAttributes.EMBEDDED_TRACKS_COUNT
AlbumArtistRepository.SortBy.ALBUMS_COUNT -> Artist.AlongAttributes.EMBEDDED_ALBUMS_COUNT
}
val orderDirection = if (sortReverse) "DESC" else "ASC"
val query = "SELECT ${Artist.TABLE}.*, " +
"COUNT(${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_SONG_ID}) as ${Artist.AlongAttributes.EMBEDDED_TRACKS_COUNT}, " +
"COUNT(${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID}) as ${Artist.AlongAttributes.EMBEDDED_ALBUMS_COUNT} " +
"FROM ${Artist.TABLE} " +
"LEFT JOIN ${ArtistSongMapping.TABLE} ON ${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"WHERE ${AlbumArtistMapping.COLUMN_IS_ALBUM_ARTIST} = 1 " +
"ORDER BY $orderBy $orderDirection"
return valuesAsFlowRaw(SimpleSQLiteQuery(query))
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,30 @@ interface AlbumStore {
@Query("SELECT * FROM ${Album.TABLE} WHERE ${Album.COLUMN_NAME} = :name LIMIT 1")
fun findByName(name: String): Album?

@Query("SELECT * FROM ${Album.TABLE} WHERE ${Album.COLUMN_ID} = :id LIMIT 1")
fun findByIdAsFlow(id: String): Flow<Album?>
@RawQuery(observedEntities = [Album::class, AlbumArtistMapping::class, AlbumSongMapping::class])
fun findByIdAsFlowRaw(query: SupportSQLiteQuery): Flow<Album.AlongAttributes?>

@RawQuery(observedEntities = [Album::class, AlbumArtistMapping::class, AlbumSongMapping::class])
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<Album.AlongAttributes>>
}

fun AlbumStore.findByIdAsFlow(id: String): Flow<Album.AlongAttributes?> {
val query = "SELECT ${Album.TABLE}.*, " +
"COUNT(${AlbumSongMapping.TABLE}.${AlbumSongMapping.COLUMN_SONG_ID}) as ${Album.AlongAttributes.EMBEDDED_TRACKS_COUNT}, " +
"COUNT(${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID}) as ${Album.AlongAttributes.EMBEDDED_ARTISTS_COUNT} " +
"FROM ${Album.TABLE} " +
"LEFT JOIN ${AlbumSongMapping.TABLE} ON ${AlbumSongMapping.TABLE}.${AlbumSongMapping.COLUMN_ALBUM_ID} = ${Album.TABLE}.${Album.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ${Album.TABLE}.${Album.COLUMN_ID} " +
"WHERE ${Album.COLUMN_ID} = ? " +
"LIMIT 1"
val args = arrayOf(id)
return findByIdAsFlowRaw(SimpleSQLiteQuery(query, args))
}

fun AlbumStore.valuesAsFlow(
sortBy: AlbumRepository.SortBy,
sortReverse: Boolean,
artistId: String? = null,
): Flow<List<Album.AlongAttributes>> {
val aliasFirstArtist = "firstArtist"
val embeddedArtistName = "firstArtistName"
Expand All @@ -52,14 +66,17 @@ fun AlbumStore.valuesAsFlow(
"FROM ${AlbumArtistMapping.TABLE} " +
"WHERE ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ${Album.COLUMN_ID} " +
"ORDER BY ${AlbumArtistMapping.COLUMN_IS_ALBUM_ARTIST} DESC"
val albumArtistMappingJoin = "" +
(if (artistId != null) "${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID} = ? " else "") +
"${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ${Album.TABLE}.${Album.COLUMN_ID}"
val query = "SELECT ${Album.TABLE}.*, " +
"COUNT(${AlbumSongMapping.TABLE}.${AlbumSongMapping.COLUMN_SONG_ID}) as ${Album.AlongAttributes.EMBEDDED_TRACKS_COUNT}, " +
"COUNT(${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID}) as ${Album.AlongAttributes.EMBEDDED_ARTISTS_COUNT}, " +
"$aliasFirstArtist.${Artist.COLUMN_NAME} as $embeddedArtistName" +
"FROM ${Album.TABLE} " +
"LEFT JOIN ${AlbumSongMapping.TABLE} ON ${AlbumSongMapping.TABLE}.${AlbumSongMapping.COLUMN_ALBUM_ID} = ${Album.TABLE}.${Album.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ${Album.TABLE}.${Album.COLUMN_ID} " +
"LEFT JOIN ${Artist.TABLE} $aliasFirstArtist ON ${Artist.TABLE}.${Artist.COLUMN_ID} = ($artistQuery)" +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON $albumArtistMappingJoin " +
"LEFT JOIN ${Artist.TABLE} $aliasFirstArtist ON ${Artist.TABLE}.${Artist.COLUMN_ID} = ($artistQuery) " +
"ORDER BY $orderBy $orderDirection"
return valuesAsFlowRaw(SimpleSQLiteQuery(query))
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.room.Update
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import io.github.zyrouge.symphony.services.groove.entities.AlbumArtistMapping
import io.github.zyrouge.symphony.services.groove.entities.AlbumSongMapping
import io.github.zyrouge.symphony.services.groove.entities.Artist
import io.github.zyrouge.symphony.services.groove.entities.ArtistSongMapping
import io.github.zyrouge.symphony.services.groove.repositories.ArtistRepository
Expand All @@ -22,6 +23,9 @@ interface ArtistStore {
@Update
suspend fun update(vararg entities: Artist): Int

@RawQuery(observedEntities = [Artist::class, ArtistSongMapping::class, AlbumArtistMapping::class])
fun findByIdAsFlowRaw(query: SupportSQLiteQuery): Flow<Artist.AlongAttributes?>

@RawQuery(observedEntities = [Artist::class, ArtistSongMapping::class, AlbumArtistMapping::class])
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<Artist.AlongAttributes>>

Expand All @@ -31,9 +35,24 @@ interface ArtistStore {
@MapColumn(Artist.COLUMN_ID) String>
}

fun ArtistStore.findByIdAsFlow(id: String): Flow<Artist.AlongAttributes?> {
val query = "SELECT ${Artist.TABLE}.*, " +
"COUNT(${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_SONG_ID}) as ${Artist.AlongAttributes.EMBEDDED_TRACKS_COUNT}, " +
"COUNT(${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID}) as ${Artist.AlongAttributes.EMBEDDED_ALBUMS_COUNT} " +
"FROM ${Artist.TABLE} " +
"LEFT JOIN ${AlbumSongMapping.TABLE} ON ${AlbumSongMapping.TABLE}.${AlbumSongMapping.COLUMN_ALBUM_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"WHERE ${Artist.COLUMN_ID} = ? " +
"LIMIT 1"
val args = arrayOf(id)
return findByIdAsFlowRaw(SimpleSQLiteQuery(query, args))
}

fun ArtistStore.valuesAsFlow(
sortBy: ArtistRepository.SortBy,
sortReverse: Boolean,
albumId: String? = null,
onlyAlbumArtists: Boolean = false,
): Flow<List<Artist.AlongAttributes>> {
val orderBy = when (sortBy) {
ArtistRepository.SortBy.CUSTOM -> "${Artist.TABLE}.${Artist.COLUMN_ID}"
Expand All @@ -42,12 +61,20 @@ fun ArtistStore.valuesAsFlow(
ArtistRepository.SortBy.ALBUMS_COUNT -> Artist.AlongAttributes.EMBEDDED_ALBUMS_COUNT
}
val orderDirection = if (sortReverse) "DESC" else "ASC"
val albumArtistMappingJoin = "" +
(if (albumId != null) "${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID} = ? " else "") +
(if (onlyAlbumArtists) "${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_IS_ALBUM_ARTIST} = 1 " else "") +
"${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID}"
val query = "SELECT ${Artist.TABLE}.*, " +
"COUNT(${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_SONG_ID}) as ${Artist.AlongAttributes.EMBEDDED_TRACKS_COUNT}, " +
"COUNT(${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ALBUM_ID}) as ${Artist.AlongAttributes.EMBEDDED_ALBUMS_COUNT} " +
"FROM ${Artist.TABLE} " +
"LEFT JOIN ${ArtistSongMapping.TABLE} ON ${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON ${AlbumArtistMapping.TABLE}.${AlbumArtistMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID}" +
"LEFT JOIN ${ArtistSongMapping.TABLE} ON ${ArtistSongMapping.TABLE}.${ArtistSongMapping.TABLE}.${ArtistSongMapping.COLUMN_ARTIST_ID} = ${Artist.TABLE}.${Artist.COLUMN_ID} " +
"LEFT JOIN ${AlbumArtistMapping.TABLE} ON $albumArtistMappingJoin " +
"ORDER BY $orderBy $orderDirection"
return valuesAsFlowRaw(SimpleSQLiteQuery(query))
val args = mutableListOf<Any>()
if (albumId != null) {
args.add(albumId)
}
return valuesAsFlowRaw(SimpleSQLiteQuery(query, args.toTypedArray()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ interface GenreStore {
@Insert
suspend fun insert(vararg entities: Genre): List<String>

@RawQuery(observedEntities = [Genre::class, GenreSongMapping::class])
fun findByIdAsFlowRaw(query: SupportSQLiteQuery): Flow<Genre.AlongAttributes?>

@RawQuery(observedEntities = [Genre::class, GenreSongMapping::class])
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<Genre.AlongAttributes>>

Expand All @@ -26,6 +29,18 @@ interface GenreStore {
@MapColumn(Genre.COLUMN_ID) String>
}

fun GenreStore.findByIdAsFlow(id: String): Flow<Genre.AlongAttributes?> {
val query = "SELECT ${Genre.TABLE}.*, " +
"COUNT(${GenreSongMapping.TABLE}.${GenreSongMapping.COLUMN_SONG_ID}) as ${Genre.AlongAttributes.EMBEDDED_TRACKS_COUNT} " +
"FROM ${Genre.TABLE} " +
"LEFT JOIN ${GenreSongMapping.TABLE} ON ${GenreSongMapping.TABLE}.${GenreSongMapping.COLUMN_GENRE_ID} = ${Genre.TABLE}.${Genre.COLUMN_ID} " +
"LEFT JOIN ${GenreSongMapping.TABLE} ON ${GenreSongMapping.TABLE}.${GenreSongMapping.COLUMN_GENRE_ID} = ${Genre.TABLE}.${Genre.COLUMN_ID} " +
"WHERE ${Genre.COLUMN_ID} = ? " +
"LIMIT 1"
val args = arrayOf(id)
return findByIdAsFlowRaw(SimpleSQLiteQuery(query, args))
}

fun GenreStore.valuesAsFlow(
sortBy: GenreRepository.SortBy,
sortReverse: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.MapColumn
import androidx.room.Query
import androidx.room.RawQuery
import androidx.room.Update
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import io.github.zyrouge.symphony.services.groove.entities.MediaTreeFolder
import io.github.zyrouge.symphony.services.groove.entities.MediaTreeSongFile
import io.github.zyrouge.symphony.services.groove.repositories.MediaTreeRepository
import kotlinx.coroutines.flow.Flow

@Dao
interface MediaTreeFolderStore {
@Insert()
@Insert
suspend fun insert(vararg entities: MediaTreeFolder): List<String>

@Update()
@Update
suspend fun update(vararg entities: MediaTreeFolder): Int

@Query("SELECT id FROM ${MediaTreeFolder.TABLE} WHERE ${MediaTreeFolder.COLUMN_PARENT_ID} = :parentId")
Expand All @@ -26,4 +32,26 @@ interface MediaTreeFolderStore {

@Query("SELECT * FROM ${MediaTreeFolder.TABLE} WHERE ${MediaTreeFolder.COLUMN_PARENT_ID} = :parentId")
fun entriesNameMapped(parentId: String): Map<@MapColumn(MediaTreeFolder.COLUMN_NAME) String, MediaTreeFolder>

@RawQuery(observedEntities = [MediaTreeFolder::class, MediaTreeSongFile::class])
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<MediaTreeFolder.AlongAttributes>>
}

fun MediaTreeFolderStore.valuesAsFlow(
sortBy: MediaTreeRepository.SortBy,
sortReverse: Boolean,
): Flow<List<MediaTreeFolder.AlongAttributes>> {
val orderBy = when (sortBy) {
MediaTreeRepository.SortBy.CUSTOM -> "${MediaTreeFolder.TABLE}.${MediaTreeFolder.COLUMN_ID}"
MediaTreeRepository.SortBy.TITLE -> "${MediaTreeFolder.TABLE}.${MediaTreeFolder.COLUMN_NAME}"
MediaTreeRepository.SortBy.TRACKS_COUNT -> MediaTreeFolder.AlongAttributes.EMBEDDED_TRACKS_COUNT
}
val orderDirection = if (sortReverse) "DESC" else "ASC"
val query = "SELECT ${MediaTreeFolder.TABLE}.*, " +
"COUNT(${MediaTreeSongFile.TABLE}.${MediaTreeSongFile.COLUMN_ID}) as ${MediaTreeFolder.AlongAttributes.EMBEDDED_TRACKS_COUNT} " +
"FROM ${MediaTreeFolder.TABLE} " +
"LEFT JOIN ${MediaTreeSongFile.TABLE} ON ${MediaTreeSongFile.TABLE}.${MediaTreeSongFile.COLUMN_PARENT_ID} = ${MediaTreeFolder.TABLE}.${MediaTreeFolder.COLUMN_ID} " +
"WHERE ${MediaTreeFolder.AlongAttributes.EMBEDDED_TRACKS_COUNT} > 0 " +
"ORDER BY $orderBy $orderDirection"
return valuesAsFlowRaw(SimpleSQLiteQuery(query))
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import io.github.zyrouge.symphony.services.groove.entities.MediaTreeLyricFile

@Dao
interface MediaTreeLyricFileStore {
@Insert()
@Insert
suspend fun insert(vararg entities: MediaTreeLyricFile): List<String>

@Update()
@Update
suspend fun update(vararg entities: MediaTreeLyricFile): Int

@Query("SELECT id FROM ${MediaTreeLyricFile.TABLE} WHERE ${MediaTreeLyricFile.COLUMN_PARENT_ID} = :parentId")
Expand Down
Loading

0 comments on commit 8d90131

Please sign in to comment.