on dev help sign in
atkey vs monad
commit8540599de45255e3e77772787fbc03e663842b84 authorEitan Chatav <eitan.chatav@gmail.com> dateSat 8 Jul 2017, 21:59 (9 years ago) parentafd8683
2 files changed+35−24
squeel-postgresql/exe/Example.hs+4−4 squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+31−20
squeel-postgresql/exe/Example.hs+4−4
@@ -34,14 +34,14 @@ main = do
3434 ( createTable #students
3535 ( (text & notNull) `As` #name
3636 :* Nil ) )
37− & (pqThen . pqExec)
37+ & pqThenExec
3838 ( createTable #table1
3939 ( (int4 & notNull) `As` #col1
4040 :* (int4 & notNull) `As` #col2
4141 :* Nil ) )
42− & (pqThen . pqExec)
42+ & pqThenExec
4343 ( insertInto #table1 ( 1 `As` #col1 :* 2 `As` #col2 :* Nil ) )
44− & (pqThen . pqExec)
44+ & pqThenExec
4545 ( insertInto #table1 ( 3 `As` #col1 :* 4 `As` #col2 :* Nil ) )
4646 Char8.putStrLn "querying"
4747 connection2 <- flip execPQ connection1 $ do
@@ -63,7 +63,7 @@ main = do
6363 Char8.putStrLn "tearing down database"
6464 connection3 :: Connection '[] <- flip execPQ connection2 $ pqExec
6565 (dropTable #table1 :: Statement '[] '[] Tables '["students" ::: StudentsColumns])
66− & (pqThen . pqExec) (dropTable #students)
66+ & pqThenExec (dropTable #students)
6767 finish connection3
6868
6969type Columns =
squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+31−20
@@ -75,20 +75,33 @@ class AtkeyPQ pq where
7575 -> pq db0 db2 m y
7676 pqThen pq2 pq1 = pq1 & pqBind (\ _ -> pq2)
7777
78−class AtkeyPQ pq => MonadPQ pq where
79−
8078 pqExec
8179 :: MonadBase IO io
8280 => Statement '[] '[] db0 db1
8381 -> pq db0 db1 io (Maybe (Result '[]))
8482
85− pqPrepare
86− :: (MonadBase IO io, ToOids ps)
87− => ByteString
88− -> Statement ps xs db0 db1
89− -> pq db0 db1 io (Maybe (Result '[]), PreparedStatement ps xs db0 db1)
90−
91−class MonadQuery db m | m -> db where
83+ pqThenExec
84+ :: MonadBase IO io
85+ => Statement '[] '[] db1 db2
86+ -> pq db0 db1 io x
87+ -> pq db0 db2 io (Maybe (Result '[]))
88+ pqThenExec = pqThen . pqExec
89+
90+ -- pqPrepare
91+ -- :: (MonadBase IO io, ToOids ps)
92+ -- => ByteString
93+ -- -> Statement ps xs db0 db1
94+ -- -> pq db0 db1 io (Maybe (Result '[]), PreparedStatement ps xs db0 db1)
95+
96+ -- pqThenPrepare
97+ -- :: MonadBase IO io
98+ -- => ByteString
99+ -- -> Statement ps xs schema0 schema1
100+ -- -> pq db0 db1 io x
101+ -- -> pq db1 db1 io (Maybe (Result '[]), PreparedStatement ps xs schema0 schema1)
102+ -- pqThenPrepare name st pq = pqThen (pqPrepare name st) pq
103+
104+class MonadPQ db m | m -> db where
92105
93106 pqExecParams
94107 :: (ToOids ps, AllZip HasEncoding ps xs)
@@ -118,22 +131,20 @@ instance AtkeyPQ PQ where
118131 (x', conn') <- x conn
119132 runPQ (f x') conn'
120133
121−instance MonadPQ PQ where
122−
123134 pqExec (UnsafeStatement q) = PQ $ \ (Connection conn) -> do
124135 result <- liftBase $ LibPQ.exec conn q
125136 return (Result <$> result, Connection conn)
126137
127− pqPrepare statementName (UnsafeStatement q :: Statement ps ys db0 db1) =
128− PQ $ \ (Connection conn) -> do
129− result <- liftBase $
130− LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
131− return
132− ( ( Result <$> result
133− , UnsafePreparedStatement statementName
134− ) , Connection conn )
138+ -- pqPrepare statementName (UnsafeStatement q :: Statement ps ys db0 db1) =
139+ -- PQ $ \ (Connection conn) -> do
140+ -- result <- liftBase $
141+ -- LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
142+ -- return
143+ -- ( ( Result <$> result
144+ -- , UnsafePreparedStatement statementName
145+ -- ) , Connection conn )
135146
136−instance MonadBase IO io => MonadQuery db (PQ db db io) where
147+instance MonadBase IO io => MonadPQ db (PQ db db io) where
137148
138149 pqExecParams (UnsafeStatement q :: Statement ps ys db0 db1) params =
139150 PQ $ \ (Connection conn) -> do