繁体   English   中英

Yesod Esqueleto-如何用内在分页表达选择?

[英]Yesod Esqueleto - How can I express selects with inner pagination?

我正在做一个分页的资源,这将需要一个内部选择,这是我已经用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定义这样的选择,该选择类似于这样的结构(当然, 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)

但这并没有驱使我找到正确的解决方案。 如果有人可以帮助我(甚至说我会做一些选择会更好,因为我尝试的操作在EsqueletoEsqueleto ),请发表评论或回答我的问题。

我已经在github上阅读了此问题,并得出结论Esqueleto并非旨在以我尝试的方式支持select from ,因此我做了不同的事情:

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'

这并不是我一直在寻找的东西,但是现在我将使用它。 如果有人有更好的方法,请告诉我。

暂无
暂无

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

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