NarayanJr NarayanJr - 1 month ago 12
Scala Question

How do you print out filled in Anorm SQL statements in Play framework

How can I print out the SQL state that is going to be run using Anorm.

Example

SQL(s"SELECT $terms FROM $PERSON_TABLE WHERE name = {name}").on("name" -> "Bill")


Printing that gives me this

SimpleSql(anorm.SqlQuery$$anon$1@1197b8ab,Map(name -> ParameterValue(Bill)),<function1>,false)


I want to see the exact query that is going to run on the server,

SELECT id, name, state FROM person WHERE name = "Bill";


I dont want to use interpolation like this

s"SELECT $terms FROM $PERSON_TABLE WHERE name = $name"


I need to be able to see all the escaped characters and what is wrapped in (') and what is wrapped in (")

Using:

Play 2.4.1-Scala

Thanks,

Adam

Answer

You can use the debuging utility from Acolyte.

import acolyte.jdbc.AcolyteDSL

AcolyteDSL.debuging() { implicit con =>
  // debug any JDBC execution within

  SQL"SELECT * FROM Test WHERE id = $id"
  // Will print the stmt prepared for
}
Comments