Exposed
Exposed copied to clipboard
Adding hint text for a query SQL
Adds the ability to mutate the query SQL with hint text. Besides, adjusted AbstractQuery.copyTo function to public visibility and open to make extending the Query class easier.
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()
}
}
Hi @xJoeWoo It would be great to include this feature in the upcoming release. So I will rebase and address the requested changes above on this branch next week if you are not able to. And the additional adjustment to the visibility modifier to make the Query class more extensible will be done in a separate dedicated PR (so I can add relevant documentation and tests).
Changes made:
- Rebase and add
apiDump - Remove changes involving extension of custom query classes (now in PR #2173 )
- Switch naming to
comment()to align with the standard sql - Add
adjustComments()that allows existing query comments to be removed/updated - Adjust test accordingly