on master help sign in
Maybe fixed direction bug (?)
commite141ccc11672d4cda34722bc4331bb61f9c17296 authoriko <ilyakooo0@gmail.com> dateSat 4 May 2019, 15:02 (7 years ago) parent484e250
1 file changed+49−38
denis/src/Data/Query.hs+49−38
denis/src/Data/Query.hs+49−38
@@ -176,15 +176,20 @@ isNotFalse x = UnsafeExpression $ renderExpression x <+> "IS NOT FALSE"
176176getLastPostsQ
177177 :: TableEndo _ _ _ _
178178 -> Limit
179− -> ConditionOp Schema _ _ _ _ _ _
179+ -> P.PaginationDirection
180180 -> Query Schema ('Null (PG Int64) ': params) (RowPG PostRowResponse)
181−getLastPostsQ te lim (.^) = getPostQ
182− (orderBy [#postElements ! #rowElementOrd & Asc] . orderBy [#posts ! #postRowId & Desc])
183− (limit lim . te . where_ (isNotFalse $ #posts ! #postRowId .^ param @1) . orderBy [#posts ! #postRowId & Desc])
181+getLastPostsQ te lim dir = getPostQ
182+ (orderBy [#postElements ! #rowElementId & Desc, #postElements ! #rowElementOrd & Asc])
183+ (limit lim .
184+ te .
185+ where_ (ifThenElse (isNull $ param @1) true $ #posts ! #postRowId .^ param @1) .
186+ orderBy [#posts ! #postRowId & order])
187+ where
188+ ((.^), order) = directionToOperator dir
184189
185190getLastNPostsForUserQ
186191 :: Limit
187− -> ConditionOp Schema _ _ _ _ _ _
192+ -> P.PaginationDirection
188193 -> Query Schema (TuplePG (Maybe PostId, UserId)) (RowPG PostRowResponse)
189194getLastNPostsForUserQ = getLastPostsQ $ where_ (#posts ! #postRowAuthorId .== param @2)
190195
@@ -246,24 +251,21 @@ updateNamedChannelQ = update_ #channels
246251
247252getChannelPostsQ
248253 :: Limit
249− -> ConditionOp Schema _ _ _ _ _ _
254+ -> P.PaginationDirection
250255 -> [UserId]
251256 -> Vector Text
252257 -> Query
253258 Schema
254259 (TuplePG (Only (Maybe PostId))) -- ^ post id from which to start
255260 (RowPG PostRowResponse)
256−getChannelPostsQ lim (.^) uIds tags = selectStar $ from (subquery $ postsQ' `As` #bar) &
257− where_ (isNotFalse $ #postRowId .^ param @1) &
258− orderBy [#rowElementOrd & Asc] & limit lim . orderBy [#postRowId & Desc]
261+getChannelPostsQ lim dir uIds tags = getLastPostsQ
262+ (where_ $ jsonbLit tags .?| #postRowTags .|| isUser #postRowAuthorId)
263+ lim
264+ dir
259265 where
260− unionWithUsers = case idsToColumn uIds of
261− Just uIdsQ -> selectDotStar #posts' (from $ subquery (uIdsQ `As` #uIds) &
262− innerJoin (subquery $ getPostQ id id `As` #posts')
263− (#uIds ! #id .== #posts' ! #postRowAuthorId)) &
264− union
265− Nothing -> id
266− postsQ' = unionWithUsers $ getPostQ id $ where_ $ jsonbLit tags .?| #postRowTags
266+ isUser = case idsToColumn uIds of
267+ Just uIdsQ -> (`in_` uIdsQ)
268+ Nothing -> const $ false
267269
268270getTagsQ :: Query Schema '[] (RowPG (Only String))
269271getTagsQ = selectDistinct (unsafeFunction "unnest" #postRowTags `as` #fromOnly) $
@@ -323,9 +325,9 @@ type ChatQueryParams = TuplePG
323325
324326getChatsQ
325327 :: Limit
326− -> ConditionOp Schema _ _ _ _ _ _
328+ -> P.PaginationDirection
327329 -> Query Schema ChatQueryParams (RowPG ChatsResponse)
328−getChatsQ lim (.^) = select (
330+getChatsQ lim dir = select (
329331 #chats ! #messageId `as` #chatResponseMessageId :*
330332 #messages ! #messageStorageAuthorId `as` #chatResponseMessageAuthorId :*
331333 #messages ! #messageStorageDestinationGroupId `as` #chatResponseMessageDestinationGroupId :*
@@ -345,9 +347,11 @@ getChatsQ lim (.^) = select (
345347 where_ (isNull (#groupChats ! #groupChatUsers) .||
346348 (#groupChats ! #groupChatUsers .? param @2)) &
347349 where_ (isNotFalse $ #chats ! #messageId .^ param @3) &
348− orderBy [#chats ! #messageId & Desc] &
349− limit lim
350+ orderBy [#chats ! #messageId & order] &
351+ limit lim &
352+ orderBy [#chats ! #messageId & Desc]
350353 where
354+ ((.^), order) = directionToOperator dir
351355 chatsQ :: Query Schema _ ChatsQueryResponse
352356 chatsQ = unionAll groupChats userChats
353357 where
@@ -381,33 +385,39 @@ getChatsQ lim (.^) = select (
381385
382386getMessagesQ
383387 :: Limit
384− -> ConditionOp Schema _ _ _ _ _ _
388+ -> P.PaginationDirection
385389 -> Query Schema (TuplePG
386390 ( UserId
387391 , UserId
388392 , Maybe MessageId
389393 )) (RowPG MessageStorage)
390−getMessagesQ lim (.^) = selectStar (from (table #messages) &
394+getMessagesQ lim dir = selectStar (from (table #messages) &
391395 where_ (isNotNull #messageStorageDestinationUserId .&&
392396 ((#messageStorageAuthorId .== param @1 .&& #messageStorageDestinationUserId .== param @2) .||
393397 (#messageStorageDestinationUserId .== param @1 .&& #messageStorageAuthorId .== param @2))) &
394398 where_ (isNotFalse $ #messageStorageId .^ param @3) &
395− orderBy [#messageStorageId & Desc] &
396− limit lim)
399+ orderBy [#messageStorageId & order] &
400+ limit lim &
401+ orderBy [#messageStorageId & Desc])
402+ where
403+ ((.^), order) = directionToOperator dir
397404
398405getGroupMessagesQ
399406 :: Limit
400− -> ConditionOp Schema _ _ _ _ _ _
407+ -> P.PaginationDirection
401408 -> Query Schema (TuplePG
402409 ( GroupChatId
403410 , Maybe MessageId
404411 )) (RowPG MessageStorage)
405−getGroupMessagesQ lim (.^) = selectStar (from (table #messages) &
412+getGroupMessagesQ lim dir = selectStar (from (table #messages) &
406413 where_ (isNotNull #messageStorageDestinationGroupId .&&
407414 (#messageStorageDestinationGroupId .== param @1)) &
408415 where_ (isNotFalse $ #messageStorageId .^ param @2) &
409− orderBy [#messageStorageId & Desc] &
410− limit lim)
416+ orderBy [#messageStorageId & order] &
417+ limit lim &
418+ orderBy [#messageStorageId & Desc])
419+ where
420+ ((.^), order) = directionToOperator dir
411421
412422-- String -- user id as String
413423getGroupChatForUserQ :: Query Schema (TuplePG (String, GroupChatId)) (RowPG GroupChat)
@@ -473,7 +483,7 @@ getPostsForUser
473483 -> UserId
474484 -> StaticPQ (Maybe (ResponseWithUsers [Post]))
475485getPostsForUser pId lim dir uId = do
476− postRows <- runQueryParams (getLastNPostsForUserQ lim $ directionToOperator dir) (pId, uId) >>= getRows
486+ postRows <- runQueryParams (getLastNPostsForUserQ lim dir) (pId, uId) >>= getRows
477487 wrapIntoResponse (fromMaybe [] . rowsToPosts $ postRows)
478488
479489getPosts :: [PostId] -> StaticPQ (Maybe (ResponseWithUsers [Post]))
@@ -490,7 +500,7 @@ getLastPosts
490500 -> P.PaginationDirection
491501 -> StaticPQ (Maybe (ResponseWithUsers [Post]))
492502getLastPosts pId lim dir = do
493− postRows <- runQueryParams (getLastPostsQ id lim $ directionToOperator dir) (Only pId) >>= getRows
503+ postRows <- runQueryParams (getLastPostsQ id lim dir) (Only pId) >>= getRows
494504 rowsToPosts postRows `liftMaybe` wrapIntoResponse
495505
496506getUser :: UserId -> StaticPQ User
@@ -535,7 +545,7 @@ getChannelPosts uId pId lim dir cId = do
535545 channel <- getChannelForUser uId cId
536546 postRows <- runQueryParams (getChannelPostsQ
537547 lim
538− (directionToOperator dir)
548+ dir
539549 (toList $ namedChannelFullPeopleIds channel)
540550 (namedChannelFullTags channel))
541551 (Only pId)
@@ -549,7 +559,7 @@ getAnonymousChannelPosts
549559 -> AnonymousChannel
550560 -> StaticPQ (ResponseWithUsers [Post])
551561getAnonymousChannelPosts pId lim dir (AnonymousChannel tags people) = do
552− postRows <- runQueryParams (getChannelPostsQ lim (directionToOperator dir) people tags) (Only pId) >>= getRows
562+ postRows <- runQueryParams (getChannelPostsQ lim dir people tags) (Only pId) >>= getRows
553563 fmap (fromMaybe $ ResponseWithUsers mempty mempty) $ rowsToPosts postRows `liftMaybe` wrapIntoResponse
554564
555565getAllChannels :: UserId -> StaticPQ [NamedChannel UserId]
@@ -572,7 +582,7 @@ getDialogs
572582 -> P.PaginationDirection
573583 -> StaticPQ (ResponseWithUsers [Dialog])
574584getDialogs uId mId lim dir = do
575− resp <- runQueryParams (getChatsQ lim $ directionToOperator dir) (uId, Just $ show uId, mId) >>= getRows
585+ resp <- runQueryParams (getChatsQ lim dir) (uId, Just $ show uId, mId) >>= getRows
576586 (>>= fromMaybe500) $ (responseToDialogs uId resp `liftMaybe` wrapIntoResponse)
577587
578588getMessagesInUserChat
@@ -583,7 +593,7 @@ getMessagesInUserChat
583593 -> P.PaginationDirection
584594 -> StaticPQ [Message]
585595getMessagesInUserChat selfId uId mId lim dir = do
586− resp <- runQueryParams (getMessagesQ lim $ directionToOperator dir) (selfId, uId, mId) >>= getRows
596+ resp <- runQueryParams (getMessagesQ lim dir) (selfId, uId, mId) >>= getRows
587597 fromMaybe500 . sequence . map restoreMessage $ resp
588598
589599getMessagesInGroupChat
@@ -595,7 +605,7 @@ getMessagesInGroupChat
595605 -> StaticPQ [Message]
596606getMessagesInGroupChat selfId gId mId lim dir = do
597607 _ <- getGroupChatForUser selfId gId
598− resp <- runQueryParams (getGroupMessagesQ lim $ directionToOperator dir) (gId, mId) >>= getRows
608+ resp <- runQueryParams (getGroupMessagesQ lim dir) (gId, mId) >>= getRows
599609 (return . sequence . map restoreMessage $ resp) >>= fromMaybe500
600610
601611sendMessage :: UserId -> MessageCreation -> StaticPQ MessageId
@@ -645,9 +655,10 @@ getGroupChatForUser uId gId = do
645655
646656directionToOperator
647657 :: P.PaginationDirection
648− -> ConditionOp schema from grouping params nullity0 nullity1 (ty :: PGType)
649−directionToOperator P.BackPagination = (.<)
650−directionToOperator P.ForwardPagination = (.>)
658+ -> (ConditionOp schema0 from0 grouping0 params0 nullity0 nullity1 (ty :: PGType),
659+ (Expression schema2 from2 grouping2 params2 _ -> SortExpression schema2 from2 grouping2 params2))
660+directionToOperator P.BackPagination = ((.<), Desc)
661+directionToOperator P.ForwardPagination = ((.>), Asc)
651662
652663foldMaybe :: [Maybe a] -> Maybe [a]
653664foldMaybe [] = Just []