简体   繁体   English

外部加入Esqueleto

[英]Outer Joins with Esqueleto

I am a bit confused about how outer joins work with esqueleto. 关于外连接如何与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. 据我所知,在SQL端,此查询将搜索可能具有关联事件的代表。 If they do not have an associated event, the event fields (like EventSeconds) will be "null". 如果它们没有关联的事件,则事件字段(如EventSeconds)将为“null”。 On the Haskell side, these should be translated into Maybe Seconds (well, ints, but you get the idea). 在Haskell方面,这些应该被翻译成Maybe Seconds(嗯,整数,但你明白了)。

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). (注意我在这里打开了ViewPatterns)。 This type checks. 这种类型检查。 But it fails if I use (?.) and (fromMaybe 3600) . 但如果我使用(?。)和(fromMaybe 360​​0)它就会失败。 unValue in the pattern analysis. 模式分析中的unValue。

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). 这个想法是(事件)确实有一个类似于Maybe的类型(我不确定它的类型是什么,但我知道它有一个嵌入的Maybe)。 So when I was comparing to the rep, I was comparing a (Maybe a) to an a. 因此,当我与代表进行比较时,我将a(也许a)与a进行比较。 'just' wrapped the a into a (Maybe a), without having to deconstruct the intermediate type. 'just'将a包装成(也许是a),而不必解构中间类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM