外加入Esqueleto [英] Outer Joins with Esqueleto

查看:129
本文介绍了外加入Esqueleto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了以下查询(简化):

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




$ b

据我所知,在SQL方面,这个查询将搜索可能有相关事件。如果它们没有关联的事件,则事件字段(如EventSeconds)将为null。在Haskell方面,这些应该被翻译成Maybe Seconds(好吧,整数,但你明白了)。

那么当我运行这个查询时,实际发生了什么与我的代表关系没有任何关系?如何解构元组来保持默认值?



目前,我有一些内容:

  case listToMaybe lrep of 
Just(entityVal - > rep
,unValue - > seconds
) - >做东西

(请注意,我在此处打开ViewPatterns)。这种类型检查。但是如果我使用(?。)和(可能来自3600),它就会失败。

解决方案

我可以通过在正确的地方添加'just'来解决这个问题:

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

case listToMaybe lrep of
Just(entityVal - > rep
,(fromMaybe 360​​0)。unValue - > seconds
) - >做的东西

想法是(事件)确实有类似Maybe的类型(我不是确定它的类型是什么,但我知道它有一个嵌入的可能)。所以当我和代表比较的时候,我正在比较一个(也许是a)和a。 'just'将a包装成(也许a),而不必解构中间类型。


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.

解决方案

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.

这篇关于外加入Esqueleto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆