@@ -106,6 +106,7 @@ module Database.PostgreSQL.Simple.FromField
106 , PQ.Oid(..)
106 , PQ.Oid(..)
107 , PQ.Format(..)
107 , PQ.Format(..)
108 , pgArrayFieldParser
108 , pgArrayFieldParser
110 , optionalField
111 , optionalField
111 , fromJSONField
112 , fromJSONField
@@ -314,11 +315,11 @@ instance FromField Char where
316instance FromField Int16 where
317instance FromField Int16 where
317− fromField = atto ok16 $ signed decimal
318+ fromField = attoFieldParser ok16 $ signed decimal
319-- | int2, int4
320-- | int2, int4
320instance FromField Int32 where
321instance FromField Int32 where
321− fromField = atto ok32 $ signed decimal
322+ fromField = attoFieldParser ok32 $ signed decimal
323#if WORD_SIZE_IN_BITS < 64
324#if WORD_SIZE_IN_BITS < 64
324-- | int2, int4, and if compiled as 64-bit code, int8 as well.
325-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -328,36 +329,36 @@ instance FromField Int32 where
328-- This library was compiled as 64-bit code.
329-- This library was compiled as 64-bit code.
330instance FromField Int where
331instance FromField Int where
331− fromField = atto okInt $ signed decimal
332+ fromField = attoFieldParser okInt $ signed decimal
333-- | int2, int4, int8
334-- | int2, int4, int8
334instance FromField Int64 where
335instance FromField Int64 where
335− fromField = atto ok64 $ signed decimal
336+ fromField = attoFieldParser ok64 $ signed decimal
337-- | int2, int4, int8
338-- | int2, int4, int8
338instance FromField Integer where
339instance FromField Integer where
339− fromField = atto ok64 $ signed decimal
340+ fromField = attoFieldParser ok64 $ signed decimal
341-- | int2, float4 (Uses attoparsec's 'double' routine, for
342-- | int2, float4 (Uses attoparsec's 'double' routine, for
342-- better accuracy convert to 'Scientific' or 'Rational' first)
343-- better accuracy convert to 'Scientific' or 'Rational' first)
343instance FromField Float where
344instance FromField Float where
344− fromField = atto ok (realToFrac <$> pg_double)
345+ fromField = attoFieldParser ok (realToFrac <$> pg_double)
345 where ok = eq TI.float4Oid \/ eq TI.int2Oid
346 where ok = eq TI.float4Oid \/ eq TI.int2Oid
347-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
348-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
348-- better accuracy convert to 'Scientific' or 'Rational' first)
349-- better accuracy convert to 'Scientific' or 'Rational' first)
349instance FromField Double where
350instance FromField Double where
350− fromField = atto ok pg_double
351+ fromField = attoFieldParser ok pg_double
351 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
352 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
353-- | int2, int4, int8, float4, float8, numeric
354-- | int2, int4, int8, float4, float8, numeric
354instance FromField (Ratio Integer) where
355instance FromField (Ratio Integer) where
355− fromField = atto ok pg_rational
356+ 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
357 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
359-- | int2, int4, int8, float4, float8, numeric
359instance FromField Scientific where
360instance FromField Scientific where
360− fromField = atto ok rational
361+ 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
362 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
363unBinary :: Binary t -> t
364unBinary :: Binary t -> t
@@ -385,7 +386,7 @@ instance FromField SB.ByteString where
387instance FromField PQ.Oid where
388instance FromField PQ.Oid where
388− fromField f dat = PQ.Oid <$> atto (== TI.oidOid) decimal f dat
389+ fromField f dat = PQ.Oid <$> attoFieldParser (== TI.oidOid) decimal f dat
390-- | bytea, name, text, \"char\", bpchar, varchar, unknown
391-- | bytea, name, text, \"char\", bpchar, varchar, unknown
391instance FromField LB.ByteString where
392instance FromField LB.ByteString where
@@ -664,10 +665,22 @@ returnError mkErr f msg = do
664 (show (typeOf (undefined :: a)))
665 (show (typeOf (undefined :: a)))
668+-- | Construct a field parser from an attoparsec parser. An 'Incompatible' error is thrown if the
669+-- PostgreSQL oid does not match the specified predicate.
672+-- instance FromField Int16 where
673+-- fromField = attoFieldParser ok16 (signed decimal)
667−atto :: forall a. (Typeable a)
677+attoFieldParser :: forall a. (Typeable a)
668− => Compat -> Parser a -> Field -> Maybe ByteString
678+ => (PQ.Oid -> Bool)
669− -> Conversion a
679+ -- ^ Predicate for whether the postgresql type oid is compatible with this parser
681+ -- ^ An attoparsec parser.
670−atto types p0 f dat = doFromField f types (go p0) dat
683+attoFieldParser types p0 f dat = doFromField f types (go p0) dat
672 go :: Parser a -> ByteString -> Conversion a
685 go :: Parser a -> ByteString -> Conversion a