compileUpdate method

CompiledQuery compileUpdate(
  1. QueryAst query,
  2. Map<String, Object?> values
)

Implementation

CompiledQuery compileUpdate(QueryAst query, Map<String, Object?> values) {
  final c = Compilation(this);
  final json = <String, List<(List<String>, Object?)>>{};
  final sets = <String>[];
  for (final MapEntry(:key, :value) in values.entries) {
    if (key.contains('->')) {
      final (column, path) = _jsonParts(key);
      (json[column] ??= []).add((path, value));
    } else {
      sets.add('${wrap(key)} = ${_value(value, c)}');
    }
  }
  // One assignment per column: a second one would be refused, or lost.
  for (final MapEntry(:key, :value) in json.entries) {
    sets.add('$key = ${jsonUpdate(key, value, c)}');
  }
  final buffer = StringBuffer(
    'UPDATE ${wrap(query.table)} SET ${sets.join(', ')}',
  );
  final where = _wheres(query.wheres, c);
  if (where.isNotEmpty) {
    buffer.write(' WHERE $where');
  }
  return CompiledQuery(buffer.toString(), c.bindings);
}