From 745c5cd276f789f791cc343307af3cd35e3ef8d5 Mon Sep 17 00:00:00 2001 From: Felipe Lessa Date: Sun, 27 Jan 2013 21:16:07 -0200 Subject: [PATCH] Allow complex ORDER BYs by correctly putting parenthesis. --- src/Database/Esqueleto/Internal/Sql.hs | 2 +- test/Test.hs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 4f92c53..032c9f0 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -762,7 +762,7 @@ makeOrderBy :: Connection -> [OrderByClause] -> (TLB.Builder, [PersistValue]) makeOrderBy _ [] = mempty makeOrderBy conn os = first ("\nORDER BY " <>) $ uncommas' (map mk os) where - mk (EOrderBy t (ERaw _ f)) = first (<> orderByType t) (f conn) + mk (EOrderBy t (ERaw p f)) = first ((<> orderByType t) . parensM p) (f conn) orderByType ASC = " ASC" orderByType DESC = " DESC" diff --git a/test/Test.hs b/test/Test.hs index 0bddffc..414bf92 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -385,6 +385,21 @@ main = do return p liftIO $ ret `shouldBe` [ p1e, p4e, p3e, p2e ] + it "works with a sub_select" $ + run $ do + [p1k, p2k, p3k, p4k] <- mapM insert [p1, p2, p3, p4] + [b1k, b2k, b3k, b4k] <- mapM (insert . BlogPost "") [p1k, p2k, p3k, p4k] + ret <- select $ + from $ \b -> do + orderBy [desc $ sub_select $ + from $ \p -> do + where_ (p ^. PersonId ==. b ^. BlogPostAuthorId) + return (p ^. PersonName) + ] + return (b ^. BlogPostId) + liftIO $ ret `shouldBe` (Value <$> [b2k, b3k, b4k, b1k]) + + describe "selectDistinct" $ it "works on a simple example" $ run $ do