on dev help sign in
docs
commit27f756b545c466d1713382244cbb95ebf638a105 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 21 Jun 2018, 21:52 (8 years ago) parenta1b7db8
2 files changed+18−7
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+6−6 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+12−1
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+6−6
@@ -85,18 +85,18 @@ Then we can perform roundtrip queries;
8585
8686>>> :{
8787let
88− qry1 :: Query Schema
88+ querySchwarma :: Query Schema
8989 '[ 'NotNull (EnumFrom Schwarma)]
9090 '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
91− qry1 = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
91+ querySchwarma = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
9292:}
9393
9494>>> :{
9595let
96− qry2 :: Query Schema
96+ queryPerson :: Query Schema
9797 '[ 'NotNull (CompositeFrom Person)]
9898 '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
99− qry2 = values_ (parameter @1 #person `As` #fromOnly :* Nil)
99+ queryPerson = values_ (parameter @1 #person `As` #fromOnly :* Nil)
100100:}
101101
102102And finally drop the types.
@@ -112,10 +112,10 @@ Now let's run it.
112112>>> :{
113113let
114114 session = do
115− result1 <- runQueryParams qry1 (Only Chicken)
115+ result1 <- runQueryParams querySchwarma (Only Chicken)
116116 Just (Only schwarma) <- firstRow result1
117117 liftBase $ print (schwarma :: Schwarma)
118− result2 <- runQueryParams qry2 (Only (Person (Just "Faisal") (Just 24)))
118+ result2 <- runQueryParams queryPerson (Only (Person (Just "Faisal") (Just 24)))
119119 Just (Only person) <- firstRow result2
120120 liftBase $ print (person :: Person)
121121in
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+12−1
@@ -189,6 +189,11 @@ class KnownNat n => HasParameter
189189 (params :: [NullityType])
190190 (ty :: NullityType)
191191 | 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)
192197 parameter
193198 :: TypeExpression schema (PGTypeOf ty)
194199 -> Expression schema relations grouping params ty
@@ -199,10 +204,16 @@ instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
199204instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
200205 => HasParameter n schema (ty' : params) ty
201206
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)
202213param
203214 :: forall n schema params relations grouping ty
204215 . (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
205− => Expression schema relations grouping params ty
216+ => Expression schema relations grouping params ty -- ^ param
206217param = parameter @n pgtype
207218
208219instance (HasUnique relation relations columns, Has column columns ty)