Skip to content

Commit 3f12b21

Browse files
committed
Merge pull request #10 from anttih/ps-0.7
WIP: updates for ps-0.7
2 parents 8dd1ce0 + 48d6c9d commit 3f12b21

File tree

7 files changed

+150
-152
lines changed

7 files changed

+150
-152
lines changed

bower.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"output"
1515
],
1616
"dependencies": {
17-
"purescript-arrays": "~0.3.7",
18-
"purescript-either": "~0.1.8",
19-
"purescript-foreign": "~0.4.2",
20-
"purescript-foldable-traversable": "~0.3.1",
21-
"purescript-transformers": "~0.5.5",
17+
"purescript-arrays": "~0.4.0",
18+
"purescript-either": "~0.2.0",
19+
"purescript-foreign": "~0.5.0",
20+
"purescript-foldable-traversable": "~0.4.0",
21+
"purescript-transformers": "~0.6.0",
2222
"purescript-aff": "~0.10.1",
23-
"purescript-integers": "~0.1.0"
23+
"purescript-integers": "~0.2.0"
2424
}
2525
}

src/Database/Postgres.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
// module Database.Postgres
3+
4+
var pg = require('pg');
5+
6+
exports["connect'"] = function (conString) {
7+
return function(success, error) {
8+
var client = new pg.Client(conString);
9+
client.connect(function(err) {
10+
if (err) {
11+
error(err);
12+
} else {
13+
success(client);
14+
}
15+
})
16+
return client;
17+
};
18+
}
19+
20+
exports._withClient = function (conString, cb) {
21+
return function(success, error) {
22+
pg.connect(conString, function(err, client, done) {
23+
if (err) {
24+
done(true);
25+
return error(err);
26+
}
27+
cb(client)(function(v) {
28+
done();
29+
success(v);
30+
}, function(err) {
31+
done();
32+
error(err);
33+
})
34+
});
35+
};
36+
}
37+
38+
exports.runQuery_ = function (queryStr) {
39+
return function(client) {
40+
return function(success, error) {
41+
client.query(queryStr, function(err, result) {
42+
if (err) {
43+
error(err);
44+
} else {
45+
success(result.rows);
46+
}
47+
})
48+
};
49+
};
50+
}
51+
52+
exports.runQuery = function (queryStr) {
53+
return function(params) {
54+
return function(client) {
55+
return function(success, error) {
56+
client.query(queryStr, params, function(err, result) {
57+
if (err) return error(err);
58+
success(result.rows);
59+
})
60+
};
61+
};
62+
};
63+
}
64+
65+
exports.runQueryValue_ = function (queryStr) {
66+
return function(client) {
67+
return function(success, error) {
68+
client.query(queryStr, function(err, result) {
69+
if (err) return error(err);
70+
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
71+
})
72+
};
73+
};
74+
}
75+
76+
exports.runQueryValue = function (queryStr) {
77+
return function(params) {
78+
return function(client) {
79+
return function(success, error) {
80+
client.query(queryStr, params, function(err, result) {
81+
if (err) return error(err);
82+
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
83+
})
84+
};
85+
};
86+
};
87+
}
88+
89+
exports.end = function (client) {
90+
return function() {
91+
client.end();
92+
};
93+
}
94+
95+
exports.disconnect = function () {
96+
pg.end();
97+
}

src/Database/Postgres.purs

Lines changed: 15 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Database.Postgres
1616
, withClient
1717
) where
1818

19+
import Prelude
1920
import Control.Alt
2021
import Control.Apply ((*>))
2122
import Control.Monad.Eff
@@ -45,7 +46,7 @@ type ConnectionString = String
4546
type 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
6465
connect = 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
6869
execute (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.
7576
query :: 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)
7879
query (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)
8485
query_ (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
8990
queryOne :: 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)
9293
queryOne (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.
103104
queryValue :: 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)
106107
queryValue (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
134135
liftError :: forall e a. ForeignError -> Aff e a
135136
liftError 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

src/Database/Postgres/SqlValue.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
// module Database.Postgres.SqlValue
4+
5+
exports.unsafeToSqlValue = function (x) {
6+
return x;
7+
}
8+
9+
exports.nullSqlValue = null;

src/Database/Postgres/SqlValue.purs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Database.Postgres.SqlValue
44
, toSql
55
) where
66

7+
import Prelude ((<<<))
78
import Data.Int
89
import Data.Maybe
910

@@ -25,10 +26,6 @@ instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where
2526
toSql Nothing = nullSqlValue
2627
toSql (Just x) = toSql x
2728

28-
foreign import unsafeToSqlValue """
29-
function unsafeToSqlValue(x) {
30-
return x;
31-
}
32-
""" :: forall a. a -> SqlValue
29+
foreign import unsafeToSqlValue :: forall a. a -> SqlValue
3330

34-
foreign import nullSqlValue "var nullSqlValue = null;" :: SqlValue
31+
foreign import nullSqlValue :: SqlValue

src/Database/Postgres/Transaction.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Database.Postgres.Transaction where
22

3+
import Prelude
34
import Control.Apply ((*>))
45
import Control.Monad.Aff
56
import Control.Monad.Error.Class (throwError)

0 commit comments

Comments
 (0)