Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres distinct on example (Exposed/issues/500) #3

Merged
merged 2 commits into from
Oct 18, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.example.api.tweeter.search.TweeterSearchHandler
import com.example.api.tweeter.search.TweeterSearchRequest
import com.example.api.tweeter.search.TweeterSearchResponse
import com.example.config.Jackson
import com.example.util.exposed.functions.postgres.distinctOn
import com.example.util.exposed.query.toSQL
import com.fasterxml.jackson.databind.JsonNode
import io.burt.jmespath.Expression
import io.burt.jmespath.JmesPath
Expand All @@ -19,11 +21,13 @@ import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import mu.KLogging
import org.funktionale.option.Option
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greaterEq
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*
import java.time.Instant
import java.util.*


@RestController
class TweeterApiController(
private val repo: TweetsRepo,
Expand All @@ -37,6 +41,33 @@ class TweeterApiController(
fun getOne(@PathVariable id: UUID): TweetDto =
repo[id].toTweetsDto()

@GetMapping("/api/tweeter/distinctOn")
@Transactional(readOnly = true)
fun getDistinct(): List<TweetDto> {
val selectDistinctOn: CustomFunction<Boolean?> = distinctOn(
TweetsTable.message,
TweetsTable.comment
)
val selectColumns: List<Column<*>> = (TweetsTable.columns)
val selectWhere: Op<Boolean> = (
TweetsTable.createdAt.greaterEq(Instant.EPOCH)
)

val query: Query = TweetsTable
.slice(
selectDistinctOn,
*selectColumns.toTypedArray()
)
.select { selectWhere }

val SQL: String = query.toSQL()
logger.info { "==== Select Distinct On Example === SQL: $SQL " }
val dtos: List<TweetDto> = query
.map { with(TweetsTable) { it.toTweetsRecord() } }
.map { it.toTweetsDto() }
return dtos
}

@PutMapping("/api/tweeter")
fun createOne(@RequestBody req: CreateTweetRequest): TweetDto =
req.toRecord(id = UUID.randomUUID(), now = Instant.now())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.util.exposed.functions.common

import org.jetbrains.exposed.sql.BooleanColumnType
import org.jetbrains.exposed.sql.CustomFunction
import org.jetbrains.exposed.sql.Expression
import org.jetbrains.exposed.sql.QueryBuilder

/**
* Boolean Function (with optional postfix)
*/
fun CustomBooleanFunction(
functionName: String, postfix: String = "", vararg params: Expression<*>
): CustomFunction<Boolean?> =
object : CustomFunction<Boolean?>(functionName, BooleanColumnType(), *params) {
override fun toQueryBuilder(queryBuilder: QueryBuilder) {
super.toQueryBuilder(queryBuilder)
if (postfix.isNotEmpty()) {
queryBuilder.append(postfix)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.util.exposed.functions.postgres

import com.example.util.exposed.functions.common.CustomBooleanFunction
import org.jetbrains.exposed.sql.CustomFunction
import org.jetbrains.exposed.sql.Expression

/**
* SELECT DISTINCT ON (a,b,c) TRUE, a,b,x,y,z FROM table WHERE ...
* see: https://github.com/JetBrains/Exposed/issues/500
*/
fun distinctOn(vararg expressions: Expression<*>): CustomFunction<Boolean?> = CustomBooleanFunction(
functionName = "DISTINCT ON",
postfix = " TRUE",
params = *expressions
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.util.exposed.query

import org.jetbrains.exposed.sql.Query
import org.jetbrains.exposed.sql.QueryBuilder

fun Query.toSQL():String = prepareSQL(QueryBuilder(false))