git.iko.soy/typeable/postgresql-simple
on master help sign in
Expose attoFieldParser
515a303Victor Nawothnig
CHANGES.md+6−1
@@ -1,6 +1,11 @@
1### Version 0.6.3 (2020-11-15)
1### Version 0.6.3 (2020-11-15)
2
2
3− * Add 'fromFieldJSONByteString`
3+ * Add `fromFieldJSONByteString`
4+ Thanks to tomjaguarpaw for the implementation
5+ https://github.com/lpsmith/postgresql-simple/pull/222
6+ * Add `attoFieldParser`
7+ Thanks to Victor Nawothnig for the implementation
8+ https://github.com/haskellari/postgresql-simple/pull/45
4
9
5### Version 0.6.2 (2019-04-26)
10### Version 0.6.2 (2019-04-26)
6
11
src/Database/PostgreSQL/Simple/FromField.hs+27−14
@@ -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
109+ , attoFieldParser
109
110
110 , optionalField
111 , optionalField
111 , fromJSONField
112 , fromJSONField
@@ -314,11 +315,11 @@ instance FromField Char where
314
315
315-- | int2
316-- | int2
316instance FromField Int16 where
317instance FromField Int16 where
317− fromField = atto ok16 $ signed decimal
318+ fromField = attoFieldParser ok16 $ signed decimal
318
319
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
322
323
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.
329#endif
330#endif
330instance FromField Int where
331instance FromField Int where
331− fromField = atto okInt $ signed decimal
332+ fromField = attoFieldParser okInt $ signed decimal
332
333
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
336
337
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
340
341
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
346
347
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
352
353
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
357
358
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
362
363
363unBinary :: Binary t -> t
364unBinary :: Binary t -> t
@@ -385,7 +386,7 @@ instance FromField SB.ByteString where
385
386
386-- | oid
387-- | oid
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
389
390
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)))
665 msg
666 msg
666
667
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
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
680+ -> Parser a
681+ -- ^ An attoparsec parser.
682+ -> FieldParser a
670−atto types p0 f dat = doFromField f types (go p0) dat
683+attoFieldParser types p0 f dat = doFromField f types (go p0) dat
671 where
684 where
672 go :: Parser a -> ByteString -> Conversion a
685 go :: Parser a -> ByteString -> Conversion a
673 go p s =
686 go p s =