query<T> method

Future<List<T>> query<T>(
  1. Object tableEntityInstance, {
  2. String? where,
  3. List<Object?>? whereArgs,
  4. String? orderBy,
  5. int? limit,
  6. int? offset,
})

Entities of type T matching where (? placeholders take their values from whereArgs), sorted by orderBy (inserted as is: never from user input), limit rows from offset. Returns [] when nothing matches.

Implementation

Future<List<T>> query<T>(
  Object tableEntityInstance, {
  String? where,
  List<Object?>? whereArgs,
  String? orderBy,
  int? limit,
  int? offset,
}) async {
  if (limit != null && limit < 0) {
    throw ArgumentError.value(limit, 'limit', 'must not be negative');
  }
  if (offset != null && offset < 0) {
    throw ArgumentError.value(offset, 'offset', 'must not be negative');
  }
  final StringBuffer sql = StringBuffer(
    'SELECT * FROM ${quoteIdentifier(_tableFor<T>())}${whereClause(where)}',
  );
  final List<Object?> args = toSqlValues(whereArgs).toList();
  if (orderBy != null) sql.write(' ORDER BY $orderBy');
  if (limit != null || offset != null) {
    sql.write(' LIMIT ?');
    args.add(limit ?? -1);
  }
  if (offset != null) {
    sql.write(' OFFSET ?');
    args.add(offset);
  }
  final List<Map<String, Object?>> rows = await (await _executor()).select(
    sql.toString(),
    args,
  );
  return _entities<T>(tableEntityInstance, rows);
}