Opaleye query by String to Maybe

311 Views Asked by At

I want to run a query against my table for a given value, and return a Maybe a depending on whether a row was found.

I have this domain:

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) 

and the following definitions for the table and queries:

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

As hinted previously, I want to query by the "identifier" column, so I've written this query and wish to return a `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

this fails to compile with:

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 }

If I try to implement the function:

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

Then I'm presented with this compilation error:

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

There are 2 best solutions below

0
On BEST ANSWER

I'm not familiar with OpalEye, but I think you might just need a judicious use of listToMaybe (import Data.Maybe (listToMaybe))

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

I think the problem lies with you using undefined in the end of your function getUserByIden. Because of some complicated type level magic Opaleye is doing under the hood, you need to specify a type for the use of runQuery, or simply return the value produced instead of undefined, because undefined throws the top inference algorithm off a little bit:

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

I also added a listToMaybe call to change the type of the body of the function to match the return type you specified. runQuery, when fully applied, returns a value of type IO [SomeHaskellType], not IO (Maybe SomeHaskellType).