Kuba Petržílka
02/16/2021, 2:07 PMKuba Petržílka
02/16/2021, 5:55 PMopen class SequenceNextLongValQuery(private val sequence: Sequence): Statement<Long>(StatementType.SELECT, listOf()) {
private val transaction get() = TransactionManager.current()
override fun arguments(): Iterable<Iterable<Pair<IColumnType, Any?>>> = emptyList()
override fun prepareSQL(transaction: Transaction): String = prepareSQL(QueryBuilder(true))
private fun prepareSQL(builder: QueryBuilder): String {
builder {
append("SELECT ")
append(sequence.nextLongVal())
}
return builder.toString()
}
override fun PreparedStatementApi.executeInternal(transaction: Transaction): Long? {
val resultSet = executeQuery()
return if (resultSet.next()) resultSet.getLong(1) else null
}
fun execute(): Long? = super.execute(transaction)
}
fun Sequence.selectNextLongVal() = SequenceNextLongValQuery(this).execute()
...
val userIdSequence = Sequence("app_user_user_id_seq")
val nextUserId = userIdSequence.selectNextLongVal()
but I am not sure if there isn't any better way..
Also I don't know how to generalize it properly since there are two different implementations of NextVal: IntNextVal
and LongNextVal