Stephcraft
11/28/2024, 1:26 AMGameplayRecordTable
.select((GameplayRecordTable.duration * GameplayRecordTable.players).sum())
But the multiplication operator *
does not work with Duration
and Int
. Any way around this? ThanksRonny Bräunlich
11/28/2024, 7:43 AMjava.time.Duration
or kotlin.time.Duration
?Oleg Babichev
11/28/2024, 11:07 AMStephcraft
11/28/2024, 3:01 PMimport org.jetbrains.exposed.sql.kotlin.datetime.duration
object GameplayRecordTable: IntIdTable() {
val duration = duration("duration")
val players = integer("players")
}
So the type is kotlin.time.Duration
@Ronny BräunlichStephcraft
11/28/2024, 3:05 PM(GameplayRecordTable.duration.castTo(LongColumnType()) * GameplayRecordTable.players.castTo(LongColumnType())).sum()
SQL:
SELECT duration * players FROM `GameplayRecord`
Stephcraft
11/28/2024, 3:06 PMStephcraft
11/28/2024, 4:08 PM*
operator might be the better approachChantal Loncle
11/28/2024, 5:25 PMTimesOp<T, S : T>
parameters.
So the existing infix operator can be customized to fit any type combination that you require:
// for example, this assumes that the query result you want is of type Duration
class DurationTimesOp<S : Number>(
expr1: Expression<Duration>,
expr2: Expression<S>
) : CustomOperator<Duration>(
"*",
KotlinDurationColumnType(),
expr1,
expr2
)
infix operator fun <S : Number> ExpressionWithColumnType<Duration>.times(
other: Expression<S>
): DurationTimesOp<S> = DurationTimesOp(this, other)
This would allow you to continue using your first original query above.
If Duration * Int
is just one of many combos that you need, you could take this even further and eliminate the type-safety imposed by Exposed by creating your own version that accepts any combination:
class GeneralTimesOp<T, S>(
expr1: Expression<T>,
expr2: Expression<S>,
columnType: IColumnType<T & Any>
) : CustomOperator<T>(
"*",
columnType,
expr1,
expr2
)
infix operator fun <T, S> ExpressionWithColumnType<T>.times(
other: Expression<S>
): GeneralTimesOp<T, S> = GeneralTimesOp(this, other, this.columnType)
MySQL, for example, technically even allows Int * String
as valid SQL.Stephcraft
11/28/2024, 5:44 PM