compile method

MssqlStatement compile({
  1. MssqlDialect dialect = MssqlDialect.sql2012,
})

Implementation

MssqlStatement compile({MssqlDialect dialect = MssqlDialect.sql2012}) {
  if (serverDefaults && assignments.isNotEmpty) {
    throw StateError(
      'INSERT INTO ${source.quoted} says DEFAULT VALUES and also names '
      '${assignments.length} columns; the two contradict. Drop '
      'defaultValues(), and pass const MssqlDefault() for the columns that '
      'should take their default.',
    );
  }
  if (!serverDefaults && assignments.isEmpty) {
    throw StateError(
      'INSERT INTO ${source.quoted} has no values. Call values(), or '
      'defaultValues() if every column is server-generated.',
    );
  }
  final parameters = MssqlParameterAllocator();
  final out = StringBuffer('INSERT INTO ${source.quoted}');
  if (!serverDefaults) {
    final names = <String>[];
    final bound = <String>[];
    for (final entry in assignments.entries) {
      names.add(Col(entry.key).compile(parameters, dialect));
      bound.add(_operandSql(entry.value, parameters, dialect));
    }
    out.write(' (${names.join(', ')})');
    // OUTPUT sits between the column list and VALUES, which is also the
    // order the allocator has to see the parameters in.
    if (output != null) out.write(' ${output!.sql}');
    out.write(' VALUES (${bound.join(', ')})');
  } else {
    if (output != null) out.write(' ${output!.sql}');
    out.write(' DEFAULT VALUES');
  }
  return MssqlStatement.fromAllocator(out.toString(), parameters);
}