getUpdate<TDef extends ITableDefinition<TModel>, TModel> method

SqlRequest getUpdate<TDef extends ITableDefinition<TModel>, TModel>(
  1. Update<TDef> update(
    1. TDef e
    ),
  2. TDef tableDefinition
)

Implementation

SqlRequest getUpdate<TDef extends ITableDefinition<TModel>, TModel>(
  Update<TDef> Function(TDef e) update,
  TDef tableDefinition,
) {
  var updateObj = update(tableDefinition);

  if (updateObj is! UpdateCustom<TDef>) {
    throw Exception("only update supported is UpdateCustom");
  }

  var substitutionKeys = <String, dynamic>{};

  var setStatements = updateObj.columns.entries.map((e) {
    var subValue = getNextSubstitutionValue();
    substitutionKeys = {
      ...substitutionKeys,
      ...{"update$subValue": e.value}
    };
    return "${e.key.name} = @update$subValue";
  }).join(", ");

  var result = SqlRequest(
    "update ${tableDefinition.tableName} set $setStatements",
    substitutionKeys,
  );

  return result;
}