使用 ZIO 和 Quill 撰寫簡單的 CRUD 應用程式
域模型由用戶和角色組成(一對多)
當嘗試呼叫在 quill 背景關系中提升的物體之一(或在構建查詢的程序中獲得)上的方法時,我面臨編譯時例外:
Can't find case class property: typedCode
roleSchema.filter(r => r.typedCode == lift(roleCode)).take(1)
val dc: db.Ctx.type = db.Ctx
import dc._
def findRoleByCode(roleCode: RoleCode): Result[Option[Role]] =
dc.run(
roleSchema.filter(r => r.typedCode == lift(roleCode)).take(1)
).map(_.headOption)
架構如下:
用戶物體
case class User(
id: String,
firstName: String,
lastName: String,
age: Int
){
def typedId: UserId = UserId(id)
}
角色物體
case class Role(
code: String,
name: String
){
def typedCode: RoleCode = RoleCode(code)
}
識別符號的用戶到角色關聯和值類
case class UserToRole(roleId: RoleCode, userId: UserId)
case class RoleCode(code: String) extends AnyVal
case class UserId(id: String) extends AnyVal
我目前得出的結論是,不可能對存在于 Quill 背景關系中的物體呼叫方法。我想這是由于宏觀評估和某種型別的替換
問題是 - 獲取屬性不是問題。我可以role.code作為一個獲取String,但不能呼叫.typedCode。同樣適用于用戶
編輯
嘗試將.typedCodeand轉換.typedId為接受Unit作為引數的方法會typedCode(): RoleCode typedId(): UserId導致另一個例外:
Tree 'r.typedCode()' can't be parsed to 'Ast'
roleSchema.filter(r => r.typedCode() == lift(roleCode)).take(1)
編輯 2
在查詢中構建值類實體:RoleCode(r.code)導致:
exception during macro expansion:
scala.reflect.macros.TypecheckException: package module4.homework.dao.entity is not a value
at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$3(Typers.scala:44)
at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$2(Typers.scala:38)
at scala.reflect.macros.contexts.Typers.doTypecheck$1(Typers.scala:37)
at scala.reflect.macros.contexts.Typers.$anonfun$typecheck$7(Typers.scala:50)
at scala.reflect.internal.Trees.wrappingIntoTerm(Trees.scala:1891)
at scala.reflect.internal.Trees.wrappingIntoTerm$(Trees.scala:1888)
at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:28)
at scala.reflect.macros.contexts.Typers.typecheck(Typers.scala:50)
at scala.reflect.macros.contexts.Typers.typecheck$(Typers.scala:32)
at scala.reflect.macros.contexts.Context.typecheck(Context.scala:18)
at scala.reflect.macros.contexts.Context.typecheck(Context.scala:18)
at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:540)
at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:526)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:64)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:546)
at io.getquill.quotation.Parsing$$anonfun$propertyParser$1.applyOrElse(Parsing.scala:526)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:64)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$functionApplyParser$1.applyOrElse(Parsing.scala:579)
at io.getquill.quotation.Parsing$$anonfun$functionApplyParser$1.applyOrElse(Parsing.scala:578)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:558)
at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:552)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:61)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
at io.getquill.quotation.Parsing.io$getquill$quotation$Parsing$$equalityWithInnerTypechecksIdiomatic(Parsing.scala:603)
at io.getquill.quotation.Parsing$$anonfun$equalityOperationParser$1.applyOrElse(Parsing.scala:624)
at io.getquill.quotation.Parsing$$anonfun$equalityOperationParser$1.applyOrElse(Parsing.scala:622)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:553)
at io.getquill.quotation.Parsing$$anonfun$operationParser$1.applyOrElse(Parsing.scala:552)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:61)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:202)
at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:187)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:50)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:230)
at io.getquill.quotation.Parsing$$anonfun$queryParser$1.applyOrElse(Parsing.scala:187)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:50)
at io.getquill.quotation.Parsing$$anonfun$astParser$1.applyOrElse(Parsing.scala:48)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
at io.getquill.quotation.Parsing$Parser.unapply(Parsing.scala:44)
at io.getquill.quotation.Parsing$Parser.apply(Parsing.scala:35)
at io.getquill.quotation.Quotation.$anonfun$quote$1(Quotation.scala:28)
at io.getquill.util.Interpolator$Traceable.andReturn(Interpolator.scala:145)
at io.getquill.quotation.Quotation.quote(Quotation.scala:28)
at io.getquill.quotation.Quotation.quote$(Quotation.scala:24)
at io.getquill.dsl.QuotationMacro.quote(QuotationDsl.scala:34)
at jdk.internal.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at scala.reflect.macros.runtime.JavaReflectionRuntimes$JavaReflectionResolvers.$anonfun$resolveJavaReflectionRuntime$5(JavaReflectionRuntimes.scala:45)
at scala.tools.nsc.typechecker.Macros.macroExpandWithRuntime(Macros.scala:770)
roleSchema.filter(r => RoleCode(r.code) == lift(roleCode)).take(1)
編輯 3 我想這是設計上規定的,我們不能從查詢背景關系中呼叫方法。
解決大多數問題的唯一解決方案是更改域模型本身并使用值類作為案例類User引數Role
uj5u.com熱心網友回復:
正如評論中已經回答的那樣,Quill 會將欄位和案例類翻譯成選定的 SQL 方言。沒有辦法轉換RoleCode(code)為 SQL。您應該能夠創建案例類typedRole的typedCode一個欄位,并通過此特定案例類的覆寫apply方法來處理它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/529604.html
