on 2d8335d help sign in
rel8/src/Rel8/Table/Insert.hs
39 lines 1.2 kB history raw
1{-# language FlexibleContexts #-}
2{-# language NamedFieldPuns #-}
3{-# language TypeFamilies #-}
4{-# language ViewPatterns #-}
6module Rel8.Table.Insert
7 ( toInsert
8 , toInsertDefaults
9 )
10where
12-- base
13import Prelude
15-- rel8
16import Rel8.Kind.Necessity ( SNecessity( SOptional, SRequired ) )
17import Rel8.Schema.Context ( DB(..), Insertion(..) )
18import Rel8.Schema.HTable ( hfield, htabulate, hspecs )
19import Rel8.Schema.Spec ( SSpec(..) )
20import Rel8.Table ( fromColumns, toColumns )
21import Rel8.Table.Recontextualize ( Inserts )
24toInsert :: Inserts exprs inserts => exprs -> inserts
25toInsert (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
26 case hfield hspecs field of
27 SSpec {necessity} -> case hfield exprs field of
28 DB expr -> case necessity of
29 SRequired -> RequiredInsert expr
30 SOptional -> OptionalInsert (Just expr)
33toInsertDefaults :: Inserts exprs inserts => exprs -> inserts
34toInsertDefaults (toColumns -> exprs) = fromColumns $ htabulate $ \field ->
35 case hfield hspecs field of
36 SSpec {necessity} -> case hfield exprs field of
37 DB expr -> case necessity of
38 SRequired -> RequiredInsert expr
39 SOptional -> OptionalInsert Nothing