insert static method

Future<int> insert(
  1. String table,
  2. Map<String, Object?> values, {
  3. String column = 'id',
  4. String? connection,
})

Inserts values under the next key of table and returns the key.

Implementation

static Future<int> insert(
  String table,
  Map<String, Object?> values, {
  String column = 'id',
  String? connection,
}) async {
  final db = _manager;
  for (var attempt = 1; ; attempt++) {
    try {
      return await db.transaction(
        () async {
          final key = await _reserve(db, table, column, connection);
          await db.table(table, connection: connection).insert({
            ...values,
            column: key,
          });
          return key;
        },
        connection: connection,
        // On MySQL writers deadlock on the gap above the last row. Each
        // deadlock lets one through, so this bounds the writers at once.
        attempts: 12,
      );
    } on UniqueConstraintViolationException {
      if (attempt >= _attempts) {
        rethrow;
      }
    }
  }
}