From 55fec71ed4ef786a5b60a7b2f6f4deed956a7a95 Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Tue, 29 Oct 2019 08:49:49 -0600 Subject: [PATCH] Add instance of UnsafeSqlFunctionArgument () --- changelog.md | 7 +++++++ esqueleto.cabal | 2 +- src/Database/Esqueleto/Internal/Internal.hs | 17 ++++++++++++----- test/PostgreSQL/Test.hs | 10 +++++++++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 5890340..cd04b73 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,10 @@ +3.2.1 +======== + +- @parsonsmatt + = [#159](https://github.com/bitemyapp/esqueleto/pull/159): Add an instance of `UnsafeSqlFunction ()` for 0-argument SQL + functions. + 3.2.0 ======== diff --git a/esqueleto.cabal b/esqueleto.cabal index e71220f..57f6a6e 100644 --- a/esqueleto.cabal +++ b/esqueleto.cabal @@ -1,7 +1,7 @@ cabal-version: 1.12 name: esqueleto -version: 3.2.0 +version: 3.2.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 16bbc6a..ddc6e83 100644 --- a/src/Database/Esqueleto/Internal/Internal.hs +++ b/src/Database/Esqueleto/Internal/Internal.hs @@ -1053,12 +1053,12 @@ instance FinalResult (Unique val) where instance (FinalResult b) => FinalResult (a -> b) where finalR f = finalR (f undefined) --- | Convert a constructor for a 'Unique' key on a record to the 'UniqueDef' that defines it. You --- can supply just the constructor itself, or a value of the type - the library is capable of figuring +-- | Convert a constructor for a 'Unique' key on a record to the 'UniqueDef' that defines it. You +-- can supply just the constructor itself, or a value of the type - the library is capable of figuring -- it out from there. -- -- @since 3.1.3 -toUniqueDef :: forall a val. (KnowResult a ~ (Unique val), PersistEntity val,FinalResult a) => +toUniqueDef :: forall a val. (KnowResult a ~ (Unique val), PersistEntity val,FinalResult a) => a -> UniqueDef toUniqueDef uniqueConstructor = uniqueDef where @@ -1071,9 +1071,9 @@ toUniqueDef uniqueConstructor = uniqueDef uniqueDef = head . filter filterF . entityUniques . entityDef $ proxy -- | Render updates to be use in a SET clause for a given sql backend. --- +-- -- @since 3.1.3 -renderUpdates :: (BackendCompatible SqlBackend backend) => +renderUpdates :: (BackendCompatible SqlBackend backend) => backend -> [SqlExpr (Update val)] -> (TLB.Builder, [PersistValue]) @@ -2025,6 +2025,13 @@ unsafeSqlCastAs _ (ECompositeKey _) = throw (CompositeKeyErr SqlCastAsError) class UnsafeSqlFunctionArgument a where toArgList :: a -> [SqlExpr (Value ())] + +-- | Useful for 0-argument functions, like @now@ in Postgresql. +-- +-- @since 3.2.1 +instance UnsafeSqlFunctionArgument () where + toArgList _ = [] + instance (a ~ Value b) => UnsafeSqlFunctionArgument (SqlExpr a) where toArgList = (:[]) . veryUnsafeCoerceSqlExprValue instance UnsafeSqlFunctionArgument a => diff --git a/test/PostgreSQL/Test.hs b/test/PostgreSQL/Test.hs index b008f13..2b7b99e 100644 --- a/test/PostgreSQL/Test.hs +++ b/test/PostgreSQL/Test.hs @@ -25,7 +25,7 @@ import qualified Data.List as L import Data.Ord (comparing) import qualified Data.Text as T import qualified Data.Text.Encoding as TE -import Data.Time.Clock (getCurrentTime, diffUTCTime) +import Data.Time.Clock (getCurrentTime, diffUTCTime, UTCTime) import Database.Esqueleto hiding (random_) import qualified Database.Esqueleto.Internal.Sql as ES import Database.Esqueleto.PostgreSQL (random_) @@ -493,6 +493,14 @@ testPostgresModule = do [Value (ret :: String)] <- select $ return (EP.chr (val 65)) liftIO $ ret `shouldBe` "A" + it "allows unit for functions" $ do + vals <- run $ do + let + fn :: SqlExpr (Value UTCTime) + fn = ES.unsafeSqlFunction "now" () + select $ pure fn + vals `shouldSatisfy` ((1 ==) . length) + it "works with now" $ run $ do nowDb <- select $ return EP.now_