我想针对给定值对我的表运行查询,并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.