buildUpdateSql static method

SqlStatement buildUpdateSql(
  1. TetherModelInputBase model,
  2. String tableName, {
  3. String idColumnName = 'id',
})

Generates a structured UPDATE statement for a given input model, based on its ID.

Implementation

static SqlStatement buildUpdateSql(
  TetherModelInputBase model,
  String tableName, {
  String idColumnName = 'id',
}) {
  final map = model.toSqliteMap();
  final idValue = map.remove(idColumnName);

  if (idValue == null) {
    throw ArgumentError(
      'Cannot build UPDATE: ID column "$idColumnName" not found or null.',
    );
  }
  if (map.isEmpty) {
    throw ArgumentError(
      'Cannot build UPDATE: No columns to update (excluding ID).',
    );
  }

  final setClauses = map.keys.map((key) => '$key = ?').toList();
  final updateArgs = map.values.toList();
  final whereClause = '$idColumnName = ?';
  final whereArgs = [idValue];

  return SqlStatement(
    operationType: SqlOperationType.update,
    tableName: tableName,
    updateSetClauses: setClauses,
    updateArguments: updateArgs,
    whereClauses: [whereClause],
    whereArguments: whereArgs,
  );
}