@@ -106,6 +106,7 @@ module Database.PostgreSQL.Simple.FromField
106106 , PQ.Oid(..)
107107 , PQ.Format(..)
108108 , pgArrayFieldParser
109+ , attoFieldParser
109110
110111 , optionalField
111112 , fromJSONField
@@ -314,11 +315,11 @@ instance FromField Char where
314315
315316-- | int2
316317instance FromField Int16 where
317− fromField = atto ok16 $ signed decimal
318+ fromField = attoFieldParser ok16 $ signed decimal
318319
319320-- | int2, int4
320321instance FromField Int32 where
321− fromField = atto ok32 $ signed decimal
322+ fromField = attoFieldParser ok32 $ signed decimal
322323
323324#if WORD_SIZE_IN_BITS < 64
324325-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -328,36 +329,36 @@ instance FromField Int32 where
328329-- This library was compiled as 64-bit code.
329330#endif
330331instance FromField Int where
331− fromField = atto okInt $ signed decimal
332+ fromField = attoFieldParser okInt $ signed decimal
332333
333334-- | int2, int4, int8
334335instance FromField Int64 where
335− fromField = atto ok64 $ signed decimal
336+ fromField = attoFieldParser ok64 $ signed decimal
336337
337338-- | int2, int4, int8
338339instance FromField Integer where
339− fromField = atto ok64 $ signed decimal
340+ fromField = attoFieldParser ok64 $ signed decimal
340341
341342-- | int2, float4 (Uses attoparsec's 'double' routine, for
342343-- better accuracy convert to 'Scientific' or 'Rational' first)
343344instance FromField Float where
344− fromField = atto ok (realToFrac <$> pg_double)
345+ fromField = attoFieldParser ok (realToFrac <$> pg_double)
345346 where ok = eq TI.float4Oid \/ eq TI.int2Oid
346347
347348-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
348349-- better accuracy convert to 'Scientific' or 'Rational' first)
349350instance FromField Double where
350− fromField = atto ok pg_double
351+ fromField = attoFieldParser ok pg_double
351352 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
352353
353354-- | int2, int4, int8, float4, float8, numeric
354355instance FromField (Ratio Integer) where
355− fromField = atto ok pg_rational
356+ fromField = attoFieldParser ok pg_rational
356357 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
357358
358359-- | int2, int4, int8, float4, float8, numeric
359360instance FromField Scientific where
360− fromField = atto ok rational
361+ fromField = attoFieldParser ok rational
361362 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
362363
363364unBinary :: Binary t -> t
@@ -385,7 +386,7 @@ instance FromField SB.ByteString where
385386
386387-- | oid
387388instance 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
389390
390391-- | bytea, name, text, \"char\", bpchar, varchar, unknown
391392instance FromField LB.ByteString where
@@ -664,10 +665,22 @@ returnError mkErr f msg = do
664665 (show (typeOf (undefined :: a)))
665666 msg
666667
667−atto :: forall a. (Typeable a)
668− => Compat -> Parser a -> Field -> Maybe ByteString
669− -> Conversion a
670−atto types p0 f dat = doFromField f types (go p0) dat
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.
670+--
671+-- @
672+-- instance FromField Int16 where
673+-- fromField = attoFieldParser ok16 (signed decimal)
674+-- @
675+--
676+-- @since 0.6.3
677+attoFieldParser :: forall a. (Typeable a)
678+ => (PQ.Oid -> Bool)
679+ -- ^ Predicate for whether the postgresql type oid is compatible with this parser
680+ -> Parser a
681+ -- ^ An attoparsec parser.
682+ -> FieldParser a
683+attoFieldParser types p0 f dat = doFromField f types (go p0) dat
671684 where
672685 go :: Parser a -> ByteString -> Conversion a
673686 go p s =