Running Plain SQL dynamically in Quill using infix fails with wrong query syntax during runtime

与世无争的帅哥 提交于 2020-03-05 00:32:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!