word_search: add function for retrieving single entry by id
This commit is contained in:
0
lib/models/createEmptyDb.dart
Normal file
0
lib/models/createEmptyDb.dart
Normal file
@@ -41,6 +41,10 @@ extension JaDBConnection on DatabaseExecutor {
|
||||
pageSize,
|
||||
);
|
||||
|
||||
///
|
||||
Future<WordSearchResult?> jadbGetWordById(int id) =>
|
||||
getWordByIdWithDbConnection(this, id);
|
||||
|
||||
/// Search for a word in the database, and return the count of results.
|
||||
Future<int?> jadbSearchWordCount(
|
||||
String word, {
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:jadb/models/word_search/word_search_result.dart';
|
||||
import 'package:jadb/search/word_search/data_query.dart';
|
||||
import 'package:jadb/search/word_search/entry_id_query.dart';
|
||||
import 'package:jadb/search/word_search/regrouping.dart';
|
||||
import 'package:jadb/table_names/jmdict.dart';
|
||||
import 'package:sqflite_common/sqlite_api.dart';
|
||||
|
||||
enum SearchMode {
|
||||
@@ -95,3 +96,58 @@ Future<int?> searchWordCountWithDbConnection(
|
||||
|
||||
return entryIdCount;
|
||||
}
|
||||
|
||||
Future<WordSearchResult?> getWordByIdWithDbConnection(
|
||||
DatabaseExecutor connection,
|
||||
int id,
|
||||
) async {
|
||||
if (id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final exists = await connection.rawQuery(
|
||||
'SELECT EXISTS(SELECT 1 FROM "${JMdictTableNames.entry}" WHERE "entryId" = ?)',
|
||||
[id],
|
||||
).then((value) => value.isNotEmpty && value.first.values.first == 1);
|
||||
|
||||
if (!exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final LinearWordQueryData linearWordQueryData =
|
||||
await fetchLinearWordQueryData(
|
||||
connection,
|
||||
[id],
|
||||
);
|
||||
|
||||
final result = regroupWordSearchResults(
|
||||
entryIds: [ScoredEntryId(id, 0)],
|
||||
readingElements: linearWordQueryData.readingElements,
|
||||
kanjiElements: linearWordQueryData.kanjiElements,
|
||||
jlptTags: linearWordQueryData.jlptTags,
|
||||
commonEntries: linearWordQueryData.commonEntries,
|
||||
senses: linearWordQueryData.senses,
|
||||
senseAntonyms: linearWordQueryData.senseAntonyms,
|
||||
senseDialects: linearWordQueryData.senseDialects,
|
||||
senseFields: linearWordQueryData.senseFields,
|
||||
senseGlossaries: linearWordQueryData.senseGlossaries,
|
||||
senseInfos: linearWordQueryData.senseInfos,
|
||||
senseLanguageSources: linearWordQueryData.senseLanguageSources,
|
||||
senseMiscs: linearWordQueryData.senseMiscs,
|
||||
sensePOSs: linearWordQueryData.sensePOSs,
|
||||
senseRestrictedToKanjis: linearWordQueryData.senseRestrictedToKanjis,
|
||||
senseRestrictedToReadings: linearWordQueryData.senseRestrictedToReadings,
|
||||
senseSeeAlsos: linearWordQueryData.senseSeeAlsos,
|
||||
exampleSentences: linearWordQueryData.exampleSentences,
|
||||
readingElementInfos: linearWordQueryData.readingElementInfos,
|
||||
readingElementRestrictions: linearWordQueryData.readingElementRestrictions,
|
||||
kanjiElementInfos: linearWordQueryData.kanjiElementInfos,
|
||||
);
|
||||
|
||||
assert(
|
||||
result.length == 1,
|
||||
'Expected exactly one result for entryId $id, but got ${result.length}',
|
||||
);
|
||||
|
||||
return result.firstOrNull;
|
||||
}
|
||||
|
||||
@@ -6,11 +6,16 @@ import 'setup_database_connection.dart';
|
||||
void main() {
|
||||
test("Search a word", () async {
|
||||
final connection = await setup_database_connection();
|
||||
|
||||
final result = await connection.jadbSearchWord("kana");
|
||||
expect(result, isNotNull);
|
||||
});
|
||||
|
||||
test("Get a word by id", () async {
|
||||
final connection = await setup_database_connection();
|
||||
final result = await connection.jadbGetWordById(1577090);
|
||||
expect(result, isNotNull);
|
||||
});
|
||||
|
||||
test(
|
||||
"Serialize all words",
|
||||
() async {
|
||||
|
||||
Reference in New Issue
Block a user