on dev help sign in
parse+increment+print

and insertable instance for defaults

commit9bcc261f3314fc9e57032075c376186a03cee4d9 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateFri 14 Jun 2019, 22:26 (7 years ago) parent8ed0cb1
1 file changed+21−10
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Parameter.hs+21−10
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Parameter.hs+21−10
@@ -35,6 +35,9 @@ module Squeal.PostgreSQL.Expression.Parameter
3535 ) where
3636
3737import Data.ByteString (ByteString)
38+import qualified Data.ByteString as ByteString
39+import qualified Data.ByteString.Char8 as Char8
40+import Data.Int (Int32)
3841import Generics.SOP (NP(..), hmap, SListI)
3942import GHC.TypeLits
4043
@@ -105,18 +108,19 @@ instance
105108 , PGTyped schemas ty
106109 , SListI columns
107110 , Insertable schemas params row columns ) =>
108− Insertable schemas (ty ': params) (col ::: ty ': row)
109− (col ::: 'NoDef :=> ty ': columns) where
110− params =
111− Set (param @1) `as` (Alias @col)
112− :* hmap (overValue paramsPlus1) (params @schemas)
111+ Insertable schemas params row (col ::: 'Def :=> ty ': columns) where
112+ params = Default `as` (Alias @col) :* (params @schemas)
113113instance
114114 ( KnownSymbol col
115115 , PGTyped schemas ty
116116 , SListI columns
117117 , Insertable schemas params row columns ) =>
118− Insertable schemas params row (col ::: 'Def :=> ty ': columns) where
119− params = Default `as` (Alias @col) :* (params @schemas)
118+ Insertable schemas (ty ': params) (col ::: ty ': row)
119+ (col ::: 'NoDef :=> ty ': columns) where
120+ params =
121+ Set (param @1) `as` (Alias @col)
122+ :* hmap (overValue paramPlus1) (params @schemas)
123+
120124overValue
121125 :: ( forall o c g s f x
122126 . Expression o c g s p0 f x -> Expression o c g s p1 f x )
@@ -130,13 +134,20 @@ overValue f = \case
130134 Set expr `As` alias -> Set (f expr) `as` alias
131135 Default `As` alias -> Default `as` alias
132136
133−paramsPlus1
137+paramPlus1
134138 :: Expression outer commons grp schemas params from ty
135139 -> Expression outer commons grp schemas (param ': params) from ty
136−paramsPlus1 = UnsafeExpression . paramsPlus1_ . renderSQL
140+paramPlus1 = UnsafeExpression . paramsPlus1_ . renderSQL
137141 where
138142 paramsPlus1_ :: ByteString -> ByteString
139− paramsPlus1_ = undefined -- replace $n with $n+1
143+ paramsPlus1_ p =
144+ let
145+ (left_n, right) = ByteString.breakSubstring "::" p
146+ isDigit w = w - 48 <= 9
147+ (left, n) = ByteString.break isDigit left_n
148+ add1 = Char8.pack . show . (+ 1) . read @Int32 . Char8.unpack
149+ in
150+ left <> add1 n <> right
140151
141152insertParamsInto
142153 :: ( Has sch schemas schema