Skip to content

Commit

Permalink
Add support for scientific
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsmith committed Mar 23, 2014
1 parent c867b47 commit 87135c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Database/PostgreSQL/Simple/FromField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ import qualified Data.Text.Encoding as ST
import qualified Data.Text.Lazy as LT
import Data.UUID (UUID)
import qualified Data.UUID as UUID
import Data.Scientific (Scientific)

-- | Exception thrown if conversion from a SQL value to a Haskell
-- value fails.
Expand Down Expand Up @@ -315,6 +316,11 @@ instance FromField (Ratio Integer) where
fromField = atto ok rational
where ok = $(mkCompats [TI.float4,TI.float8,TI.int2,TI.int4,TI.numeric])

-- | int2, int4, float4, float8, numeric
instance FromField Scientific where
fromField = atto ok rational
where ok = $(mkCompats [TI.float4,TI.float8,TI.int2,TI.int4,TI.numeric])

unBinary :: Binary t -> t
unBinary (Binary x) = x

Expand Down
6 changes: 6 additions & 0 deletions src/Database/PostgreSQL/Simple/ToField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as ST
import qualified Data.Text.Encoding as ST
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Builder as LT
import Data.UUID (UUID)
import qualified Data.UUID as UUID
import Data.Vector (Vector)
import qualified Data.Vector as V
import qualified Database.PostgreSQL.LibPQ as PQ
import Database.PostgreSQL.Simple.Time
import Data.Scientific (Scientific, scientificBuilder)

-- | How to render an element when substituting it into a query.
data Action =
Expand Down Expand Up @@ -172,6 +174,10 @@ instance ToField Double where
| otherwise = Plain (double v)
{-# INLINE toField #-}

instance ToField Scientific where
toField x = toField (LT.toLazyText (scientificBuilder x))
{-# INLINE toField #-}

instance ToField (Binary SB.ByteString) where
toField (Binary bs) = EscapeByteA bs
{-# INLINE toField #-}
Expand Down

0 comments on commit 87135c1

Please sign in to comment.