compile method
Implementation
MssqlStatement compile({MssqlDialect dialect = MssqlDialect.sql2012}) {
if (query is MssqlQuery && (query as MssqlQuery).ctes.isNotEmpty) {
throw StateError(
'The SELECT of INSERT INTO ${source.quoted} carries its own WITH '
'clause, which SQL Server accepts only at the very start of a '
'statement — before INSERT, not between INSERT and SELECT. Move it '
'with MssqlInsertSelect.withExpression(); the expression stays '
'visible to the SELECT.',
);
}
final projected = query.projectionCount;
if (projected != null && projected != columns.length) {
throw StateError(
'INSERT INTO ${source.quoted} names ${columns.length} target columns '
'(${columns.join(', ')}) but the query projects $projected. Each '
'target takes the projected expression in the same position, so the '
'two lists must have the same length and the same order; SQL Server '
'then converts each value to its target column\'s type, and refuses '
'the pairs it cannot convert.',
);
}
final parameters = MssqlParameterAllocator();
final out = StringBuffer();
if (ctes.isNotEmpty) {
final written = ctes.map((c) => c.compile(parameters, dialect));
out.write('WITH ${written.join(', ')} ');
}
final names = columns
.map((name) => Col(name).compile(parameters, dialect))
.join(', ');
out.write('INSERT INTO ${source.quoted} ($names)');
if (output != null) out.write(' ${output!.sql}');
out.write(' ${query.compileInto(parameters, dialect)}');
return MssqlStatement.fromAllocator(out.toString(), parameters);
}