Skip to content

Commit

Permalink
Initially overhaul and reorganize the APIs
Browse files Browse the repository at this point in the history
Major changes:

1. move some code to finer-grained subpackages
1. initially overhaul the APIs related to the creation of Vert.x SQL clients and `DatabaseClient`s

   The `create...andSetRole` functions are removed, and their functionalities are merged into the `create...` functions.

1. add TODOs as guidance for the remaining work
  • Loading branch information
ShreckYe committed Nov 16, 2024
1 parent e959812 commit dfd0d3b
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.huanshankeji.exposedvertxsqlclient

import com.huanshankeji.exposedvertxsqlclient.vertx.sqlclient.setRole

// TODO make it not sealed with some common methods, for example, `SqlConnectOptions.setFrom`
sealed interface ConnectionConfig {
val userAndRole: String
val database: String
Expand All @@ -14,6 +17,9 @@ sealed interface ConnectionConfig {
override val userAndRole: String get() = user
}

/**
* @see setRole
*/
class UnixDomainSocketWithPeerAuthentication(
val path: String,
val role: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package com.huanshankeji.exposedvertxsqlclient
import arrow.core.*
import com.huanshankeji.collections.singleOrNullIfEmpty
import com.huanshankeji.exposedvertxsqlclient.ConnectionConfig.Socket
import com.huanshankeji.exposedvertxsqlclient.ConnectionConfig.UnixDomainSocketWithPeerAuthentication
import com.huanshankeji.exposedvertxsqlclient.local.LocalConnectionConfig
import com.huanshankeji.exposedvertxsqlclient.postgresql.exposed.exposedDatabaseConnectPostgreSql
import com.huanshankeji.exposedvertxsqlclient.sql.selectExpression
import com.huanshankeji.os.isCurrentOsLinux
import com.huanshankeji.vertx.kotlin.coroutines.coroutineToFuture
import com.huanshankeji.vertx.kotlin.sqlclient.executeBatchAwaitForSqlResultSequence
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
import io.vertx.kotlin.coroutines.coAwait
import io.vertx.kotlin.sqlclient.poolOptionsOf
import io.vertx.pgclient.PgConnectOptions
import io.vertx.pgclient.impl.PgPoolOptions
import io.vertx.sqlclient.*
import kotlinx.coroutines.coroutineScope
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.Query
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.Statement
import org.jetbrains.exposed.sql.statements.UpdateStatement
Expand Down Expand Up @@ -100,7 +100,7 @@ class DatabaseClient<out VertxSqlClient : SqlClient>(
fun <T> exposedTransaction(statement: ExposedTransaction.() -> T) =
transaction(exposedDatabase, statement)

private fun Statement<*>.prepareSqlAndLogIfNeeded(transaction: Transaction) =
private fun Statement<*>.prepareSqlAndLogIfNeeded(transaction: ExposedTransaction) =
prepareSQL(transaction).also {
if (logSql) logger.info("Prepared SQL: $it.")
}
Expand Down Expand Up @@ -464,46 +464,37 @@ suspend fun <SqlConnectionT : SqlConnection> DatabaseClient<SqlConnectionT>.with
): Boolean =
withSavepointAndRollbackIfThrowsOrLeft(savepointName) { if (function(it)) Unit.right() else Unit.left() }.isRight()

// TODO: use `ConnectionConfig` as the argument directly

// TODO move to the `postgresql` package

// can be used for a shared Exposed `Database` among `DatabaseClient`s
fun createPgPoolDatabaseClient(
vertx: Vertx?,
vertxSqlClientConnectionConfig: ConnectionConfig,
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, poolOptions: PoolOptions = poolOptionsOf(),
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, extraPgPoolOptions: PgPoolOptions.() -> Unit = {},
exposedDatabase: Database
): DatabaseClient<Pool> =
DatabaseClient(
with(vertxSqlClientConnectionConfig) {
when (this) {
is Socket ->
createSocketPgPool(vertx, host, port, database, user, password, extraPgConnectOptions, poolOptions)

is UnixDomainSocketWithPeerAuthentication ->
createPeerAuthenticationUnixDomainSocketPgPoolAndSetRole(
vertx, path, database, role, extraPgConnectOptions, poolOptions
)
}
},
createPgPool(vertx, vertxSqlClientConnectionConfig, extraPgConnectOptions, extraPgPoolOptions),
exposedDatabase
)

fun createPgPoolDatabaseClient(
vertx: Vertx?,
vertxSqlClientConnectionConfig: ConnectionConfig,
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, poolOptions: PoolOptions = poolOptionsOf(),
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, extraPgPoolOptions: PgPoolOptions.() -> Unit = {},
exposedSocketConnectionConfig: Socket
): DatabaseClient<Pool> =
createPgPoolDatabaseClient(
vertx, vertxSqlClientConnectionConfig, extraPgConnectOptions, poolOptions,
vertx, vertxSqlClientConnectionConfig, extraPgConnectOptions, extraPgPoolOptions,
exposedDatabaseConnectPostgreSql(exposedSocketConnectionConfig)
)

/** It may be more efficient to use a single shared [Database] to generate SQLs for multiple [DatabaseClient]s/[SqlClient]s. */
fun createPgPoolDatabaseClient(
vertx: Vertx?,
vertxSqlClientConnectionType: ConnectionType, localConnectionConfig: LocalConnectionConfig,
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, poolOptions: PoolOptions = poolOptionsOf(),
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, extraPgPoolOptions: PgPoolOptions.() -> Unit = {},
exposedDatabase: Database? = null
) =
with(localConnectionConfig) {
Expand All @@ -514,24 +505,24 @@ fun createPgPoolDatabaseClient(

if (exposedDatabase === null)
createPgPoolDatabaseClient(
vertx, connectionConfig, extraPgConnectOptions, poolOptions, socketConnectionConfig
vertx, connectionConfig, extraPgConnectOptions, extraPgPoolOptions, socketConnectionConfig
)
else
createPgPoolDatabaseClient(
vertx, connectionConfig, extraPgConnectOptions, poolOptions, exposedDatabase
vertx, connectionConfig, extraPgConnectOptions, extraPgPoolOptions, exposedDatabase
)
}

fun createBetterPgPoolDatabaseClient(
vertx: Vertx?,
localConnectionConfig: LocalConnectionConfig,
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, poolOptions: PoolOptions = poolOptionsOf(),
extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, extraPgPoolOptions: PgPoolOptions.() -> Unit = {},
exposedDatabase: Database? = null
) =
createPgPoolDatabaseClient(
vertx,
if (isCurrentOsLinux()) ConnectionType.UnixDomainSocketWithPeerAuthentication else ConnectionType.Socket,
localConnectionConfig,
extraPgConnectOptions, poolOptions,
extraPgConnectOptions, extraPgPoolOptions,
exposedDatabase
)
Loading

0 comments on commit dfd0d3b

Please sign in to comment.