page method

Future<MssqlPage<R>> page({
  1. int size = 25,
  2. int offset = 0,
  3. bool total = false,
  4. MssqlReadConsistency consistency = MssqlReadConsistency.committed,
})

One OFFSET page of projected rows.

Needs a unique orderBy on the projection. The root entity primary key is not added: a distinct or grouped result does not have that identity.

Implementation

Future<MssqlPage<R>> page({
  int size = 25,
  int offset = 0,
  bool total = false,
  MssqlReadConsistency consistency = MssqlReadConsistency.committed,
}) async {
  if (offset < 0) {
    throw ArgumentError.value(offset, 'offset', 'Cannot be negative.');
  }
  if (size <= 0) {
    throw ArgumentError.value(size, 'size', 'Must be positive.');
  }
  final dialect = await _resolvedDialect();
  final query = _select;
  if (query.ordering.isEmpty) {
    throw StateError(
      'Projection ${projection.name} needs an explicit unique orderBy '
      'before page(). The root primary key is not added to a projection.',
    );
  }
  if (query.distinct || query.grouping.isNotEmpty) {
    MssqlKeyset.requireProjectedColumns(
      query.ordering,
      projection.columns.map((c) => c.alias),
      projection.name,
    );
  }
  return mssqlRunConsistent(
    session: session,
    consistency: consistency,
    body: (s) async {
      final fetched = await query
          .paged(offset: offset, rows: size + 1)
          .get(
            s,
            dialect: dialect,
            timeout: timeout,
            cancellationToken: options.cancellationToken,
            options: options,
          );
      final hasMore = fetched.length > size;
      final slice = hasMore ? fetched.sublist(0, size) : fetched;
      return MssqlPage<R>(
        rows: slice.map(_map).toList(growable: false),
        offset: offset,
        requestedRows: size,
        hasMore: hasMore,
        total: total ? await _countOn(s, dialect: dialect) : null,
      );
    },
  );
}