addInsertStatement method
Implementation
void addInsertStatement(InsertStatement insert) {
currentInsertStatement = insert;
// For INSERT FROM SELECT statements, we move the select statement into a
// CTE. This allows re-ordering columns from the select statement into the
// right columns for the insert. The final SQL statement will look like
// this:
// WITH _source AS $select INSERT INTO $table (...) SELECT ... FROM _source
if (insert.source case InsertFromSelect(
:final select,
:final columnNameToSelectColumnName,
)) {
for (final column in columnNameToSelectColumnName.values) {
select.structure.createNameForColumn(column);
}
statement.buffer.write('WITH _source AS (');
select.compileWith(this);
statement.buffer.write(')');
}
addInsertStatementMode(insert);
statement.space();
addFromResultSet(
FromResultSet(insert.table),
isWatching: false,
write: UpdateKind.insert,
);
statement.space();
addInsertColumnNames(insert);
(insert.source ?? InsertDefaultValues()).compileWith(this);
if (insert.upsertClause case final upsert?) {
if (insert.source is InsertFromSelect) {
// Resolve parsing ambiguity (a `ON` from the conflict clause could also
// be parsed as a join).
statement.buffer.write(' WHERE TRUE ');
} else {
statement.space();
}
upsert.compileWith(this);
}
if (insert.returning case final returning?) {
statement.space();
returning.compileWith(this);
}
}