mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-04 23:59:29 +00:00
set method in realizations
This commit is contained in:
@@ -107,6 +107,12 @@ open class MapperWriteOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||
}.toMap()
|
||||
)
|
||||
|
||||
override suspend fun set(toSet: Map<FromKey, List<FromValue>>) {
|
||||
to.set(
|
||||
toSet.map { (k, vs) -> k.toOutKey() to vs.map { v -> v.toOutValue() } }.toMap()
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun clear(k: FromKey) = to.clear(k.toOutKey())
|
||||
}
|
||||
|
||||
|
@@ -87,6 +87,29 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||
val (clearedKeys, inserted) = helper.writableTransaction {
|
||||
toSet.mapNotNull { (k, _) ->
|
||||
if (delete(tableName, "$idColumnName=?", arrayOf(k.asId())) > 0) {
|
||||
k
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} to toSet.flatMap { (k, values) ->
|
||||
values.map { v ->
|
||||
insert(
|
||||
tableName,
|
||||
null,
|
||||
contentValuesOf(idColumnName to k.asId(), valueColumnName to v.asValue())
|
||||
)
|
||||
k to v
|
||||
}
|
||||
}
|
||||
}
|
||||
clearedKeys.forEach { _onDataCleared.emit(it) }
|
||||
inserted.forEach { newPair -> _onNewValue.emit(newPair) }
|
||||
}
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = helper.readableTransaction {
|
||||
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use {
|
||||
it.count > 0
|
||||
|
Reference in New Issue
Block a user