在Haskell中使用土拨鼠库,我希望实现一个使用" uuid"当后端是Postgresql时,否则只需使用" varchar"对于任何其他后端。虽然根据Groundhog.Core中的注释,这应该是可能的,我不完全确定如何从proxy db
解包dbType,并且在groundhog-examples中没有这样的示例,因为列类型在这些例子中已被硬编码。
如果我想在Postgresql的案例匹配方面提供一些帮助,我会在排序之后解决剩下的问题。我在这里:
instance PrimitivePersistField UUID where
toPrimitivePersistValue _ uuid = PersistString $ show uuid
fromPrimitivePersistValue _ (PersistString a) = fromJust $ UUIDmethods.fromString a
instance PersistField UUID where
persistName _ = "UUID"
toPersistValues = primToPersistValue
fromPersistValues = primFromPersistValue
dbType db _ = case db of
Postgresql _ -> DbTypePrimitive (DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive (DbOther $ OtherTypeDef [Left "varchar"]) False Nothing Nothing
在编译时出现了这个问题:
Couldn't match expected type ‘proxy db’
with actual type ‘Postgresql’
Relevant bindings include
db :: proxy db (bound at basicGroundhog.hs:34:10)
dbType :: proxy db -> UUID -> DbType
(bound at basicGroundhag.hs:34:3)
In the pattern: Postgresql _
In a case alternative:
Postgresql _
-> DbTypePrimitive
(DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
In the expression:
case db of {
Postgresql _
-> DbTypePrimitive
(DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive
(DbOther $ OtherTypeDef [Left "varchar"]) False Nothing Nothing }
答案 0 :(得分:1)
您可以使用backendName
在运行时检查数据库。最好将DbString
放在默认情况下,以便土拨鼠可以选择更合适的类型。没有最大长度声明的Varchar在MySQL中无效。
dbType db _ = case backendName db of
"postgresql" -> DbTypePrimitive (DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive DbString False Nothing Nothing