Allow complex ORDER BYs by correctly putting parenthesis.

This commit is contained in:
Felipe Lessa 2013-01-27 21:16:07 -02:00
parent 55f08c9b38
commit 745c5cd276
2 changed files with 16 additions and 1 deletions

View File

@ -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"

View File

@ -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