我想用普通SQL构造我的查询,然后使用Quill运行它,我正在使用中缀运算符。 我的代码是这样的。
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"
它可以是动态计算的任何内容。
def rawQuery = quote { (myQuery: String) =>
infix"""$myQuery"""
.as[Query[CombinedEmployee]]
}
我在呼唤
ctx.translate(rawQuery(lift(sql)))
这打印
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
这是一个错误的语法,当我使用run
方法执行此操作时,这失败了。 编写纯SQL的方式是否正确?如果没有,有没有其他方法可以运行普通 SQL 并将模型建模到案例类中?
这可以通过在$
之前添加一个#
来解决
def rawQuery = quote { (myQuery: String) =>
infix"""#$myQuery"""
.as[Query[CombinedEmployee]]
}