1{-# language FlexibleContexts #-}
2{-# language NamedFieldPuns #-}
3{-# language TypeFamilies #-}
4{-# language ViewPatterns #-}
6module Rel8.Table.Insert
16import Rel8.Expr ( Col(..) )
17import Rel8.Kind.Necessity ( SNecessity( SOptional, SRequired ) )
18import Rel8.Schema.HTable ( hfield, htabulate, hspecs )
19import Rel8.Schema.Insert ( Inserts, Col(..) )
20import Rel8.Schema.Spec ( SSpec(..) )
21import Rel8.Table ( fromColumns, toColumns )
24-- | @toInsert@ converts a 'Table' of 'Expr's into a 'Table' that can be used
25-- with 'Rel8.insert'. This will override any columns that have default values
26-- to use exactly what is given. If you want to use default values, you can
27-- either override the result of @toInsert@, or use 'toInsertDefaults'.
28toInsert :: Inserts exprs inserts => exprs -> inserts
29toInsert (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
30 case hfield hspecs field of
31 SSpec {necessity} -> case hfield exprs field of
32 DB expr -> case necessity of
33 SRequired -> RequiredInsert expr
34 SOptional -> OptionalInsert (Just expr)
37-- | @toInsertDefaults@ converts a 'Table' of 'Expr's into a 'Table' that can
38-- be used with 'Rel8.insert'. Any columns that have a default value will
39-- override whatever is in the input expression.
41-- One example where this is useful is for any table that has a special @id@
42-- column, which has a default value to draw a new value from a sequence. If we
43-- use 'toInsertDefaults', we can provide a dummy value that will be replaced
44-- with a call to @DEFAULT@.
45toInsertDefaults :: Inserts exprs inserts => exprs -> inserts
46toInsertDefaults (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
47 case hfield hspecs field of
48 SSpec {necessity} -> case hfield exprs field of
49 DB expr -> case necessity of
50 SRequired -> RequiredInsert expr
51 SOptional -> OptionalInsert Nothing
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list