insert method

Future<int> insert(
  1. Map<String, Object?> values, {
  2. ConflictAlgorithm? conflictAlgorithm,
  3. bool isReThrow = false,
})

Insert. Return the id of the last inserted row. 0 or -1 maybe returned if insert failed.

Implementation

Future<int> insert(Map<String, Object?> values,
    {ConflictAlgorithm? conflictAlgorithm, bool isReThrow = false}) async {
  try {
    batch?.insert(tableName, values, conflictAlgorithm: conflictAlgorithm);
    if (batch != null) return 0; // fake result, please get the real result from `batch.commit()`
    return await executor.insert(tableName, values, conflictAlgorithm: conflictAlgorithm);
  } catch (e, s) {
    /// Prevent retry recursion, thrown to the caller
    if (isReThrow) rethrow;

    /// Retry create table and do insert again
    if (e is DatabaseException && e.isNoSuchTableError()) {
      if ((await createTableIfNeeded()) == true) {
        return await insert(values, conflictAlgorithm: conflictAlgorithm, isReThrow: true);
      }
    }
    BoxerLogger.f(null, 'Insert error: $e, $s');
    BoxerLogger.reportFatal(e, s);
    return -1;
  }
}