gpt4 book ai didi

scala - 如何在运行时使用 quill 定义表名

转载 作者:行者123 更新时间:2023-12-04 16:05:44 29 4
gpt4 key购买 nike

我是 quill 的新手,我试图在运行时定义一个表,但我收到一个编译错误。有什么解决方法吗?或者它只是不可能使用 quill ?
代码示例是:

case class ExampleCaseClass(id : String, version : String)
class Example (db: CassandraAsyncContext[SnakeCase] , table : String ) {
import db._

def selectByVarId = quote {
(id: String, version: String) =>
querySchema[ExampleCaseClass](table).filter(example => (example.id == id) && (example.version == version))
}
}

和错误:
Error:(114, 36) Tree Error:(124, 25) Tree 'Example.this.db.querySchema[***.ExampleCaseClass](Example.this.table)' can't be parsed to 'Ast'
def selectById = quote {

最佳答案

从羽毛笔 3.+ 使用 dynamicQuerySchema[Person]("Person") dynamic-query-api

    import io.getquill.{Literal, MirrorSqlDialect, SqlMirrorContext}
import java.time.LocalDate

final case class PersonId(value: Int) extends AnyVal

final case class Person(
id: PersonId,
firstName: String,
lastName: String,
birthDate: LocalDate)

val context = new SqlMirrorContext(MirrorSqlDialect, Literal)

val person = "Person"
val person2 = "Person2"

import context._

val personContext = dynamicQuerySchema[Person](person)
val person2Context = dynamicQuerySchema[Person](person2)
val query = translate(personContext.filter(_.id == lift(PersonId(1))))
val query2 = translate(person2Context.filter(_.id == lift(PersonId(1))))
println(s"$query")
println(s"$query2")
输出
SELECT v0.id, v0.firstName, v0.lastName, v0.birthDate, v0.addressId FROM Person v0 WHERE v0.id = 1
SELECT v0.id, v0.firstName, v0.lastName, v0.birthDate, v0.addressId FROM Person2 v0 WHERE v0.id = 1

关于scala - 如何在运行时使用 quill 定义表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353397/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com