db: bind named parameters directly and cover the repository dispatch

SQLite gives a repeated named parameter one index, so rawQuery can pass the
SQL through unchanged and bind the distinct names in first-appearance order
instead of rewriting them to positional placeholders.

Use named arguments for the repository dispatch, since the adjacent
descending/downloaded booleans were easy to transpose, and add a repository
test that collects the downloaded album grid. Fix the query-timing labels so
the unqualified rows measure the same default path as before.
This commit is contained in:
austinried
2026-11-03 01:03:45 +00:00
parent e8cbb93714
commit c3a5382c27
5 changed files with 158 additions and 60 deletions
@@ -186,45 +186,20 @@ internal fun rediscoverAlbumsQuery(
),
)
// RoomRawQuery binds by position, so the named fragments are rewritten once here instead of
// every caller carrying an index for each repeated :sourceId.
// RoomRawQuery takes a positional bind function, but SQLite gives a repeated named parameter one
// index, so the SQL can keep its names and only the first-appearance order needs binding.
internal fun rawQuery(
sql: String,
bindings: Map<String, Any?>,
): RoomRawQuery {
val names = mutableListOf<String>()
val positional =
buildString(sql.length) {
var index = 0
var quote: Char? = null
while (index < sql.length) {
val char = sql[index]
if (quote != null) {
append(char)
if (char == quote) quote = null
index++
continue
}
if (char == '\'' || char == '"') {
quote = char
append(char)
index++
continue
}
if (char == ':' && sql.getOrNull(index + 1).let { it != null && (it.isLetter() || it == '_') }) {
var end = index + 1
while (end < sql.length && (sql[end].isLetterOrDigit() || sql[end] == '_')) end++
names += sql.substring(index + 1, end)
append('?')
index = end
continue
}
append(char)
index++
}
}
val values = names.map(bindings::getValue)
return RoomRawQuery(positional) { statement ->
val values =
Regex(":(\\w+)")
.findAll(sql)
.map { it.groupValues[1] }
.distinct()
.map(bindings::getValue)
.toList()
return RoomRawQuery(sql) { statement ->
values.forEachIndexed { index, value -> statement.bind(index + 1, value) }
}
}
@@ -195,7 +195,16 @@ class LibraryRepository(
userPreferences.sortNameMode().flatMapLatest { mode ->
pager(40, enablePlaceholders = true) {
val dao = db.libraryDao()
val query = albumsPageQuery(sourceId, starred.ordinal, search, sort, descending, downloaded, mode)
val query =
albumsPageQuery(
sourceId = sourceId,
starredFilter = starred.ordinal,
search = search,
sort = sort,
descending = descending,
downloaded = downloaded,
sortNameMode = mode,
)
if (downloaded) dao.downloadedAlbumsPaging(query) else dao.albumsPaging(query)
}
}
@@ -211,7 +220,16 @@ class LibraryRepository(
userPreferences.sortNameMode().flatMapLatest { mode ->
pager(60) {
val dao = db.libraryDao()
val query = artistsPageQuery(sourceId, starred.ordinal, search, sort, descending, downloaded, mode)
val query =
artistsPageQuery(
sourceId = sourceId,
starredFilter = starred.ordinal,
search = search,
sort = sort,
descending = descending,
downloaded = downloaded,
sortNameMode = mode,
)
if (downloaded) dao.downloadedArtistsPaging(query) else dao.artistsPaging(query)
}
}
@@ -225,7 +243,14 @@ class LibraryRepository(
): Flow<PagingData<Playlist>> =
pager(40) {
val dao = db.libraryDao()
val query = playlistsPageQuery(sourceId, search, sort, descending, downloaded)
val query =
playlistsPageQuery(
sourceId = sourceId,
search = search,
sort = sort,
descending = descending,
downloaded = downloaded,
)
if (downloaded) dao.downloadedPlaylistsPaging(query) else dao.playlistsPaging(query)
}