cli/query_word: allow querying with jmdict id
This commit is contained in:
@@ -5,26 +5,24 @@ import 'package:jadb/cli/args.dart';
|
||||
import 'package:jadb/search.dart';
|
||||
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:sqflite_common/sqflite.dart';
|
||||
|
||||
class QueryWord extends Command {
|
||||
final name = "query-word";
|
||||
final description = "Query the database for word data";
|
||||
final invocation = "jadb query-word [options] (<word> | <ID>)";
|
||||
|
||||
QueryWord() {
|
||||
addLibsqliteArg(argParser);
|
||||
addJadbArg(argParser);
|
||||
argParser.addOption(
|
||||
'word',
|
||||
abbr: 'w',
|
||||
help: 'The word to search for.',
|
||||
valueHelp: 'WORD',
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> run() async {
|
||||
if (argResults!.option('libsqlite') == null ||
|
||||
argResults!.option('jadb') == null) {
|
||||
print(argParser.usage);
|
||||
print("You need to provide both libsqlite and jadb paths.");
|
||||
print('');
|
||||
printUsage();
|
||||
exit(64);
|
||||
}
|
||||
|
||||
@@ -33,8 +31,38 @@ class QueryWord extends Command {
|
||||
libsqlitePath: argResults!.option('libsqlite')!,
|
||||
);
|
||||
|
||||
final String searchWord = argResults!.option('word') ?? 'かな';
|
||||
if (argResults!.rest.isEmpty) {
|
||||
print('You need to provide a word or ID to search for.');
|
||||
print('');
|
||||
printUsage();
|
||||
exit(64);
|
||||
}
|
||||
|
||||
final String searchWord = argResults!.rest.join(" ");
|
||||
final int? maybeId = int.tryParse(searchWord);
|
||||
|
||||
if (maybeId != null && maybeId >= 1000000) {
|
||||
await _searchId(db, maybeId);
|
||||
} else {
|
||||
await _searchWord(db, searchWord);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _searchId(DatabaseExecutor db, int id) async {
|
||||
final time = Stopwatch()..start();
|
||||
final result = await JaDBConnection(db).jadbGetWordById(id);
|
||||
time.stop();
|
||||
|
||||
if (result == null) {
|
||||
print("Invalid ID");
|
||||
} else {
|
||||
print(result.toString());
|
||||
}
|
||||
|
||||
print("Query took ${time.elapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
Future<void> _searchWord(DatabaseExecutor db, String searchWord) async {
|
||||
final time = Stopwatch()..start();
|
||||
final count = await JaDBConnection(db).jadbSearchWordCount(searchWord);
|
||||
time.stop();
|
||||
|
||||
Reference in New Issue
Block a user