Skip to content

Commit

Permalink
Remove "NOT IN" operator from repository commit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Oct 11, 2024
1 parent f0009a6 commit 37d5425
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class AppRepository(
) {
private suspend fun commitApps(apps: Collection<App>) = withContext(Dispatchers.IO) {
database.transaction {
// Remove missing apps from database
val currentIds = apps.map { it.id }
database.apps.removeNotIn(currentIds)

// Upsert apps
// Remove apps found in database but not in committed list
database.apps.getAll()
.executeAsList()
.map { it.id }
.subtract(apps.map { it.id }.toSet())
.map { id -> database.apps.removeById(id) }

// Upsert all found
apps.map { app -> commitApp(app) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class ChannelRepository(
) {
private suspend fun commitChannels(type: ChannelType, channels: Collection<Channel>) = withContext(Dispatchers.IO) {
database.transaction {
// Remove missing channels from database
val currentIds = channels.map { it.id }
database.channels.removeNotIn(type, currentIds)
// Remove channels found in database but not in committed list
database.channels.getByType(type)
.executeAsList()
.map { it.id }
.subtract(channels.map { it.id }.toSet())
.map { id -> database.channels.removeById(id) }

// Upsert channels
channels.map { channel -> commitChannel(channel) }
}
}


private suspend fun commitChannel(channel: Channel) = withContext(Dispatchers.IO) {
database.channels.upsert(
id = channel.id,
Expand All @@ -44,9 +46,12 @@ class ChannelRepository(
programs: Collection<ChannelProgram>,
) = withContext(Dispatchers.IO) {
database.transaction {
// Remove missing channels from database
val currentIds = programs.map { it.id }
database.channelPrograms.removeNotIn(channelId, currentIds)
// Remove channels found in database but not in committed list
database.channelPrograms.getByChannel(channelId)
.executeAsList()
.map { it.id }
.subtract(programs.map { it.id }.toSet())
.map { id -> database.channelPrograms.removeById(id) }

// Upsert channels
programs.map { program -> commitChannelProgram(program) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class InputRepository(
) {
private suspend fun commitInputs(inputs: Collection<Input>) = withContext(Dispatchers.IO) {
database.transaction {
// Remove missing inputs from database
val currentIds = inputs.map { it.id }
database.inputs.removeNotIn(currentIds)
// Remove inputs found in database but not in committed list
database.inputs.getAll()
.executeAsList()
.map { it.id }
.subtract(inputs.map { it.id }.toSet())
.map { id -> database.inputs.removeById(id) }

// Upsert inputs
inputs.map { input -> commitInput(input) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ updateFavoriteOrder {
UPDATE App SET favoriteOrder = :order WHERE id = :id;
}

removeById:
DELETE FROM App WHERE id = :id;

removeByPackageName:
DELETE FROM App WHERE packageName = :packageName;

removeNotIn:
DELETE FROM App WHERE id NOT IN :ids;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ SELECT * FROM Channel WHERE (SELECT favoriteOrder FROM App WHERE App.packageName
getById:
SELECT * FROM Channel WHERE id = :id LIMIT 1;

getByType:
SELECT * FROM Channel WHERE type = :type;

upsert {
UPDATE Channel SET
type = :type,
Expand Down Expand Up @@ -52,5 +55,8 @@ upsert {
);
}

removeNotIn:
DELETE FROM Channel WHERE type = :type AND id NOT IN :ids;
removeById:
DELETE FROM Channel WHERE id = :id;

removeByType:
DELETE FROM Channel WHERE type = :type;
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,8 @@ upsert {
);
}

removeNotIn:
DELETE FROM ChannelProgram WHERE channelId = :channelId AND id NOT IN :ids;
removeById:
DELETE FROM ChannelProgram WHERE id = :id;

removeByChannelId:
DELETE FROM ChannelProgram WHERE channelId = :channelId;
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ upsert {
);
}

removeById:
DELETE FROM Input WHERE id = :id;

removeByPackageName:
DELETE FROM Input WHERE packageName = :packageName;

removeNotIn:
DELETE FROM Input WHERE id NOT IN :ids;

0 comments on commit 37d5425

Please sign in to comment.