Add SongAlbumMap

This commit is contained in:
vfsfitvnm
2022-06-30 13:49:05 +02:00
parent 58f821534e
commit d07d3f23b2
8 changed files with 810 additions and 49 deletions

View File

@@ -37,6 +37,9 @@ interface Database {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(info: SongInPlaylist): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(songAlbumMap: SongAlbumMap): Long
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insert(info: List<Artist>): List<Long>
@@ -55,6 +58,9 @@ interface Database {
@Query("SELECT * FROM Artist WHERE id = :id")
fun artist(id: String): Flow<Artist?>
@Query("SELECT * FROM Album WHERE id = :id")
fun album(id: String): Flow<Album?>
@Transaction
@Query("SELECT * FROM Song WHERE id = :id")
fun songWithInfo(id: String): DetailedSong?
@@ -102,6 +108,9 @@ interface Database {
@Update
fun update(artist: Artist)
@Update
fun update(album: Album)
@Update
fun update(songInPlaylist: SongInPlaylist)
@@ -132,6 +141,11 @@ interface Database {
@RewriteQueriesToDropUnusedColumns
fun artistSongs(artistId: String): Flow<List<DetailedSong>>
// @Transaction
// @Query("SELECT * FROM Song JOIN SongArtistMap ON Song.id = SongArtistMap.songId WHERE SongArtistMap.artistId = :artistId ORDER BY Song.ROWID DESC")
// @RewriteQueriesToDropUnusedColumns
// fun albumSongs(albumId: String): Flow<List<DetailedSong>>
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insertQueue(queuedMediaItems: List<QueuedMediaItem>)
@@ -150,6 +164,7 @@ interface Database {
Artist::class,
SongArtistMap::class,
Album::class,
SongAlbumMap::class,
SearchQuery::class,
QueuedMediaItem::class,
],
@@ -167,7 +182,6 @@ interface Database {
AutoMigration(from = 6, to = 7),
AutoMigration(from = 7, to = 8, spec = DatabaseInitializer.From7To8Migration::class),
AutoMigration(from = 9, to = 10),
AutoMigration(from = 10, to = 11),
],
)
@TypeConverters(Converters::class)
@@ -182,7 +196,8 @@ abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
if (!::Instance.isInitialized) {
Instance = Room
.databaseBuilder(this@Context, DatabaseInitializer::class.java, "data.db")
.addMigrations(From8To9Migration())
// .addMigrations(From8To9Migration())
.addMigrations(From8To9Migration(), From10To11Migration())
.build()
}
}
@@ -232,6 +247,25 @@ abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
it.execSQL("DROP TABLE SongWithAuthors;")
}
}
class From10To11Migration : Migration(10, 11) {
override fun migrate(it: SupportSQLiteDatabase) {
it.query(SimpleSQLiteQuery("SELECT id, albumId FROM Song;")).use { cursor ->
val songAlbumMapValues = ContentValues(2)
while (cursor.moveToNext()) {
songAlbumMapValues.put("songId", cursor.getString(0))
songAlbumMapValues.put("albumId", cursor.getString(1))
it.insert("SongAlbumMap", CONFLICT_IGNORE, songAlbumMapValues)
}
}
it.execSQL("CREATE TABLE IF NOT EXISTS `Song_new` (`id` TEXT NOT NULL, `title` TEXT NOT NULL, `artistsText` TEXT, `durationText` TEXT NOT NULL, `thumbnailUrl` TEXT, `lyrics` TEXT, `likedAt` INTEGER, `totalPlayTimeMs` INTEGER NOT NULL, `loudnessDb` REAL, `contentLength` INTEGER, PRIMARY KEY(`id`))")
it.execSQL("INSERT INTO Song_new(id, title, artistsText, durationText, thumbnailUrl, lyrics, likedAt, totalPlayTimeMs, loudnessDb, contentLength) SELECT id, title, artistsText, durationText, thumbnailUrl, lyrics, likedAt, totalPlayTimeMs, loudnessDb, contentLength FROM Song;")
it.execSQL("DROP TABLE Song;")
it.execSQL("ALTER TABLE Song_new RENAME TO Song;")
}
}
}
@TypeConverters

View File

@@ -6,8 +6,9 @@ import androidx.room.PrimaryKey
@Entity
data class Album(
@PrimaryKey val id: String,
val title: String,
val title: String?,
val thumbnailUrl: String?,
val year: String?,
val authorsText: String?
val authorsText: String?,
val shareUrl: String?
)

View File

@@ -8,14 +8,15 @@ import androidx.room.Relation
data class DetailedSong(
@Embedded val song: Song,
@Relation(
entity = Album::class,
parentColumn = "albumId",
entityColumn = "id"
) val album: Album?,
entity = SongAlbumMap::class,
entityColumn = "songId",
parentColumn = "id"
)
val albumId: String?,
@Relation(
entity = Artist::class,
parentColumn = "id",
entityColumn = "id",
parentColumn = "id",
associateBy = Junction(
value = SongArtistMap::class,
parentColumn = "songId",

View File

@@ -3,20 +3,10 @@ package it.vfsfitvnm.vimusic.models
import androidx.room.*
@Entity(
foreignKeys = [
ForeignKey(
entity = Album::class,
parentColumns = ["id"],
childColumns = ["albumId"],
onDelete = ForeignKey.SET_NULL
),
]
)
@Entity
data class Song(
@PrimaryKey val id: String,
val title: String,
val albumId: String?,
val artistsText: String? = null,
val durationText: String,
val thumbnailUrl: String?,

View File

@@ -0,0 +1,31 @@
package it.vfsfitvnm.vimusic.models
import androidx.compose.runtime.Immutable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
@Immutable
@Entity(
primaryKeys = ["songId", "albumId"],
foreignKeys = [
ForeignKey(
entity = Song::class,
parentColumns = ["id"],
childColumns = ["songId"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = Album::class,
parentColumns = ["id"],
childColumns = ["albumId"],
onDelete = ForeignKey.CASCADE
)
]
)
data class SongAlbumMap(
@ColumnInfo(index = true) val songId: String,
@ColumnInfo(index = true) val albumId: String,
val position: Int?
)