commitSync method

int? commitSync(
  1. String atKey,
  2. CommitOp operation
)

Synchronous commit used inside a keystore write's transaction so the value row and its commit entry are one atomic unit. Returns the allocated commit id, or -1 for an elided key. Re-entrant via SqliteDatabase.runInTransaction: joins the caller's transaction when there is one, else opens its own.

Implementation

int? commitSync(String atKey, CommitOp operation) {
  if (_isElided(atKey)) return -1;
  return _db.runInTransaction(() {
    _db.raw.execute(
        "UPDATE counters SET value = value + 1 WHERE name = 'last_commit_id';");
    final commitId = _db.raw
        .select("SELECT value FROM counters WHERE name = 'last_commit_id';")
        .first
        .values
        .first as int;
    _db.raw.execute(
      'INSERT INTO commit_log (atkey, commit_id, operation, op_time) '
      'VALUES (?, ?, ?, ?) '
      'ON CONFLICT(atkey) DO UPDATE SET '
      'commit_id = excluded.commit_id, operation = excluded.operation, '
      'op_time = excluded.op_time;',
      [
        atKey,
        commitId,
        operation.name,
        DateTime.now().toUtcMillisecondsPrecision().millisecondsSinceEpoch,
      ],
    );
    return commitId;
  });
}