replacing of transaction to newSuspendedTransaction
This commit is contained in:
parent
43f148bf4b
commit
0820c6d691
@ -6,6 +6,7 @@ import com.insanusmokrassar.postssystem.utils.repos.pagination.*
|
|||||||
import org.jetbrains.exposed.sql.Table
|
import org.jetbrains.exposed.sql.Table
|
||||||
import org.jetbrains.exposed.sql.select
|
import org.jetbrains.exposed.sql.select
|
||||||
import org.jetbrains.exposed.sql.selectAll
|
import org.jetbrains.exposed.sql.selectAll
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
|
abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
|
||||||
@ -16,7 +17,7 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
|
|||||||
Table(tableName)
|
Table(tableName)
|
||||||
{
|
{
|
||||||
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
|
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
|
||||||
return transaction(database) {
|
return newSuspendedTransaction(db = database) {
|
||||||
selectAll().paginate(pagination).map {
|
selectAll().paginate(pagination).map {
|
||||||
it.asObject
|
it.asObject
|
||||||
}.createPaginationResult(
|
}.createPaginationResult(
|
||||||
@ -26,14 +27,14 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
override suspend fun getById(id: IdType): ObjectType? {
|
override suspend fun getById(id: IdType): ObjectType? {
|
||||||
return transaction(database) {
|
return newSuspendedTransaction(db = database) {
|
||||||
select {
|
select {
|
||||||
selectById(id)
|
selectById(id)
|
||||||
}.limit(1).firstOrNull() ?.asObject
|
}.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()
|
select { selectById(id) }.limit(1).any()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.asFlow
|
|||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
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> {
|
override suspend fun create(vararg values: InputValueType): List<ObjectType> {
|
||||||
onBeforeCreate(*values)
|
onBeforeCreate(*values)
|
||||||
return transaction(database) {
|
return newSuspendedTransaction(db = database) {
|
||||||
values.map { value -> createWithoutNotification(value) }
|
values.map { value -> createWithoutNotification(value) }
|
||||||
}.also {
|
}.also {
|
||||||
it.forEach {
|
it.forEach {
|
||||||
@ -52,7 +53,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
|
|
||||||
protected open suspend fun onBeforeUpdate(vararg value: UpdatedValuePair<IdType, InputValueType>) {}
|
protected open suspend fun onBeforeUpdate(vararg value: UpdatedValuePair<IdType, InputValueType>) {}
|
||||||
private fun updateWithoutNotification(id: IdType, value: InputValueType): ObjectType? {
|
private fun updateWithoutNotification(id: IdType, value: InputValueType): ObjectType? {
|
||||||
return transaction(database) {
|
return transaction(db = database) {
|
||||||
update(
|
update(
|
||||||
{
|
{
|
||||||
selectById(this, id)
|
selectById(this, id)
|
||||||
@ -82,7 +83,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
override suspend fun update(vararg values: UpdatedValuePair<IdType, InputValueType>): List<ObjectType> {
|
override suspend fun update(vararg values: UpdatedValuePair<IdType, InputValueType>): List<ObjectType> {
|
||||||
onBeforeUpdate(*values)
|
onBeforeUpdate(*values)
|
||||||
return (
|
return (
|
||||||
transaction(database) {
|
newSuspendedTransaction(db = database) {
|
||||||
values.map { (id, value) -> updateWithoutNotification(id, value) }
|
values.map { (id, value) -> updateWithoutNotification(id, value) }
|
||||||
}.filter {
|
}.filter {
|
||||||
it != null
|
it != null
|
||||||
@ -96,7 +97,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
protected open suspend fun onBeforeDelete(vararg ids: IdType) {}
|
protected open suspend fun onBeforeDelete(vararg ids: IdType) {}
|
||||||
override suspend fun deleteById(vararg ids: IdType) {
|
override suspend fun deleteById(vararg ids: IdType) {
|
||||||
onBeforeDelete(*ids)
|
onBeforeDelete(*ids)
|
||||||
transaction(database) {
|
newSuspendedTransaction(db = database) {
|
||||||
deleteWhere(null, null) {
|
deleteWhere(null, null) {
|
||||||
selectByIds(ids)
|
selectByIds(ids)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import kotlinx.coroutines.channels.Channel
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
||||||
@ -24,7 +25,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
|||||||
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(k: Key, v: Value) {
|
||||||
transaction(database) {
|
newSuspendedTransaction(db = database) {
|
||||||
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
||||||
update({ keyColumn.eq(k) }) {
|
update({ keyColumn.eq(k) }) {
|
||||||
it[valueColumn] = v
|
it[valueColumn] = v
|
||||||
@ -40,7 +41,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(k: Key) {
|
||||||
transaction(database) {
|
newSuspendedTransaction(db = database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
deleteWhere { keyColumn.eq(k) }
|
||||||
}
|
}
|
||||||
onValueRemovedChannel.send(k)
|
onValueRemovedChannel.send(k)
|
||||||
|
@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.exposed.commons.paginate
|
|||||||
import com.insanusmokrassar.postssystem.utils.repos.StandardReadKeyValueRepo
|
import com.insanusmokrassar.postssystem.utils.repos.StandardReadKeyValueRepo
|
||||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
|
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
|
abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
|
||||||
@ -13,23 +14,23 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
|
|||||||
) : StandardReadKeyValueRepo<Key, Value>, Table() {
|
) : StandardReadKeyValueRepo<Key, Value>, Table() {
|
||||||
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
|
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)
|
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()
|
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 {
|
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
it[keyColumn]
|
it[keyColumn]
|
||||||
}
|
}
|
||||||
}.createPaginationResult(pagination, count())
|
}.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 {
|
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
it[valueColumn]
|
it[valueColumn]
|
||||||
}
|
}
|
||||||
|
@ -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.budgetmanager.core.utils.repo.onetomany.ColumnAllocator
|
||||||
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
|
import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
||||||
@ -16,7 +17,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
|||||||
database
|
database
|
||||||
) {
|
) {
|
||||||
override suspend fun add(k: Key, v: Value) {
|
override suspend fun add(k: Key, v: Value) {
|
||||||
transaction(database) {
|
newSuspendedTransaction(db = database) {
|
||||||
insert {
|
insert {
|
||||||
it[keyColumn] = k
|
it[keyColumn] = k
|
||||||
it[valueColumn] = v
|
it[valueColumn] = v
|
||||||
@ -25,10 +26,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: 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) {
|
override suspend fun clear(k: Key) {
|
||||||
transaction(database) { deleteWhere { keyColumn.eq(k) } }
|
newSuspendedTransaction(db = database) { deleteWhere { keyColumn.eq(k) } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.exposed.commons.paginate
|
|||||||
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
|
import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
|
||||||
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
|
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
typealias ColumnAllocator<T> = Table.() -> Column<T>
|
typealias ColumnAllocator<T> = Table.() -> Column<T>
|
||||||
@ -16,33 +17,33 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
|
|||||||
protected val keyColumn: Column<Key> = keyColumnAllocator()
|
protected val keyColumn: Column<Key> = keyColumnAllocator()
|
||||||
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
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(
|
override suspend fun get(
|
||||||
k: Key,
|
k: Key,
|
||||||
pagination: Pagination,
|
pagination: Pagination,
|
||||||
reversed: Boolean
|
reversed: Boolean
|
||||||
): PaginationResult<Value> = transaction(database) {
|
): PaginationResult<Value> = newSuspendedTransaction(db = database) {
|
||||||
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
|
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
|
||||||
}.createPaginationResult(
|
}.createPaginationResult(
|
||||||
pagination,
|
pagination,
|
||||||
count(k)
|
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] }
|
selectAll().paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
|
||||||
}.createPaginationResult(
|
}.createPaginationResult(
|
||||||
pagination,
|
pagination,
|
||||||
count()
|
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()
|
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()
|
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user