compileStable method

  1. @override
String compileStable(
  1. MssqlParameterAllocator parameters,
  2. MssqlDialect dialect
)
override

Writes the SQL. Called at most once per statement.

Implementation

@override
String compileStable(
  MssqlParameterAllocator parameters,
  MssqlDialect dialect,
) {
  dialect.require(
    dialect.supportsDateBucket,
    feature: 'dateBucket()',
    requires:
        'SQL Server 2022 or newer, at database compatibility level 160 or '
        'higher. For an older target, group by truncatedTo() or '
        'monthStart(), which snap to a calendar boundary with '
        'DATEADD/DATEDIFF and need no version at all',
  );
  final inner = operand.compile(parameters, dialect);
  final at = origin;
  // SQL Server requires the origin and operand to have matching types.
  final from = at == null
      ? ''
      : ', ${parameters.bind(_calendarBound(operand, at, zoneOffset))}';
  return 'DATE_BUCKET(${unit.datePart}, ${parameters.bind(width)}, '
      '$inner$from)';
}