Search code examples
scalaplayframeworkplayframework-2.0anorm

Scala Play Framework Anorm SQL.on disable wrapping replacements with ' '


Whenever I replace placeholders in the SQL query using on it surrounds the replacement with '', is there a way to prevent this?

It means I can't do things like

SQL("SELECT * FROM {table} blah").on("table" -> tabletouse) 

because it wraps the table name with '' which causes an SQL syntax error.


Solution

  • you could certainly combine both approaches, using the format function for data you don't want to be escaped

    SQL(
      """
        select %s from %s
        where
          name = {name} and
          date between {start} and {end}
        order by %s
      """.format(fields, table, order)
    ).on(
      'name     -> name,
      'start    -> startDate,
      'end      -> endDate
    )
    

    Just take into account that the data you are sending using the format function should NOT come from user input, otherwise it should be properly sanitized