exists method

Future<bool> exists({
  1. required String table,
  2. required String where,
  3. bool verbose = false,
})

Check if a value exists in the table

Implementation

Future<bool> exists(
    {required String table,
    required String where,
    bool verbose = false}) async {
  /// [table] is the table to use and [where] the sql where clause
  ///
  /// Returns a future with true if the data exists
  try {
    if (!_isReady) {
      throw DatabaseNotReady();
    }
    final timer = Stopwatch()..start();
    final q = 'SELECT COUNT(*) FROM $table WHERE $where';
    late int count;
    await _db!.transaction((txn) async {
      count = Sqflite.firstIntValue(await txn.rawQuery(q))!;
    });
    timer.stop();
    if (verbose) {
      final msg = "$q in ${timer.elapsedMilliseconds} ms";
      print(msg);
    }
    if (count > 0) {
      return true;
    }
  } catch (e) {
    rethrow;
  }
  return false;
}