Outer Joins with Esqueleto

╄→尐↘猪︶ㄣ 提交于 2019-12-10 22:00:35

问题


I am a bit confused about how outer joins work with esqueleto.

I have created the following query (simplified):

select $ from $ \(rep `LeftOuterJoin` event) -> do
          on (rep ^. RepAtomId  ==. event   ^. EventAtomId )
          where_ (rep ^. RepAtomId  ==. val aid)
          return $ (rep, event ^. EventSeconds)

As far as I know, on the SQL-side, this query will search for reps that may have an associated event. If they do not have an associated event, the event fields (like EventSeconds) will be "null". On the Haskell side, these should be translated into Maybe Seconds (well, ints, but you get the idea).

So what actually happens when I run this query and there is nothing to adjoin to my rep relation? How do I deconstruct the tuple to stick a default in?

Currently, I have something along the lines of:

case listToMaybe lrep of
  Just ( entityVal -> rep
       , unValue -> seconds
       ) -> do stuff

(Note that I have ViewPatterns turned on here). This type checks. But it fails if I use (?.) and (fromMaybe 3600) . unValue in the pattern analysis.


回答1:


I was able to fix this by adding a 'just' in the right place:

select $ from $ \(rep `LeftOuterJoin` event) -> do
         on (just (rep ^. RepAtomId)  ==. event  ?. EventAtomId )
         where_ (rep ^. RepAtomId  ==. val aid)
         return $ (rep, event ?. EventSeconds)

case listToMaybe lrep of
  Just ( entityVal -> rep
       , (fromMaybe 3600) . unValue -> seconds
       ) -> do stuff

The idea being that (event) does have a Maybe-like type (I'm not sure what it's type is, but I know it has an embedded Maybe). So when I was comparing to the rep, I was comparing a (Maybe a) to an a. 'just' wrapped the a into a (Maybe a), without having to deconstruct the intermediate type.



来源:https://stackoverflow.com/questions/23967523/outer-joins-with-esqueleto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!