简体   繁体   中英

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

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 ??

This can be solved by adding a # before $

def rawQuery = quote { (myQuery: String) =>
 infix"""#$myQuery"""
 .as[Query[CombinedEmployee]]

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM