https://kotlinlang.org logo
#exposed
Title
# exposed
e

Eric Thomas

03/29/2023, 10:10 PM
Is it possible to store the raw Sql statements Exposed passes to the DB in a variable? I'm aware logging the statements via
addLogger(StdOutSqlLogger)
but how can I access/interact with this info? ex:
Copy code
Exposed: transaction { table.select { table.id eq 123 } }
Raw SQL ran against DB: SELECT * FROM TABLE WHERE ID = 123;

//desired result
val executedSql = SELECT * FROM TABLE WHERE ID = 123;
println(executedSql)
How can I access the raw sql executed against the DB?
a

Alexey Soshin

03/30/2023, 11:52 AM
Hi Eric, You can do something like this:
Copy code
transaction { 
    val rawSQL = table.select { table.id eq 123 }.prepareSQL(this) 

    println(rawSQL)
}
Note that this will not execute your SQL, it will only print it
e

Eric Thomas

03/30/2023, 3:59 PM
Thank you very much, that worked! Is it possible to do the same thing from an entity? ex:
Copy code
object TestTable : UUIDTable(columnName = "id", name = "test_table") {
    val data = uuid("data")
}

class TestRecord(id: EntityID<UUID>) : UUIDEntity(id) {
    var data by TestTable.data
}

val rawSQL = transaction {
    TestRecord.findById(<id>).prepareSQL(this)
}

println("rawSQL: $rawSQL")
@Alexey Soshin
a

Alexey Soshin

03/31/2023, 10:22 AM
Not when you use
findById
, since it already returns you an Entity back, not a query
Also note that not all
findById
invocations will results in a query: some may find the data in cache
339 Views