insert method

Future insert(
  1. String otype,
  2. Map<String, dynamic> data, {
  3. Map<String, String>? types,
  4. String? append,
})

Inserts the entity specified in data. Note: all fields found in data are written. You have to remove unnecessary files by yourself, such as fdOtype.

  • types - a map of (field-name, field-type). If specified, the type of the field will be retrieved from types, if any.
  • append - the extra clause to append to the insert statement. Example, final oid = await insert(..., append: returning "$fdOid");

Implementation

Future<dynamic> insert(String otype, Map<String, dynamic> data,
    {Map<String, String>? types, String? append}) {
  final sql = StringBuffer('insert into "')..write(otype)..write('"('),
    values = StringBuffer(" values("),
    cvter = _pool!.typeConverter;

  bool first = true;
  data.forEach((fd, val) {
    if (first) first = false;
    else {
      sql.write(',');
      values.write(',');
    }
    sql..write('"')..write(fd)..write('"');
    values.write(cvter.encode(val, types?[fd]));
  });

  sql.write(')');
  values.write(')');
  bool bReturning = false;
  if (append != null) {
    bReturning = append.trim().startsWith('returning');
    values..write(' ')..write(append);
  }

  sql.write(values);
  final stmt = sql.toString();
  if (bReturning)
    return query(stmt, data).first.then(_firstCol);

  return execute(stmt, data);
}