on master help sign in
Add Rel8.Expr.Default.unsafeDefault
commitc6ffdabf9655e268d82012734143a90cf233fcf9 authorOllie Charles <oliver.charles@circuithub.com> dateTue 29 Jun 2021, 22:45 (5 years ago) parent6950d69
3 files changed+39−1
rel8.cabal+1 src/Rel8.hs+3−1 src/Rel8/Expr/Default.hs+35
rel8.cabal+1
@@ -66,6 +66,7 @@ library
6666 Rel8.Expr.Aggregate
6767 Rel8.Expr.Array
6868 Rel8.Expr.Bool
69+ Rel8.Expr.Default
6970 Rel8.Expr.Eq
7071 Rel8.Expr.Function
7172 Rel8.Expr.Null
src/Rel8.hs+3−1
@@ -250,14 +250,15 @@ module Rel8
250250 , Insert(..)
251251 , OnConflict(..)
252252 , insert
253+ , unsafeDefault
253254
254255 -- ** @DELETE@
255256 , Delete(..)
256257 , delete
257258
258259 -- ** @UPDATE@
259− , update
260260 , Update(..)
261+ , update
261262
262263 -- ** @.. RETURNING@
263264 , Returning(..)
@@ -289,6 +290,7 @@ import Rel8.Column.These
289290import Rel8.Expr
290291import Rel8.Expr.Aggregate
291292import Rel8.Expr.Bool
293+import Rel8.Expr.Default
292294import Rel8.Expr.Eq
293295import Rel8.Expr.Function
294296import Rel8.Expr.Null
src/Rel8/Expr/Default.hsnew file+35
@@ -0,0 +1,35 @@
1+module Rel8.Expr.Default
2+ ( unsafeDefault
3+ )
4+where
5+
6+-- base
7+import Prelude ()
8+
9+-- opaleye
10+import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
11+
12+-- rel8
13+import Rel8.Expr ( Expr )
14+import Rel8.Expr.Opaleye ( fromPrimExpr )
15+
16+
17+-- | Corresponds to the SQL @DEFAULT@ expression.
18+--
19+-- This 'Expr' is unsafe for numerous reasons, and should be used with care:
20+--
21+-- 1. This 'Expr' only makes sense in an @INSERT@ or @UPDATE@ statement.
22+--
23+-- 2. Rel8 is not able to verify that a particular column actually has a
24+-- @DEFAULT@ value. Trying to use @unsafeDefault@ where there is no default
25+-- will cause a runtime crash
26+--
27+-- 3. @DEFAULT@ values can not be transformed. For example, the innocuous Rel8
28+-- code @unsafeDefault + 1@ will crash, despite type checking.
29+--
30+-- Given all these caveats, we suggest avoiding the use of default values where
31+-- possible, instead being explicit. A common scenario where default values are
32+-- used is with auto-incrementing identifier columns. In this case, we suggest
33+-- using 'Rel8.nextval' instead.
34+unsafeDefault :: Expr a
35+unsafeDefault = fromPrimExpr Opaleye.DefaultInsertExpr