0

我想针对给定值对我的表运行查询,并Maybe a根据是否找到行返回 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) 

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

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

如前所述,我想通过“标识符”列进行查询,所以我编写了这个查询并希望返回一个 `IO (Maybe User)

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.
4

2 回答 2

3

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

getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = do
  listToMaybe <$> runQuery c (userByIdenQuery $ pgString iden)
于 2016-09-03T14:10:02.783 回答
1

我认为问题在于你undefined在函数末尾使用getUserByIden。由于 Opaleye 在后台执行了一些复杂的类型级别魔法,因此您需要指定一个类型以供使用runQuery,或者简单地返回生成的值而不是 undefined,因为 undefined 会稍微抛出顶级推理算法:

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

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

于 2016-09-03T15:13:53.737 回答