on master help sign in
Add Rel8.Expr.Default.unsafeDefault
c6ffdabOllie Charles
rel8.cabal+1
@@ -66,6 +66,7 @@ library
66 Rel8.Expr.Aggregate
66 Rel8.Expr.Aggregate
67 Rel8.Expr.Array
67 Rel8.Expr.Array
68 Rel8.Expr.Bool
68 Rel8.Expr.Bool
69+ Rel8.Expr.Default
69 Rel8.Expr.Eq
70 Rel8.Expr.Eq
70 Rel8.Expr.Function
71 Rel8.Expr.Function
71 Rel8.Expr.Null
72 Rel8.Expr.Null
src/Rel8.hs+3−1
@@ -250,14 +250,15 @@ module Rel8
250 , Insert(..)
250 , Insert(..)
251 , OnConflict(..)
251 , OnConflict(..)
252 , insert
252 , insert
253+ , unsafeDefault
253
254
254 -- ** @DELETE@
255 -- ** @DELETE@
255 , Delete(..)
256 , Delete(..)
256 , delete
257 , delete
257
258
258 -- ** @UPDATE@
259 -- ** @UPDATE@
259− , update
260 , Update(..)
260 , Update(..)
261+ , update
261
262
262 -- ** @.. RETURNING@
263 -- ** @.. RETURNING@
263 , Returning(..)
264 , Returning(..)
@@ -289,6 +290,7 @@ import Rel8.Column.These
289import Rel8.Expr
290import Rel8.Expr
290import Rel8.Expr.Aggregate
291import Rel8.Expr.Aggregate
291import Rel8.Expr.Bool
292import Rel8.Expr.Bool
293+import Rel8.Expr.Default
292import Rel8.Expr.Eq
294import Rel8.Expr.Eq
293import Rel8.Expr.Function
295import Rel8.Expr.Function
294import Rel8.Expr.Null
296import 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