on dev help sign in
profunctor
commitf2a2a4921180a54e2a126a7ed766b94082a0d67a authorEitan Chatav <eitan.chatav@gmail.com> dateSat 30 Jun 2018, 11:09 (8 years ago) parent5f4d34e
2 files changed+23
squeal-postgresql/squeal-postgresql.cabal+1 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+22
squeal-postgresql/squeal-postgresql.cabal+1
@@ -50,6 +50,7 @@ library
5050 , network-ip >= 0.3.0.2
5151 , postgresql-binary >= 0.12.1
5252 , postgresql-libpq >= 0.9.4.1
53+ , profunctors >= 5.2.2
5354 , resource-pool >= 0.2.3.2
5455 , scientific >= 0.3.5.3
5556 , text >= 1.2.3.0
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+22
@@ -74,7 +74,9 @@ module Squeal.PostgreSQL.Query
7474
7575import Control.DeepSeq
7676import Data.ByteString (ByteString)
77+import Data.Kind
7778import Data.Monoid hiding (All)
79+import Data.Profunctor
7880import Data.String
7981import Data.Word
8082import Generics.SOP hiding (from)
@@ -82,6 +84,7 @@ import GHC.TypeLits
8284
8385import qualified GHC.Generics as GHC
8486
87+import Squeal.PostgreSQL.Binary
8588import Squeal.PostgreSQL.Expression
8689import Squeal.PostgreSQL.Render
8790import Squeal.PostgreSQL.Schema
@@ -285,6 +288,25 @@ newtype Query
285288 deriving (GHC.Generic,Show,Eq,Ord,NFData)
286289instance RenderSQL (Query schema params columns) where renderSQL = renderQuery
287290
291+data QueryFrom
292+ (schema :: SchemaType)
293+ (x :: Type)
294+ (y :: Type)
295+ where
296+ QueryFrom
297+ :: (x -> NP (K (Maybe ByteString)) params)
298+ -> Query schema params result
299+ -> (NP (K (Maybe ByteString)) result -> y)
300+ -> QueryFrom schema x y
301+instance Profunctor (QueryFrom schema) where
302+ dimap f g (QueryFrom params q result) = QueryFrom (params.f) q (g.result)
303+
304+queryFrom
305+ :: (ToParams x (ParamsFrom x), FromRow (ResultFrom y) y)
306+ => Query schema (ParamsFrom x) (ResultFrom y)
307+ -> QueryFrom schema x y
308+queryFrom q = QueryFrom toParams q fromRow
309+
288310type family Query' schema x y where
289311 Query' schema x y = Query schema (ParamsFrom x) (ResultFrom y)
290312