From ada69d008a18d39be50ecefe92efe3342c545404 Mon Sep 17 00:00:00 2001 From: rightfold Date: Sat, 7 Jan 2017 15:30:31 +0100 Subject: [PATCH] Add ByteString support Fixes #5. --- bower.json | 3 ++- src/Database/PostgreSQL.js | 4 ++++ src/Database/PostgreSQL.purs | 13 ++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 7c9841c..88288bf 100644 --- a/bower.json +++ b/bower.json @@ -12,7 +12,8 @@ "purescript-either": "^2.0.0", "purescript-maybe": "^2.0.1", "purescript-foldable-traversable": "^2.0.0", - "purescript-newtype": "^1.1.0" + "purescript-newtype": "^1.1.0", + "purescript-bytestrings": "^0.0.3" }, "repository": { "type": "git", diff --git a/src/Database/PostgreSQL.js b/src/Database/PostgreSQL.js index 31915af..f4e2501 100644 --- a/src/Database/PostgreSQL.js +++ b/src/Database/PostgreSQL.js @@ -47,3 +47,7 @@ exports._query = function(client) { }; }; }; + +exports.unsafeIsBuffer = function(x) { + return x instanceof Buffer; +}; diff --git a/src/Database/PostgreSQL.purs b/src/Database/PostgreSQL.purs index 4937b18..4c49748 100644 --- a/src/Database/PostgreSQL.purs +++ b/src/Database/PostgreSQL.purs @@ -22,8 +22,9 @@ module Database.PostgreSQL import Control.Monad.Aff (Aff) import Control.Monad.Error.Class (catchError, throwError) import Control.Monad.Except (runExcept) +import Data.ByteString (ByteString) import Data.Either (Either(..)) -import Data.Foreign (Foreign, readArray, readChar, readString, toForeign) +import Data.Foreign (Foreign, readArray, readChar, readString, toForeign, unsafeFromForeign) import Data.List (List) import Data.List as List import Data.Maybe (fromJust, Maybe(..)) @@ -134,6 +135,16 @@ instance fromSQLValueArray :: (FromSQLValue a) => FromSQLValue (Array a) where instance fromSQLValueList :: (FromSQLValue a) => FromSQLValue (List a) where fromSQLValue = map List.fromFoldable <<< traverse fromSQLValue <=< fromRight <<< runExcept <<< readArray +instance toSQLValueByteString :: ToSQLValue ByteString where + toSQLValue = toForeign + +instance fromSQLValueByteString :: FromSQLValue ByteString where + fromSQLValue x + | unsafeIsBuffer x = Just $ unsafeFromForeign x + | otherwise = Nothing + +foreign import unsafeIsBuffer :: ∀ a. a -> Boolean + -- | Create a new connection pool. foreign import newPool :: ∀ eff