replacing of transaction to newSuspendedTransaction

This commit is contained in:
InsanusMokrassar 2020-07-31 10:37:00 +06:00
parent 43f148bf4b
commit 0820c6d691
6 changed files with 29 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
@ -16,7 +17,7 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
Table(tableName)
{
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
return transaction(database) {
return newSuspendedTransaction(db = database) {
selectAll().paginate(pagination).map {
it.asObject
}.createPaginationResult(
@ -26,14 +27,14 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
}
}
override suspend fun getById(id: IdType): ObjectType? {
return transaction(database) {
return newSuspendedTransaction(db = database) {
select {
selectById(id)
}.limit(1).firstOrNull() ?.asObject
}
}
override suspend fun contains(id: IdType): Boolean = transaction(database) {
override suspend fun contains(id: IdType): Boolean = newSuspendedTransaction(db = database) {
select { selectById(id) }.limit(1).any()
}
}

View File

@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.asFlow
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
@ -41,7 +42,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun create(vararg values: InputValueType): List<ObjectType> {
onBeforeCreate(*values)
return transaction(database) {
return newSuspendedTransaction(db = database) {
values.map { value -> createWithoutNotification(value) }
}.also {
it.forEach {
@ -52,7 +53,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
protected open suspend fun onBeforeUpdate(vararg value: UpdatedValuePair<IdType, InputValueType>) {}
private fun updateWithoutNotification(id: IdType, value: InputValueType): ObjectType? {
return transaction(database) {
return transaction(db = database) {
update(
{
selectById(this, id)
@ -82,7 +83,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun update(vararg values: UpdatedValuePair<IdType, InputValueType>): List<ObjectType> {
onBeforeUpdate(*values)
return (
transaction(database) {
newSuspendedTransaction(db = database) {
values.map { (id, value) -> updateWithoutNotification(id, value) }
}.filter {
it != null
@ -96,7 +97,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
protected open suspend fun onBeforeDelete(vararg ids: IdType) {}
override suspend fun deleteById(vararg ids: IdType) {
onBeforeDelete(*ids)
transaction(database) {
newSuspendedTransaction(db = database) {
deleteWhere(null, null) {
selectByIds(ids)
}

View File

@ -6,6 +6,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedKeyValueRepo<Key, Value>(
@ -24,7 +25,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
override suspend fun set(k: Key, v: Value) {
transaction(database) {
newSuspendedTransaction(db = database) {
if (select { keyColumn.eq(k) }.limit(1).any()) {
update({ keyColumn.eq(k) }) {
it[valueColumn] = v
@ -40,7 +41,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
}
override suspend fun unset(k: Key) {
transaction(database) {
newSuspendedTransaction(db = database) {
deleteWhere { keyColumn.eq(k) }
}
onValueRemovedChannel.send(k)

View File

@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.StandardReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
@ -13,23 +14,23 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
) : StandardReadKeyValueRepo<Key, Value>, Table() {
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
override suspend fun get(k: Key): Value? = transaction(database) {
override suspend fun get(k: Key): Value? = newSuspendedTransaction(db = database) {
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
}
override suspend fun contains(key: Key): Boolean = transaction(database) {
override suspend fun contains(key: Key): Boolean = newSuspendedTransaction(db = database) {
select { keyColumn.eq(key) }.limit(1).any()
}
override suspend fun count(): Long = transaction(database) { selectAll().count() }
override suspend fun count(): Long = newSuspendedTransaction(db = database) { selectAll().count() }
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = newSuspendedTransaction(db = database) {
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
it[keyColumn]
}
}.createPaginationResult(pagination, count())
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(database) {
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = newSuspendedTransaction(db = database) {
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
it[valueColumn]
}

View File

@ -4,6 +4,7 @@ import com.insanusmokrassar.budgetmanager.core.utils.repo.onetomany.AbstractOneT
import com.insanusmokrassar.budgetmanager.core.utils.repo.onetomany.ColumnAllocator
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
@ -16,7 +17,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
database
) {
override suspend fun add(k: Key, v: Value) {
transaction(database) {
newSuspendedTransaction(db = database) {
insert {
it[keyColumn] = k
it[valueColumn] = v
@ -25,10 +26,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
}
override suspend fun remove(k: Key, v: Value) {
transaction(database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
newSuspendedTransaction(db = database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
}
override suspend fun clear(k: Key) {
transaction(database) { deleteWhere { keyColumn.eq(k) } }
newSuspendedTransaction(db = database) { deleteWhere { keyColumn.eq(k) } }
}
}

View File

@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
typealias ColumnAllocator<T> = Table.() -> Column<T>
@ -16,33 +17,33 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
protected val keyColumn: Column<Key> = keyColumnAllocator()
protected val valueColumn: Column<Value> = valueColumnAllocator()
override suspend fun count(k: Key): Long = transaction(database) { select { keyColumn.eq(k) }.count() }
override suspend fun count(k: Key): Long = newSuspendedTransaction(db = database) { select { keyColumn.eq(k) }.count() }
override suspend fun count(): Long = transaction(database) { selectAll().count() }
override suspend fun count(): Long = newSuspendedTransaction(db = database) { selectAll().count() }
override suspend fun get(
k: Key,
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> = transaction(database) {
): PaginationResult<Value> = newSuspendedTransaction(db = database) {
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
}.createPaginationResult(
pagination,
count(k)
)
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = newSuspendedTransaction(db = database) {
selectAll().paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
}.createPaginationResult(
pagination,
count()
)
override suspend fun contains(k: Key): Boolean = transaction(database) {
override suspend fun contains(k: Key): Boolean = newSuspendedTransaction(db = database) {
select { keyColumn.eq(k) }.limit(1).any()
}
override suspend fun contains(k: Key, v: Value): Boolean = transaction(database) {
override suspend fun contains(k: Key, v: Value): Boolean = newSuspendedTransaction(db = database) {
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
}
}