122 lines
3.2 KiB
Dart
122 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:args/command_runner.dart';
|
|
import 'package:jadb/_data_ingestion/open_local_db.dart';
|
|
import 'package:jadb/cli/args.dart';
|
|
import 'package:jadb/search.dart';
|
|
import 'package:sqflite_common/sqflite.dart';
|
|
|
|
class QueryWord extends Command {
|
|
@override
|
|
final name = 'query-word';
|
|
@override
|
|
final description = 'Query the database for word data';
|
|
@override
|
|
final invocation = 'jadb query-word [options] (<word> | <ID>)';
|
|
|
|
QueryWord() {
|
|
addLibsqliteArg(argParser);
|
|
addJadbArg(argParser);
|
|
|
|
argParser.addFlag('json', abbr: 'j', help: 'Output results in JSON format');
|
|
|
|
argParser.addOption('page', abbr: 'p', valueHelp: 'NUM', defaultsTo: '0');
|
|
|
|
argParser.addOption('pageSize', valueHelp: 'NUM', defaultsTo: '30');
|
|
}
|
|
|
|
@override
|
|
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, argResults!.flag('json'));
|
|
} else {
|
|
await _searchWord(
|
|
db,
|
|
searchWord,
|
|
argResults!.flag('json'),
|
|
int.parse(argResults!.option('page')!),
|
|
int.parse(argResults!.option('pageSize')!),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _searchId(DatabaseExecutor db, int id, bool jsonOutput) async {
|
|
final time = Stopwatch()..start();
|
|
final result = await JaDBConnection(db).jadbGetWordById(id);
|
|
time.stop();
|
|
|
|
if (result == null) {
|
|
print('Invalid ID');
|
|
} else {
|
|
if (jsonOutput) {
|
|
print(JsonEncoder.withIndent(' ').convert(result));
|
|
} else {
|
|
print(result.toString());
|
|
}
|
|
}
|
|
|
|
print('Query took ${time.elapsedMilliseconds}ms');
|
|
}
|
|
|
|
Future<void> _searchWord(
|
|
DatabaseExecutor db,
|
|
String searchWord,
|
|
bool jsonOutput,
|
|
int page,
|
|
int pageSize,
|
|
) 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, page: page, pageSize: pageSize);
|
|
time2.stop();
|
|
|
|
if (result == null) {
|
|
print('Invalid search');
|
|
} else if (result.isEmpty) {
|
|
print('No matches');
|
|
} else {
|
|
if (jsonOutput) {
|
|
print(JsonEncoder.withIndent(' ').convert(result));
|
|
} 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');
|
|
}
|
|
}
|