on dev help sign in
docs
27f756bEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+6−6
@@ -85,18 +85,18 @@ Then we can perform roundtrip queries;
85
85
86>>> :{
86>>> :{
87let
87let
88− qry1 :: Query Schema
88+ querySchwarma :: Query Schema
89 '[ 'NotNull (EnumFrom Schwarma)]
89 '[ 'NotNull (EnumFrom Schwarma)]
90 '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
90 '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
91− qry1 = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
91+ querySchwarma = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
92:}
92:}
93
93
94>>> :{
94>>> :{
95let
95let
96− qry2 :: Query Schema
96+ queryPerson :: Query Schema
97 '[ 'NotNull (CompositeFrom Person)]
97 '[ 'NotNull (CompositeFrom Person)]
98 '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
98 '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
99− qry2 = values_ (parameter @1 #person `As` #fromOnly :* Nil)
99+ queryPerson = values_ (parameter @1 #person `As` #fromOnly :* Nil)
100:}
100:}
101
101
102And finally drop the types.
102And finally drop the types.
@@ -112,10 +112,10 @@ Now let's run it.
112>>> :{
112>>> :{
113let
113let
114 session = do
114 session = do
115− result1 <- runQueryParams qry1 (Only Chicken)
115+ result1 <- runQueryParams querySchwarma (Only Chicken)
116 Just (Only schwarma) <- firstRow result1
116 Just (Only schwarma) <- firstRow result1
117 liftBase $ print (schwarma :: Schwarma)
117 liftBase $ print (schwarma :: Schwarma)
118− result2 <- runQueryParams qry2 (Only (Person (Just "Faisal") (Just 24)))
118+ result2 <- runQueryParams queryPerson (Only (Person (Just "Faisal") (Just 24)))
119 Just (Only person) <- firstRow result2
119 Just (Only person) <- firstRow result2
120 liftBase $ print (person :: Person)
120 liftBase $ print (person :: Person)
121in
121in
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+12−1
@@ -189,6 +189,11 @@ class KnownNat n => HasParameter
189 (params :: [NullityType])
189 (params :: [NullityType])
190 (ty :: NullityType)
190 (ty :: NullityType)
191 | n params -> ty where
191 | n params -> ty where
192+ -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
193+ --
194+ -- >>> let expr = parameter @1 int4 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
195+ -- >>> printSQL expr
196+ -- ($1 :: int4)
192 parameter
197 parameter
193 :: TypeExpression schema (PGTypeOf ty)
198 :: TypeExpression schema (PGTypeOf ty)
194 -> Expression schema relations grouping params ty
199 -> Expression schema relations grouping params ty
@@ -199,10 +204,16 @@ instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
199instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
204instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
200 => HasParameter n schema (ty' : params) ty
205 => HasParameter n schema (ty' : params) ty
201
206
207+-- | `param` takes a `Nat` using type application and for basic types,
208+-- infers a `TypeExpression`.
209+--
210+-- >>> let expr = param @1 :: Expression sch rels grp '[ 'Null 'PGint4] ('Null 'PGint4)
211+-- >>> printSQL expr
212+-- ($1 :: int4)
202param
213param
203 :: forall n schema params relations grouping ty
214 :: forall n schema params relations grouping ty
204 . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
215 . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
205− => Expression schema relations grouping params ty
216+ => Expression schema relations grouping params ty -- ^ param
206param = parameter @n pgtype
217param = parameter @n pgtype
207
218
208instance (HasUnique relation relations columns, Has column columns ty)
219instance (HasUnique relation relations columns, Has column columns ty)