@@ -16,6 +16,7 @@ module Database.Postgres
16
16
, withClient
17
17
) where
18
18
19
+ import Prelude
19
20
import Control.Alt
20
21
import Control.Apply ((*>))
21
22
import Control.Monad.Eff
@@ -45,7 +46,7 @@ type ConnectionString = String
45
46
type ConnectionInfo =
46
47
{ host :: String
47
48
, db :: String
48
- , port :: Number
49
+ , port :: Int
49
50
, user :: String
50
51
, password :: String
51
52
}
@@ -64,7 +65,7 @@ connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client
64
65
connect = connect' <<< mkConnectionString
65
66
66
67
-- | 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
68
69
execute (Query sql) params client = void $ runQuery sql params client
69
70
70
71
-- | Runs a query and returns nothing
@@ -74,21 +75,21 @@ execute_ (Query sql) client = void $ runQuery_ sql client
74
75
-- | Runs a query and returns all results.
75
76
query :: forall eff a p
76
77
. (IsForeign a )
77
- => Query a -> [ SqlValue ] -> Client -> Aff (db :: DB | eff ) [ a ]
78
+ => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) ( Array a )
78
79
query (Query sql) params client = do
79
80
rows <- runQuery sql params client
80
81
either liftError pure (sequence $ read <$> rows)
81
82
82
83
-- | 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 )
84
85
query_ (Query sql) client = do
85
86
rows <- runQuery_ sql client
86
87
either liftError pure (sequence $ read <$> rows)
87
88
88
89
-- | Runs a query and returns the first row, if any
89
90
queryOne :: forall eff a
90
91
. (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 )
92
93
queryOne (Query sql) params client = do
93
94
rows <- runQuery sql params client
94
95
maybe (pure Nothing ) (either liftError (pure <<< Just )) $ read <$> (rows !! 0 )
@@ -102,7 +103,7 @@ queryOne_ (Query sql) client = do
102
103
-- | Runs a query and returns a single value, if any.
103
104
queryValue :: forall eff a
104
105
. (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 )
106
107
queryValue (Query sql) params client = do
107
108
val <- runQueryValue sql params client
108
109
pure $ either (const Nothing ) Just (read val)
@@ -134,127 +135,18 @@ withClient info p = runFn2 _withClient (mkConnectionString info) p
134
135
liftError :: forall e a . ForeignError -> Aff e a
135
136
liftError err = throwError $ error (show err)
136
137
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
142
139
140
+ foreign import _withClient :: forall eff a . Fn2 ConnectionString (Client -> Aff (db :: DB | eff ) a ) (Aff (db :: DB | eff ) a )
143
141
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 )
202
143
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 )
217
145
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
230
147
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
245
149
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
253
151
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