search: add function for fetching multiple kanji at once
This commit is contained in:
@@ -19,6 +19,10 @@ extension JaDBConnection on DatabaseExecutor {
|
||||
Future<KanjiSearchResult?> jadbSearchKanji(String kanji) =>
|
||||
searchKanjiWithDbConnection(this, kanji);
|
||||
|
||||
/// Search for a kanji in the database.
|
||||
Future<Map<String, KanjiSearchResult>> jadbGetManyKanji(Set<String> kanji) =>
|
||||
searchManyKanjiWithDbConnection(this, kanji);
|
||||
|
||||
/// Filter a list of characters, and return the ones that are listed in the kanji dictionary.
|
||||
Future<List<String>> filterKanji(
|
||||
List<String> kanji, {
|
||||
|
||||
@@ -219,3 +219,25 @@ Future<KanjiSearchResult?> searchKanjiWithDbConnection(
|
||||
dictionaryReferences: dictionaryReferences,
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Use fewer queries with `IN` clauses to reduce the number of queries
|
||||
|
||||
Future<Map<String, KanjiSearchResult>> searchManyKanjiWithDbConnection(
|
||||
DatabaseExecutor connection,
|
||||
Set<String> kanji,
|
||||
) async {
|
||||
if (kanji.isEmpty) {
|
||||
return {};
|
||||
}
|
||||
|
||||
final results = <String, KanjiSearchResult>{};
|
||||
|
||||
for (final k in kanji) {
|
||||
final result = await searchKanjiWithDbConnection(connection, k);
|
||||
if (result != null) {
|
||||
results[k] = result;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user