Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ allprojects {
}
}

version = "0.0.4"
version = "0.0.5"
group = "io.github.ustudiocompany"

configurations.all {
Expand Down
1 change: 1 addition & 0 deletions library/jdbc/core/api/jdbc-core-library.api
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public final class io/github/ustudiocompany/uframework/jdbc/sql/ParametrizedSql
public synthetic fun <init> (Ljava/lang/String;Ljava/util/Map;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getParameters ()Ljava/util/Map;
public final fun getValue ()Ljava/lang/String;
public fun toString ()Ljava/lang/String;
}

public final class io/github/ustudiocompany/uframework/jdbc/sql/ParametrizedSql$Companion {
Expand Down
3 changes: 3 additions & 0 deletions library/jdbc/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ plugins {

dependencies {
implementation(project(":failure-library"))
implementation(project(":logging-slf4j-extension-library"))

implementation(libs.airflux.commons.types) { isChanging = true }
implementation(libs.postgresql)
implementation(libs.slf4j.api)
implementation(libs.logback.classic)

/* Tests */
testImplementation(project(":jdbc-kotest-matchers-library"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public class ParametrizedSql private constructor(
public val parameters: Map<String, Int>
) {

override fun toString(): String {
return "Sql: '${value.trim()}' \n " +
"Parameters: ${parameters.entries.joinToString(", ") { "${it.key}: ${it.value}" }}"
}

public companion object {
private const val PARAMETER_NAME_PLACEHOLDER = "?"
private const val PREFIX_PARAMETER_NAME = ':'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("ImportOrdering")

package io.github.ustudiocompany.uframework.jdbc.transaction

import io.github.airflux.commons.types.maybe.Maybe
Expand All @@ -12,28 +14,41 @@ import io.github.ustudiocompany.uframework.jdbc.statement.JDBCNamedPreparedState
import io.github.ustudiocompany.uframework.jdbc.statement.JDBCPreparedStatement
import io.github.ustudiocompany.uframework.jdbc.statement.JDBCPreparedStatementInstance
import io.github.ustudiocompany.uframework.jdbc.statement.JDBCStatement
import io.github.ustudiocompany.uframework.telemetry.logging.logger.slf4jextension.debug
import io.github.ustudiocompany.uframework.telemetry.logging.logger.slf4jextension.error
import io.github.ustudiocompany.uframework.telemetry.logging.logger.slf4jextension.warn
import java.sql.Connection
import java.sql.PreparedStatement
import org.slf4j.LoggerFactory

internal class TransactionInstance(
private val unwrappedConnection: Connection,
) : Transaction, JDBCConnection {

private val logger = LoggerFactory.getLogger(TransactionInstance::class.java)

override val connection: JDBCConnection
get() = this

override fun commit(): Maybe<JDBCError> = Maybe.catch(
catch = { exception ->
JDBCError(description = "Error while committing transaction", exception = exception)
val errorDescription = "Error while committing transaction."
logger.error { errorDescription }
JDBCError(description = errorDescription, exception = exception)
},
block = { unwrappedConnection.commit() }
)

override fun rollback(): Maybe<JDBCError> = Maybe.catch(
catch = { exception ->
JDBCError(description = "Error while rolling back transaction", exception = exception)
val errorDescription = "Error while rolling back transaction."
logger.error { errorDescription }
JDBCError(description = errorDescription, exception = exception)
},
block = { unwrappedConnection.rollback() }
block = {
logger.warn { "Transaction would be rolled back." }
unwrappedConnection.rollback()
}
)

override fun close() {
Expand All @@ -48,18 +63,22 @@ internal class TransactionInstance(
override fun preparedStatement(
sql: String,
timeout: JDBCStatement.Timeout
): JDBCResult<JDBCPreparedStatement> =
prepareStatement(sql, timeout)
): JDBCResult<JDBCPreparedStatement> {
logger.debug { "Executing Query: \n $sql" }
return prepareStatement(sql, timeout)
.map { statement -> JDBCPreparedStatementInstance(statement = statement) }
}

override fun namedPreparedStatement(
sql: ParametrizedSql,
timeout: JDBCStatement.Timeout
): JDBCResult<JDBCNamedPreparedStatement> =
prepareStatement(sql.value, timeout)
): JDBCResult<JDBCNamedPreparedStatement> {
logger.debug { "Executing ParametrizedSql: \n $sql" }
return prepareStatement(sql.value, timeout)
.map { statement ->
JDBCNamedPreparedStatementInstance(parameters = sql.parameters, statement = statement)
}
}

private fun prepareStatement(
sql: String,
Expand Down
Loading