From 7f769cc673196162c4ed166aa787994c7869fdd2 Mon Sep 17 00:00:00 2001 From: Ben Levy Date: Sun, 21 Jun 2020 11:00:12 -0500 Subject: [PATCH] =?UTF-8?q?Add=20test=20for=20calling=20sql=20functions=20?= =?UTF-8?q?on=20aliased=20values;=20Fixed=20unsafeSql=E2=80=A6=20(#189)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add test for calling sql functions on aliased values; Fixed unsafeSqlFunction to handle aliaed values properly * version bump and changelog --- changelog.md | 8 +++++++- esqueleto.cabal | 2 +- src/Database/Esqueleto/Internal/Internal.hs | 12 ++++++++++-- test/Common/Test.hs | 12 ++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index e0a053e..d98f13f 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,13 @@ +3.3.3.1 +======== +- @belevy + - [#189](https://github.com/bitemyapp/esqueleto/pull/189) - Fix bug in function calls with + aliased values introduced by SubQuery joins. + 3.3.3.0 ======== - @belevy - - [#177](https://github.com/bitemyapp/esqueleto/pull/172) - Introduce new + - [#172](https://github.com/bitemyapp/esqueleto/pull/172) - Introduce new experimental module for joins, set operations (eg UNION), and safer queries from outer joins. diff --git a/esqueleto.cabal b/esqueleto.cabal index bba5ed8..ce61138 100644 --- a/esqueleto.cabal +++ b/esqueleto.cabal @@ -1,7 +1,7 @@ cabal-version: 1.12 name: esqueleto -version: 3.3.3.0 +version: 3.3.3.1 synopsis: Type-safe EDSL for SQL queries on persistent backends. description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime. . diff --git a/src/Database/Esqueleto/Internal/Internal.hs b/src/Database/Esqueleto/Internal/Internal.hs index 9a616ca..1f9bc29 100644 --- a/src/Database/Esqueleto/Internal/Internal.hs +++ b/src/Database/Esqueleto/Internal/Internal.hs @@ -1576,6 +1576,7 @@ data UnexpectedValueError = | FoldHelpError | SqlCaseError | SqlCastAsError + | SqlFunctionError | MakeOnClauseError | MakeExcError | MakeSetError @@ -2192,8 +2193,15 @@ 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 + let + valueToFunctionArg v = + case v of + ERaw _ f -> f info + EAliasedValue i _ -> aliasedValueIdentToRawSql i info + EValueReference i i' -> valueReferenceToRawSql i i' info + ECompositeKey _ -> throw (CompositeKeyErr SqlFunctionError) + (argsTLB, argsVals) = + uncommas' $ map valueToFunctionArg $ toArgList arg in (name <> parens argsTLB, argsVals) -- | (Internal) An unsafe SQL function to extract a subfield from a compound diff --git a/test/Common/Test.hs b/test/Common/Test.hs index 1620bcf..e2e6ded 100644 --- a/test/Common/Test.hs +++ b/test/Common/Test.hs @@ -2499,6 +2499,18 @@ testExperimentalFrom run = do --error . show =<< renderQuerySelect q pure () + it "can call functions on aliased values" $ do + run $ do + insert_ p1 + insert_ p3 + -- Pretend this isnt all posts + upperNames <- select $ do + author <- Experimental.from $ SelectQuery $ Experimental.from $ Table @Person + pure $ upper_ $ author ^. PersonName + + liftIO $ upperNames `shouldMatchList` [ Value "JOHN" + , Value "MIKE" + ] listsEqualOn :: (Show a1, Eq a1) => [a2] -> [a2] -> (a2 -> a1) -> Expectation listsEqualOn a b f = map f a `shouldBe` map f b