问题
I want to construct my query in plain SQL and then run it using Quill, I am using infix operator . My code is like this.
case class Employee(name: String, age: String, company_name: String)
case class Company(name: String, pin_code: String)
case class CombinedEmployee(employee_age: Int,
employee_name: Option[String],
company_name: String,
pin: Option[String])
val sql = "SELECT t1.age AS employee_age, t1.name AS employee_name, t2.name AS company_name, t2.pin as pin FROM employee t1 JOIN company t2 ON t1.company_name = t2.name"
// it can be anything which is calculated dynamically.
def rawQuery = quote { (myQuery: String) =>
infix"""$myQuery"""
.as[Query[CombinedEmployee]]
}
and I am calling
ctx.translate(rawQuery(lift(sql)))
this prints
SELECT x.employee_age, x.employee_name, x.company_name, x.pin FROM ('SELECT t1.age AS employee_age, t1.name AS employee_name, t2.name AS company_name, t2.pin as pin FROM employee t1 JOIN company t2 ON t1.company_name = t2.name') AS x
which is a wrong syntax, when I executed this using run
method this fails.
Is the way of writing plain SQL correct ?? If no, is there a different way to run Plain SQL and model into case classes ??
回答1:
This can be solved by adding a #
before $
def rawQuery = quote { (myQuery: String) =>
infix"""#$myQuery"""
.as[Query[CombinedEmployee]]
}
来源:https://stackoverflow.com/questions/60200859/running-plain-sql-dynamically-in-quill-using-infix-fails-with-wrong-query-syntax