@@ -2,7 +2,6 @@ package io.github.zyrouge.symphony.services.database.store
22
33import androidx.room.Dao
44import androidx.room.Insert
5- import androidx.room.Query
65import androidx.room.RawQuery
76import androidx.room.Update
87import androidx.sqlite.db.SimpleSQLiteQuery
@@ -15,68 +14,74 @@ import io.github.zyrouge.symphony.services.groove.repositories.AlbumRepository
1514import kotlinx.coroutines.flow.Flow
1615
1716@Dao
18- interface AlbumStore {
17+ abstract class AlbumStore {
1918 @Insert
20- suspend fun insert (vararg entities : Album ): List <String >
19+ abstract suspend fun insert (vararg entities : Album ): List <String >
2120
2221 @Update
23- suspend fun update (vararg entities : Album ): Int
22+ abstract suspend fun update (vararg entities : Album ): Int
2423
25- @Query( " SELECT * FROM ${ Album . TABLE } WHERE ${ Album . COLUMN_NAME } = :name LIMIT 1 " )
26- fun findByName (name : String ): Album ?
24+ @RawQuery
25+ protected abstract fun findByName (query : SimpleSQLiteQuery ): Album ?
2726
28- @RawQuery(observedEntities = [Album ::class , AlbumArtistMapping ::class , AlbumSongMapping ::class ])
29- fun findByIdAsFlowRaw (query : SupportSQLiteQuery ): Flow <Album .AlongAttributes ?>
27+ fun findByName (name : String ): Album ? {
28+ val query = " SELECT * FROM ${Album .TABLE } WHERE ${Album .COLUMN_NAME } = ? LIMIT 1"
29+ val args = arrayOf(name)
30+ return findByName(SimpleSQLiteQuery (query, args))
31+ }
3032
3133 @RawQuery(observedEntities = [Album ::class , AlbumArtistMapping ::class , AlbumSongMapping ::class ])
32- fun valuesAsFlowRaw (query : SupportSQLiteQuery ): Flow <List <Album .AlongAttributes >>
33- }
34+ protected abstract fun findByIdAsFlow (query : SupportSQLiteQuery ): Flow <Album .AlongAttributes ?>
3435
35- fun AlbumStore.findByIdAsFlow (id : String ): Flow <Album .AlongAttributes ?> {
36- val query = " SELECT ${Album .TABLE } .*, " +
37- " COUNT(${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_SONG_ID } ) as ${Album .AlongAttributes .EMBEDDED_TRACKS_COUNT } , " +
38- " COUNT(${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } ) as ${Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT } " +
39- " FROM ${Album .TABLE } " +
40- " LEFT JOIN ${AlbumSongMapping .TABLE } ON ${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
41- " LEFT JOIN ${AlbumArtistMapping .TABLE } ON ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
42- " WHERE ${Album .COLUMN_ID } = ? " +
43- " LIMIT 1"
44- val args = arrayOf(id)
45- return findByIdAsFlowRaw(SimpleSQLiteQuery (query, args))
46- }
36+ fun findByIdAsFlow (id : String ): Flow <Album .AlongAttributes ?> {
37+ val query = " SELECT ${Album .TABLE } .*, " +
38+ " COUNT(${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_SONG_ID } ) as ${Album .AlongAttributes .EMBEDDED_TRACKS_COUNT } , " +
39+ " COUNT(${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } ) as ${Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT } " +
40+ " FROM ${Album .TABLE } " +
41+ " LEFT JOIN ${AlbumSongMapping .TABLE } ON ${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
42+ " LEFT JOIN ${AlbumArtistMapping .TABLE } ON ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
43+ " WHERE ${Album .COLUMN_ID } = ? " +
44+ " LIMIT 1"
45+ val args = arrayOf(id)
46+ return findByIdAsFlow(SimpleSQLiteQuery (query, args))
47+ }
48+
49+ @RawQuery(observedEntities = [Album ::class , AlbumArtistMapping ::class , AlbumSongMapping ::class ])
50+ protected abstract fun valuesAsFlow (query : SupportSQLiteQuery ): Flow <List <Album .AlongAttributes >>
4751
48- fun AlbumStore.valuesAsFlow (
49- sortBy : AlbumRepository .SortBy ,
50- sortReverse : Boolean ,
51- artistId : String? = null,
52- ): Flow <List <Album .AlongAttributes >> {
53- val aliasFirstArtist = " firstArtist"
54- val embeddedArtistName = " firstArtistName"
55- val orderBy = when (sortBy) {
56- AlbumRepository .SortBy .CUSTOM -> " ${Album .TABLE } .${Album .COLUMN_ID } "
57- AlbumRepository .SortBy .ALBUM_NAME -> " ${Album .TABLE } .${Album .COLUMN_NAME } "
58- AlbumRepository .SortBy .ARTIST_NAME -> embeddedArtistName
59- AlbumRepository .SortBy .YEAR -> " ${Album .TABLE } .${Album .COLUMN_START_YEAR } "
60- AlbumRepository .SortBy .TRACKS_COUNT -> Album .AlongAttributes .EMBEDDED_TRACKS_COUNT
61- AlbumRepository .SortBy .ARTISTS_COUNT -> Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT
52+ fun valuesAsFlow (
53+ sortBy : AlbumRepository .SortBy ,
54+ sortReverse : Boolean ,
55+ artistId : String? = null,
56+ ): Flow <List <Album .AlongAttributes >> {
57+ val aliasFirstArtist = " firstArtist"
58+ val embeddedArtistName = " firstArtistName"
59+ val orderBy = when (sortBy) {
60+ AlbumRepository .SortBy .CUSTOM -> " ${Album .TABLE } .${Album .COLUMN_ID } "
61+ AlbumRepository .SortBy .ALBUM_NAME -> " ${Album .TABLE } .${Album .COLUMN_NAME } "
62+ AlbumRepository .SortBy .ARTIST_NAME -> embeddedArtistName
63+ AlbumRepository .SortBy .YEAR -> " ${Album .TABLE } .${Album .COLUMN_START_YEAR } "
64+ AlbumRepository .SortBy .TRACKS_COUNT -> Album .AlongAttributes .EMBEDDED_TRACKS_COUNT
65+ AlbumRepository .SortBy .ARTISTS_COUNT -> Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT
66+ }
67+ val orderDirection = if (sortReverse) " DESC" else " ASC"
68+ val artistQuery = " SELECT" +
69+ " TOP 1 ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } " +
70+ " FROM ${AlbumArtistMapping .TABLE } " +
71+ " WHERE ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .COLUMN_ID } " +
72+ " ORDER BY ${AlbumArtistMapping .COLUMN_IS_ALBUM_ARTIST } DESC"
73+ val albumArtistMappingJoin = " " +
74+ (if (artistId != null ) " ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } = ? " else " " ) +
75+ " ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } "
76+ val query = " SELECT ${Album .TABLE } .*, " +
77+ " COUNT(${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_SONG_ID } ) as ${Album .AlongAttributes .EMBEDDED_TRACKS_COUNT } , " +
78+ " COUNT(${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } ) as ${Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT } , " +
79+ " $aliasFirstArtist .${Artist .COLUMN_NAME } as $embeddedArtistName " +
80+ " FROM ${Album .TABLE } " +
81+ " LEFT JOIN ${AlbumSongMapping .TABLE } ON ${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
82+ " LEFT JOIN ${AlbumArtistMapping .TABLE } ON $albumArtistMappingJoin " +
83+ " LEFT JOIN ${Artist .TABLE } $aliasFirstArtist ON ${Artist .TABLE } .${Artist .COLUMN_ID } = ($artistQuery ) " +
84+ " ORDER BY $orderBy $orderDirection "
85+ return valuesAsFlow(SimpleSQLiteQuery (query))
6286 }
63- val orderDirection = if (sortReverse) " DESC" else " ASC"
64- val artistQuery = " SELECT" +
65- " TOP 1 ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } " +
66- " FROM ${AlbumArtistMapping .TABLE } " +
67- " WHERE ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .COLUMN_ID } " +
68- " ORDER BY ${AlbumArtistMapping .COLUMN_IS_ALBUM_ARTIST } DESC"
69- val albumArtistMappingJoin = " " +
70- (if (artistId != null ) " ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } = ? " else " " ) +
71- " ${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } "
72- val query = " SELECT ${Album .TABLE } .*, " +
73- " COUNT(${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_SONG_ID } ) as ${Album .AlongAttributes .EMBEDDED_TRACKS_COUNT } , " +
74- " COUNT(${AlbumArtistMapping .TABLE } .${AlbumArtistMapping .COLUMN_ARTIST_ID } ) as ${Album .AlongAttributes .EMBEDDED_ARTISTS_COUNT } , " +
75- " $aliasFirstArtist .${Artist .COLUMN_NAME } as $embeddedArtistName " +
76- " FROM ${Album .TABLE } " +
77- " LEFT JOIN ${AlbumSongMapping .TABLE } ON ${AlbumSongMapping .TABLE } .${AlbumSongMapping .COLUMN_ALBUM_ID } = ${Album .TABLE } .${Album .COLUMN_ID } " +
78- " LEFT JOIN ${AlbumArtistMapping .TABLE } ON $albumArtistMappingJoin " +
79- " LEFT JOIN ${Artist .TABLE } $aliasFirstArtist ON ${Artist .TABLE } .${Artist .COLUMN_ID } = ($artistQuery ) " +
80- " ORDER BY $orderBy $orderDirection "
81- return valuesAsFlowRaw(SimpleSQLiteQuery (query))
8287}
0 commit comments