cli/query_word: add --json flag
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:jadb/_data_ingestion/open_local_db.dart';
|
||||
@@ -15,6 +16,12 @@ class QueryWord extends Command {
|
||||
QueryWord() {
|
||||
addLibsqliteArg(argParser);
|
||||
addJadbArg(argParser);
|
||||
|
||||
argParser.addFlag(
|
||||
'json',
|
||||
abbr: 'j',
|
||||
help: 'Output results in JSON format',
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> run() async {
|
||||
@@ -42,13 +49,25 @@ class QueryWord extends Command {
|
||||
final int? maybeId = int.tryParse(searchWord);
|
||||
|
||||
if (maybeId != null && maybeId >= 1000000) {
|
||||
await _searchId(db, maybeId);
|
||||
await _searchId(
|
||||
db,
|
||||
maybeId,
|
||||
argResults!.flag('json'),
|
||||
);
|
||||
} else {
|
||||
await _searchWord(db, searchWord);
|
||||
await _searchWord(
|
||||
db,
|
||||
searchWord,
|
||||
argResults!.flag('json'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _searchId(DatabaseExecutor db, int id) async {
|
||||
Future<void> _searchId(
|
||||
DatabaseExecutor db,
|
||||
int id,
|
||||
bool jsonOutput,
|
||||
) async {
|
||||
final time = Stopwatch()..start();
|
||||
final result = await JaDBConnection(db).jadbGetWordById(id);
|
||||
time.stop();
|
||||
@@ -56,13 +75,21 @@ class QueryWord extends Command {
|
||||
if (result == null) {
|
||||
print("Invalid ID");
|
||||
} else {
|
||||
print(result.toString());
|
||||
if (jsonOutput) {
|
||||
print(JsonEncoder.withIndent(' ').convert(result));
|
||||
} else {
|
||||
print(result.toString());
|
||||
}
|
||||
}
|
||||
|
||||
print("Query took ${time.elapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
Future<void> _searchWord(DatabaseExecutor db, String searchWord) async {
|
||||
Future<void> _searchWord(
|
||||
DatabaseExecutor db,
|
||||
String searchWord,
|
||||
bool jsonOutput,
|
||||
) async {
|
||||
final time = Stopwatch()..start();
|
||||
final count = await JaDBConnection(db).jadbSearchWordCount(searchWord);
|
||||
time.stop();
|
||||
@@ -76,9 +103,13 @@ class QueryWord extends Command {
|
||||
} else if (result.isEmpty) {
|
||||
print("No matches");
|
||||
} else {
|
||||
for (final e in result) {
|
||||
print(e.toString());
|
||||
print("");
|
||||
if (jsonOutput) {
|
||||
print(JsonEncoder.withIndent(' ').convert(result));
|
||||
} else {
|
||||
for (final e in result) {
|
||||
print(e.toString());
|
||||
print("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user