compileInsert method

CompiledQuery compileInsert(
  1. String table,
  2. List<Map<String, Object?>> rows, {
  3. bool ignore = false,
})

The INSERT of rows into table; ignore skips rows a unique constraint refuses.

Implementation

CompiledQuery compileInsert(
  String table,
  List<Map<String, Object?>> rows, {
  bool ignore = false,
}) {
  if (rows.isEmpty) {
    throw ArgumentError('insert() requires at least one row.');
  }
  final c = Compilation(this);
  final columns = rows.first.keys.toList();
  for (final (index, row) in rows.indexed) {
    if (row.length != columns.length || !columns.every(row.containsKey)) {
      throw ArgumentError(
        'insert(): row $index has the columns '
        '${row.keys.join(', ')}, not ${columns.join(', ')}. Every row must '
        'name the same columns.',
      );
    }
  }
  final values = rows
      .map(
        (row) => '(${columns.map((col) => _value(row[col], c)).join(', ')})',
      )
      .join(', ');
  final sql =
      '${insertKeyword(ignore)} INTO ${wrap(table)} (${columns.map(wrap).join(', ')}) VALUES $values';
  return CompiledQuery(sql, c.bindings);
}