Hey there, is there a way to add a prefix to the g...
# exposed
z
Hey there, is there a way to add a prefix to the generated SQL string? For example, adding the comment "/* FORCE_MASTER */" to switch to main database.
c
Hi @Zouyiu Ng (JoeWoo) If you just need to add a hint to the start of a select statement, for example, you could extend
Query
and override its `prepareSQL()`:
Copy code
class HintQuery(
    val hint: String,
    set: FieldSet,
    where: Op<Boolean>?
) : Query(set, where) {
    override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
        return "/*$hint*/ ${super.prepareSQL(transaction, prepared)}"
    }
}

// adjust signature (and selectAll(), select()) depending on how high you want to chain
fun Query.hint(hint: String): HintQuery = HintQuery(hint, set, where)

TestTable
    .selectAll()
    .where { TestTable.amount greaterEq 100 }
    .hint("FORCE_MASTER")
    .toList()
If the hint needs to actually be in a different location or on a different statement type, please confirm with an example of the full expected final result, so I can take a look.
z
Thanks for this solution! It shows the way to edit SQL. However, in current version, using this solution, other properties will lose in
HintQuery
except
set
and
where
, due to not copying properties from original query. Neither
copy()
nor
copyTo()
method are not available for calling. I'm making a PR to let extending
Query
easier.
👍 1