Open xJoeWoo opened 1 month ago
This solution has its limitation: only supports Query
statement. If the hint needs to be added to all statements, for example, used for tracking, another mechanism should be added to Statement
class, or the StatementInterceptor
class.
I recommend making StatementInterceptor
more powerful, like intercept then editing the statement, or even the final SQL string. This makes updating the updateTime
column(which should be updated every time the row changes) much much simpler, no need to assign updateTime
column everywhere, and no worry about forgeting to update this column.
interface StatementInterceptor {
// add this function, call in the first line of org.jetbrains.exposed.sql.statements.Statement#executeIn
fun beforeArgument(transaction: Transaction, statement: Statement<*>) {}
// even mutating the SQL, useful for adding tracking hints
fun beforeStatementPrepared(transaction: Transaction, sql: String): String = sql
}
class UpdateTimeColumnType : ColumnType<LocalDateTime>()
object UpdateTimeGlobalStatementInterceptor : GlobalStatementInterceptor {
override fun beforeArgument(transaction: Transaction, statement: Statement<*>) {
val updateStatement = statement as? UpdateStatement ?: return
val table = updateStatement.targetsSet as? Table ?: return
val column = table.columns.firstOrNull { it.columnType is UpdateTimeColumnType } as? Column<LocalDateTime> ?: return
updateStatement[column] = LocalDateTime.now()
}
}
Adds the ability to mutate the query SQL with hint text. Besides, adjusted
AbstractQuery.copyTo
function to public visibility and open to make extending theQuery
class easier.