compile method

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

Writes this expression's SQL, binding any values through parameters.

Implementation

@override
String compile(MssqlParameterAllocator parameters, MssqlDialect dialect) {
  final over = window;
  if (over != null) {
    if (distinct) {
      throw StateError(
        'SQL Server has no DISTINCT inside a window aggregate: '
        '${function.sql}(DISTINCT …) OVER (…) is rejected. Aggregate the '
        'distinct values in a derived table and window the result.',
      );
    }
    if (over.orderBy.isNotEmpty) {
      dialect.require(
        dialect.supportsWindowFrames,
        feature:
            'an ordered window aggregate '
            '(${function.sql} OVER (ORDER BY …))',
        requires:
            'SQL Server 2012 or newer, at database compatibility level 110 '
            'or higher. On an older target an aggregate window may only '
            'partition, not order: drop the orderBy() to get the aggregate '
            'over the whole partition, or compute the running total in a '
            'correlated subquery',
      );
    }
  }
  final inner = operand.compile(parameters, dialect);
  final prefix = distinct ? 'DISTINCT ' : '';
  final suffix = over == null
      ? ''
      : ' OVER (${over.compile(parameters, dialect)})';
  return '${function.sql}($prefix$inner)$suffix';
}