generated from tpl/purs
Merge pull request #16 from paluh/master
Upgrade to purescript-aff 4.0.0
This commit is contained in:
commit
3e2a2633de
@ -6,7 +6,7 @@
|
||||
"purescript-transformers": "^3.2.0",
|
||||
"purescript-lists": "^4.0.1",
|
||||
"purescript-foreign": "^4.0.0",
|
||||
"purescript-aff": "^3.0.0",
|
||||
"purescript-aff": "^4.0.0",
|
||||
"purescript-either": "^3.0.0",
|
||||
"purescript-maybe": "^3.0.0",
|
||||
"purescript-foldable-traversable": "^3.0.0",
|
||||
|
@ -9,17 +9,22 @@ exports.ffiNewPool = function(config) {
|
||||
};
|
||||
|
||||
exports.ffiConnect = function (pool) {
|
||||
return function(onError) {
|
||||
return function(onSuccess) {
|
||||
return function() {
|
||||
pool.connect(function(err, client, done) {
|
||||
if (err != null) {
|
||||
onError(err)();
|
||||
return;
|
||||
return function (onError, onSuccess) {
|
||||
var p = pool.connect(
|
||||
).then(function(client) {
|
||||
onSuccess({
|
||||
connection: client,
|
||||
done: function() {
|
||||
return client.release();
|
||||
}
|
||||
onSuccess({connection: client, done: done})();
|
||||
});
|
||||
};
|
||||
}).catch(function(err) {
|
||||
onError(err);
|
||||
});
|
||||
|
||||
return function (cancelError, cancelerError, cancelerSuccess) {
|
||||
p.cancel();
|
||||
cancelerSuccess();
|
||||
};
|
||||
};
|
||||
};
|
||||
@ -27,21 +32,20 @@ exports.ffiConnect = function(pool) {
|
||||
exports.ffiUnsafeQuery = function(client) {
|
||||
return function(sql) {
|
||||
return function(values) {
|
||||
return function(onError) {
|
||||
return function(onSuccess) {
|
||||
return function() {
|
||||
client.query({
|
||||
return function(onError, onSuccess) {
|
||||
var q = client.query({
|
||||
text: sql,
|
||||
values: values,
|
||||
rowMode: 'array',
|
||||
}, function(err, result) {
|
||||
if (err != null) {
|
||||
onError(err)();
|
||||
return;
|
||||
}
|
||||
onSuccess(result.rows)();
|
||||
}).catch(function(err) {
|
||||
onError(err);
|
||||
}).then(function(result) {
|
||||
onSuccess(result.rows);
|
||||
});
|
||||
};
|
||||
|
||||
return function (cancelError, cancelerError, cancelerSuccess) {
|
||||
q.cancel();
|
||||
cancelerSuccess();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
@ -15,11 +15,14 @@ module Database.PostgreSQL
|
||||
, unsafeQuery
|
||||
) where
|
||||
|
||||
import Control.Monad.Aff (Aff, makeAff)
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Aff (Aff, bracket)
|
||||
import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
|
||||
import Control.Monad.Eff (kind Effect, Eff)
|
||||
import Control.Monad.Eff.Class (liftEff)
|
||||
import Control.Monad.Eff.Exception (Error, error)
|
||||
import Control.Monad.Error.Class (catchError, throwError, withResource)
|
||||
import Control.Monad.Eff.Exception (error)
|
||||
import Control.Monad.Error.Class (catchError, throwError)
|
||||
import Data.Array (head)
|
||||
import Data.Either (Either(..))
|
||||
import Data.Foreign (Foreign)
|
||||
@ -30,7 +33,6 @@ import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1
|
||||
import Database.PostgreSQL.Row as Row
|
||||
import Database.PostgreSQL.Value (class FromSQLValue)
|
||||
import Database.PostgreSQL.Value as Value
|
||||
import Prelude
|
||||
|
||||
foreign import data POSTGRESQL :: Effect
|
||||
|
||||
@ -73,20 +75,31 @@ withConnection
|
||||
-> (Connection -> Aff (postgreSQL :: POSTGRESQL | eff) a)
|
||||
-> Aff (postgreSQL :: POSTGRESQL | eff) a
|
||||
withConnection p k =
|
||||
withResource (makeAff $ ffiConnect p)
|
||||
bracket
|
||||
(connect p)
|
||||
(liftEff <<< _.done)
|
||||
(k <<< _.connection)
|
||||
|
||||
type PostgreSqlEff eff = (postgreSQL :: POSTGRESQL | eff)
|
||||
|
||||
connect
|
||||
:: ∀ eff
|
||||
. Pool
|
||||
-> Aff
|
||||
(postgreSQL :: POSTGRESQL | eff)
|
||||
{ connection :: Connection
|
||||
, done :: Eff (PostgreSqlEff eff) Unit
|
||||
}
|
||||
connect = fromEffFnAff <<< ffiConnect
|
||||
|
||||
foreign import ffiConnect
|
||||
:: ∀ eff
|
||||
. Pool
|
||||
-> (Error -> Eff (postgreSQL :: POSTGRESQL | eff) Unit)
|
||||
-> ( { connection :: Connection
|
||||
, done :: Eff (postgreSQL :: POSTGRESQL | eff) Unit
|
||||
-> EffFnAff
|
||||
(PostgreSqlEff eff)
|
||||
{ connection :: Connection
|
||||
, done :: Eff (PostgreSqlEff eff) Unit
|
||||
}
|
||||
-> Eff (postgreSQL :: POSTGRESQL | eff) Unit
|
||||
)
|
||||
-> Eff (postgreSQL :: POSTGRESQL | eff) Unit
|
||||
|
||||
-- | Run an action within a transaction. The transaction is committed if the
|
||||
-- | action returns, and rolled back when the action throws. If you want to
|
||||
@ -149,16 +162,14 @@ unsafeQuery
|
||||
-> String
|
||||
-> Array Foreign
|
||||
-> Aff (postgreSQL :: POSTGRESQL | eff) (Array (Array Foreign))
|
||||
unsafeQuery c s a = makeAff $ ffiUnsafeQuery c s a
|
||||
unsafeQuery c s = fromEffFnAff <<< ffiUnsafeQuery c s
|
||||
|
||||
foreign import ffiUnsafeQuery
|
||||
:: ∀ eff
|
||||
. Connection
|
||||
-> String
|
||||
-> Array Foreign
|
||||
-> (Error -> Eff (postgreSQL :: POSTGRESQL | eff) Unit)
|
||||
-> (Array (Array Foreign) -> Eff (postgreSQL :: POSTGRESQL | eff) Unit)
|
||||
-> Eff (postgreSQL :: POSTGRESQL | eff) Unit
|
||||
-> EffFnAff (postgreSQL :: POSTGRESQL | eff) (Array (Array Foreign))
|
||||
|
||||
fromRight :: ∀ a b. Either a b -> Maybe b
|
||||
fromRight (Left _) = Nothing
|
||||
|
@ -2,14 +2,15 @@ module Test.Main
|
||||
( main
|
||||
) where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Aff (launchAff)
|
||||
import Control.Monad.Eff (Eff)
|
||||
import Control.Monad.Eff.Class (liftEff)
|
||||
import Control.Monad.Eff.Exception (EXCEPTION, error)
|
||||
import Control.Monad.Error.Class (throwError, try)
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Database.PostgreSQL (POSTGRESQL, PoolConfiguration, Query(..), Row0(..), Row1(..), Row2(..), Row6(..), execute, newPool, query, scalar, withConnection, withTransaction)
|
||||
import Prelude
|
||||
import Database.PostgreSQL (POSTGRESQL, Query(..), PoolConfiguration, execute, newPool, query, Row0(..), Row1(..), Row2(..), Row6(..), scalar, withConnection, withTransaction)
|
||||
import Test.Assert (ASSERT, assert)
|
||||
|
||||
main :: ∀ eff. Eff (assert :: ASSERT, exception :: EXCEPTION, postgreSQL :: POSTGRESQL | eff) Unit
|
||||
@ -35,6 +36,7 @@ main = void $ launchAff do
|
||||
WHERE delicious
|
||||
ORDER BY name ASC
|
||||
""") Row0
|
||||
|
||||
liftEff <<< assert $ names == [Row1 "pork", Row1 "rookworst"]
|
||||
|
||||
testTransactionCommit conn
|
||||
|
Loading…
Reference in New Issue
Block a user