on dev help sign in
parse+increment+print
9bcc261Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Parameter.hs+21−10
@@ -35,6 +35,9 @@ module Squeal.PostgreSQL.Expression.Parameter
35 ) where
35 ) where
36
36
37import Data.ByteString (ByteString)
37import Data.ByteString (ByteString)
38+import qualified Data.ByteString as ByteString
39+import qualified Data.ByteString.Char8 as Char8
40+import Data.Int (Int32)
38import Generics.SOP (NP(..), hmap, SListI)
41import Generics.SOP (NP(..), hmap, SListI)
39import GHC.TypeLits
42import GHC.TypeLits
40
43
@@ -105,18 +108,19 @@ instance
105 , PGTyped schemas ty
108 , PGTyped schemas ty
106 , SListI columns
109 , SListI columns
107 , Insertable schemas params row columns ) =>
110 , Insertable schemas params row columns ) =>
108− Insertable schemas (ty ': params) (col ::: ty ': row)
111+ Insertable schemas params row (col ::: 'Def :=> ty ': columns) where
109− (col ::: 'NoDef :=> ty ': columns) where
112+ params = Default `as` (Alias @col) :* (params @schemas)
110− params =
111− Set (param @1) `as` (Alias @col)
112− :* hmap (overValue paramsPlus1) (params @schemas)
113instance
113instance
114 ( KnownSymbol col
114 ( KnownSymbol col
115 , PGTyped schemas ty
115 , PGTyped schemas ty
116 , SListI columns
116 , SListI columns
117 , Insertable schemas params row columns ) =>
117 , Insertable schemas params row columns ) =>
118− Insertable schemas params row (col ::: 'Def :=> ty ': columns) where
118+ Insertable schemas (ty ': params) (col ::: ty ': row)
119− params = Default `as` (Alias @col) :* (params @schemas)
119+ (col ::: 'NoDef :=> ty ': columns) where
120+ params =
121+ Set (param @1) `as` (Alias @col)
122+ :* hmap (overValue paramPlus1) (params @schemas)
123+
120overValue
124overValue
121 :: ( forall o c g s f x
125 :: ( forall o c g s f x
122 . Expression o c g s p0 f x -> Expression o c g s p1 f x )
126 . Expression o c g s p0 f x -> Expression o c g s p1 f x )
@@ -130,13 +134,20 @@ overValue f = \case
130 Set expr `As` alias -> Set (f expr) `as` alias
134 Set expr `As` alias -> Set (f expr) `as` alias
131 Default `As` alias -> Default `as` alias
135 Default `As` alias -> Default `as` alias
132
136
133−paramsPlus1
137+paramPlus1
134 :: Expression outer commons grp schemas params from ty
138 :: Expression outer commons grp schemas params from ty
135 -> Expression outer commons grp schemas (param ': params) from ty
139 -> Expression outer commons grp schemas (param ': params) from ty
136−paramsPlus1 = UnsafeExpression . paramsPlus1_ . renderSQL
140+paramPlus1 = UnsafeExpression . paramsPlus1_ . renderSQL
137 where
141 where
138 paramsPlus1_ :: ByteString -> ByteString
142 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
140
151
141insertParamsInto
152insertParamsInto
142 :: ( Has sch schemas schema
153 :: ( Has sch schemas schema