@@ -2,6 +2,7 @@
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE PolyKinds #-}
7Module: Database.PostgreSQL.Simple.FromField
8Module: Database.PostgreSQL.Simple.FromField
@@ -106,6 +107,7 @@ module Database.PostgreSQL.Simple.FromField
106 , PQ.Oid(..)
107 , PQ.Oid(..)
107 , PQ.Format(..)
108 , PQ.Format(..)
108 , pgArrayFieldParser
109 , pgArrayFieldParser
110 , optionalField
112 , optionalField
111 , fromJSONField
113 , fromJSONField
@@ -114,19 +116,18 @@ module Database.PostgreSQL.Simple.FromField
115#include "MachDeps.h"
117#include "MachDeps.h"
117−import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
119+import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
118import Control.Concurrent.MVar (MVar, newMVar)
120import Control.Concurrent.MVar (MVar, newMVar)
119−import Control.Exception (Exception)
121+import Control.Exception (Exception (toException, fromException))
120import qualified Data.Aeson as JSON
122import qualified Data.Aeson as JSON
121−import qualified Data.Aeson.Internal as JSON
122−import qualified Data.Aeson.Parser as JSON (value')
123import Data.Attoparsec.ByteString.Char8 hiding (Result)
123import Data.Attoparsec.ByteString.Char8 hiding (Result)
124import Data.ByteString (ByteString)
124import Data.ByteString (ByteString)
125import qualified Data.ByteString.Char8 as B
125import qualified Data.ByteString.Char8 as B
126+import Data.Functor.Identity (Identity(Identity))
126import Data.Int (Int16, Int32, Int64)
127import Data.Int (Int16, Int32, Int64)
127import Data.IORef (IORef, newIORef)
128import Data.IORef (IORef, newIORef)
128import Data.Ratio (Ratio)
129import Data.Ratio (Ratio)
129−import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
130+import Data.Time.Compat ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay, CalendarDiffTime )
130import Data.Typeable (Typeable, typeOf)
131import Data.Typeable (Typeable, typeOf)
131import Data.Vector (Vector)
132import Data.Vector (Vector)
132import Data.Vector.Mutable (IOVector)
133import Data.Vector.Mutable (IOVector)
@@ -153,6 +154,8 @@ import qualified Data.UUID.Types as UUID
153import Data.Scientific (Scientific)
154import Data.Scientific (Scientific)
154import GHC.Real (infinity, notANumber)
155import GHC.Real (infinity, notANumber)
157+import qualified Data.Aeson.Types as JSON
156-- | Exception thrown if conversion from a SQL value to a Haskell
159-- | Exception thrown if conversion from a SQL value to a Haskell
157-- value fails.
160-- value fails.
158data ResultError = Incompatible { errSQLType :: String
161data ResultError = Incompatible { errSQLType :: String
@@ -179,7 +182,9 @@ data ResultError = Incompatible { errSQLType :: String
179 -- between metadata and actual data in a row).
182 -- between metadata and actual data in a row).
180 deriving (Eq, Show, Typeable)
183 deriving (Eq, Show, Typeable)
182−instance Exception ResultError
185+instance Exception ResultError where
186+ toException = postgresqlExceptionToException
187+ fromException = postgresqlExceptionFromException
184left :: Exception a => a -> Conversion b
189left :: Exception a => a -> Conversion b
185left = conversionError
190left = conversionError
@@ -268,6 +273,12 @@ instance FromField () where
268 | typeOid f /= TI.voidOid = returnError Incompatible f ""
273 | typeOid f /= TI.voidOid = returnError Incompatible f ""
269 | otherwise = pure ()
274 | otherwise = pure ()
276+instance (FromField a) => FromField (Const a b) where
277+ fromField f bs = Const <$> fromField f bs
279+instance (FromField a) => FromField (Identity a) where
280+ fromField f bs = Identity <$> fromField f bs
271-- | For dealing with null values. Compatible with any postgresql type
282-- | For dealing with null values. Compatible with any postgresql type
272-- compatible with type @a@. Note that the type is not checked if
283-- compatible with type @a@. Note that the type is not checked if
273-- the value is null, although it is inadvisable to rely on this
284-- the value is null, although it is inadvisable to rely on this
@@ -314,11 +325,11 @@ instance FromField Char where
316instance FromField Int16 where
327instance FromField Int16 where
317− fromField = atto ok16 $ signed decimal
328+ fromField = attoFieldParser ok16 $ signed decimal
319-- | int2, int4
330-- | int2, int4
320instance FromField Int32 where
331instance FromField Int32 where
321− fromField = atto ok32 $ signed decimal
332+ fromField = attoFieldParser ok32 $ signed decimal
323#if WORD_SIZE_IN_BITS < 64
334#if WORD_SIZE_IN_BITS < 64
324-- | int2, int4, and if compiled as 64-bit code, int8 as well.
335-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -328,36 +339,36 @@ instance FromField Int32 where
328-- This library was compiled as 64-bit code.
339-- This library was compiled as 64-bit code.
330instance FromField Int where
341instance FromField Int where
331− fromField = atto okInt $ signed decimal
342+ fromField = attoFieldParser okInt $ signed decimal
333-- | int2, int4, int8
344-- | int2, int4, int8
334instance FromField Int64 where
345instance FromField Int64 where
335− fromField = atto ok64 $ signed decimal
346+ fromField = attoFieldParser ok64 $ signed decimal
337-- | int2, int4, int8
348-- | int2, int4, int8
338instance FromField Integer where
349instance FromField Integer where
339− fromField = atto ok64 $ signed decimal
350+ fromField = attoFieldParser ok64 $ signed decimal
341-- | int2, float4 (Uses attoparsec's 'double' routine, for
352-- | int2, float4 (Uses attoparsec's 'double' routine, for
342-- better accuracy convert to 'Scientific' or 'Rational' first)
353-- better accuracy convert to 'Scientific' or 'Rational' first)
343instance FromField Float where
354instance FromField Float where
344− fromField = atto ok (realToFrac <$> pg_double)
355+ fromField = attoFieldParser ok (realToFrac <$> pg_double)
345 where ok = eq TI.float4Oid \/ eq TI.int2Oid
356 where ok = eq TI.float4Oid \/ eq TI.int2Oid
347-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
358-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
348-- better accuracy convert to 'Scientific' or 'Rational' first)
359-- better accuracy convert to 'Scientific' or 'Rational' first)
349instance FromField Double where
360instance FromField Double where
350− fromField = atto ok pg_double
361+ fromField = attoFieldParser ok pg_double
351 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
362 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
353-- | int2, int4, int8, float4, float8, numeric
364-- | int2, int4, int8, float4, float8, numeric
354instance FromField (Ratio Integer) where
365instance FromField (Ratio Integer) where
355− fromField = atto ok pg_rational
366+ fromField = attoFieldParser ok pg_rational
356 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
367 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
358-- | int2, int4, int8, float4, float8, numeric
369-- | int2, int4, int8, float4, float8, numeric
359instance FromField Scientific where
370instance FromField Scientific where
360− fromField = atto ok rational
371+ fromField = attoFieldParser ok rational
361 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
372 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
363unBinary :: Binary t -> t
374unBinary :: Binary t -> t
@@ -385,7 +396,7 @@ instance FromField SB.ByteString where
387instance FromField PQ.Oid where
398instance FromField PQ.Oid where
388− fromField f dat = PQ.Oid <$> atto (== TI.oidOid) decimal f dat
399+ fromField f dat = PQ.Oid <$> attoFieldParser (== TI.oidOid) decimal f dat
390-- | bytea, name, text, \"char\", bpchar, varchar, unknown
401-- | bytea, name, text, \"char\", bpchar, varchar, unknown
391instance FromField LB.ByteString where
402instance FromField LB.ByteString where
@@ -478,6 +489,15 @@ instance FromField LocalTimestamp where
478instance FromField Date where
489instance FromField Date where
479 fromField = ff TI.dateOid "Date" parseDate
490 fromField = ff TI.dateOid "Date" parseDate
492+-- | interval. Requires you to configure intervalstyle as @iso_8601@.
494+-- You can configure intervalstyle on every connection with a @SET@ command,
495+-- but for better performance you may want to configure it permanently in the
496+-- file found with @SHOW config_file;@ .
498+instance FromField CalendarDiffTime where
499+ fromField = ff TI.intervalOid "CalendarDiffTime" parseCalendarDiffTime
481ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
501ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
482 -> Field -> Maybe B8.ByteString -> Conversion a
502 -> Field -> Maybe B8.ByteString -> Conversion a
483ff compatOid hsType parseBS f mstr =
503ff compatOid hsType parseBS f mstr =
@@ -556,11 +576,13 @@ instance FromField UUID where
556-- | json, jsonb
576-- | json, jsonb
557instance FromField JSON.Value where
577instance FromField JSON.Value where
558 fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
578 fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
559− where parseBS bs = case parseOnly (JSON.value' <* endOfInput) bs of
579+ where parseBS bs = case JSON.eitherDecodeStrict' bs of
560 Left err -> returnError ConversionFailed f err
580 Left err -> returnError ConversionFailed f err
561 Right val -> pure val
581 Right val -> pure val
563-- | Return the JSON ByteString directly
583-- | Return the JSON ByteString directly
564fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
586fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
565fromFieldJSONByteString f mbs =
587fromFieldJSONByteString f mbs =
566 if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
588 if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
@@ -662,10 +684,22 @@ returnError mkErr f msg = do
662 (show (typeOf (undefined :: a)))
684 (show (typeOf (undefined :: a)))
687+-- | Construct a field parser from an attoparsec parser. An 'Incompatible' error is thrown if the
688+-- PostgreSQL oid does not match the specified predicate.
691+-- instance FromField Int16 where
692+-- fromField = attoFieldParser ok16 (signed decimal)
665−atto :: forall a. (Typeable a)
696+attoFieldParser :: forall a. (Typeable a)
666− => Compat -> Parser a -> Field -> Maybe ByteString
697+ => (PQ.Oid -> Bool)
667− -> Conversion a
698+ -- ^ Predicate for whether the postgresql type oid is compatible with this parser
700+ -- ^ An attoparsec parser.
668−atto types p0 f dat = doFromField f types (go p0) dat
702+attoFieldParser types p0 f dat = doFromField f types (go p0) dat
670 go :: Parser a -> ByteString -> Conversion a
704 go :: Parser a -> ByteString -> Conversion a