on dev help sign in
atkey vs monad
8540599Eitan Chatav
squeel-postgresql/exe/Example.hs+4−4
@@ -34,14 +34,14 @@ main = do
34 ( createTable #students
34 ( createTable #students
35 ( (text & notNull) `As` #name
35 ( (text & notNull) `As` #name
36 :* Nil ) )
36 :* Nil ) )
37− & (pqThen . pqExec)
37+ & pqThenExec
38 ( createTable #table1
38 ( createTable #table1
39 ( (int4 & notNull) `As` #col1
39 ( (int4 & notNull) `As` #col1
40 :* (int4 & notNull) `As` #col2
40 :* (int4 & notNull) `As` #col2
41 :* Nil ) )
41 :* Nil ) )
42− & (pqThen . pqExec)
42+ & pqThenExec
43 ( insertInto #table1 ( 1 `As` #col1 :* 2 `As` #col2 :* Nil ) )
43 ( insertInto #table1 ( 1 `As` #col1 :* 2 `As` #col2 :* Nil ) )
44− & (pqThen . pqExec)
44+ & pqThenExec
45 ( insertInto #table1 ( 3 `As` #col1 :* 4 `As` #col2 :* Nil ) )
45 ( insertInto #table1 ( 3 `As` #col1 :* 4 `As` #col2 :* Nil ) )
46 Char8.putStrLn "querying"
46 Char8.putStrLn "querying"
47 connection2 <- flip execPQ connection1 $ do
47 connection2 <- flip execPQ connection1 $ do
@@ -63,7 +63,7 @@ main = do
63 Char8.putStrLn "tearing down database"
63 Char8.putStrLn "tearing down database"
64 connection3 :: Connection '[] <- flip execPQ connection2 $ pqExec
64 connection3 :: Connection '[] <- flip execPQ connection2 $ pqExec
65 (dropTable #table1 :: Statement '[] '[] Tables '["students" ::: StudentsColumns])
65 (dropTable #table1 :: Statement '[] '[] Tables '["students" ::: StudentsColumns])
66− & (pqThen . pqExec) (dropTable #students)
66+ & pqThenExec (dropTable #students)
67 finish connection3
67 finish connection3
68
68
69type Columns =
69type Columns =
squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+31−20
@@ -75,20 +75,33 @@ class AtkeyPQ pq where
75 -> pq db0 db2 m y
75 -> pq db0 db2 m y
76 pqThen pq2 pq1 = pq1 & pqBind (\ _ -> pq2)
76 pqThen pq2 pq1 = pq1 & pqBind (\ _ -> pq2)
77
77
78−class AtkeyPQ pq => MonadPQ pq where
79−
80 pqExec
78 pqExec
81 :: MonadBase IO io
79 :: MonadBase IO io
82 => Statement '[] '[] db0 db1
80 => Statement '[] '[] db0 db1
83 -> pq db0 db1 io (Maybe (Result '[]))
81 -> pq db0 db1 io (Maybe (Result '[]))
84
82
85− pqPrepare
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
86− :: (MonadBase IO io, ToOids ps)
91+ -- :: (MonadBase IO io, ToOids ps)
87− => ByteString
92+ -- => ByteString
88− -> Statement ps xs db0 db1
93+ -- -> Statement ps xs db0 db1
89− -> pq db0 db1 io (Maybe (Result '[]), PreparedStatement ps xs db0 db1)
94+ -- -> pq db0 db1 io (Maybe (Result '[]), PreparedStatement ps xs db0 db1)
90−
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+
91−class MonadQuery db m | m -> db where
104+class MonadPQ db m | m -> db where
92
105
93 pqExecParams
106 pqExecParams
94 :: (ToOids ps, AllZip HasEncoding ps xs)
107 :: (ToOids ps, AllZip HasEncoding ps xs)
@@ -118,22 +131,20 @@ instance AtkeyPQ PQ where
118 (x', conn') <- x conn
131 (x', conn') <- x conn
119 runPQ (f x') conn'
132 runPQ (f x') conn'
120
133
121−instance MonadPQ PQ where
122−
123 pqExec (UnsafeStatement q) = PQ $ \ (Connection conn) -> do
134 pqExec (UnsafeStatement q) = PQ $ \ (Connection conn) -> do
124 result <- liftBase $ LibPQ.exec conn q
135 result <- liftBase $ LibPQ.exec conn q
125 return (Result <$> result, Connection conn)
136 return (Result <$> result, Connection conn)
126
137
127− pqPrepare statementName (UnsafeStatement q :: Statement ps ys db0 db1) =
138+ -- pqPrepare statementName (UnsafeStatement q :: Statement ps ys db0 db1) =
128− PQ $ \ (Connection conn) -> do
139+ -- PQ $ \ (Connection conn) -> do
129− result <- liftBase $
140+ -- result <- liftBase $
130− LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
141+ -- LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
131− return
142+ -- return
132− ( ( Result <$> result
143+ -- ( ( Result <$> result
133− , UnsafePreparedStatement statementName
144+ -- , UnsafePreparedStatement statementName
134− ) , Connection conn )
145+ -- ) , Connection conn )
135
146
136−instance MonadBase IO io => MonadQuery db (PQ db db io) where
147+instance MonadBase IO io => MonadPQ db (PQ db db io) where
137
148
138 pqExecParams (UnsafeStatement q :: Statement ps ys db0 db1) params =
149 pqExecParams (UnsafeStatement q :: Statement ps ys db0 db1) params =
139 PQ $ \ (Connection conn) -> do
150 PQ $ \ (Connection conn) -> do