Skip to content

WIP: updates for ps-0.7 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"output"
],
"dependencies": {
"purescript-arrays": "~0.3.7",
"purescript-either": "~0.1.8",
"purescript-foreign": "~0.4.2",
"purescript-foldable-traversable": "~0.3.1",
"purescript-transformers": "~0.5.5",
"purescript-arrays": "~0.4.0",
"purescript-either": "~0.2.0",
"purescript-foreign": "~0.5.0",
"purescript-foldable-traversable": "~0.4.0",
"purescript-transformers": "~0.6.0",
"purescript-aff": "~0.10.1",
"purescript-integers": "~0.1.0"
"purescript-integers": "~0.2.0"
}
}
97 changes: 97 additions & 0 deletions src/Database/Postgres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

// module Database.Postgres

var pg = require('pg');

exports["connect'"] = function (conString) {
return function(success, error) {
var client = new pg.Client(conString);
client.connect(function(err) {
if (err) {
error(err);
} else {
success(client);
}
})
return client;
};
}

exports._withClient = function (conString, cb) {
return function(success, error) {
pg.connect(conString, function(err, client, done) {
if (err) {
done(true);
return error(err);
}
cb(client)(function(v) {
done();
success(v);
}, function(err) {
done();
error(err);
})
});
};
}

exports.runQuery_ = function (queryStr) {
return function(client) {
return function(success, error) {
client.query(queryStr, function(err, result) {
if (err) {
error(err);
} else {
success(result.rows);
}
})
};
};
}

exports.runQuery = function (queryStr) {
return function(params) {
return function(client) {
return function(success, error) {
client.query(queryStr, params, function(err, result) {
if (err) return error(err);
success(result.rows);
})
};
};
};
}

exports.runQueryValue_ = function (queryStr) {
return function(client) {
return function(success, error) {
client.query(queryStr, function(err, result) {
if (err) return error(err);
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
})
};
};
}

exports.runQueryValue = function (queryStr) {
return function(params) {
return function(client) {
return function(success, error) {
client.query(queryStr, params, function(err, result) {
if (err) return error(err);
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
})
};
};
};
}

exports.end = function (client) {
return function() {
client.end();
};
}

exports.disconnect = function () {
pg.end();
}
138 changes: 15 additions & 123 deletions src/Database/Postgres.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Database.Postgres
, withClient
) where

import Prelude
import Control.Alt
import Control.Apply ((*>))
import Control.Monad.Eff
Expand Down Expand Up @@ -45,7 +46,7 @@ type ConnectionString = String
type ConnectionInfo =
{ host :: String
, db :: String
, port :: Number
, port :: Int
, user :: String
, password :: String
}
Expand All @@ -64,7 +65,7 @@ connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client
connect = connect' <<< mkConnectionString

-- | Runs a query and returns nothing.
execute :: forall eff a. Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) Unit
execute :: forall eff a. Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) Unit
execute (Query sql) params client = void $ runQuery sql params client

-- | Runs a query and returns nothing
Expand All @@ -74,21 +75,21 @@ execute_ (Query sql) client = void $ runQuery_ sql client
-- | Runs a query and returns all results.
query :: forall eff a p
. (IsForeign a)
=> Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) [a]
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Array a)
query (Query sql) params client = do
rows <- runQuery sql params client
either liftError pure (sequence $ read <$> rows)

-- | Just like `query` but does not make any param replacement
query_ :: forall eff a. (IsForeign a) => Query a -> Client -> Aff (db :: DB | eff) [a]
query_ :: forall eff a. (IsForeign a) => Query a -> Client -> Aff (db :: DB | eff) (Array a)
query_ (Query sql) client = do
rows <- runQuery_ sql client
either liftError pure (sequence $ read <$> rows)

-- | Runs a query and returns the first row, if any
queryOne :: forall eff a
. (IsForeign a)
=> Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a)
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
queryOne (Query sql) params client = do
rows <- runQuery sql params client
maybe (pure Nothing) (either liftError (pure <<< Just)) $ read <$> (rows !! 0)
Expand All @@ -102,7 +103,7 @@ queryOne_ (Query sql) client = do
-- | Runs a query and returns a single value, if any.
queryValue :: forall eff a
. (IsForeign a)
=> Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a)
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
queryValue (Query sql) params client = do
val <- runQueryValue sql params client
pure $ either (const Nothing) Just (read val)
Expand Down Expand Up @@ -134,127 +135,18 @@ withClient info p = runFn2 _withClient (mkConnectionString info) p
liftError :: forall e a. ForeignError -> Aff e a
liftError err = throwError $ error (show err)

finally :: forall eff a. Aff eff a -> Aff eff Unit -> Aff eff a
finally a sequel = do
res <- attempt a
sequel
either throwError pure res
foreign import connect' :: forall eff. String -> Aff (db :: DB | eff) Client

foreign import _withClient :: forall eff a. Fn2 ConnectionString (Client -> Aff (db :: DB | eff) a) (Aff (db :: DB | eff) a)

foreign import connect' """
function connect$prime(conString) {
return function(success, error) {
var pg = require('pg');
var client = new pg.Client(conString);
client.connect(function(err) {
if (err) {
error(err);
} else {
success(client);
}
})
return client;
}
}
""" :: forall eff. String -> Aff (db :: DB | eff) Client

foreign import _withClient
"""
function _withClient(conString, cb) {
return function(success, error) {
var pg = require('pg');
pg.connect(conString, function(err, client, done) {
if (err) {
done(true);
return error(err);
}
cb(client)(function(v) {
done();
success(v);
}, function(err) {
done();
error(err);
})
});
};
}
""" :: forall eff a.
Fn2
ConnectionString
(Client -> Aff (db :: DB | eff) a)
(Aff (db :: DB | eff) a)

foreign import runQuery_ """
function runQuery_(queryStr) {
return function(client) {
return function(success, error) {
client.query(queryStr, function(err, result) {
if (err) {
error(err);
} else {
success(result.rows);
}
})
};
};
}
""" :: forall eff. String -> Client -> Aff (db :: DB | eff) [Foreign]
foreign import runQuery_ :: forall eff. String -> Client -> Aff (db :: DB | eff) (Array Foreign)

foreign import runQuery """
function runQuery(queryStr) {
return function(params) {
return function(client) {
return function(success, error) {
client.query(queryStr, params, function(err, result) {
if (err) return error(err);
success(result.rows);
})
};
};
}
}
""" :: forall eff. String -> [SqlValue] -> Client -> Aff (db :: DB | eff) [Foreign]
foreign import runQuery :: forall eff. String -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Array Foreign)

foreign import runQueryValue_ """
function runQueryValue_(queryStr) {
return function(client) {
return function(success, error) {
client.query(queryStr, function(err, result) {
if (err) return error(err);
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
})
};
};
}
""" :: forall eff. String -> Client -> Aff (db :: DB | eff) Foreign
foreign import runQueryValue_ :: forall eff. String -> Client -> Aff (db :: DB | eff) Foreign

foreign import runQueryValue """
function runQueryValue(queryStr) {
return function(params) {
return function(client) {
return function(success, error) {
client.query(queryStr, params, function(err, result) {
if (err) return error(err);
success(result.rows.length > 0 ? result.rows[0][result.fields[0].name] : undefined);
})
};
};
}
}
""" :: forall eff. String -> [SqlValue] -> Client -> Aff (db :: DB | eff) Foreign
foreign import runQueryValue :: forall eff. String -> Array SqlValue -> Client -> Aff (db :: DB | eff) Foreign

foreign import end """
function end(client) {
return function() {
client.end();
};
}
""" :: forall eff. Client -> Eff (db :: DB | eff) Unit
foreign import end :: forall eff. Client -> Eff (db :: DB | eff) Unit

foreign import disconnect
"""
function disconnect() {
var pg = require('pg');
pg.end();
}
""" :: forall eff. Eff (db :: DB | eff) Unit
foreign import disconnect :: forall eff. Eff (db :: DB | eff) Unit
9 changes: 9 additions & 0 deletions src/Database/Postgres/SqlValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// module Database.Postgres.SqlValue

exports.unsafeToSqlValue = function (x) {
return x;
}

exports.nullSqlValue = null;
9 changes: 3 additions & 6 deletions src/Database/Postgres/SqlValue.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Database.Postgres.SqlValue
, toSql
) where

import Prelude ((<<<))
import Data.Int
import Data.Maybe

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

foreign import unsafeToSqlValue """
function unsafeToSqlValue(x) {
return x;
}
""" :: forall a. a -> SqlValue
foreign import unsafeToSqlValue :: forall a. a -> SqlValue

foreign import nullSqlValue "var nullSqlValue = null;" :: SqlValue
foreign import nullSqlValue :: SqlValue
1 change: 1 addition & 0 deletions src/Database/Postgres/Transaction.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Database.Postgres.Transaction where

import Prelude
import Control.Apply ((*>))
import Control.Monad.Aff
import Control.Monad.Error.Class (throwError)
Expand Down
Loading