Opaleye 查询由 String 到 Maybe

Opaleye query by String to Maybe

我想 运行 查询我的 table 给定值,并且 return Maybe a 取决于是否找到行。

我有这个域名:

data User' a b c d e f = User {  usrId :: a,
                                     usrApproved :: b,
                                     usrIden :: c, 
                                     usrImgUrl :: d,
                                     usrTitle :: e, 
                                     usrUrl :: f 
                                  }

type User = User' Int Bool String String String String

$(makeAdaptorAndInstance "pUser" ''User')

type UserColumn = User' (Column PGInt4) (Column PGBool) (Column PGText) (Column PGText) (Column PGText) (Column PGText) 

以及 table 和查询的以下定义:

userTable :: Table UserColumn UserColumn
userTable = Table "user" (pUser User {  usrId       = required "id",
                                                usrApproved = required "approved",
                                                usrIden     = required "identifier",
                                                usrImgUrl   = required "img_url",
                                                usrTitle    = required "title",
                                                usrUrl      = required "url"
                                             })

userQuery :: Query UserColumn
userQuery = queryTable userTable

如前所述,我想通过 "identifier" 列进行查询,因此我编写了此查询并希望 return `IO(可能是用户)

userByIdenQuery :: (Column PGText) -> Query UserColumn
userByIdenQuery iden = proc () -> do
  user <- userQuery -< ()
  restrict -< (adIden user) .=== iden
  returnA -< user    

getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = do
  usr <- runQuery c (userByIdenQuery $ pgString iden)
  -- But this fails to compile
  undefined -- just to minimise compilation errors

编译失败:

No instance for (Default
                   Opaleye.Internal.RunQuery.QueryRunner UserColumn haskells0)
  arising from a use of `runQuery'
The type variable `haskells0' is ambiguous
Note: there is a potential instance available:
  instance (product-profunctors-0.7.1.0:Data.Profunctor.Product.Class.ProductProfunctor
              p,
            Default p a1_0 a1_1, Default p a2_0 a2_1, Default p a3_0 a3_1,
            Default p a4_0 a4_1, Default p a5_0 a5_1, Default p a6_0 a6_1) =>
           Default
             p
             (User' a1_0 a2_0 a3_0 a4_0 a5_0 a6_0)
             (User' a1_1 a2_1 a3_1 a4_1 a5_1 a6_1)
    -- Defined at src\DB.hs:33:3
In a stmt of a 'do' block:
  usr <- runQuery c (userByIdenQuery $ pgString iden)
In the expression:
  do { usr <- runQuery c (userByIdenQuery $ pgString iden);
       undefined }
In an equation for `getUserByIden':
    getUserByIden iden c
      = do { usr <- runQuery c (userByIdenQuery $ pgString iden);
             undefined }

如果我尝试实现功能:

getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = do
  (usrId, appr, idn, imUrl, tit, url) <- runQuery c (userByIdenQuery $ pgString iden)
  return $ Just $ User usrId appr idn imUrl tit url

然后我看到这个编译错误:

Couldn't match expected type `[haskells0]'
        with actual type `(Int, Bool, String, String, String, String)'
In the pattern: (usrId, appr, idn, imUrl, tit, url)
In a stmt of a 'do' block:
  (usrId, appr, idn, imUrl, tit, url) <- runQuery
                                          c (userByIdenQuery $ pgString iden)
In the expression:
  do { (usrId, appr, idn, imUrl, tit, url) <- runQuery
                                               c (userByIdenQuery $ pgString iden);
       return $ Just $ User usrId appr idn imUrl tit url }

I really have no idea where to go with this, other than using a library other than Opaleye.

我不熟悉 OpalEye,但我认为您可能只需要明智地使用 listToMaybe (import Data.Maybe (listToMaybe))

getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = do
  listToMaybe <$> runQuery c (userByIdenQuery $ pgString iden)

我认为问题在于你在函数 getUserByIden 的末尾使用了 undefined。由于 Opaleye 在幕后进行了一些复杂的类型级魔法,您需要指定使用 runQuery 的类型,或者简单地 return 生成的值而不是 undefined,因为 undefined 会抛出顶部推理算法稍微偏离一点:

getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = listToMaybe <$> runQuery c (userByIdenQuery $ pgString iden)

我还添加了一个 listToMaybe 调用来更改函数体的类型以匹配您指定的 return 类型。 runQuery,完全应用后,return 是 IO [SomeHaskellType] 类型的值,而不是 IO (Maybe SomeHaskellType)