exists<TModel extends SqliteModel> method

  1. @override
Future<bool> exists<TModel extends SqliteModel>({
  1. Query? query,
  2. ModelRepository<SqliteModel>? repository,

Returns true if TModel exists in SQLite.

If query.where is null, existence for any record is executed.


Future<bool> exists<TModel extends SqliteModel>({
  Query? query,
  ModelRepository<SqliteModel>? repository,
}) async {
  final sqlQuery = QuerySqlTransformer<TModel>(
    modelDictionary: modelDictionary,
    query: query,
    selectStatement: false,

  final offsetRegex = RegExp(r'OFFSET \d+');
  final offsetIsPresent = sqlQuery.statement.contains(offsetRegex);
  var statement = sqlQuery.statement;

  /// COUNT(*) does not function with OFFSET.
  /// Instead, when an OFFSET is defined, a single column managed by is queried
  /// and that result is counted via Dart
  if (offsetIsPresent) {
    statement = statement.replaceFirstMapped(RegExp(r'SELECT COUNT\(\*\) FROM ([\S]+)'), (match) {
      return 'SELECT ${match.group(1)}.${InsertTable.PRIMARY_KEY_COLUMN} FROM ${match.group(1)}';

  final countQuery = await (await getDb()).rawQuery(statement, sqlQuery.values);
  final count = offsetIsPresent ? countQuery.length : sqlite_utils.firstIntValue(countQuery);

  return (count ?? 0) > 0;