on master help sign in
Document Rel8.Expr.Function
06e6c05Ollie Charles
src/Rel8/Expr/Function.hs+34
@@ -43,10 +43,44 @@ instance (arg ~ Expr a, Function args res) => Function arg (args -> res) where
43 applyArgument f a = applyArgument (f . (toPrimExpr a :))
43 applyArgument f a = applyArgument (f . (toPrimExpr a :))
44
44
45
45
46+-- | Construct an n-ary function that produces an 'Expr' that when called runs
47+-- a SQL function.
48+--
49+-- For example, here's how we can wrap PostgreSQL's @factorial@ function:
50+--
51+-- >>> :{
52+-- factorial :: Expr Int64 -> Expr Data.Scientific.Scientific
53+-- factorial = function "factorial"
54+-- :}
55+--
56+-- >>> select c $ pure $ factorial 5
57+-- [120.0]
58+--
59+-- The same approach works for any number of arguments:
60+--
61+-- >>> :{
62+-- power :: Expr Float -> Expr Float -> Expr Double
63+-- power = function "power"
64+-- :}
65+--
66+-- >>> select c $ pure $ power 9 3
67+-- [729.0]
46function :: Function args result => String -> args -> result
68function :: Function args result => String -> args -> result
47function = applyArgument . Opaleye.FunExpr
69function = applyArgument . Opaleye.FunExpr
48
70
49
71
72+-- | Construct a function call for functions with no arguments.
73+--
74+-- For example, we can call the database function @pi()@ by using
75+-- @nullaryFunction@:
76+--
77+-- >>> :{
78+-- sqlPi :: Expr Double
79+-- sqlPi = nullaryFunction "pi"
80+-- :}
81+--
82+-- >>> select c $ pure $ sqlPi
83+-- [3.141592653589793]
50nullaryFunction :: (DBType db, Nullabilizes db a) => String -> Expr a
84nullaryFunction :: (DBType db, Nullabilizes db a) => String -> Expr a
51nullaryFunction name = castExpr $ Expr (Opaleye.FunExpr name [])
85nullaryFunction name = castExpr $ Expr (Opaleye.FunExpr name [])
52
86