dave08
09/11/2024, 10:01 AMselect...
((FLOOR(RANDOM() * (100 - 0 + 1)) + 5)::BIGINT) AS random,
(OTHER.tags_token::INT[] & ?::INT[]) AS similarity
from ...
dave08
09/11/2024, 10:21 AM?::INT[]
...Toshihiro Nakamura
09/11/2024, 12:28 PMdave08
09/11/2024, 12:30 PM?:INT[]
operator? The ?
here is a list or collection type...dave08
09/11/2024, 12:44 PMenum class PostgresType(val typeStr: String) {
BIGINT("BIGINT"),
INT_ARRAY("INT[]")
}
private inline fun <reified S : Any, reified D : Any, reified INNER : Any> cast(
expression: ColumnExpression<S, INNER>,
destinationType: PostgresType,
): ColumnExpression<D, INNER> {
val name = "cast"
val o1 = Operand.Column(expression)
return UserDefinedExpression(
typeOf<D>(),
typeOf<INNER>(),
{ it as D },
name,
listOf(o1),
) {
visit(o1)
append("::${destinationType.name}")
}
}
dave08
09/11/2024, 12:51 PM(FLOOR(RANDOM() * (100 - 0 + 1)) + 5)::BIGINT
seems pretty complex to implement... I really just need to dump it as one of the columns... any way to do that?Toshihiro Nakamura
09/11/2024, 1:26 PMHow do I handle theThere are various ways to approach it, but treating it as a string might be a good option.operator? The?:INT[]
here is a list or collection type...?
enum class PostgresType(val typeStr: String) {
BIGINT("BIGINT"),
INT_ARRAY("INT[]")
}
private fun cast(
value: String,
destinationType: PostgresType,
): ColumnExpression<String, String> {
val name = "cast"
val o1 = Operand.SimpleArgument(typeOf<String>(), value)
return columnExpression({it}, name, listOf(o1)) {
append("(")
visit(o1)
append("::${destinationType.typeStr})")
}
}
the query is very dynamic...Rather than expressing it in a single query, it might be better to split it into multiple queries.
dave08
09/11/2024, 1:43 PMdave08
09/11/2024, 1:48 PMcast("(FLOOR(RANDOM() * (100 - 0 + 1)) + 5)", <http://PostgresType.INT|PostgresType.INT>_ARRAY)
? But then how would I handle an actual db field or Operator type (?
) if it would take in a String type?dave08
09/11/2024, 1:50 PMRather than expressing it in a single query, it might be better to split it into multiple queries.There's other cases where I'm using templates heavily, but in this case, I need to add joins and orders on the fly and the dsl makes this much nicer and safer, and I can unit test the result much easier.
dave08
09/11/2024, 1:54 PMdave08
09/11/2024, 1:55 PMToshihiro Nakamura
09/11/2024, 1:56 PMval query = QueryDsl.select(
floor(
random() *
(literal(BigDecimal(100))
- literal(BigDecimal(0))
+ literal(BigDecimal(1)))
+ literal(BigDecimal(5))
)
)
private fun floor(
expression: ColumnExpression<BigDecimal, BigDecimal>,
): ColumnExpression<BigDecimal, BigDecimal> {
val name = "floor"
val o1 = Operand.Column(expression)
return columnExpression(expression, name, listOf(o1)) {
append("floor(")
visit(o1)
append(")")
}
}
Operator type (Is)?
?
an operator? Isn’t it a bind parameter?dave08
09/11/2024, 2:08 PMToshihiro Nakamura
09/11/2024, 2:11 PMdave08
09/12/2024, 9:42 AM