【问题标题】:Yesod Esqueleto - How can I express selects with inner pagination?Yesod Esqueleto - 如何使用内部分页来表达选择?
【发布时间】:2016-08-08 12:34:21
【问题描述】:

我正在做一个分页资源,这将需要一个内部选择,我已经用 sql 术语设计了它。它的结构如下:

select * 
  from (
    select w.*, d.distance
      from `Work` w
      inner join AdrDistance d on d.nhood2 = w.nhood
    where d.nhood1 = 1 -- this will be a variable
    order by d.distance
    limit 0, 10 -- this will be pagination
  ) w
  inner join WImage wi on wi.`work` = w.id

我的实体定义:

Work
  ...

WImage
  work WorkId
  url Text

AdrNhood
  city AdrCityId
  name Text maxlen=100
  lat Double
  lng Double

-- This is a view with a computed column I used for ordering
AdrDistance
  nhood1 AdrNhoodId
  nhood2 AdrNhoodId
  distance Distance -- type Distance = Int - in Meters

如何在Esqueleto 中定义这样的选择,它类似于这样的结构(当然是通过一次查询)?

更新

我试着走这条路:

worksByNhood nId offset' limit' = 
  from $ \wi -> do
  (w, d) <- from $ \(w `InnerJoin` d) -> do
    on $ d ^. AdrDistanceNhood2 ==. w ^. WorkNhood
    where_ (d ^. AdrDistanceNhood1 ==. val nId)
    orderBy [asc (d ^. AdrDistanceDistance)]
    offset offset'
    limit limit'
    return (w, d)
  where_ (wi ^. WImageWork ==. w ^. WorkId)
  return (w, d ^. AdrDistanceDistance, wi)

但这并没有促使我找到正确的解决方案。如果有人可以帮助我(甚至说我最好做几个选择,因为我正在尝试的东西在Esqueleto 中不可行),请评论或回答我的问题。

【问题讨论】:

  • 我已经更新了问题,我之前确实输入了错误的实体定义。

标签: sql yesod esqueleto


【解决方案1】:

我已阅读 this issue on github 并得出结论,Esqueleto 的设计并非像我尝试的那样在 froms 中支持 selects,所以我采取了不同的做法:

worksByNhood nId offset' limit' = do
  works <- select $ from $ \(w `InnerJoin` d) -> do
    on $ d ^. AdrDistanceNhood2 ==. w ^. WorkNhood
    where_ (d ^. AdrDistanceNhood1 ==. val nId)
    orderBy [asc (d ^. AdrDistanceDistance)]
    offset offset'
    limit limit'
    return (w, d ^. AdrDistanceDistance)
  works' <- forM works $ \(w@(Entity wId _), d) -> do
    images <- select $ from $ \wi -> do
      where_ (wi ^. WImageWork ==. val wId)
      return wi
    return (w, d, images);
  return works'

这不是我想要的,但现在我会使用它。如果有人有更好的方法,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多