Skip to content

Commit

Permalink
Adapt to the new SELECT DSL
Browse files Browse the repository at this point in the history
Resolve #8

Some functions' signatures are changed so binary compatibility is not maintained. The project version is bumped BTW.
  • Loading branch information
ShreckYe committed Nov 14, 2024
1 parent 9a6fb12 commit c03ede0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ val exampleName1 =
databaseClient.select(Examples) { select(Examples.name).where(Examples.id eq 1) }.single()[Examples.name]
// This function still depends on the old SELECT DSL and will be updated.
val exampleName2 =
databaseClient.selectSingleColumn(Examples, Examples.name) { selectAll().where(Examples.id eq 2) }.single()
databaseClient.selectSingleColumn(Examples, Examples.name) { where(Examples.id eq 2) }.single()

val deleteRowCount1 = databaseClient.deleteWhere(Examples) { id eq 1 }
assert(deleteRowCount1 == 1)
Expand All @@ -120,8 +120,8 @@ val episodeIIFilmDetails = FilmDetails(2, "Star Wars: Episode II – Attack of t
val filmWithDirectorId = FilmWithDirectorId(filmId, episodeIIFilmDetails)
databaseClient.insert(Films, filmWithDirectorId, Mappers.filmWithDirectorId) // insert with the ID

val fullFilms = databaseClient.select(filmsLeftJoinDirectors, Mappers.fullFilm) {
select(Films.filmId inList listOf(1, 2)) // This API still depends on the old SELECT DSL and will be refactored.
val fullFilms = databaseClient.selectWithMapper(filmsLeftJoinDirectors, Mappers.fullFilm) {
where(Films.filmId inList listOf(1, 2)) // This API still depends on the old SELECT DSL and will be refactored.
}
```

Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/VersionsAndDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import com.huanshankeji.CommonDependencies
import com.huanshankeji.CommonGradleClasspathDependencies
import com.huanshankeji.CommonVersions

val projectVersion = "0.4.0-SNAPSHOT"
val projectVersion = "0.5.0-SNAPSHOT"

// don't use a snapshot version in a main branch
val commonVersions = CommonVersions(kotlinCommon = "0.5.1")
val commonVersions = CommonVersions(kotlinCommon = "0.6.0-SNAPSHOT")
val commonDependencies = CommonDependencies(commonVersions)
val commonGradleClasspathDependencies = CommonGradleClasspathDependencies(commonVersions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,33 @@ suspend inline fun DatabaseClient<*>.select(
select(columnSet, buildQuery, { this })


// TODO adapt to the new SELECT DSL or deprecate
/**
* SQL: `SELECT <expression> FROM <table>;`.
* Examples: `SELECT COUNT(*) FROM <table>;`, `SELECT SUM(<column>) FROM <table>;`.
*/
@ExperimentalEvscApi
suspend fun <T> DatabaseClient<*>.selectTableExpression(
columnSet: ColumnSet, expression: Expression<T>, buildQuery: FieldSet.() -> Query
suspend fun <T> DatabaseClient<*>.selectColumnSetExpression(
columnSet: ColumnSet, expression: Expression<T>, buildQuery: Query.() -> Query
): RowSet<T> =
select(columnSet, { slice(expression).buildQuery() }, { this[expression] })
select(columnSet, { select(expression).buildQuery() }, { this[expression] })

// This function with `mapper` is not really useful
@ExperimentalEvscApi
suspend inline fun <ColumnT, DataT> DatabaseClient<*>.selectSingleColumn(
columnSet: ColumnSet,
column: Column<ColumnT>,
buildQuery: FieldSet.() -> Query,
buildQuery: Query.() -> Query,
crossinline mapper: ColumnT.() -> DataT
): RowSet<DataT> =
select(columnSet, { slice(column).buildQuery() }, { this[column].mapper() })


@Deprecated("Use `selectSingleColumn`.", ReplaceWith("selectSingleColumn<T, R>(columnSet, column, buildQuery, mapper)"))
@ExperimentalEvscApi
suspend inline fun <T, R> DatabaseClient<*>.executeSingleColumnSelectQuery(
columnSet: ColumnSet, column: Column<T>, buildQuery: FieldSet.() -> Query, crossinline mapper: T.() -> R
): RowSet<R> =
selectSingleColumn(columnSet, column, buildQuery, mapper)
select(columnSet, { select(column).buildQuery() }, { this[column].mapper() })

// TODO adapt to the new SELECT DSL or deprecate
suspend fun <T> DatabaseClient<*>.selectSingleColumn(
columnSet: ColumnSet, column: Column<T>, buildQuery: FieldSet.() -> Query
): RowSet<T> =
selectTableExpression(columnSet, column, buildQuery)

@Deprecated("Use `selectSingleColumn`.", ReplaceWith("selectSingleColumn<T>(columnSet, column, buildQuery)"))
suspend fun <T> DatabaseClient<*>.executeSingleColumnSelectQuery(
columnSet: ColumnSet, column: Column<T>, buildQuery: FieldSet.() -> Query
columnSet: ColumnSet, column: Column<T>, buildQuery: Query.() -> Query
): RowSet<T> =
selectSingleColumn(columnSet, column, buildQuery)
selectColumnSetExpression(columnSet, column, buildQuery)

// TODO adapt to the new SELECT DSL or deprecate
suspend fun <T : Comparable<T>> DatabaseClient<*>.selectSingleEntityIdColumn(
columnSet: ColumnSet, column: Column<EntityID<T>>, buildQuery: FieldSet.() -> Query
columnSet: ColumnSet, column: Column<EntityID<T>>, buildQuery: Query.() -> Query
): RowSet<T> =
selectSingleColumn(columnSet, column, buildQuery) { value }

Expand All @@ -87,7 +70,7 @@ suspend fun <T : Comparable<T>> DatabaseClient<*>.selectSingleEntityIdColumn(
*/
// see: https://github.com/JetBrains/Exposed/issues/621
suspend fun <T : Any> DatabaseClient<*>.selectExpression(clazz: KClass<T>, expression: Expression<T?>): T? =
executeForVertxSqlClientRowSet(Table.Dual.slice(expression).selectAll())
executeForVertxSqlClientRowSet(Table.Dual.select(expression))
.single()[clazz.java, 0]

suspend inline fun <reified T> DatabaseClient<*>.selectExpression(expression: Expression<T>): T =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@file:OptIn(InternalApi::class)

package com.huanshankeji.exposedvertxsqlclient.sql.mapping

import com.huanshankeji.InternalApi
import com.huanshankeji.exposed.BuildWhere
import com.huanshankeji.exposed.SELECT_DSL_DEPRECATION_MESSAGE
import com.huanshankeji.exposed.datamapping.DataQueryMapper
import com.huanshankeji.exposed.datamapping.DataUpdateMapper
import com.huanshankeji.exposed.datamapping.updateBuilderSetter
Expand Down Expand Up @@ -30,12 +34,22 @@ suspend fun <Data : Any> DatabaseClient<*>.executeVertxSqlClientRowQuery(
): RowSet<Data> =
executeWithMapping(query, rowDataQueryMapper::rowToData)

@Deprecated(
SELECT_DSL_DEPRECATION_MESSAGE,
ReplaceWith("this.selectWithMapper<Data>(columnSet, dataQueryMapper, buildQuery)")
)
@ExperimentalEvscApi
suspend fun <Data : Any> DatabaseClient<*>.select(
columnSet: ColumnSet, dataQueryMapper: DataQueryMapper<Data>, buildQuery: FieldSet.() -> Query
) =
executeQuery(columnSet.slice(dataQueryMapper.neededColumns).buildQuery(), dataQueryMapper)

@ExperimentalEvscApi
suspend fun <Data : Any> DatabaseClient<*>.selectWithMapper(
columnSet: ColumnSet, dataQueryMapper: DataQueryMapper<Data>, buildQuery: Query.() -> Query
) =
executeQuery(columnSet.select(dataQueryMapper.neededColumns).buildQuery(), dataQueryMapper)

@ExperimentalEvscApi
suspend fun <Data : Any> DatabaseClient<*>.insert(
table: Table, data: Data, dataUpdateMapper: DataUpdateMapper<Data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ suspend fun examples(vertx: Vertx) {
databaseClient.select(Examples) { select(Examples.name).where(Examples.id eq 1) }.single()[Examples.name]
// This function still depends on the old SELECT DSL and will be updated.
val exampleName2 =
databaseClient.selectSingleColumn(Examples, Examples.name) { selectAll().where(Examples.id eq 2) }.single()
databaseClient.selectSingleColumn(Examples, Examples.name) { where(Examples.id eq 2) }.single()

val deleteRowCount1 = databaseClient.deleteWhere(Examples) { id eq 1 }
assert(deleteRowCount1 == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.huanshankeji.exposedvertxsqlclient
import com.huanshankeji.exposed.datamapping.classproperty.PropertyColumnMappingConfig
import com.huanshankeji.exposed.datamapping.classproperty.reflectionBasedClassPropertyDataMapper
import com.huanshankeji.exposedvertxsqlclient.sql.mapping.insert
import com.huanshankeji.exposedvertxsqlclient.sql.mapping.select
import com.huanshankeji.exposedvertxsqlclient.sql.mapping.selectWithMapper
import io.vertx.sqlclient.Pool
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.select

// copied and adapted from https://github.com/huanshankeji/exposed-adt-mapping/blob/main/lib/src/test/kotlin/com/huanshankeji/exposed/datamapping/classproperty/Examples.kt
// Update accordingly to keep the code consistent.
Expand Down Expand Up @@ -95,7 +94,7 @@ suspend fun mappingExamples(databaseClient: DatabaseClient<Pool>) {
val filmWithDirectorId = FilmWithDirectorId(filmId, episodeIIFilmDetails)
databaseClient.insert(Films, filmWithDirectorId, Mappers.filmWithDirectorId) // insert with the ID

val fullFilms = databaseClient.select(filmsLeftJoinDirectors, Mappers.fullFilm) {
select(Films.filmId inList listOf(1, 2)) // This API still depends on the old SELECT DSL and will be refactored.
val fullFilms = databaseClient.selectWithMapper(filmsLeftJoinDirectors, Mappers.fullFilm) {
where(Films.filmId inList listOf(1, 2)) // This API still depends on the old SELECT DSL and will be refactored.
}
}

0 comments on commit c03ede0

Please sign in to comment.