UpsertionAdapter<T> class

Inserts entities and updates them instead when they already exist.

Mirrors the behavior of Room's @Upsert: an insertion is attempted first and, if it fails because of a unique constraint conflict, the entity gets updated by its primary key. If no row with the primary key exists, e.g. because the conflict was caused by another unique index, the original exception is rethrown.

Upserts that don't return ids use a single batch of native INSERT ... ON CONFLICT DO UPDATE statements instead when SQLite supports them (version 3.24.0 and above, but not for virtual tables), which behaves the same but avoids a round trip and an exception per existing entity.

Virtual tables like FTS tables don't enforce unique primary keys, thus an insertion never conflicts. Their entities get updated by primary key first and only inserted when no row got updated.

Constructors

UpsertionAdapter(DatabaseExecutor database, String entityName, List<String> primaryKeyColumnName, Map<String, Object?> valueMapper(T), [StreamController<String>? changeListener])

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
upsert(T item) → Future<void>
upsertAndReturnId(T item) → Future<int>
Returns the row id of the inserted entity or -1 if it got updated.
upsertList(List<T> items) → Future<void>
upsertListAndReturnIds(List<T> items) → Future<List<int>>
Returns the row ids of the inserted entities or -1 for updated ones.

Operators

operator ==(Object other) → bool
The equality operator.
inherited