90 lines
2.3 KiB
Dart
90 lines
2.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:jadb/_data_ingestion/open_local_db.dart';
|
|
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);
|
|
}
|
|
|
|
Future<void> run() async {
|
|
if (argResults!.option('libsqlite') == null ||
|
|
argResults!.option('jadb') == null) {
|
|
print("You need to provide both libsqlite and jadb paths.");
|
|
print('');
|
|
printUsage();
|
|
exit(64);
|
|
}
|
|
|
|
final db = await openLocalDb(
|
|
jadbPath: argResults!.option('jadb')!,
|
|
libsqlitePath: argResults!.option('libsqlite')!,
|
|
);
|
|
|
|
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();
|
|
|
|
final time2 = Stopwatch()..start();
|
|
final result = await JaDBConnection(db).jadbSearchWord(searchWord);
|
|
time2.stop();
|
|
|
|
if (result == null) {
|
|
print("Invalid search");
|
|
} else if (result.isEmpty) {
|
|
print("No matches");
|
|
} else {
|
|
for (final e in result) {
|
|
print(e.toString());
|
|
print("");
|
|
}
|
|
}
|
|
|
|
print("Total count: ${count}");
|
|
print("Count query took ${time.elapsedMilliseconds}ms");
|
|
print("Query took ${time2.elapsedMilliseconds}ms");
|
|
}
|
|
}
|