update method
Updates the entity with the given OID into database. *
-
data
- the content of the entity. It might contain
- more fields than what are specified in
fields
. - And, these fields shall be ignored.
-
fields
- the fields to update. If null, all fields indata
- shall be stored.
Implementation
@override
Future update(Entity entity, Map data, Iterable<String>? fields) {
assert(fields == null || fields is Set
|| fields.toSet().length == fields.length, "Dup? $fields");
final sql = StringBuffer('update "')
..write(entity.otype)..write('" set ');
final fds = fields == null ? data.keys: fields;
bool first = true;
for (final fd in fds) {
if (fd == fdOtype || fd == fdOid)
continue;
if (first) first = false;
else sql.write(',');
sql..write('"')..write(fd)..write('"')..write("=@")..write(fd);
final dbType = entity.getDBType(fd);
if (dbType?.isNotEmpty ?? false) sql..write(':')..write(dbType);
if (!data.containsKey(fd)) //postgresql2 driver needs every field
data[fd] = null;
}
if (first)
return Future.value(); //nothing to update
sql.write(' where "oid"=@oid');
data[fdOid] = entity.oid;
var sqlStatement = sql.toString();
if (entity is SqlFlavor) {
final flavor = (entity as SqlFlavor).updateFlavor;
if (flavor != null) sqlStatement = flavor(sqlStatement, data);
}
return access.execute(sqlStatement, data);
}