buildInsertSql static method
Generates a structured INSERT statement for a list of input models.
Implementation
static SqlStatement buildInsertSql(
List<TetherModelInputBase> models,
String tableName,
) {
if (models.isEmpty) {
throw ArgumentError(
'Cannot build INSERT statement from empty model list.',
);
}
final firstModelMap = models.first.toSqliteMap();
if (firstModelMap.isEmpty) {
throw ArgumentError(
'Cannot build INSERT statement: First model has no data.',
);
}
final columns = firstModelMap.keys.toList();
final columnCount = columns.length;
final valuePlaceholderGroup =
'(${List.filled(columnCount, '?').join(', ')})';
final allPlaceholders = List.filled(
models.length,
valuePlaceholderGroup,
).join(', ');
final allArguments = <Object?>[];
for (final model in models) {
final map = model.toSqliteMap();
if (map.length != columnCount ||
map.keys.join(',') != columns.join(',')) {
throw ArgumentError(
'Inconsistent model structure detected for bulk INSERT.',
);
}
allArguments.addAll(map.values);
}
return SqlStatement(
operationType: SqlOperationType.insert,
tableName: tableName,
insertColumns: columns,
insertValuesPlaceholders: allPlaceholders,
insertArguments: allArguments,
);
}