From 9338cfdc5e00f22edf6af2cb5100ee8ea9bc142e Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Mon, 17 Jun 2019 14:41:11 -0600 Subject: [PATCH 1/2] Add failing test --- .gitignore | 1 + test/Common/Test.hs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/.gitignore b/.gitignore index 17bdc76d8..0d294355d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ stack.yaml.lock .cabal-sandbox/ cabal.sandbox.config .hspec-failures +stack.yaml.lock diff --git a/test/Common/Test.hs b/test/Common/Test.hs index ce8805805..c905a9ad6 100644 --- a/test/Common/Test.hs +++ b/test/Common/Test.hs @@ -219,6 +219,22 @@ testSelect run = do ret <- select $ return nothing liftIO $ ret `shouldBe` [ Value (Nothing :: Maybe Int) ] + describe "sub_select" $ do + it "works inside of sum" $ do + run $ do + ret <- + select $ + pure $ + sum_ $ + sub_select $ + from $ \foo -> do + pure (foo ^. FooName) + nonSub <- + select $ + from $ \foo -> do + pure (sum_ (foo ^. FooName)) + + liftIO $ ret `shouldBe` (nonSub :: [Value (Maybe Int)]) testSelectSource :: Run -> Spec testSelectSource run = do From ce2236d232908806a7310f32186bc99f59b881ac Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Mon, 17 Jun 2019 16:11:27 -0600 Subject: [PATCH 2/2] uhhhh this passes all the tests????? --- src/Database/Esqueleto/Internal/Internal.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Database/Esqueleto/Internal/Internal.hs b/src/Database/Esqueleto/Internal/Internal.hs index 151af41eb..92469fa96 100644 --- a/src/Database/Esqueleto/Internal/Internal.hs +++ b/src/Database/Esqueleto/Internal/Internal.hs @@ -634,11 +634,11 @@ v `notIn` e = ifNotEmptyList e True $ unsafeSqlBinOp " NOT IN " v (veryUnsafeCo -- return person -- @ exists :: SqlQuery () -> SqlExpr (Value Bool) -exists = unsafeSqlFunction "EXISTS " . existsHelper +exists = unsafeSqlFunctionNoParens "EXISTS " . existsHelper -- | @NOT EXISTS@ operator. notExists :: SqlQuery () -> SqlExpr (Value Bool) -notExists = unsafeSqlFunction "NOT EXISTS " . existsHelper +notExists = unsafeSqlFunctionNoParens "NOT EXISTS " . existsHelper -- | @SET@ clause used on @UPDATE@s. Note that while it's not -- a type error to use this function on a @SELECT@, it will @@ -1716,6 +1716,14 @@ unsafeSqlValue v = ERaw Never $ const (v, mempty) unsafeSqlFunction :: UnsafeSqlFunctionArgument a => TLB.Builder -> a -> SqlExpr (Value b) unsafeSqlFunction name arg = + ERaw Never $ \info -> + let (argsTLB, argsVals) = + uncommas' $ map (\(ERaw _ f) -> f info) $ toArgList arg + in (name <> parens (parens argsTLB), argsVals) + +unsafeSqlFunctionNoParens :: UnsafeSqlFunctionArgument a => + TLB.Builder -> a -> SqlExpr (Value b) +unsafeSqlFunctionNoParens name arg = ERaw Never $ \info -> let (argsTLB, argsVals) = uncommas' $ map (\(ERaw _ f) -> f info) $ toArgList arg