5

Is there a way I can add comments into the sql that is formed by slick without writing a raw sql statement? This is to keep track of the code in application that launched the sql.

binshi
  • 1,248
  • 2
  • 17
  • 33

2 Answers2

3

I'm sorry to say no. Currently in the 3.3.1 version of Slick, there is no way to add a comment to queries other that creating the SQL code by hand.

It may be get added later as there is an open issue years ago for that:

https://github.com/slick/slick/issues/468

Amoo Hesam
  • 461
  • 3
  • 13
0

We accomplished this by downcasting DBIOAction and using SqlAction.overrideStatements:

val databaseAction: DBIOAction[A, NoStream, E] = ??? // your action 
val comment: String = ??? // your comment
val annotatedAction = databaseAction match {
      case sqlAction: SqlAction[A, NoStream, E] => sqlAction.overrideStatements(sqlAction.statements.map { s =>
        "/* " + comment + " */ " + s
      })
      case otherAction => otherAction
    }

db.run(annotatedAction)

It causes all SQL statements to be prefixed with a comment. You can make the comment be some unique identifier to help you connect SQL to your code, or you could do it automatically by finding a relevant stack frame in Thread.currentThread.getStackTrace before running the query.

Jonathan Crosmer
  • 786
  • 5
  • 19