-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from c0de-wizard/database-cleanup
Make Databse IDs Great
- Loading branch information
Showing
83 changed files
with
925 additions
and
802 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core/database/src/commonMain/kotlin/com.thomaskioko.tvmaniac.db/DatabaseIdsExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.thomaskioko.tvmaniac.db | ||
|
||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
value class CategoryId(val id: Long) | ||
|
||
@JvmInline | ||
value class EpisodeId(val traktId: Long) | ||
|
||
@JvmInline | ||
value class EpisodeImageId(val traktId: Long) | ||
|
||
@JvmInline | ||
value class SeasonId(val id: Long) | ||
|
||
@JvmInline | ||
value class ShowId(val traktId: Long) | ||
|
||
@JvmInline | ||
value class SimilarShowId(val id: Long) | ||
|
||
@JvmInline | ||
value class Id<out T>(val id: Long) |
8 changes: 8 additions & 0 deletions
8
core/database/src/commonMain/kotlin/com.thomaskioko.tvmaniac.db/IdAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.thomaskioko.tvmaniac.db | ||
|
||
import app.cash.sqldelight.ColumnAdapter | ||
|
||
internal class IdAdapter<T> : ColumnAdapter<Id<T>, Long> { | ||
override fun decode(databaseValue: Long): Id<T> = Id(id = databaseValue) | ||
override fun encode(value: Id<T>): Long = value.id | ||
} |
30 changes: 0 additions & 30 deletions
30
core/database/src/commonMain/sqldelight/com/thomaskioko/tvmaniac/core/db/SeasonEpisodes.sq
This file was deleted.
Oops, something went wrong.
45 changes: 26 additions & 19 deletions
45
core/database/src/commonMain/sqldelight/com/thomaskioko/tvmaniac/core/db/episode_image.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
import com.thomaskioko.tvmaniac.db.EpisodeId; | ||
import com.thomaskioko.tvmaniac.db.EpisodeImageId; | ||
import com.thomaskioko.tvmaniac.db.Id; | ||
|
||
CREATE TABLE episode_image ( | ||
trakt_id INTEGER NOT NULL PRIMARY KEY, | ||
tmdb_id INTEGER NOT NULL, | ||
image_url TEXT DEFAULT NULL, | ||
FOREIGN KEY (trakt_id) REFERENCES episodes(trakt_id) ON DELETE CASCADE | ||
id INTEGER AS Id<EpisodeId> NOT NULL PRIMARY KEY, | ||
tmdb_id INTEGER AS Id<EpisodeImageId>, | ||
image_url TEXT DEFAULT NULL, | ||
FOREIGN KEY(id) REFERENCES episode(id) ON DELETE CASCADE, | ||
UNIQUE(id) | ||
); | ||
|
||
insertOrReplace: | ||
INSERT OR REPLACE INTO episode_image( | ||
trakt_id, | ||
tmdb_id, | ||
image_url | ||
id, | ||
tmdb_id, | ||
image_url | ||
) | ||
VALUES(?,?,?); | ||
|
||
episodeImage: | ||
SELECT show.tmdb_id, seasons.season_number, episodes.trakt_id, episodes.episode_number | ||
FROM episodes | ||
LEFT JOIN seasons ON seasons.id = episodes.season_id | ||
LEFT JOIN show ON show.trakt_id = seasons.show_trakt_id | ||
LEFT OUTER JOIN episode_image ON episode_image.trakt_id = episodes.trakt_id | ||
WHERE episode_image.image_url IS NULL; | ||
|
||
delete: | ||
DELETE FROM episode_image WHERE trakt_id = ?; | ||
|
||
deleteAll: | ||
DELETE FROM episode_image; | ||
SELECT | ||
show.tmdb_id, | ||
season.season_number, | ||
episode.episode_number | ||
FROM | ||
episode | ||
LEFT JOIN | ||
season ON season.id = episode.season_id | ||
LEFT JOIN | ||
show ON show.id = season.show_id | ||
LEFT OUTER JOIN | ||
episode_image ON episode_image.id = episode.id | ||
WHERE | ||
episode_image.image_url IS NULL; |
64 changes: 36 additions & 28 deletions
64
core/database/src/commonMain/sqldelight/com/thomaskioko/tvmaniac/core/db/episodes.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
CREATE TABLE episodes ( | ||
trakt_id INTEGER NOT NULL PRIMARY KEY, | ||
season_id INTEGER NOT NULL, | ||
tmdb_id INTEGER, | ||
title TEXT NOT NULL, | ||
overview TEXT NOT NULL, | ||
ratings REAL NOT NULL, | ||
runtime INTEGER NOT NULL, | ||
votes INTEGER NOT NULL, | ||
episode_number TEXT NOT NULL, | ||
FOREIGN KEY (season_id) REFERENCES seasons(id) | ||
import com.thomaskioko.tvmaniac.db.EpisodeId; | ||
import com.thomaskioko.tvmaniac.db.Id; | ||
import com.thomaskioko.tvmaniac.db.SeasonId; | ||
|
||
CREATE TABLE episode ( | ||
id INTEGER AS Id<EpisodeId> NOT NULL PRIMARY KEY, | ||
season_id INTEGER AS Id<SeasonId> NOT NULL, | ||
tmdb_id INTEGER, | ||
title TEXT NOT NULL, | ||
overview TEXT NOT NULL, | ||
ratings REAL NOT NULL, | ||
runtime INTEGER NOT NULL, | ||
votes INTEGER NOT NULL, | ||
episode_number TEXT NOT NULL, | ||
FOREIGN KEY(season_id) REFERENCES season(id) | ||
); | ||
|
||
insertOrReplace: | ||
INSERT OR REPLACE INTO episodes( | ||
trakt_id, | ||
season_id, | ||
tmdb_id, | ||
title, | ||
overview, | ||
ratings, | ||
runtime, | ||
votes, | ||
episode_number | ||
INSERT OR REPLACE INTO episode( | ||
id, | ||
season_id, | ||
tmdb_id, | ||
title, | ||
overview, | ||
ratings, | ||
runtime, | ||
votes, | ||
episode_number | ||
) | ||
VALUES(?,?,?,?,?,?,?,?,?); | ||
|
||
episodeById: | ||
episodesById: | ||
SELECT * | ||
FROM episodes | ||
LEFT OUTER JOIN episode_image ON episode_image.trakt_id = episodes.trakt_id | ||
WHERE episodes.trakt_id = ? | ||
ORDER BY episode_number ASC ; | ||
FROM | ||
episode | ||
LEFT OUTER JOIN | ||
episode_image ON episode_image.id = episode.id | ||
WHERE | ||
episode.id = ? | ||
ORDER BY | ||
episode_number ASC; | ||
|
||
delete: | ||
DELETE FROM episodes WHERE trakt_id = ?; | ||
DELETE FROM episode WHERE id = ?; | ||
|
||
deleteAll: | ||
DELETE FROM episodes; | ||
DELETE FROM episode; | ||
|
88 changes: 66 additions & 22 deletions
88
core/database/src/commonMain/sqldelight/com/thomaskioko/tvmaniac/core/db/season.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,76 @@ | ||
CREATE TABLE seasons ( | ||
id INTEGER NOT NULL PRIMARY KEY, | ||
show_trakt_id INTEGER NOT NULL, | ||
season_number INTEGER NOT NULL, | ||
episode_count INTEGER NOT NULL, | ||
name TEXT NOT NULL, | ||
overview TEXT, | ||
FOREIGN KEY (show_trakt_id) REFERENCES show(trakt_id) | ||
import com.thomaskioko.tvmaniac.db.Id; | ||
import com.thomaskioko.tvmaniac.db.SeasonId; | ||
import com.thomaskioko.tvmaniac.db.ShowId; | ||
|
||
CREATE TABLE season ( | ||
id INTEGER AS Id<SeasonId> NOT NULL PRIMARY KEY, | ||
show_id INTEGER AS Id<ShowId> NOT NULL, | ||
season_number INTEGER NOT NULL, | ||
title TEXT NOT NULL, | ||
episode_count INTEGER NOT NULL, | ||
overview TEXT, | ||
FOREIGN KEY(show_id) REFERENCES show(id) | ||
); | ||
|
||
insertOrReplace: | ||
INSERT OR REPLACE INTO seasons( | ||
id, | ||
show_trakt_id, | ||
season_number, | ||
episode_count, | ||
name, | ||
overview | ||
INSERT OR REPLACE INTO season( | ||
id, | ||
show_id, | ||
season_number, | ||
episode_count, | ||
title, | ||
overview | ||
) | ||
VALUES(?,?,?,?,?,?); | ||
|
||
seasonById: | ||
SELECT * | ||
FROM seasons | ||
WHERE seasons.show_trakt_id = ? AND season_number != 0 | ||
ORDER BY season_number ASC; | ||
seasonsByShowId: | ||
SELECT | ||
show.id AS show_id, | ||
season.id AS season_id, | ||
season.title AS season_title, | ||
season.season_number | ||
FROM | ||
show | ||
JOIN | ||
season ON show.id = season.show_id | ||
WHERE | ||
show.id = ?; | ||
|
||
seasonEpisodeDetailsById: | ||
SELECT | ||
show.id AS show_id, | ||
show.title AS show_title, | ||
season.id AS season_id, | ||
season.title AS season_title, | ||
season.overview AS season_overview, | ||
season.season_number, | ||
season.episode_count, | ||
episode.id AS episode_id, | ||
episode.season_id AS episode_season_id, | ||
episode.title AS episode_title, | ||
episode.episode_number, | ||
episode.overview, | ||
episode.runtime, | ||
episode.ratings, | ||
episode.votes, | ||
episode_image.image_url AS episode_image_url | ||
FROM | ||
show | ||
INNER JOIN | ||
season ON season.show_id = show.id | ||
INNER JOIN | ||
episode ON episode.season_id = season.id | ||
LEFT OUTER JOIN | ||
episode_image ON episode_image.id = episode.id | ||
WHERE | ||
show.id = ? | ||
AND | ||
season_number != 0 | ||
ORDER BY | ||
season.season_number, episode_number ASC; | ||
|
||
delete: | ||
DELETE FROM seasons WHERE show_trakt_id = ?; | ||
DELETE FROM season WHERE show_id = ?; | ||
|
||
deleteAll: | ||
DELETE FROM seasons; | ||
DELETE FROM season; |
Oops, something went wrong.