Add instance of UnsafeSqlFunctionArgument ()
This commit is contained in:
parent
c2ecf9c1a4
commit
55fec71ed4
@ -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
|
3.2.0
|
||||||
========
|
========
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
cabal-version: 1.12
|
cabal-version: 1.12
|
||||||
|
|
||||||
name: esqueleto
|
name: esqueleto
|
||||||
version: 3.2.0
|
version: 3.2.1
|
||||||
synopsis: Type-safe EDSL for SQL queries on persistent backends.
|
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.
|
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.
|
||||||
.
|
.
|
||||||
|
|||||||
@ -2025,6 +2025,13 @@ unsafeSqlCastAs _ (ECompositeKey _) = throw (CompositeKeyErr SqlCastAsError)
|
|||||||
|
|
||||||
class UnsafeSqlFunctionArgument a where
|
class UnsafeSqlFunctionArgument a where
|
||||||
toArgList :: a -> [SqlExpr (Value ())]
|
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
|
instance (a ~ Value b) => UnsafeSqlFunctionArgument (SqlExpr a) where
|
||||||
toArgList = (:[]) . veryUnsafeCoerceSqlExprValue
|
toArgList = (:[]) . veryUnsafeCoerceSqlExprValue
|
||||||
instance UnsafeSqlFunctionArgument a =>
|
instance UnsafeSqlFunctionArgument a =>
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import qualified Data.List as L
|
|||||||
import Data.Ord (comparing)
|
import Data.Ord (comparing)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.Encoding as TE
|
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 Database.Esqueleto hiding (random_)
|
||||||
import qualified Database.Esqueleto.Internal.Sql as ES
|
import qualified Database.Esqueleto.Internal.Sql as ES
|
||||||
import Database.Esqueleto.PostgreSQL (random_)
|
import Database.Esqueleto.PostgreSQL (random_)
|
||||||
@ -493,6 +493,14 @@ testPostgresModule = do
|
|||||||
[Value (ret :: String)] <- select $ return (EP.chr (val 65))
|
[Value (ret :: String)] <- select $ return (EP.chr (val 65))
|
||||||
liftIO $ ret `shouldBe` "A"
|
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" $
|
it "works with now" $
|
||||||
run $ do
|
run $ do
|
||||||
nowDb <- select $ return EP.now_
|
nowDb <- select $ return EP.now_
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user