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 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.content.api.ContentRepo
import com.insanusmokrassar.postssystem.core.exposed.content.* import com.insanusmokrassar.postssystem.core.exposed.content.*
import com.insanusmokrassar.postssystem.core.generateContentId import com.insanusmokrassar.postssystem.core.generateContentId
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import com.insanusmokrassar.postssystem.exposed.commons.paginate import com.insanusmokrassar.postssystem.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel 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
private val Content.type private val Content.type
@ -58,14 +57,14 @@ private class ContentRepoDatabaseTable(
is SpecialContent -> specialHolder.putContent(id, content) is SpecialContent -> specialHolder.putContent(id, content)
} }
} }
override suspend fun getContent(id: ContentId): Content? = newSuspendedTransaction( override suspend fun getContent(id: ContentId): Content? = transaction(
db = database db = database
) { ) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn) select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn)
} ?.holder ?.getContent(id) } ?.holder ?.getContent(id)
override suspend fun removeContent(id: ContentId) { override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn) select { idColumn.eq(id) }.limit(1).firstOrNull() ?.get(typeColumn)
@ -75,7 +74,7 @@ private class ContentRepoDatabaseTable(
override suspend fun registerContent(content: Content): RegisteredContent? { override suspend fun registerContent(content: Content): RegisteredContent? {
val id = generateContentId() val id = generateContentId()
val type = content.type val type = content.type
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
insert { insert {
@ -96,7 +95,7 @@ private class ContentRepoDatabaseTable(
} }
override suspend fun deleteContent(id: ContentId): Boolean { override suspend fun deleteContent(id: ContentId): Boolean {
val content = getContentById(id) ?: return false val content = getContentById(id) ?: return false
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
deleteWhere { deleteWhere {
@ -118,7 +117,7 @@ private class ContentRepoDatabaseTable(
} }
override suspend fun getContentsIds(): Set<ContentId> { override suspend fun getContentsIds(): Set<ContentId> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
selectAll().map { it[idColumn] } selectAll().map { it[idColumn] }
@ -126,14 +125,14 @@ private class ContentRepoDatabaseTable(
} }
override suspend fun getContentById(id: ContentId): RegisteredContent? { override suspend fun getContentById(id: ContentId): RegisteredContent? {
val content = getContent(id) ?: return null val content = getContent(id) ?: return null
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
select { idColumn.eq(id) }.limit(1).firstOrNull() ?.asRegisteredContent(content) select { idColumn.eq(id) }.limit(1).firstOrNull() ?.asRegisteredContent(content)
} }
} }
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> { override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
selectAll().count() to selectAll().paginate(pagination).map { it[idColumn] } 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.generatePostId
import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo 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.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import com.soywiz.klock.* import com.soywiz.klock.*
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel 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
private class PostsRepoContentRelations( private class PostsRepoContentRelations(
@ -29,24 +28,24 @@ private class PostsRepoContentRelations(
} }
} }
suspend fun getPostContents(postId: PostId): List<ContentId> { fun getPostContents(postId: PostId): List<ContentId> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
select { postIdColumn.eq(postId) }.map { it[contentIdColumn] } select { postIdColumn.eq(postId) }.map { it[contentIdColumn] }
} }
} }
suspend fun getContentPosts(contentId: ContentId): List<PostId> { fun getContentPosts(contentId: ContentId): List<PostId> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] } select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] }
} }
} }
suspend fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) { fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
val leftToPut = contentIds.toSet() - getPostContents(postId) val leftToPut = contentIds.toSet() - getPostContents(postId)
@ -58,8 +57,8 @@ private class PostsRepoContentRelations(
} }
} }
} }
suspend fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean { fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
deleteWhere { deleteWhere {
@ -92,14 +91,12 @@ private class PostsRepoDatabaseTable(
override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow() override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow()
init { init {
transaction ( transaction (db = database) {
db = database
) {
SchemaUtils.createMissingTablesAndColumns(this@PostsRepoDatabaseTable) SchemaUtils.createMissingTablesAndColumns(this@PostsRepoDatabaseTable)
} }
} }
private suspend fun ResultRow.toRegisteredPost(): RegisteredPost = get(idColumn).let { id -> private fun ResultRow.toRegisteredPost(): RegisteredPost = get(idColumn).let { id ->
SimpleRegisteredPost( SimpleRegisteredPost(
id, id,
contentsTable.getPostContents(id), contentsTable.getPostContents(id),
@ -109,7 +106,7 @@ private class PostsRepoDatabaseTable(
override suspend fun createPost(post: Post): RegisteredPost? { override suspend fun createPost(post: Post): RegisteredPost? {
val id = generatePostId() val id = generatePostId()
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
insert { insert {
@ -124,7 +121,7 @@ private class PostsRepoDatabaseTable(
override suspend fun deletePost(id: PostId): Boolean { override suspend fun deletePost(id: PostId): Boolean {
val post = getPostById(id) ?: return false val post = getPostById(id) ?: return false
return (newSuspendedTransaction( return (transaction(
db = database db = database
) { ) {
deleteWhere { idColumn.eq(id) } deleteWhere { idColumn.eq(id) }
@ -137,7 +134,7 @@ private class PostsRepoDatabaseTable(
} }
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean { override suspend fun updatePostContent(postId: PostId, post: Post): Boolean {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
val alreadyLinked = contentsTable.getPostContents(postId) val alreadyLinked = contentsTable.getPostContents(postId)
@ -155,7 +152,7 @@ private class PostsRepoDatabaseTable(
} }
} }
override suspend fun getPostsIds(): Set<PostId> { override suspend fun getPostsIds(): Set<PostId> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
selectAll().map { it[idColumn] }.toSet() selectAll().map { it[idColumn] }.toSet()
@ -163,7 +160,7 @@ private class PostsRepoDatabaseTable(
} }
override suspend fun getPostById(id: PostId): RegisteredPost? { override suspend fun getPostById(id: PostId): RegisteredPost? {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
select { idColumn.eq(id) }.firstOrNull() ?.toRegisteredPost() select { idColumn.eq(id) }.firstOrNull() ?.toRegisteredPost()
@ -171,7 +168,7 @@ private class PostsRepoDatabaseTable(
} }
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> { override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
val postsIds = contentsTable.getContentPosts(id) val postsIds = contentsTable.getContentPosts(id)
@ -180,7 +177,7 @@ private class PostsRepoDatabaseTable(
} }
override suspend fun getPostsByCreatingDates(from: DateTime, to: DateTime): List<RegisteredPost> { override suspend fun getPostsByCreatingDates(from: DateTime, to: DateTime): List<RegisteredPost> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
select { creationDateColumn.between(from, to) }.map { it.toRegisteredPost() } select { creationDateColumn.between(from, to) }.map { it.toRegisteredPost() }
@ -188,7 +185,7 @@ private class PostsRepoDatabaseTable(
} }
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> { override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> {
return newSuspendedTransaction( return transaction(
db = database db = database
) { ) {
val posts = selectAll().paginate(pagination).orderBy(creationDateColumn).map { 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 com.insanusmokrassar.postssystem.core.content.ContentId
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.api.ExposedBlob import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
private class BinaryContentHolderRepoTable( 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 db = database
) { ) {
select { select {
@ -41,7 +40,7 @@ private class BinaryContentHolderRepoTable(
} }
override suspend fun removeContent(id: ContentId) { override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
deleteWhere { idColumn.eq(id) } deleteWhere { idColumn.eq(id) }
@ -49,7 +48,7 @@ private class BinaryContentHolderRepoTable(
} }
override suspend fun putContent(id: ContentId, content: BinaryContent) { override suspend fun putContent(id: ContentId, content: BinaryContent) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
insert { 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.ContentId
import com.insanusmokrassar.postssystem.core.content.SpecialContent import com.insanusmokrassar.postssystem.core.content.SpecialContent
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
private class SpecialContentHolderRepoTable( 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 db = database
) { ) {
select { select {
@ -32,7 +31,7 @@ private class SpecialContentHolderRepoTable(
} }
override suspend fun removeContent(id: ContentId) { override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
deleteWhere { idColumn.eq(id) } deleteWhere { idColumn.eq(id) }
@ -40,7 +39,7 @@ private class SpecialContentHolderRepoTable(
} }
override suspend fun putContent(id: ContentId, content: SpecialContent) { override suspend fun putContent(id: ContentId, content: SpecialContent) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
insert { 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.ContentId
import com.insanusmokrassar.postssystem.core.content.TextContent import com.insanusmokrassar.postssystem.core.content.TextContent
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
private class TextContentHolderRepoTable( 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 db = database
) { ) {
select { select {
@ -32,7 +31,7 @@ private class TextContentHolderRepoTable(
} }
override suspend fun removeContent(id: ContentId) { override suspend fun removeContent(id: ContentId) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
deleteWhere { idColumn.eq(id) } deleteWhere { idColumn.eq(id) }
@ -40,7 +39,7 @@ private class TextContentHolderRepoTable(
} }
override suspend fun putContent(id: ContentId, content: TextContent) { override suspend fun putContent(id: ContentId, content: TextContent) {
newSuspendedTransaction( transaction(
db = database db = database
) { ) {
insert { insert {

View File

@ -1,7 +1,7 @@
package com.insanusmokrassar.postssystem.core.exposed package com.insanusmokrassar.postssystem.core.exposed
import com.insanusmokrassar.postssystem.core.post.SimplePost 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.Database
import org.jetbrains.exposed.sql.transactions.transactionManager import org.jetbrains.exposed.sql.transactions.transactionManager
import java.io.File import java.io.File

View File

@ -1,2 +1 @@
exposed_version=0.23.1
test_sqlite_version=3.28.0 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.TriggerControlKey
import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
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
class ExposedPublishingKeysRepo( 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 db = database
) { ) {
select { triggerControlKeyColumn.eq(key) }.limit(1).firstOrNull() ?.getOrNull(postIdColumn) 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 db = database
) { ) {
select { postIdColumn.eq(postId) }.limit(1).firstOrNull() ?.getOrNull(triggerControlKeyColumn) select { postIdColumn.eq(postId) }.limit(1).firstOrNull() ?.getOrNull(triggerControlKeyColumn)
} }
override suspend fun setPostTriggerControlKey(postId: PostId, key: TriggerControlKey): Boolean = newSuspendedTransaction( override suspend fun setPostTriggerControlKey(postId: PostId, key: TriggerControlKey): Boolean {
db = database
) {
unsetPostTriggerControlKey(postId) unsetPostTriggerControlKey(postId)
insert { return transaction(
it[postIdColumn] = postId db = database
it[triggerControlKeyColumn] = triggerControlKeyColumn ) {
}.getOrNull(postIdColumn) == postId 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 db = database
) { ) {
deleteWhere { deleteWhere {

View File

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

View File

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

View File

@ -1,2 +1 @@
exposed_version=0.23.1
test_sqlite_version=3.28.0 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.exposed.commons.paginate
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.pagination.* import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.*
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 import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>( abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
@ -17,7 +14,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 newSuspendedTransaction(db = database) { return transaction(db = database) {
selectAll().paginate(pagination).map { selectAll().paginate(pagination).map {
it.asObject it.asObject
}.createPaginationResult( }.createPaginationResult(
@ -27,14 +24,14 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
} }
} }
override suspend fun getById(id: IdType): ObjectType? { override suspend fun getById(id: IdType): ObjectType? {
return newSuspendedTransaction(db = database) { return transaction(db = database) {
select { select {
selectById(id) selectById(id)
}.limit(1).firstOrNull() ?.asObject }.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() 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.*
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>(
@ -42,7 +41,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 newSuspendedTransaction(db = database) { return transaction(db = database) {
values.map { value -> createWithoutNotification(value) } values.map { value -> createWithoutNotification(value) }
}.also { }.also {
it.forEach { it.forEach {
@ -83,7 +82,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 (
newSuspendedTransaction(db = database) { transaction(db = database) {
values.map { (id, value) -> updateWithoutNotification(id, value) } values.map { (id, value) -> updateWithoutNotification(id, value) }
}.filter { }.filter {
it != null it != null
@ -97,7 +96,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)
newSuspendedTransaction(db = database) { transaction(db = database) {
deleteWhere(null, null) { deleteWhere(null, null) {
selectByIds(ids) selectByIds(ids)
} }

View File

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

View File

@ -1,8 +1,6 @@
package com.insanusmokrassar.postssystem.utils.repos.exposed package com.insanusmokrassar.postssystem.utils.repos.exposed
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
fun Table.initTable(database: Database) { 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.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>(
@ -25,7 +24,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) {
newSuspendedTransaction(db = database) { transaction(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
@ -41,7 +40,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
} }
override suspend fun unset(k: Key) { override suspend fun unset(k: Key) {
newSuspendedTransaction(db = database) { transaction(db = database) {
deleteWhere { keyColumn.eq(k) } deleteWhere { keyColumn.eq(k) }
} }
onValueRemovedChannel.send(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.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>(
@ -14,23 +13,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? = newSuspendedTransaction(db = database) { override suspend fun get(k: Key): Value? = transaction(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 = newSuspendedTransaction(db = database) { override suspend fun contains(key: Key): Boolean = transaction(db = database) {
select { keyColumn.eq(key) }.limit(1).any() 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 { 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> = 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 { selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
it[valueColumn] 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.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>(
@ -17,7 +16,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
database database
) { ) {
override suspend fun add(k: Key, v: Value) { override suspend fun add(k: Key, v: Value) {
newSuspendedTransaction(db = database) { transaction(db = database) {
insert { insert {
it[keyColumn] = k it[keyColumn] = k
it[valueColumn] = v it[valueColumn] = v
@ -26,10 +25,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
} }
override suspend fun remove(k: Key, v: 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) { 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.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>
@ -17,33 +16,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 = 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( override suspend fun get(
k: Key, k: Key,
pagination: Pagination, pagination: Pagination,
reversed: Boolean reversed: Boolean
): PaginationResult<Value> = newSuspendedTransaction(db = database) { ): PaginationResult<Value> = transaction(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> = newSuspendedTransaction(db = database) { override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(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 = newSuspendedTransaction(db = database) { override suspend fun contains(k: Key): Boolean = transaction(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 = 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() select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
} }
} }