在 redstone_mapper_pg 中插入多行的正确方法是什么?

How is right way to insert many rows in redstone_mapper_pg?

我使用 redstone_mapper_pg 并且我需要向数据库中插入很多行 table:

class Rate {
  @Field() String zone_id;
  @Field() String cost;
}

@app.Route("/rplan", methods: const [app.POST])
addRPlan(@Decode() List<Rate> rate) async {
  try {
    await pgsql.execute('begin');
    rate.forEach((row) async {
      try {
        await pgsql.execute('insert into t_rate (zone_id,cost) '
          'values (@zone_id,@cost)', row);
      } catch(err) {
        await pgsql.execute('rollback');
        return new Future.error(err);
      }
    });
  } catch(err) {
    await pgsql.execute('rollback');
    return new Future.error(err);
  }
  await pgsql.execute('end');
  return new Future.value('OK');
}
  1. 单独插入的循环是否是在 Dart postgresql 驱动程序中插入多行的正确方法?
  2. 如果我像上面那样使用 rate.forEach((row) async { 我有错误的执行链 begin-end-insert-insert 因为 .forEach 方法异步调用参数函数。 rate.forEach(await (row) async { 做同样的事情。使用 await rate.forEach(await (row) async { 给出右链 begin-insert-insert-end 但插入相对于 begin-end 异步执行。只有标准的 for(int i=0; i<rate.length; i++) { 循环才能给出所需的结果。有没有办法在我的代码中使用 .forEach 方法?
  1. 在一个更大的语句中插入多条记录会更有效率(来源 https://kaiv.wordpress.com/2007/07/19/faster-insert-for-multiple-rows/

多行插入SQL文件

insert into things (thing) values ('thing nr. 0'),
('thing nr. 1'),
('thing nr. 2'),
('thing nr. 3'),
...
('thing nr. 99999),
('thing nr. 100000);

多次插入语句SQL文件

begin;
insert into things (thing) values ('thing nr. 0');
insert into things (thing) values ('thing nr. 1');
insert into things (thing) values ('thing nr. 2');
....
insert into things (thing) values ('thing nr. 99999');
insert into things (thing) values ('thing nr. 100000');
commit;
  1. 使用
await for(row in rate) {
  try {
    await pgsql.execute('insert into t_rate (zone_id,cost) '
      'values (@zone_id,@cost)', row);
  } catch(err) {
    await pgsql.execute('rollback');
    return new Future.error(err);
  }
});

而不是

rate.forEach((row) async {