diff --git a/mysql/api/exposed-vertx-sql-client-postgresql.api b/mysql/api/exposed-vertx-sql-client-postgresql.api new file mode 100644 index 0000000..4926d01 --- /dev/null +++ b/mysql/api/exposed-vertx-sql-client-postgresql.api @@ -0,0 +1,27 @@ +public final class com/huanshankeji/exposedvertxsqlclient/postgresql/DatabaseClientKt { + public static final fun withPgTransaction (Lcom/huanshankeji/exposedvertxsqlclient/DatabaseClient;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class com/huanshankeji/exposedvertxsqlclient/postgresql/exposed/ExposedDatabasesKt { + public static final fun exposedDatabaseConnectPostgresql (Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig$Socket;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun exposedDatabaseConnectPostgresql$default (Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig$Socket;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; + public static final fun exposedDatabaseConnectPostgresqlWithParameterConnectionConfig (Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig$Socket;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun exposedDatabaseConnectPostgresqlWithParameterConnectionConfig$default (Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig$Socket;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; +} + +public final class com/huanshankeji/exposedvertxsqlclient/postgresql/local/LocalConnectionConfigKt { + public static final field DEFAULT_POSTGRESQL_UNIX_DOMAIN_SOCKET_PATH Ljava/lang/String; + public static final fun defaultPostgresqlLocalConnectionConfig (Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/huanshankeji/exposedvertxsqlclient/local/LocalConnectionConfig; + public static synthetic fun defaultPostgresqlLocalConnectionConfig$default (Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/huanshankeji/exposedvertxsqlclient/local/LocalConnectionConfig; +} + +public final class com/huanshankeji/exposedvertxsqlclient/postgresql/vertx/pgclient/PgClientsKt { + public static final fun createGenericPgClientWithBuilder (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lio/vertx/sqlclient/ClientBuilder;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lio/vertx/sqlclient/SqlClient; + public static final fun createPgClient (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lio/vertx/sqlclient/SqlClient; + public static synthetic fun createPgClient$default (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/vertx/sqlclient/SqlClient; + public static final fun createPgConnection (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun createPgConnection$default (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; + public static final fun createPgPool (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lio/vertx/sqlclient/Pool; + public static synthetic fun createPgPool$default (Lio/vertx/core/Vertx;Lcom/huanshankeji/exposedvertxsqlclient/ConnectionConfig;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/vertx/sqlclient/Pool; +} + diff --git a/mysql/build.gradle.kts b/mysql/build.gradle.kts new file mode 100644 index 0000000..a872117 --- /dev/null +++ b/mysql/build.gradle.kts @@ -0,0 +1,15 @@ +import com.huanshankeji.cpnProject + +plugins { + `lib-conventions` +} + +dependencies { + with(commonDependencies.vertx) { implementation(platformStackDepchain()) } // needed + implementation(cpnProject(project, ":core")) + + runtimeOnly(commonDependencies.postgreSql()) // TODO change to the MySQL JDBC dependency + implementation(commonDependencies.vertx.moduleWithoutVersion("mysql-client")) + implementation(commonDependencies.kotlinCommon.core()) // for `Untested` + implementation(commonDependencies.kotlinCommon.vertx()) // for `PgPoolOptions.setUpConventionally` // TODO check if this is necessary for MySQL, and remove if not +} diff --git a/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/DatabaseClient.kt b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/DatabaseClient.kt new file mode 100644 index 0000000..54ea5d2 --- /dev/null +++ b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/DatabaseClient.kt @@ -0,0 +1,9 @@ +package com.huanshankeji.exposedvertxsqlclient.postgresql + +import com.huanshankeji.exposedvertxsqlclient.DatabaseClient +import com.huanshankeji.exposedvertxsqlclient.withTypedTransaction +import io.vertx.pgclient.PgConnection +import io.vertx.sqlclient.Pool + +suspend fun DatabaseClient.withPgTransaction(function: suspend (DatabaseClient) -> T): T = + withTypedTransaction(function) diff --git a/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/exposed/ExposedDatabases.kt b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/exposed/ExposedDatabases.kt new file mode 100644 index 0000000..623f632 --- /dev/null +++ b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/exposed/ExposedDatabases.kt @@ -0,0 +1,33 @@ +package com.huanshankeji.exposedvertxsqlclient.postgresql.exposed + +import com.huanshankeji.exposedvertxsqlclient.ConnectionConfig +import com.huanshankeji.exposedvertxsqlclient.ExperimentalEvscApi +import com.huanshankeji.exposedvertxsqlclient.exposed.exposedDatabaseConnect +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.DatabaseConfig +import org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager +import org.jetbrains.exposed.sql.transactions.TransactionManager +import java.sql.Connection + +/** + * @see exposedDatabaseConnect + */ +@ExperimentalEvscApi +fun ConnectionConfig.Socket.exposedDatabaseConnectMysql( + setupConnection: (Connection) -> Unit = {}, + databaseConfig: DatabaseConfig? = null, + manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) } +) = + exposedDatabaseConnect( + "mysql", "com.mysql.cj.jdbc.Driver", setupConnection, databaseConfig, manager + ) + +@ExperimentalEvscApi +@JvmName("exposedDatabaseConnectPostgresqlWithParameterConnectionConfig") +fun exposedDatabaseConnectMysql( + socketConnectionConfig: ConnectionConfig.Socket, + setupConnection: (Connection) -> Unit = {}, + databaseConfig: DatabaseConfig? = null, + manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) } +) = + socketConnectionConfig.exposedDatabaseConnectMysql(setupConnection, databaseConfig, manager) diff --git a/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/local/LocalConnectionConfig.kt b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/local/LocalConnectionConfig.kt new file mode 100644 index 0000000..6187485 --- /dev/null +++ b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/local/LocalConnectionConfig.kt @@ -0,0 +1,16 @@ +package com.huanshankeji.exposedvertxsqlclient.postgresql.local + +import com.huanshankeji.exposedvertxsqlclient.ExperimentalEvscApi +import com.huanshankeji.exposedvertxsqlclient.local.LocalConnectionConfig + +// TODO consider moving to "kotlin-common" + +const val DEFAULT_POSTGRESQL_UNIX_DOMAIN_SOCKET_PATH = "/var/run/postgresql" + +@ExperimentalEvscApi +fun defaultPostgresqlLocalConnectionConfig( + socketConnectionPort: Int? = null, user: String, socketConnectionPassword: String, database: String +) = + LocalConnectionConfig( + socketConnectionPort, DEFAULT_POSTGRESQL_UNIX_DOMAIN_SOCKET_PATH, user, socketConnectionPassword, database + ) diff --git a/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/vertx/pgclient/PgClients.kt b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/vertx/pgclient/PgClients.kt new file mode 100644 index 0000000..21542cd --- /dev/null +++ b/mysql/src/main/kotlin/com/huanshankeji/exposedvertxsqlclient/postgresql/vertx/pgclient/PgClients.kt @@ -0,0 +1,103 @@ +@file:OptIn(ExperimentalEvscApi::class) + +package com.huanshankeji.exposedvertxsqlclient.postgresql.vertx.pgclient + +import com.huanshankeji.Untested +import com.huanshankeji.exposedvertxsqlclient.ConnectionConfig +import com.huanshankeji.exposedvertxsqlclient.ExperimentalEvscApi +import com.huanshankeji.exposedvertxsqlclient.vertx.sqlclient.CoConnectHandler +import com.huanshankeji.exposedvertxsqlclient.vertx.sqlclient.createGenericSqlClient +import com.huanshankeji.exposedvertxsqlclient.vertx.sqlclient.createGenericSqlClientWithBuilder +import com.huanshankeji.exposedvertxsqlclient.vertx.sqlclient.createGenericSqlConnection +import com.huanshankeji.vertx.pgclient.setUpConventionally +import io.vertx.core.Vertx +import io.vertx.pgclient.PgBuilder +import io.vertx.pgclient.PgConnectOptions +import io.vertx.pgclient.PgConnection +import io.vertx.pgclient.impl.PgPoolOptions +import io.vertx.sqlclient.ClientBuilder +import io.vertx.sqlclient.Pool +import io.vertx.sqlclient.SqlClient + +/** + * @see createGenericSqlClient + */ +// made not inline anymore for easier debugging +@ExperimentalEvscApi +fun > createGenericPgClientWithBuilder( + vertx: Vertx?, + connectionConfig: ConnectionConfig, + clientBuilder: ClientBuilderT, + extraPgConnectOptions: PgConnectOptions.() -> Unit, + extraPgPoolOptions: PgPoolOptions.() -> Unit, + connectHandlerExtra: CoConnectHandler +): SqlClientT = + createGenericSqlClientWithBuilder( + vertx, + connectionConfig, + clientBuilder, + PgConnectOptions(), + extraPgConnectOptions, + extraPgPoolOptions, + connectHandlerExtra, + PgPoolOptions() + ) + +fun createPgClient( + vertx: Vertx?, + connectionConfig: ConnectionConfig, + extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, + extraPoolOptions: PgPoolOptions.() -> Unit = {}, + connectHandlerExtra: CoConnectHandler = null, +): SqlClient = + createGenericPgClientWithBuilder( + vertx, + connectionConfig, + PgBuilder.client(), + extraPgConnectOptions, + extraPoolOptions, + connectHandlerExtra + ) + +/** + * [PgPoolOptions.pipelined] is enabled by default. + * @see PgPoolOptions.setUpConventionally + * @see createGenericSqlClient + */ +fun createPgPool( + vertx: Vertx?, + connectionConfig: ConnectionConfig, + extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, + extraPoolOptions: PgPoolOptions.() -> Unit = {}, + connectHandlerExtra: CoConnectHandler = null, +): Pool = + createGenericPgClientWithBuilder( + vertx, + connectionConfig, + PgBuilder.pool(), + extraPgConnectOptions, + { + setUpConventionally() + extraPoolOptions() + }, + connectHandlerExtra + ) + +/** + * @see createGenericSqlClient + */ +@Untested +suspend fun createPgConnection( + vertx: Vertx?, + connectionConfig: ConnectionConfig, + extraPgConnectOptions: PgConnectOptions.() -> Unit = {}, + connectHandlerExtra: CoConnectHandler = null +): PgConnection = + createGenericSqlConnection( + vertx, + connectionConfig, + PgConnection::connect, + PgConnectOptions(), + extraPgConnectOptions, + connectHandlerExtra + ) diff --git a/settings.gradle.kts b/settings.gradle.kts index 3b86ea5..21267e6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,6 +3,7 @@ rootProject.name = "exposed-vertx-sql-client" include("core") include("sql-dsl") include("sql-dsl-with-mapper") +include("mysql") include("postgresql") include("integrated")