@@ -39,28 +39,32 @@ import Squeel.PostgreSQL.Binary
3939import Squeel.PostgreSQL.Statement
4040import Squeel.PostgreSQL.Schema
4141
42−newtype Connection (db :: [(Symbol,[(Symbol,ColumnType)])]) =
42+newtype Connection (schema :: [(Symbol,[(Symbol,ColumnType)])]) =
4343 Connection { unConnection :: LibPQ.Connection }
4444
4545newtype PQ
46− (db0 :: [(Symbol,[(Symbol,ColumnType)])])
47− (db1 :: [(Symbol,[(Symbol,ColumnType)])])
46+ (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
47+ (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
4848 (m :: * -> *)
4949 (x :: *) =
50− PQ { runPQ :: Connection db0 -> m (x, Connection db1) }
50+ PQ { unPQ :: Connection schema0 -> m (x, Connection schema1) }
5151 deriving Functor
5252
53−evalPQ :: Functor m => PQ db0 db1 m x -> Connection db0 -> m x
54−evalPQ (PQ pq) = fmap fst . pq
53+evalPQ :: Functor m => Connection schema0 -> PQ schema0 schema1 m x -> m x
54+evalPQ conn (PQ pq) = fmap fst $ pq conn
5555
56−execPQ :: Functor m => PQ db0 db1 m x -> Connection db0 -> m (Connection db1)
57−execPQ (PQ pq) = fmap snd . pq
56+runPQ
57+ :: Functor m
58+ => Connection schema0
59+ -> PQ schema0 schema1 m x
60+ -> m (Connection schema1)
61+runPQ conn (PQ pq) = fmap snd $ pq conn
5862
5963pqAp
6064 :: Monad m
61− => PQ db0 db1 m (x -> y)
62− -> PQ db1 db2 m x
63− -> PQ db0 db2 m y
65+ => PQ schema0 schema1 m (x -> y)
66+ -> PQ schema1 schema2 m x
67+ -> PQ schema0 schema2 m y
6468pqAp (PQ f) (PQ x) = PQ $ \ conn -> do
6569 (f', conn') <- f conn
6670 (x', conn'') <- x conn'
@@ -68,61 +72,52 @@ pqAp (PQ f) (PQ x) = PQ $ \ conn -> do
6872
6973pqBind
7074 :: Monad m
71− => (x -> PQ db1 db2 m y)
72− -> PQ db0 db1 m x
73− -> PQ db0 db2 m y
75+ => (x -> PQ schema1 schema2 m y)
76+ -> PQ schema0 schema1 m x
77+ -> PQ schema0 schema2 m y
7478pqBind f (PQ x) = PQ $ \ conn -> do
7579 (x', conn') <- x conn
76− runPQ (f x') conn'
80+ unPQ (f x') conn'
7781
7882pqThen
7983 :: Monad m
80− => PQ db1 db2 m y
81− -> PQ db0 db1 m x
82− -> PQ db0 db2 m y
84+ => PQ schema1 schema2 m y
85+ -> PQ schema0 schema1 m x
86+ -> PQ schema0 schema2 m y
8387pqThen pq2 pq1 = pq1 & pqBind (\ _ -> pq2)
8488
8589pqExec
8690 :: MonadBase IO io
87− => Definition db0 db1
88− -> PQ db0 db1 io (Maybe (Result '[]))
91+ => Definition schema0 schema1
92+ -> PQ schema0 schema1 io (Maybe (Result '[]))
8993pqExec (UnsafeDefinition q) = PQ $ \ (Connection conn) -> do
9094 result <- liftBase $ LibPQ.exec conn q
9195 return (Result <$> result, Connection conn)
9296
9397pqThenExec
9498 :: MonadBase IO io
95− => Definition db1 db2
96− -> PQ db0 db1 io x
97− -> PQ db0 db2 io (Maybe (Result '[]))
99+ => Definition schema1 schema2
100+ -> PQ schema0 schema1 io x
101+ -> PQ schema0 schema2 io (Maybe (Result '[]))
98102pqThenExec = pqThen . pqExec
99103
100−class Monad m => MonadPQ db m | m -> db where
104+class Monad m => MonadPQ schema m | m -> schema where
101105
102106 pqExecParams
103107 :: (ToOids ps, AllZip HasEncoding ps xs)
104− => Manipulation db ps ys
105− -> NP I xs
106− -> m (Maybe (Result ys))
108+ => Manipulation schema ps ys -> NP I xs -> m (Maybe (Result ys))
107109 default pqExecParams
108− :: ( MonadTrans t
109− , MonadPQ db m1
110− , m ~ t m1
111− , ToOids ps
112− , AllZip HasEncoding ps xs )
113− => Manipulation db ps ys
114− -> NP I xs
115− -> m (Maybe (Result ys))
110+ :: (MonadTrans t, MonadPQ schema m1, m ~ t m1)
111+ => (ToOids ps, AllZip HasEncoding ps xs)
112+ => Manipulation schema ps ys -> NP I xs -> m (Maybe (Result ys))
116113 pqExecParams manipulation params = lift $ pqExecParams manipulation params
117114
118− pqExecNil
119− :: Manipulation db '[] ys
120− -> m (Maybe (Result ys))
115+ pqExecNil :: Manipulation schema '[] ys -> m (Maybe (Result ys))
121116 pqExecNil statement = pqExecParams statement Nil
122117
123−instance MonadBase IO io => MonadPQ db (PQ db db io) where
118+instance MonadBase IO io => MonadPQ schema (PQ schema schema io) where
124119
125− pqExecParams (UnsafeManipulation q :: Manipulation db ps ys) params =
120+ pqExecParams (UnsafeManipulation q :: Manipulation schema ps ys) params =
126121 PQ $ \ (Connection conn) -> do
127122 let
128123 paramValues = encodings (Proxy :: Proxy ps) params
@@ -134,44 +129,45 @@ instance MonadBase IO io => MonadPQ db (PQ db db io) where
134129 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
135130 return (Result <$> result, Connection conn)
136131
137−instance Monad m => Applicative (PQ db db m) where
132+instance Monad m => Applicative (PQ schema schema m) where
138133 pure x = PQ $ \ conn -> pure (x, conn)
139134 (<*>) = pqAp
140135
141−instance Monad m => Monad (PQ db db m) where
136+instance Monad m => Monad (PQ schema schema m) where
142137 return = pure
143138 (>>=) = flip pqBind
144139
145−instance MonadTrans (PQ db db) where
140+instance MonadTrans (PQ schema schema) where
146141 lift m = PQ $ \ conn -> do
147142 x <- m
148143 return (x, conn)
149144
150−instance MonadBase b m => MonadBase b (PQ db db m) where
145+instance MonadBase b m => MonadBase b (PQ schema schema m) where
151146 liftBase = lift . liftBase
152147
153−type PQRun db = forall m x. Monad m => PQ db db m x -> m (x, Connection db)
148+type PQRun schema =
149+ forall m x. Monad m => PQ schema schema m x -> m (x, Connection schema)
154150
155−pqliftWith :: Functor m => (PQRun db -> m a) -> PQ db db m a
151+pqliftWith :: Functor m => (PQRun schema -> m a) -> PQ schema schema m a
156152pqliftWith f = PQ $ \ conn ->
157− fmap (\ x -> (x, conn)) (f $ \ pq -> runPQ pq conn)
153+ fmap (\ x -> (x, conn)) (f $ \ pq -> unPQ pq conn)
158154
159−instance MonadBaseControl b m => MonadBaseControl b (PQ db db m) where
160− type StM (PQ db db m) x = StM m (x, Connection db)
155+instance MonadBaseControl b m => MonadBaseControl b (PQ schema schema m) where
156+ type StM (PQ schema schema m) x = StM m (x, Connection schema)
161157 liftBaseWith f =
162158 pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
163159 restoreM = PQ . const . restoreM
164160
165−connectdb :: MonadBase IO io => ByteString -> io (Connection db)
161+connectdb :: MonadBase IO io => ByteString -> io (Connection schema)
166162connectdb = fmap Connection . liftBase . LibPQ.connectdb
167163
168−finish :: MonadBase IO io => Connection db -> io ()
164+finish :: MonadBase IO io => Connection schema -> io ()
169165finish = liftBase . LibPQ.finish . unConnection
170166
171167withConnection
172168 :: MonadBaseControl IO io
173169 => ByteString
174− -> (Connection db -> io x)
170+ -> (Connection schema -> io x)
175171 -> io x
176172withConnection connString action =
177173 bracket (connectdb connString) finish action