Skip to content

Commit

Permalink
Move the code that belongs to the modules "sql-dsl" and "sql-dsl-with…
Browse files Browse the repository at this point in the history
…-mapper" into these modules

Miscellaneous changes:
1. remove the PostgreSQL dependencies in the "core" module
1. add and use the `@InternalApi` opt-in annotation
1. update `executeExpression` to throw since the core module doesn't depend on the "sql-dsl" module anymore
1. add an example of `DatabaseClient.selectExpression` with `exists` in the example code
  • Loading branch information
ShreckYe committed Nov 19, 2024
1 parent 2d7b2a5 commit 59a26a6
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ val exampleName1 =
val exampleName2 =
databaseClient.selectSingleColumn(Examples, Examples.name) { where(Examples.id eq 2) }.single()

val examplesExist = databaseClient.selectExpression(exists(Examples.selectAll()))

val deleteRowCount1 = databaseClient.deleteWhere(Examples) { id eq 1 }
assert(deleteRowCount1 == 1)
val deleteRowCount2 = databaseClient.deleteIgnoreWhere(Examples) { id eq 2 }
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/lib-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.huanshankeji.team.`Shreck Ye`
import com.huanshankeji.team.pomForTeamDefaultOpenSource
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask

plugins {
id("conventions")
Expand All @@ -13,3 +14,7 @@ publishing.publications.getByName<MavenPublication>("maven") {
`Shreck Ye`()
}
}

tasks.named<KotlinCompilationTask<*>>("compileKotlin").configure {
compilerOptions.freeCompilerArgs.add("-opt-in=com.huanshankeji.exposedvertxsqlclient.InternalApi")
}
8 changes: 0 additions & 8 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ dependencies {
// TODO: remove the Exposed JDBC dependency and the PostgresSQL dependency when there is no need to to generate SQLs with an Exposed transaction
runtimeOnly(commonDependencies.exposed.module("jdbc"))
api(commonDependencies.kotlinCommon.exposed())
implementation("com.huanshankeji:exposed-adt-mapping:${DependencyVersions.exposedAdtMapping}") // TODO remove when code is moved out

with(commonDependencies.vertx) {
implementation(platformStackDepchain())
Expand All @@ -22,10 +21,3 @@ dependencies {

implementation(commonDependencies.kotlinCommon.net())
}

// TODO remove when code is moved out
// for PostgreSQL
dependencies {
runtimeOnly(commonDependencies.postgreSql())
implementation(commonDependencies.vertx.moduleWithoutVersion("pg-client"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package com.huanshankeji.exposedvertxsqlclient

import arrow.core.*
import com.huanshankeji.collections.singleOrNullIfEmpty
import com.huanshankeji.exposedvertxsqlclient.sql.selectExpression
import com.huanshankeji.vertx.kotlin.coroutines.coroutineToFuture
import com.huanshankeji.vertx.kotlin.sqlclient.executeBatchAwaitForSqlResultSequence
import io.vertx.core.buffer.Buffer
import io.vertx.kotlin.coroutines.coAwait
import io.vertx.pgclient.PgConnectOptions
import io.vertx.sqlclient.*
import kotlinx.coroutines.coroutineScope
import org.jetbrains.exposed.dao.id.EntityID
Expand Down Expand Up @@ -65,8 +63,8 @@ fun String.toVertxPgClientPreparedSql(): String {
fun Statement<*>.getVertxPgClientPreparedSql(transaction: ExposedTransaction) =
prepareSQL(transaction).toVertxPgClientPreparedSql()


internal fun dbAssert(b: Boolean) {
@InternalApi
fun dbAssert(b: Boolean) {
if (!b)
throw AssertionError()
}
Expand Down Expand Up @@ -103,7 +101,7 @@ class DatabaseClient<out VertxSqlClientT : SqlClient>(
}

suspend fun executePlainSql(sql: String): RowSet<Row> =
/** Use [SqlClient.preparedQuery] here because of [PgConnectOptions.setCachePreparedStatements]. */
/** Use [SqlClient.preparedQuery] here because of [SqlConnectOptions.setCachePreparedStatements]. */
vertxSqlClient.preparedQuery(sql).execute().coAwait()

suspend fun executePlainSqlUpdate(sql: String): Int =
Expand Down Expand Up @@ -211,20 +209,20 @@ class DatabaseClient<out VertxSqlClientT : SqlClient>(


@Deprecated(
"Use `selectExpression` instead`",
"Use `selectExpression` in the \"sql-dsl\" module instead.",
ReplaceWith(
"selectExpression<T>(clazz, expression)", "com.huanshankeji.exposedvertxsqlclient.sql.selectExpression"
)
)
suspend fun <T : Any> executeExpression(clazz: KClass<T>, expression: Expression<T?>): T? =
selectExpression(clazz, expression)
throw NotImplementedError()

@Deprecated(
"Use `selectExpression` instead`",
"Use `selectExpression` in the \"sql-dsl\" module instead.",
ReplaceWith("selectExpression<T>(expression)", "com.huanshankeji.exposedvertxsqlclient.sql.selectExpression")
)
suspend inline fun <reified T> executeExpression(expression: Expression<T>): T =
selectExpression(expression)
throw NotImplementedError()

suspend fun isWorking(): Boolean =
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.huanshankeji.exposedvertxsqlclient

@RequiresOptIn("This API is internal in the Exposed Vert.x SQL Client library.")
@Retention(AnnotationRetention.BINARY)
annotation class InternalApi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.selectAll

object Examples : IntIdTable("examples") {
val name = varchar("name", 64)
Expand Down Expand Up @@ -84,6 +86,8 @@ suspend fun examples(vertx: Vertx) {
val exampleName2 =
databaseClient.selectSingleColumn(Examples, Examples.name) { where(Examples.id eq 2) }.single()

val examplesExist = databaseClient.selectExpression(exists(Examples.selectAll()))

val deleteRowCount1 = databaseClient.deleteWhere(Examples) { id eq 1 }
assert(deleteRowCount1 == 1)
val deleteRowCount2 = databaseClient.deleteIgnoreWhere(Examples) { id eq 2 }
Expand Down
3 changes: 3 additions & 0 deletions sql-dsl-with-mapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ plugins {
dependencies {
with(commonDependencies.vertx) { implementation(platformStackDepchain()) } // needed
implementation(cpnProject(project, ":core"))
implementation(cpnProject(project, ":sql-dsl"))

implementation("com.huanshankeji:exposed-adt-mapping:${DependencyVersions.exposedAdtMapping}") // for `updateBuilderSetter`, `DataQueryMapper` and `DataUpdateMapper`
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import com.huanshankeji.vertx.sqlclient.datamapping.RowDataQueryMapper
import io.vertx.sqlclient.Row
import kotlin.reflect.KClass

// TODO all definitions are made private because they are not complete yet

/**
* @see ClassPropertyColumnMappings
*/
// since Kotlin 2.0.0: "'Nothing' property type can't be specified with type alias."
typealias ClassPropertyColumnIndexMappings<Data> = Unit // TODO
private typealias ClassPropertyColumnIndexMappings<Data> = Unit // TODO

typealias VertxSqlClientRowDataQueryMapper<Data> = RowDataQueryMapper<Data>
private typealias VertxSqlClientRowDataQueryMapper<Data> = RowDataQueryMapper<Data>

/**
* @see ReflectionBasedClassPropertyDataMapper
*/
class ReflectionBasedClassPropertyIndexVertxSqlClientRowDataQueryMapper<Data : Any>(
private class ReflectionBasedClassPropertyIndexVertxSqlClientRowDataQueryMapper<Data : Any>(
val clazz: KClass<Data>,
val classPropertyColumnIndexMappings: ClassPropertyColumnIndexMappings<Data>
) : RowDataQueryMapper<Data> {
Expand Down
2 changes: 2 additions & 0 deletions sql-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ plugins {
dependencies {
with(commonDependencies.vertx) { implementation(platformStackDepchain()) } // needed
implementation(cpnProject(project, ":core"))

compileOnly(commonDependencies.kotlinCommon.vertx()) // for `sortDataAndExecuteBatch`
}

0 comments on commit 59a26a6

Please sign in to comment.