@@ -16,6 +16,7 @@ module Database.Postgres
1616 , withClient
1717 ) where
1818
19+ import Prelude
1920import Control.Alt
2021import Control.Apply ((*>))
2122import Control.Monad.Eff
@@ -45,7 +46,7 @@ type ConnectionString = String
4546type ConnectionInfo =
4647 { host :: String
4748 , db :: String
48- , port :: Number
49+ , port :: Int
4950 , user :: String
5051 , password :: String
5152 }
@@ -64,7 +65,7 @@ connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client
6465connect = connect' <<< mkConnectionString
6566
6667-- | Runs a query and returns nothing.
67- execute :: forall eff a . Query a -> [ SqlValue ] -> Client -> Aff (db :: DB | eff ) Unit
68+ execute :: forall eff a . Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) Unit
6869execute (Query sql) params client = void $ runQuery sql params client
6970
7071-- | Runs a query and returns nothing
@@ -74,21 +75,21 @@ execute_ (Query sql) client = void $ runQuery_ sql client
7475-- | Runs a query and returns all results.
7576query :: forall eff a p
7677 . (IsForeign a )
77- => Query a -> [ SqlValue ] -> Client -> Aff (db :: DB | eff ) [ a ]
78+ => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) ( Array a )
7879query (Query sql) params client = do
7980 rows <- runQuery sql params client
8081 either liftError pure (sequence $ read <$> rows)
8182
8283-- | Just like `query` but does not make any param replacement
83- query_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) [ a ]
84+ query_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) ( Array a )
8485query_ (Query sql) client = do
8586 rows <- runQuery_ sql client
8687 either liftError pure (sequence $ read <$> rows)
8788
8889-- | Runs a query and returns the first row, if any
8990queryOne :: forall eff a
9091 . (IsForeign a )
91- => Query a -> [ SqlValue ] -> Client -> Aff (db :: DB | eff ) (Maybe a )
92+ => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
9293queryOne (Query sql) params client = do
9394 rows <- runQuery sql params client
9495 maybe (pure Nothing ) (either liftError (pure <<< Just )) $ read <$> (rows !! 0 )
@@ -102,7 +103,7 @@ queryOne_ (Query sql) client = do
102103-- | Runs a query and returns a single value, if any.
103104queryValue :: forall eff a
104105 . (IsForeign a )
105- => Query a -> [ SqlValue ] -> Client -> Aff (db :: DB | eff ) (Maybe a )
106+ => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
106107queryValue (Query sql) params client = do
107108 val <- runQueryValue sql params client
108109 pure $ either (const Nothing ) Just (read val)
@@ -134,127 +135,18 @@ withClient info p = runFn2 _withClient (mkConnectionString info) p
134135liftError :: forall e a . ForeignError -> Aff e a
135136liftError err = throwError $ error (show err)
136137
137- finally :: forall eff a . Aff eff a -> Aff eff Unit -> Aff eff a
138- finally a sequel = do
139- res <- attempt a
140- sequel
141- either throwError pure res
138+ foreign import connect' :: forall eff . String -> Aff (db :: DB | eff ) Client
142139
140+ foreign import _withClient :: forall eff a . Fn2 ConnectionString (Client -> Aff (db :: DB | eff ) a ) (Aff (db :: DB | eff ) a )
143141
144- foreign import connect' " " "
145- function connect$prime(conString) {
146- return function(success, error) {
147- var pg = require('pg');
148- var client = new pg.Client(conString);
149- client.connect(function(err) {
150- if (err) {
151- error(err);
152- } else {
153- success(client);
154- }
155- })
156- return client;
157- }
158- }
159- " " " :: forall eff . String -> Aff (db :: DB | eff ) Client
160-
161- foreign import _withClient
162- " " "
163- function _withClient(conString, cb) {
164- return function(success, error) {
165- var pg = require('pg');
166- pg.connect(conString, function(err, client, done) {
167- if (err) {
168- done(true);
169- return error(err);
170- }
171- cb(client)(function(v) {
172- done();
173- success(v);
174- }, function(err) {
175- done();
176- error(err);
177- })
178- });
179- };
180- }
181- " " " :: forall eff a .
182- Fn2
183- ConnectionString
184- (Client -> Aff (db :: DB | eff ) a )
185- (Aff (db :: DB | eff ) a )
186-
187- foreign import runQuery_ " " "
188- function runQuery_(queryStr) {
189- return function(client) {
190- return function(success, error) {
191- client.query(queryStr, function(err, result) {
192- if (err) {
193- error(err);
194- } else {
195- success(result.rows);
196- }
197- })
198- };
199- };
200- }
201- " " " :: forall eff . String -> Client -> Aff (db :: DB | eff ) [Foreign ]
142+ foreign import runQuery_ :: forall eff . String -> Client -> Aff (db :: DB | eff ) (Array Foreign )
202143
203- foreign import runQuery " " "
204- function runQuery(queryStr) {
205- return function(params) {
206- return function(client) {
207- return function(success, error) {
208- client.query(queryStr, params, function(err, result) {
209- if (err) return error(err);
210- success(result.rows);
211- })
212- };
213- };
214- }
215- }
216- " " " :: forall eff . String -> [SqlValue ] -> Client -> Aff (db :: DB | eff ) [Foreign ]
144+ foreign import runQuery :: forall eff . String -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Array Foreign )
217145
218- foreign import runQueryValue_ " " "
219- function runQueryValue_(queryStr) {
220- return function(client) {
221- return function(success, error) {
222- client.query(queryStr, function(err, result) {
223- if (err) return error(err);
224- success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
225- })
226- };
227- };
228- }
229- " " " :: forall eff . String -> Client -> Aff (db :: DB | eff ) Foreign
146+ foreign import runQueryValue_ :: forall eff . String -> Client -> Aff (db :: DB | eff ) Foreign
230147
231- foreign import runQueryValue " " "
232- function runQueryValue(queryStr) {
233- return function(params) {
234- return function(client) {
235- return function(success, error) {
236- client.query(queryStr, params, function(err, result) {
237- if (err) return error(err);
238- success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
239- })
240- };
241- };
242- }
243- }
244- " " " :: forall eff . String -> [SqlValue ] -> Client -> Aff (db :: DB | eff ) Foreign
148+ foreign import runQueryValue :: forall eff . String -> Array SqlValue -> Client -> Aff (db :: DB | eff ) Foreign
245149
246- foreign import end " " "
247- function end(client) {
248- return function() {
249- client.end();
250- };
251- }
252- " " " :: forall eff . Client -> Eff (db :: DB | eff ) Unit
150+ foreign import end :: forall eff . Client -> Eff (db :: DB | eff ) Unit
253151
254- foreign import disconnect
255- " " "
256- function disconnect() {
257- var pg = require('pg');
258- pg.end();
259- }
260- " " " :: forall eff . Eff (db :: DB | eff ) Unit
152+ foreign import disconnect :: forall eff . Eff (db :: DB | eff ) Unit
0 commit comments