return old transactions

This commit is contained in:
InsanusMokrassar 2020-07-31 11:28:27 +06:00
parent 0820c6d691
commit a683dab358
20 changed files with 75 additions and 99 deletions

View File

@ -1,2 +1 @@
exposed_version=0.23.1
test_sqlite_version=3.28.0

View File

@ -4,14 +4,13 @@ import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.ContentRepo
import com.insanusmokrassar.postssystem.core.exposed.content.*
import com.insanusmokrassar.postssystem.core.generateContentId
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import com.insanusmokrassar.postssystem.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import kotlinx.coroutines.channels.BroadcastChannel
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
private val Content.type
@ -58,14 +57,14 @@ private class ContentRepoDatabaseTable(
is SpecialContent -> specialHolder.putContent(id, content)
}
}
override suspend fun getContent(id: ContentId): Content? = newSuspendedTransaction(
override suspend fun getContent(id: ContentId): Content? = transaction(
db = database
) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn)
} ?.holder ?.getContent(id)
override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction(
transaction(
db = database
) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn)
@ -75,7 +74,7 @@ private class ContentRepoDatabaseTable(
override suspend fun registerContent(content: Content): RegisteredContent? {
val id = generateContentId()
val type = content.type
return newSuspendedTransaction(
return transaction(
db = database
) {
insert {
@ -96,7 +95,7 @@ private class ContentRepoDatabaseTable(
}
override suspend fun deleteContent(id: ContentId): Boolean {
val content = getContentById(id) ?: return false
return newSuspendedTransaction(
return transaction(
db = database
) {
deleteWhere {
@ -118,7 +117,7 @@ private class ContentRepoDatabaseTable(
}
override suspend fun getContentsIds(): Set<ContentId> {
return newSuspendedTransaction(
return transaction(
db = database
) {
selectAll().map { it[idColumn] }
@ -126,14 +125,14 @@ private class ContentRepoDatabaseTable(
}
override suspend fun getContentById(id: ContentId): RegisteredContent? {
val content = getContent(id) ?: return null
return newSuspendedTransaction(
return transaction(
db = database
) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.asRegisteredContent(content)
}
}
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return newSuspendedTransaction(
return transaction(
db = database
) {
selectAll().count() to selectAll().paginate(pagination).map { it[idColumn] }

View File

@ -4,15 +4,14 @@ import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.generatePostId
import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import com.insanusmokrassar.postssystem.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import com.soywiz.klock.*
import kotlinx.coroutines.channels.BroadcastChannel
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
private class PostsRepoContentRelations(
@ -29,24 +28,24 @@ private class PostsRepoContentRelations(
}
}
suspend fun getPostContents(postId: PostId): List<ContentId> {
return newSuspendedTransaction(
fun getPostContents(postId: PostId): List<ContentId> {
return transaction(
db = database
) {
select { postIdColumn.eq(postId) }.map { it[contentIdColumn] }
}
}
suspend fun getContentPosts(contentId: ContentId): List<PostId> {
return newSuspendedTransaction(
fun getContentPosts(contentId: ContentId): List<PostId> {
return transaction(
db = database
) {
select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] }
}
}
suspend fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) {
newSuspendedTransaction(
fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) {
transaction(
db = database
) {
val leftToPut = contentIds.toSet() - getPostContents(postId)
@ -58,8 +57,8 @@ private class PostsRepoContentRelations(
}
}
}
suspend fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean {
return newSuspendedTransaction(
fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean {
return transaction(
db = database
) {
deleteWhere {
@ -92,14 +91,12 @@ private class PostsRepoDatabaseTable(
override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow()
init {
transaction (
db = database
) {
transaction (db = database) {
SchemaUtils.createMissingTablesAndColumns(this@PostsRepoDatabaseTable)
}
}
private suspend fun ResultRow.toRegisteredPost(): RegisteredPost = get(idColumn).let { id ->
private fun ResultRow.toRegisteredPost(): RegisteredPost = get(idColumn).let { id ->
SimpleRegisteredPost(
id,
contentsTable.getPostContents(id),
@ -109,7 +106,7 @@ private class PostsRepoDatabaseTable(
override suspend fun createPost(post: Post): RegisteredPost? {
val id = generatePostId()
return newSuspendedTransaction(
return transaction(
db = database
) {
insert {
@ -124,7 +121,7 @@ private class PostsRepoDatabaseTable(
override suspend fun deletePost(id: PostId): Boolean {
val post = getPostById(id) ?: return false
return (newSuspendedTransaction(
return (transaction(
db = database
) {
deleteWhere { idColumn.eq(id) }
@ -137,7 +134,7 @@ private class PostsRepoDatabaseTable(
}
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean {
return newSuspendedTransaction(
return transaction(
db = database
) {
val alreadyLinked = contentsTable.getPostContents(postId)
@ -155,7 +152,7 @@ private class PostsRepoDatabaseTable(
}
}
override suspend fun getPostsIds(): Set<PostId> {
return newSuspendedTransaction(
return transaction(
db = database
) {
selectAll().map { it[idColumn] }.toSet()
@ -163,7 +160,7 @@ private class PostsRepoDatabaseTable(
}
override suspend fun getPostById(id: PostId): RegisteredPost? {
return newSuspendedTransaction(
return transaction(
db = database
) {
select { idColumn.eq(id) }.firstOrNull() ?.toRegisteredPost()
@ -171,7 +168,7 @@ private class PostsRepoDatabaseTable(
}
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> {
return newSuspendedTransaction(
return transaction(
db = database
) {
val postsIds = contentsTable.getContentPosts(id)
@ -180,7 +177,7 @@ private class PostsRepoDatabaseTable(
}
override suspend fun getPostsByCreatingDates(from: DateTime, to: DateTime): List<RegisteredPost> {
return newSuspendedTransaction(
return transaction(
db = database
) {
select { creationDateColumn.between(from, to) }.map { it.toRegisteredPost() }
@ -188,7 +185,7 @@ private class PostsRepoDatabaseTable(
}
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> {
return newSuspendedTransaction(
return transaction(
db = database
) {
val posts = selectAll().paginate(pagination).orderBy(creationDateColumn).map {

View File

@ -4,7 +4,6 @@ import com.insanusmokrassar.postssystem.core.content.BinaryContent
import com.insanusmokrassar.postssystem.core.content.ContentId
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
private class BinaryContentHolderRepoTable(
@ -24,7 +23,7 @@ private class BinaryContentHolderRepoTable(
}
}
override suspend fun getContent(id: ContentId): BinaryContent? = newSuspendedTransaction (
override suspend fun getContent(id: ContentId): BinaryContent? = transaction (
db = database
) {
select {
@ -41,7 +40,7 @@ private class BinaryContentHolderRepoTable(
}
override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction(
transaction(
db = database
) {
deleteWhere { idColumn.eq(id) }
@ -49,7 +48,7 @@ private class BinaryContentHolderRepoTable(
}
override suspend fun putContent(id: ContentId, content: BinaryContent) {
newSuspendedTransaction(
transaction(
db = database
) {
insert {

View File

@ -3,7 +3,6 @@ package com.insanusmokrassar.postssystem.core.exposed.content
import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.content.SpecialContent
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
private class SpecialContentHolderRepoTable(
@ -21,7 +20,7 @@ private class SpecialContentHolderRepoTable(
}
}
override suspend fun getContent(id: ContentId): SpecialContent? = newSuspendedTransaction(
override suspend fun getContent(id: ContentId): SpecialContent? = transaction(
db = database
) {
select {
@ -32,7 +31,7 @@ private class SpecialContentHolderRepoTable(
}
override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction(
transaction(
db = database
) {
deleteWhere { idColumn.eq(id) }
@ -40,7 +39,7 @@ private class SpecialContentHolderRepoTable(
}
override suspend fun putContent(id: ContentId, content: SpecialContent) {
newSuspendedTransaction(
transaction(
db = database
) {
insert {

View File

@ -3,7 +3,6 @@ package com.insanusmokrassar.postssystem.core.exposed.content
import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.content.TextContent
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
private class TextContentHolderRepoTable(
@ -21,7 +20,7 @@ private class TextContentHolderRepoTable(
}
}
override suspend fun getContent(id: ContentId): TextContent? = newSuspendedTransaction(
override suspend fun getContent(id: ContentId): TextContent? = transaction(
db = database
) {
select {
@ -32,7 +31,7 @@ private class TextContentHolderRepoTable(
}
override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction(
transaction(
db = database
) {
deleteWhere { idColumn.eq(id) }
@ -40,7 +39,7 @@ private class TextContentHolderRepoTable(
}
override suspend fun putContent(id: ContentId, content: TextContent) {
newSuspendedTransaction(
transaction(
db = database
) {
insert {

View File

@ -1,7 +1,7 @@
package com.insanusmokrassar.postssystem.core.exposed
import com.insanusmokrassar.postssystem.core.post.SimplePost
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.transactionManager
import java.io.File

View File

@ -1,2 +1 @@
exposed_version=0.23.1
test_sqlite_version=3.28.0

View File

@ -4,7 +4,6 @@ import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.publishing.TriggerControlKey
import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
class ExposedPublishingKeysRepo(
@ -22,29 +21,31 @@ class ExposedPublishingKeysRepo(
}
}
override suspend fun getPostIdByTriggerControlKey(key: TriggerControlKey): PostId? = newSuspendedTransaction(
override suspend fun getPostIdByTriggerControlKey(key: TriggerControlKey): PostId? = transaction(
db = database
) {
select { triggerControlKeyColumn.eq(key) }.limit(1).firstOrNull() ?.getOrNull(postIdColumn)
}
override suspend fun getTriggerControlKeyByPostId(postId: PostId): TriggerControlKey? = newSuspendedTransaction(
override suspend fun getTriggerControlKeyByPostId(postId: PostId): TriggerControlKey? = transaction(
db = database
) {
select { postIdColumn.eq(postId) }.limit(1).firstOrNull() ?.getOrNull(triggerControlKeyColumn)
}
override suspend fun setPostTriggerControlKey(postId: PostId, key: TriggerControlKey): Boolean = newSuspendedTransaction(
db = database
) {
override suspend fun setPostTriggerControlKey(postId: PostId, key: TriggerControlKey): Boolean {
unsetPostTriggerControlKey(postId)
insert {
it[postIdColumn] = postId
it[triggerControlKeyColumn] = triggerControlKeyColumn
}.getOrNull(postIdColumn) == postId
return transaction(
db = database
) {
insert {
it[postIdColumn] = postId
it[triggerControlKeyColumn] = triggerControlKeyColumn
}.getOrNull(postIdColumn) == postId
}
}
override suspend fun unsetPostTriggerControlKey(postId: PostId): Boolean = newSuspendedTransaction(
override suspend fun unsetPostTriggerControlKey(postId: PostId): Boolean = transaction(
db = database
) {
deleteWhere {

View File

@ -1,2 +1,2 @@
exposed_version=0.23.1
exposed_version=0.26.1
test_sqlite_version=3.28.0

View File

@ -32,8 +32,6 @@ repositories {
dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "org.jetbrains.exposed:exposed-core:$exposed_version"
api "org.jetbrains.exposed:exposed-jdbc:$exposed_version"
if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") {
api "com.insanusmokrassar:postssystem.utils.repos:$core_version"

View File

@ -1,2 +1 @@
exposed_version=0.23.1
test_sqlite_version=3.28.0

View File

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

View File

@ -8,7 +8,6 @@ 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>(
@ -42,7 +41,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun create(vararg values: InputValueType): List<ObjectType> {
onBeforeCreate(*values)
return newSuspendedTransaction(db = database) {
return transaction(db = database) {
values.map { value -> createWithoutNotification(value) }
}.also {
it.forEach {
@ -83,7 +82,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun update(vararg values: UpdatedValuePair<IdType, InputValueType>): List<ObjectType> {
onBeforeUpdate(*values)
return (
newSuspendedTransaction(db = database) {
transaction(db = database) {
values.map { (id, value) -> updateWithoutNotification(id, value) }
}.filter {
it != null
@ -97,7 +96,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
protected open suspend fun onBeforeDelete(vararg ids: IdType) {}
override suspend fun deleteById(vararg ids: IdType) {
onBeforeDelete(*ids)
newSuspendedTransaction(db = database) {
transaction(db = database) {
deleteWhere(null, null) {
selectByIds(ids)
}

View File

@ -1,9 +1,6 @@
package com.insanusmokrassar.postssystem.utils.repos.exposed
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SqlExpressionBuilder
import org.jetbrains.exposed.sql.*
interface ExposedCRUDRepo<ObjectType, IdType> {
val database: Database

View File

@ -1,8 +1,6 @@
package com.insanusmokrassar.postssystem.utils.repos.exposed
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
fun Table.initTable(database: Database) {

View File

@ -6,7 +6,6 @@ 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>(
@ -25,7 +24,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
override suspend fun set(k: Key, v: Value) {
newSuspendedTransaction(db = database) {
transaction(db = database) {
if (select { keyColumn.eq(k) }.limit(1).any()) {
update({ keyColumn.eq(k) }) {
it[valueColumn] = v
@ -41,7 +40,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
}
override suspend fun unset(k: Key) {
newSuspendedTransaction(db = database) {
transaction(db = database) {
deleteWhere { keyColumn.eq(k) }
}
onValueRemovedChannel.send(k)

View File

@ -4,7 +4,6 @@ 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>(
@ -14,23 +13,23 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
) : StandardReadKeyValueRepo<Key, Value>, Table() {
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
override suspend fun get(k: Key): Value? = newSuspendedTransaction(db = database) {
override suspend fun get(k: Key): Value? = transaction(db = database) {
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
}
override suspend fun contains(key: Key): Boolean = newSuspendedTransaction(db = database) {
override suspend fun contains(key: Key): Boolean = transaction(db = database) {
select { keyColumn.eq(key) }.limit(1).any()
}
override suspend fun count(): Long = newSuspendedTransaction(db = database) { selectAll().count() }
override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = newSuspendedTransaction(db = database) {
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(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> = newSuspendedTransaction(db = database) {
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(db = database) {
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
it[valueColumn]
}

View File

@ -4,7 +4,6 @@ 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>(
@ -17,7 +16,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
database
) {
override suspend fun add(k: Key, v: Value) {
newSuspendedTransaction(db = database) {
transaction(db = database) {
insert {
it[keyColumn] = k
it[valueColumn] = v
@ -26,10 +25,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
}
override suspend fun remove(k: Key, v: Value) {
newSuspendedTransaction(db = database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
transaction(db = database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
}
override suspend fun clear(k: Key) {
newSuspendedTransaction(db = database) { deleteWhere { keyColumn.eq(k) } }
transaction(db = database) { deleteWhere { keyColumn.eq(k) } }
}
}

View File

@ -4,7 +4,6 @@ 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>
@ -17,33 +16,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 = newSuspendedTransaction(db = database) { select { keyColumn.eq(k) }.count() }
override suspend fun count(k: Key): Long = transaction(db = database) { select { keyColumn.eq(k) }.count() }
override suspend fun count(): Long = newSuspendedTransaction(db = database) { selectAll().count() }
override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
override suspend fun get(
k: Key,
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> = newSuspendedTransaction(db = database) {
): PaginationResult<Value> = transaction(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> = newSuspendedTransaction(db = database) {
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(db = database) {
selectAll().paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
}.createPaginationResult(
pagination,
count()
)
override suspend fun contains(k: Key): Boolean = newSuspendedTransaction(db = database) {
override suspend fun contains(k: Key): Boolean = transaction(db = database) {
select { keyColumn.eq(k) }.limit(1).any()
}
override suspend fun contains(k: Key, v: Value): Boolean = newSuspendedTransaction(db = database) {
override suspend fun contains(k: Key, v: Value): Boolean = transaction(db = database) {
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
}
}