@@ -2,7 +2,6 @@
22{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
33{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
44{-# LANGUAGE RecordWildCards #-}
5−{-# LANGUAGE PolyKinds #-}
65
76{- |
87Module: Database.PostgreSQL.Simple.FromField
@@ -107,7 +106,6 @@ module Database.PostgreSQL.Simple.FromField
107106 , PQ.Oid(..)
108107 , PQ.Format(..)
109108 , pgArrayFieldParser
110− , attoFieldParser
111109
112110 , optionalField
113111 , fromJSONField
@@ -116,18 +114,19 @@ module Database.PostgreSQL.Simple.FromField
116114
117115#include "MachDeps.h"
118116
119−import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
117+import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
120118import Control.Concurrent.MVar (MVar, newMVar)
121−import Control.Exception (Exception (toException, fromException))
119+import Control.Exception (Exception)
122120import qualified Data.Aeson as JSON
121+import qualified Data.Aeson.Internal as JSON
122+import qualified Data.Aeson.Parser as JSON (value')
123123import Data.Attoparsec.ByteString.Char8 hiding (Result)
124124import Data.ByteString (ByteString)
125125import qualified Data.ByteString.Char8 as B
126−import Data.Functor.Identity (Identity(Identity))
127126import Data.Int (Int16, Int32, Int64)
128127import Data.IORef (IORef, newIORef)
129128import Data.Ratio (Ratio)
130−import Data.Time.Compat ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay, CalendarDiffTime )
129+import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
131130import Data.Typeable (Typeable, typeOf)
132131import Data.Vector (Vector)
133132import Data.Vector.Mutable (IOVector)
@@ -154,8 +153,6 @@ import qualified Data.UUID.Types as UUID
154153import Data.Scientific (Scientific)
155154import GHC.Real (infinity, notANumber)
156155
157−import qualified Data.Aeson.Types as JSON
158−
159156-- | Exception thrown if conversion from a SQL value to a Haskell
160157-- value fails.
161158data ResultError = Incompatible { errSQLType :: String
@@ -182,9 +179,7 @@ data ResultError = Incompatible { errSQLType :: String
182179 -- between metadata and actual data in a row).
183180 deriving (Eq, Show, Typeable)
184181
185−instance Exception ResultError where
186− toException = postgresqlExceptionToException
187− fromException = postgresqlExceptionFromException
182+instance Exception ResultError
188183
189184left :: Exception a => a -> Conversion b
190185left = conversionError
@@ -273,12 +268,6 @@ instance FromField () where
273268 | typeOid f /= TI.voidOid = returnError Incompatible f ""
274269 | otherwise = pure ()
275270
276−instance (FromField a) => FromField (Const a b) where
277− fromField f bs = Const <$> fromField f bs
278−
279−instance (FromField a) => FromField (Identity a) where
280− fromField f bs = Identity <$> fromField f bs
281−
282271-- | For dealing with null values. Compatible with any postgresql type
283272-- compatible with type @a@. Note that the type is not checked if
284273-- the value is null, although it is inadvisable to rely on this
@@ -325,11 +314,11 @@ instance FromField Char where
325314
326315-- | int2
327316instance FromField Int16 where
328− fromField = attoFieldParser ok16 $ signed decimal
317+ fromField = atto ok16 $ signed decimal
329318
330319-- | int2, int4
331320instance FromField Int32 where
332− fromField = attoFieldParser ok32 $ signed decimal
321+ fromField = atto ok32 $ signed decimal
333322
334323#if WORD_SIZE_IN_BITS < 64
335324-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -339,36 +328,36 @@ instance FromField Int32 where
339328-- This library was compiled as 64-bit code.
340329#endif
341330instance FromField Int where
342− fromField = attoFieldParser okInt $ signed decimal
331+ fromField = atto okInt $ signed decimal
343332
344333-- | int2, int4, int8
345334instance FromField Int64 where
346− fromField = attoFieldParser ok64 $ signed decimal
335+ fromField = atto ok64 $ signed decimal
347336
348337-- | int2, int4, int8
349338instance FromField Integer where
350− fromField = attoFieldParser ok64 $ signed decimal
339+ fromField = atto ok64 $ signed decimal
351340
352341-- | int2, float4 (Uses attoparsec's 'double' routine, for
353342-- better accuracy convert to 'Scientific' or 'Rational' first)
354343instance FromField Float where
355− fromField = attoFieldParser ok (realToFrac <$> pg_double)
344+ fromField = atto ok (realToFrac <$> pg_double)
356345 where ok = eq TI.float4Oid \/ eq TI.int2Oid
357346
358347-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
359348-- better accuracy convert to 'Scientific' or 'Rational' first)
360349instance FromField Double where
361− fromField = attoFieldParser ok pg_double
350+ fromField = atto ok pg_double
362351 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
363352
364353-- | int2, int4, int8, float4, float8, numeric
365354instance FromField (Ratio Integer) where
366− fromField = attoFieldParser ok pg_rational
355+ fromField = atto ok pg_rational
367356 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
368357
369358-- | int2, int4, int8, float4, float8, numeric
370359instance FromField Scientific where
371− fromField = attoFieldParser ok rational
360+ fromField = atto ok rational
372361 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
373362
374363unBinary :: Binary t -> t
@@ -396,7 +385,7 @@ instance FromField SB.ByteString where
396385
397386-- | oid
398387instance FromField PQ.Oid where
399− fromField f dat = PQ.Oid <$> attoFieldParser (== TI.oidOid) decimal f dat
388+ fromField f dat = PQ.Oid <$> atto (== TI.oidOid) decimal f dat
400389
401390-- | bytea, name, text, \"char\", bpchar, varchar, unknown
402391instance FromField LB.ByteString where
@@ -489,15 +478,6 @@ instance FromField LocalTimestamp where
489478instance FromField Date where
490479 fromField = ff TI.dateOid "Date" parseDate
491480
492−-- | interval. Requires you to configure intervalstyle as @iso_8601@.
493−--
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;@ .
497−--
498−instance FromField CalendarDiffTime where
499− fromField = ff TI.intervalOid "CalendarDiffTime" parseCalendarDiffTime
500−
501481ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
502482 -> Field -> Maybe B8.ByteString -> Conversion a
503483ff compatOid hsType parseBS f mstr =
@@ -576,13 +556,11 @@ instance FromField UUID where
576556-- | json, jsonb
577557instance FromField JSON.Value where
578558 fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
579− where parseBS bs = case JSON.eitherDecodeStrict' bs of
559+ where parseBS bs = case parseOnly (JSON.value' <* endOfInput) bs of
580560 Left err -> returnError ConversionFailed f err
581561 Right val -> pure val
582562
583563-- | Return the JSON ByteString directly
584−--
585−-- @since 0.6.3
586564fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
587565fromFieldJSONByteString f mbs =
588566 if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
@@ -684,22 +662,10 @@ returnError mkErr f msg = do
684662 (show (typeOf (undefined :: a)))
685663 msg
686664
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.
689−--
690−-- @
691−-- instance FromField Int16 where
692−-- fromField = attoFieldParser ok16 (signed decimal)
693−-- @
694−--
695−-- @since 0.6.3
696−attoFieldParser :: forall a. (Typeable a)
697− => (PQ.Oid -> Bool)
698− -- ^ Predicate for whether the postgresql type oid is compatible with this parser
699− -> Parser a
700− -- ^ An attoparsec parser.
701− -> FieldParser a
702−attoFieldParser types p0 f dat = doFromField f types (go p0) dat
665+atto :: forall a. (Typeable a)
666+ => Compat -> Parser a -> Field -> Maybe ByteString
667+ -> Conversion a
668+atto types p0 f dat = doFromField f types (go p0) dat
703669 where
704670 go :: Parser a -> ByteString -> Conversion a
705671 go p s =