Skip to content

Commit

Permalink
binary: handle empty ByteStringS correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Nov 3, 2014
1 parent 1ac70cf commit 25dfff3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hpqtypes.cabal
@@ -1,5 +1,5 @@
name: hpqtypes
version: 1.2.2
version: 1.2.3
synopsis: Haskell bindings to libpqtypes

description: Efficient and easy-to-use bindings to (slightly modified)
Expand Down
12 changes: 5 additions & 7 deletions src/Database/PostgreSQL/PQTypes/Binary.hs
Expand Up @@ -6,14 +6,12 @@ module Database.PostgreSQL.PQTypes.Binary (
) where

import Control.Applicative
import Data.ByteString.Unsafe
import Data.Typeable
import qualified Data.ByteString.Char8 as BS

import Database.PostgreSQL.PQTypes.Format
import Database.PostgreSQL.PQTypes.FromSQL
import Database.PostgreSQL.PQTypes.Internal.C.Types
import Database.PostgreSQL.PQTypes.Internal.Utils
import Database.PostgreSQL.PQTypes.ToSQL

-- | Wrapper for (de)serializing underlying type as 'bytea'.
Expand All @@ -29,11 +27,11 @@ instance PQFormat (Binary BS.ByteString) where

instance FromSQL (Binary BS.ByteString) where
type PQBase (Binary BS.ByteString) = PGbytea
fromSQL Nothing = unexpectedNULL
fromSQL (Just PGbytea{..}) = Binary
<$> BS.packCStringLen (pgByteaData, fromIntegral pgByteaLen)
-- use handler for ByteStringS for convenience
fromSQL mbase = Binary <$> fromSQL mbase

instance ToSQL (Binary BS.ByteString) where
type PQDest (Binary BS.ByteString) = PGbytea
toSQL (Binary bs) _ conv = unsafeUseAsCStringLen bs $ \cslen ->
put (cStringLenToBytea cslen) conv
-- use handler for ByteStringS as it properly handles
-- the case when underlying ByteString pointer is NULL.
toSQL (Binary bs) = toSQL bs

0 comments on commit 25dfff3

Please sign in to comment.