@@ -1,3 +1,4 @@
1+{-# language BlockArguments #-}
1{-# language DisambiguateRecordFields #-}
2{-# language DisambiguateRecordFields #-}
2{-# language FlexibleContexts #-}
3{-# language FlexibleContexts #-}
3{-# language NamedFieldPuns #-}
4{-# language NamedFieldPuns #-}
@@ -12,6 +13,7 @@ module Rel8.Table.Opaleye
12 , tableFields
13 , tableFields
13 , unpackspec
14 , unpackspec
14 , valuesspec
15 , valuesspec
@@ -38,9 +40,10 @@ import Rel8.Expr.Opaleye
38 ( fromPrimExpr, toPrimExpr
40 ( fromPrimExpr, toPrimExpr
39 , traversePrimExpr
41 , traversePrimExpr
40 , fromColumn, toColumn
42 , fromColumn, toColumn
42import Rel8.Kind.Necessity ( SNecessity( SRequired, SOptional ) )
45import Rel8.Kind.Necessity ( SNecessity( SRequired, SOptional ) )
43−import Rel8.Schema.HTable ( htabulateA, hfield, htraverse, hspecs )
46+import Rel8.Schema.HTable ( htabulateA, hfield, htraverse, hspecs, htabulate )
44import Rel8.Schema.Insert ( Insert, Inserts, Col(..) )
47import Rel8.Schema.Insert ( Insert, Inserts, Col(..) )
45import Rel8.Schema.Name ( Name, Selects, Col(..) )
48import Rel8.Schema.Name ( Name, Selects, Col(..) )
46import Rel8.Schema.Spec ( SSpec(..) )
49import Rel8.Schema.Spec ( SSpec(..) )
@@ -124,3 +127,15 @@ toPackMap as = Opaleye.PackMap $ \f () ->
124 unwrapApplicative .
127 unwrapApplicative .
125 htraverse (\(DB a) -> WrapApplicative $ DB <$> traversePrimExpr f a) $
128 htraverse (\(DB a) -> WrapApplicative $ DB <$> traversePrimExpr f a) $
126 toColumns as
129 toColumns as
132+-- | Transform a table by adding 'CAST' to all columns. This is most useful for
133+-- finalising a SELECT or RETURNING statement, guaranteed that the output
134+-- matches what is encoded in each columns TypeInformation.
135+castTable :: Table Expr a => a -> a
136+castTable (toColumns -> as) = fromColumns $ htabulate \i ->
137+ case hfield hspecs i of
141+ DB (scastExpr info expr)