@@ -1,8 +1,7 @@
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4−{-# LANGUAGE RecordWildCards #-}
4+{-# LANGUAGE RecordWildCards, TemplateHaskell #-}
5−{-# LANGUAGE PolyKinds #-}
8Module: Database.PostgreSQL.Simple.FromField
7Module: Database.PostgreSQL.Simple.FromField
@@ -29,19 +28,12 @@ conversion routine, which sacrifices some accuracy for speed. If you
29need accuracy, consider first converting data to a 'Scientific' or 'Rational'
28need accuracy, consider first converting data to a 'Scientific' or 'Rational'
30type, and then converting to a floating-point type. If you are defining
29type, and then converting to a floating-point type. If you are defining
31your own 'Database.PostgreSQL.Simple.FromRow.FromRow' instances, this can be
30your own 'Database.PostgreSQL.Simple.FromRow.FromRow' instances, this can be
32−achieved simply by
31+acheived simply by
33@'fromRational' '<$>' 'Database.PostgreSQL.Simple.FromRow.field'@, although
32@'fromRational' '<$>' 'Database.PostgreSQL.Simple.FromRow.field'@, although
34−this idiom is additionally compatible with PostgreSQL's @int8@ and @numeric@
33+this idiom is additionally compatible with PostgreSQL's @numeric@ type.
35−types. If this is unacceptable, you may find
34+If this is unacceptable, you may find
36'Database.PostgreSQL.Simple.FromRow.fieldWith' useful.
35'Database.PostgreSQL.Simple.FromRow.fieldWith' useful.
38−Also note that while converting to a 'Double' through the 'Scientific' type
39−is likely somewhat faster than converting through the 'Rational' type,
40−the 'Scientific' type has no way to represent @NaN@ and @±Infinity@ values.
41−Thus, if you need precision conversion of regular floating point values
42−and the possibility of receiving these special values from the backend,
43−stick with 'Rational'.
45Because 'FromField' is a typeclass, one may provide conversions to
37Because 'FromField' is a typeclass, one may provide conversions to
46additional Haskell types without modifying postgresql-simple. This is
38additional Haskell types without modifying postgresql-simple. This is
47particularly useful for supporting PostgreSQL types that postgresql-simple
39particularly useful for supporting PostgreSQL types that postgresql-simple
@@ -106,28 +98,23 @@ module Database.PostgreSQL.Simple.FromField
106 , typeOid
98 , typeOid
107 , PQ.Oid(..)
99 , PQ.Oid(..)
108 , PQ.Format(..)
100 , PQ.Format(..)
109− , pgArrayFieldParser
113 , fromJSONField
102 , fromJSONField
114− , fromFieldJSONByteString
117#include "MachDeps.h"
105#include "MachDeps.h"
119−import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
107+import Control.Applicative ( (<|>), (<$>), pure, (*>) )
120import Control.Concurrent.MVar (MVar, newMVar)
108import Control.Concurrent.MVar (MVar, newMVar)
121−import Control.Exception (Exception (toException, fromException))
109+import Control.Exception (Exception)
122import qualified Data.Aeson as JSON
110import qualified Data.Aeson as JSON
123import Data.Attoparsec.ByteString.Char8 hiding (Result)
111import Data.Attoparsec.ByteString.Char8 hiding (Result)
124import Data.ByteString (ByteString)
112import Data.ByteString (ByteString)
125import qualified Data.ByteString.Char8 as B
113import qualified Data.ByteString.Char8 as B
126−import Data.Functor.Identity (Identity(Identity))
127import Data.Int (Int16, Int32, Int64)
114import Data.Int (Int16, Int32, Int64)
128import Data.IORef (IORef, newIORef)
115import Data.IORef (IORef, newIORef)
129import Data.Ratio (Ratio)
116import Data.Ratio (Ratio)
130−import Data.Time.Compat ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay, CalendarDiffTime )
117+import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
131import Data.Typeable (Typeable, typeOf)
118import Data.Typeable (Typeable, typeOf)
132import Data.Vector (Vector)
119import Data.Vector (Vector)
133import Data.Vector.Mutable (IOVector)
120import Data.Vector.Mutable (IOVector)
@@ -138,6 +125,7 @@ import Database.PostgreSQL.Simple.Ok
138import Database.PostgreSQL.Simple.Types
125import Database.PostgreSQL.Simple.Types
139import Database.PostgreSQL.Simple.TypeInfo as TI
126import Database.PostgreSQL.Simple.TypeInfo as TI
140import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
127import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
128+import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
141import Database.PostgreSQL.Simple.Time
129import Database.PostgreSQL.Simple.Time
142import Database.PostgreSQL.Simple.Arrays as Arrays
130import Database.PostgreSQL.Simple.Arrays as Arrays
143import qualified Database.PostgreSQL.LibPQ as PQ
131import qualified Database.PostgreSQL.LibPQ as PQ
@@ -147,15 +135,11 @@ import qualified Data.ByteString.Lazy as LB
147import qualified Data.Text as ST
135import qualified Data.Text as ST
148import qualified Data.Text.Encoding as ST
136import qualified Data.Text.Encoding as ST
149import qualified Data.Text.Lazy as LT
137import qualified Data.Text.Lazy as LT
150−import Data.CaseInsensitive (CI)
138+import Data.UUID (UUID)
151−import qualified Data.CaseInsensitive as CI
152−import Data.UUID.Types (UUID)
153−import qualified Data.UUID.Types as UUID
139+import qualified Data.UUID as UUID
154import Data.Scientific (Scientific)
140import Data.Scientific (Scientific)
155import GHC.Real (infinity, notANumber)
141import GHC.Real (infinity, notANumber)
157−import qualified Data.Aeson.Types as JSON
159-- | Exception thrown if conversion from a SQL value to a Haskell
143-- | Exception thrown if conversion from a SQL value to a Haskell
160-- value fails.
144-- value fails.
161data ResultError = Incompatible { errSQLType :: String
145data ResultError = Incompatible { errSQLType :: String
@@ -182,9 +166,7 @@ data ResultError = Incompatible { errSQLType :: String
182 -- between metadata and actual data in a row).
166 -- between metadata and actual data in a row).
183 deriving (Eq, Show, Typeable)
167 deriving (Eq, Show, Typeable)
185−instance Exception ResultError where
169+instance Exception ResultError
186− toException = postgresqlExceptionToException
187− fromException = postgresqlExceptionFromException
189left :: Exception a => a -> Conversion b
171left :: Exception a => a -> Conversion b
190left = conversionError
172left = conversionError
@@ -225,7 +207,7 @@ typename field = typname <$> typeInfo field
226typeInfo :: Field -> Conversion TypeInfo
208typeInfo :: Field -> Conversion TypeInfo
227typeInfo Field{..} = Conversion $ \conn -> do
209typeInfo Field{..} = Conversion $ \conn -> do
228− Ok <$> (getTypeInfo conn typeOid)
210+ Ok <$> (getTypeInfo conn =<< PQ.ftype result column)
230typeInfoByOid :: PQ.Oid -> Conversion TypeInfo
212typeInfoByOid :: PQ.Oid -> Conversion TypeInfo
231typeInfoByOid oid = Conversion $ \conn -> do
213typeInfoByOid oid = Conversion $ \conn -> do
@@ -250,11 +232,9 @@ tableOid Field{..} = toMaybeOid (unsafeDupablePerformIO (PQ.ftable result column
250 then Nothing
232 then Nothing
251 else Just x
233 else Just x
253−-- | If the column has a table associated with it, this returns the
235+-- | If the column has a table associated with it, this returns the number
254−-- number of the associated table column. Table columns have
236+-- off the associated table column. Numbering starts from 0. Analogous
255−-- nonzero numbers. Zero is returned if the specified column is not
256−-- a simple reference to a table column, or when using pre-3.0
257−-- protocol. Analogous to libpq's @PQftablecol@.
237+-- to libpq's @PQftablecol@.
259tableColumn :: Field -> Int
239tableColumn :: Field -> Int
260tableColumn Field{..} = fromCol (unsafeDupablePerformIO (PQ.ftablecol result column))
240tableColumn Field{..} = fromCol (unsafeDupablePerformIO (PQ.ftablecol result column))
@@ -270,33 +250,16 @@ format Field{..} = unsafeDupablePerformIO (PQ.fformat result column)
271instance FromField () where
251instance FromField () where
272 fromField f _bs
252 fromField f _bs
273− | typeOid f /= TI.voidOid = returnError Incompatible f ""
253+ | typeOid f /= $(inlineTypoid TI.void) = returnError Incompatible f ""
274 | otherwise = pure ()
254 | 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
282-- | For dealing with null values. Compatible with any postgresql type
256-- | For dealing with null values. Compatible with any postgresql type
283-- compatible with type @a@. Note that the type is not checked if
257-- compatible with type @a@. Note that the type is not checked if
284-- the value is null, although it is inadvisable to rely on this
258-- the value is null, although it is inadvisable to rely on this
285-- behavior.
259-- behavior.
286−instance FromField a => FromField (Maybe a) where
260+instance (FromField a) => FromField (Maybe a) where
287− fromField = optionalField fromField
289−-- | For dealing with SQL @null@ values outside of the 'FromField' class.
290−-- Alternatively, one could use 'Control.Applicative.optional', but that
291−-- also turns type and conversion errors into 'Nothing', whereas this is
292−-- more specific and turns only @null@ values into 'Nothing'.
294−optionalField :: FieldParser a -> FieldParser (Maybe a)
295−optionalField p f mv =
297− Nothing -> pure Nothing
261+ fromField _ Nothing = pure Nothing
298− Just _ -> Just <$> p f mv
262+ fromField f bs = Just <$> fromField f bs
299−{-# INLINE optionalField #-}
301-- | compatible with any data type, but the value must be null
264-- | compatible with any data type, but the value must be null
302instance FromField Null where
265instance FromField Null where
@@ -306,30 +269,30 @@ instance FromField Null where
307instance FromField Bool where
270instance FromField Bool where
308 fromField f bs
271 fromField f bs
309− | typeOid f /= TI.boolOid = returnError Incompatible f ""
272+ | typeOid f /= $(inlineTypoid TI.bool) = returnError Incompatible f ""
310 | bs == Nothing = returnError UnexpectedNull f ""
273 | bs == Nothing = returnError UnexpectedNull f ""
311 | bs == Just "t" = pure True
274 | bs == Just "t" = pure True
312 | bs == Just "f" = pure False
275 | bs == Just "f" = pure False
313 | otherwise = returnError ConversionFailed f ""
276 | otherwise = returnError ConversionFailed f ""
315−-- | \"char\", bpchar
278+-- | \"char\"
316instance FromField Char where
279instance FromField Char where
317− fromField f bs0 =
280+ fromField f bs =
318− if (eq TI.charOid \/ eq TI.bpcharOid) (typeOid f)
281+ if typeOid f /= $(inlineTypoid TI.char)
282+ then returnError Incompatible f ""
319− then case bs0 of
283+ else case bs of
320 Nothing -> returnError UnexpectedNull f ""
284 Nothing -> returnError UnexpectedNull f ""
321 Just bs -> if B.length bs /= 1
285 Just bs -> if B.length bs /= 1
322 then returnError ConversionFailed f "length not 1"
286 then returnError ConversionFailed f "length not 1"
323 else return $! (B.head bs)
287 else return $! (B.head bs)
324− else returnError Incompatible f ""
327instance FromField Int16 where
290instance FromField Int16 where
328− fromField = attoFieldParser ok16 $ signed decimal
291+ fromField = atto ok16 $ signed decimal
330-- | int2, int4
293-- | int2, int4
331instance FromField Int32 where
294instance FromField Int32 where
332− fromField = attoFieldParser ok32 $ signed decimal
295+ fromField = atto ok32 $ signed decimal
334#if WORD_SIZE_IN_BITS < 64
297#if WORD_SIZE_IN_BITS < 64
335-- | int2, int4, and if compiled as 64-bit code, int8 as well.
298-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -339,37 +302,37 @@ instance FromField Int32 where
339-- This library was compiled as 64-bit code.
302-- This library was compiled as 64-bit code.
341instance FromField Int where
304instance FromField Int where
342− fromField = attoFieldParser okInt $ signed decimal
305+ fromField = atto okInt $ signed decimal
344-- | int2, int4, int8
307-- | int2, int4, int8
345instance FromField Int64 where
308instance FromField Int64 where
346− fromField = attoFieldParser ok64 $ signed decimal
309+ fromField = atto ok64 $ signed decimal
348-- | int2, int4, int8
311-- | int2, int4, int8
349instance FromField Integer where
312instance FromField Integer where
350− fromField = attoFieldParser ok64 $ signed decimal
313+ fromField = atto ok64 $ signed decimal
352-- | int2, float4 (Uses attoparsec's 'double' routine, for
315-- | int2, float4 (Uses attoparsec's 'double' routine, for
353-- better accuracy convert to 'Scientific' or 'Rational' first)
316-- better accuracy convert to 'Scientific' or 'Rational' first)
354instance FromField Float where
317instance FromField Float where
355− fromField = attoFieldParser ok (realToFrac <$> pg_double)
318+ fromField = atto ok (realToFrac <$> pg_double)
356− where ok = eq TI.float4Oid \/ eq TI.int2Oid
319+ where ok = $(mkCompats [TI.float4,TI.int2])
358-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
321-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
359-- better accuracy convert to 'Scientific' or 'Rational' first)
322-- better accuracy convert to 'Scientific' or 'Rational' first)
360instance FromField Double where
323instance FromField Double where
361− fromField = attoFieldParser ok pg_double
324+ fromField = atto ok pg_double
362− where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
325+ where ok = $(mkCompats [TI.float4,TI.float8,TI.int2,TI.int4])
364−-- | int2, int4, int8, float4, float8, numeric
327+-- | int2, int4, float4, float8, numeric
365instance FromField (Ratio Integer) where
328instance FromField (Ratio Integer) where
366− fromField = attoFieldParser ok pg_rational
329+ fromField = atto ok pg_rational
367− where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
330+ where ok = $(mkCompats [TI.float4,TI.float8,TI.int2,TI.int4,TI.numeric])
369−-- | int2, int4, int8, float4, float8, numeric
332+-- | int2, int4, float4, float8, numeric
370instance FromField Scientific where
333instance FromField Scientific where
371− fromField = attoFieldParser ok rational
334+ fromField = atto ok rational
372− where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
335+ where ok = $(mkCompats [TI.float4,TI.float8,TI.int2,TI.int4,TI.numeric])
374unBinary :: Binary t -> t
337unBinary :: Binary t -> t
375unBinary (Binary x) = x
338unBinary (Binary x) = x
@@ -390,13 +353,13 @@ pg_rational
391-- | bytea, name, text, \"char\", bpchar, varchar, unknown
354-- | bytea, name, text, \"char\", bpchar, varchar, unknown
392instance FromField SB.ByteString where
355instance FromField SB.ByteString where
393− fromField f dat = if typeOid f == TI.byteaOid
356+ fromField f dat = if typeOid f == $(inlineTypoid TI.bytea)
394 then unBinary <$> fromField f dat
357 then unBinary <$> fromField f dat
395 else doFromField f okText' pure dat
358 else doFromField f okText' pure dat
398instance FromField PQ.Oid where
361instance FromField PQ.Oid where
399− fromField f dat = PQ.Oid <$> attoFieldParser (== TI.oidOid) decimal f dat
362+ fromField f dat = PQ.Oid <$> atto (== $(inlineTypoid TI.oid)) decimal f dat
401-- | bytea, name, text, \"char\", bpchar, varchar, unknown
364-- | bytea, name, text, \"char\", bpchar, varchar, unknown
402instance FromField LB.ByteString where
365instance FromField LB.ByteString where
@@ -404,7 +367,7 @@ instance FromField LB.ByteString where
405unescapeBytea :: Field -> SB.ByteString
368unescapeBytea :: Field -> SB.ByteString
406 -> Conversion (Binary SB.ByteString)
369 -> Conversion (Binary SB.ByteString)
407−unescapeBytea f str' = case unsafeDupablePerformIO (PQ.unescapeBytea str') of
370+unescapeBytea f str = case unsafeDupablePerformIO (PQ.unescapeBytea str) of
408 Nothing -> returnError ConversionFailed f "unescapeBytea failed"
371 Nothing -> returnError ConversionFailed f "unescapeBytea failed"
409 Just str -> pure (Binary str)
372 Just str -> pure (Binary str)
@@ -427,85 +390,54 @@ instance FromField ST.Text where
427instance FromField LT.Text where
390instance FromField LT.Text where
428 fromField f dat = LT.fromStrict <$> fromField f dat
391 fromField f dat = LT.fromStrict <$> fromField f dat
431−instance FromField (CI ST.Text) where
432− fromField f mdat = do
435− then returnError Incompatible f ""
437− Nothing -> returnError UnexpectedNull f ""
438− Just dat -> either left (pure . CI.mk)
439− (ST.decodeUtf8' dat)
442−instance FromField (CI LT.Text) where
443− fromField f mdat = do
446− then returnError Incompatible f ""
448− Nothing -> returnError UnexpectedNull f ""
449− Just dat -> either left (pure . CI.mk . LT.fromStrict)
450− (ST.decodeUtf8' dat)
452-- | name, text, \"char\", bpchar, varchar
393-- | name, text, \"char\", bpchar, varchar
453instance FromField [Char] where
394instance FromField [Char] where
454 fromField f dat = ST.unpack <$> fromField f dat
395 fromField f dat = ST.unpack <$> fromField f dat
456-- | timestamptz
397-- | timestamptz
457instance FromField UTCTime where
398instance FromField UTCTime where
458− fromField = ff TI.timestamptzOid "UTCTime" parseUTCTime
399+ fromField = ff $(inlineTypoid TI.timestamptz) "UTCTime" parseUTCTime
460-- | timestamptz
401-- | timestamptz
461instance FromField ZonedTime where
402instance FromField ZonedTime where
462− fromField = ff TI.timestamptzOid "ZonedTime" parseZonedTime
403+ fromField = ff $(inlineTypoid TI.timestamptz) "ZonedTime" parseZonedTime
464-- | timestamp
405-- | timestamp
465instance FromField LocalTime where
406instance FromField LocalTime where
466− fromField = ff TI.timestampOid "LocalTime" parseLocalTime
407+ fromField = ff $(inlineTypoid TI.timestamp) "LocalTime" parseLocalTime
469instance FromField Day where
410instance FromField Day where
470− fromField = ff TI.dateOid "Day" parseDay
411+ fromField = ff $(inlineTypoid TI.date) "Day" parseDay
473instance FromField TimeOfDay where
414instance FromField TimeOfDay where
474− fromField = ff TI.timeOid "TimeOfDay" parseTimeOfDay
415+ fromField = ff $(inlineTypoid TI.time) "TimeOfDay" parseTimeOfDay
476-- | timestamptz
417-- | timestamptz
477instance FromField UTCTimestamp where
418instance FromField UTCTimestamp where
478− fromField = ff TI.timestamptzOid "UTCTimestamp" parseUTCTimestamp
419+ fromField = ff $(inlineTypoid TI.timestamptz) "UTCTimestamp" parseUTCTimestamp
480-- | timestamptz
421-- | timestamptz
481instance FromField ZonedTimestamp where
422instance FromField ZonedTimestamp where
482− fromField = ff TI.timestamptzOid "ZonedTimestamp" parseZonedTimestamp
423+ fromField = ff $(inlineTypoid TI.timestamptz) "ZonedTimestamp" parseZonedTimestamp
484-- | timestamp
425-- | timestamp
485instance FromField LocalTimestamp where
426instance FromField LocalTimestamp where
486− fromField = ff TI.timestampOid "LocalTimestamp" parseLocalTimestamp
427+ fromField = ff $(inlineTypoid TI.timestamp) "LocalTimestamp" parseLocalTimestamp
489instance FromField Date where
430instance FromField Date where
490− fromField = ff TI.dateOid "Date" parseDate
431+ fromField = ff $(inlineTypoid TI.date) "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
501ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
433ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
502 -> Field -> Maybe B8.ByteString -> Conversion a
434 -> Field -> Maybe B8.ByteString -> Conversion a
503−ff compatOid hsType parseBS f mstr =
435+ff compatOid hsType parse f mstr =
504 if typeOid f /= compatOid
436 if typeOid f /= compatOid
505 then err Incompatible ""
437 then err Incompatible ""
506 else case mstr of
438 else case mstr of
507 Nothing -> err UnexpectedNull ""
439 Nothing -> err UnexpectedNull ""
508− Just str -> case parseBS str of
440+ Just str -> case parse str of
509 Left msg -> err ConversionFailed msg
441 Left msg -> err ConversionFailed msg
510 Right val -> return val
442 Right val -> return val
@@ -527,29 +459,27 @@ instance (FromField a, FromField b) => FromField (Either a b) where
528-- | any postgresql array whose elements are compatible with type @a@
460-- | any postgresql array whose elements are compatible with type @a@
529instance (FromField a, Typeable a) => FromField (PGArray a) where
461instance (FromField a, Typeable a) => FromField (PGArray a) where
530− fromField = pgArrayFieldParser fromField
462+ fromField f mdat = do
532−pgArrayFieldParser :: Typeable a => FieldParser a -> FieldParser (PGArray a)
533−pgArrayFieldParser fieldParser f mdat = do
534 info <- typeInfo f
463 info <- typeInfo f
535 case info of
464 case info of
536 TI.Array{} ->
465 TI.Array{} ->
537 case mdat of
466 case mdat of
538 Nothing -> returnError UnexpectedNull f ""
467 Nothing -> returnError UnexpectedNull f ""
539 Just dat -> do
468 Just dat -> do
540− case parseOnly (fromArray fieldParser info f) dat of
469+ case parseOnly (fromArray info f) dat of
541 Left err -> returnError ConversionFailed f err
470 Left err -> returnError ConversionFailed f err
542 Right conv -> PGArray <$> conv
471 Right conv -> PGArray <$> conv
543 _ -> returnError Incompatible f ""
472 _ -> returnError Incompatible f ""
474+fromArray :: (FromField a)
545−fromArray :: FieldParser a -> TypeInfo -> Field -> Parser (Conversion [a])
475+ => TypeInfo -> Field -> Parser (Conversion [a])
546−fromArray fieldParser typInfo f = sequence . (parseIt <$>) <$> array delim
476+fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
548− delim = typdelim (typelem typInfo)
478+ delim = typdelim (typelem typeInfo)
549− fElem = f{ typeOid = typoid (typelem typInfo) }
479+ fElem = f{ typeOid = typoid (typelem typeInfo) }
551 parseIt item =
481 parseIt item =
552− fieldParser f' $ if item == Arrays.Plain "NULL" then Nothing else Just item'
482+ fromField f' $ if item' == "NULL" then Nothing else Just item'
554 item' = fmt delim item
484 item' = fmt delim item
555 f' | Arrays.Array _ <- item = f
485 f' | Arrays.Array _ <- item = f
@@ -564,7 +494,7 @@ instance (FromField a, Typeable a) => FromField (IOVector a) where
565instance FromField UUID where
495instance FromField UUID where
566 fromField f mbs =
496 fromField f mbs =
567− if typeOid f /= TI.uuidOid
497+ if typeOid f /= $(inlineTypoid TI.uuid)
568 then returnError Incompatible f ""
498 then returnError Incompatible f ""
569 else case mbs of
499 else case mbs of
570 Nothing -> returnError UnexpectedNull f ""
500 Nothing -> returnError UnexpectedNull f ""
@@ -573,26 +503,26 @@ instance FromField UUID where
573 Nothing -> returnError ConversionFailed f "Invalid UUID"
503 Nothing -> returnError ConversionFailed f "Invalid UUID"
574 Just uuid -> pure uuid
504 Just uuid -> pure uuid
576−-- | json, jsonb
506+-- | json
577instance FromField JSON.Value where
507instance FromField JSON.Value where
578− fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
508+ fromField f mbs =
579− where parseBS bs = case JSON.eitherDecodeStrict' bs of
580− Left err -> returnError ConversionFailed f err
581− Right val -> pure val
583−-- | Return the JSON ByteString directly
586−fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
587−fromFieldJSONByteString f mbs =
588− if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
509+ if typeOid f /= $(inlineTypoid TI.json) && typeOid f /= $(inlineTypoid TI.jsonb)
589 then returnError Incompatible f ""
510 then returnError Incompatible f ""
590 else case mbs of
511 else case mbs of
591 Nothing -> returnError UnexpectedNull f ""
512 Nothing -> returnError UnexpectedNull f ""
592− Just bs -> pure bs
513+ Just bs ->
514+#if MIN_VERSION_aeson(0,6,3)
515+ case JSON.eitherDecodeStrict' bs of
516+#elif MIN_VERSION_bytestring(0,10,0)
517+ case JSON.eitherDecode' $ LB.fromStrict bs of
519+ case JSON.eitherDecode' $ LB.fromChunks [bs] of
521+ Left err -> returnError ConversionFailed f err
522+ Right val -> pure val
594-- | Parse a field to a JSON 'JSON.Value' and convert that into a
524-- | Parse a field to a JSON 'JSON.Value' and convert that into a
595−-- Haskell value using the 'JSON.FromJSON' instance.
525+-- Haskell value using 'JSON.fromJSON'.
597-- This can be used as the default implementation for the 'fromField'
527-- This can be used as the default implementation for the 'fromField'
598-- method for Haskell types that have a JSON representation in
528-- method for Haskell types that have a JSON representation in
@@ -600,23 +530,14 @@ fromFieldJSONByteString f mbs =
601-- The 'Typeable' constraint is required to show more informative
531-- The 'Typeable' constraint is required to show more informative
602-- error messages when parsing fails.
532-- error messages when parsing fails.
604−-- Note that @fromJSONField :: FieldParser ('Maybe' Foo)@ will return
605−-- @'Nothing'@ on the json @null@ value, and return an exception on SQL @null@
606−-- value. Alternatively, one could write @'optionalField' fromJSONField@
607−-- that will return @Nothing@ on SQL @null@, and otherwise will call
608−-- @fromJSONField :: FieldParser Foo@ and then return @'Just'@ the
609−-- result value, or return its exception. If one would
610−-- like to return @Nothing@ on both the SQL @null@ and json @null@ values,
611−-- one way to do it would be to write
612−-- @\\f mv -> 'Control.Monad.join' '<$>' optionalField fromJSONField f mv@
613fromJSONField :: (JSON.FromJSON a, Typeable a) => FieldParser a
533fromJSONField :: (JSON.FromJSON a, Typeable a) => FieldParser a
614fromJSONField f mbBs = do
534fromJSONField f mbBs = do
615 value <- fromField f mbBs
535 value <- fromField f mbBs
616− case JSON.ifromJSON value of
536+ case JSON.fromJSON value of
617− JSON.IError path err -> returnError ConversionFailed f $
537+ JSON.Error err -> returnError ConversionFailed f $
618− "JSON decoding error: " ++ (JSON.formatError path err)
538+ "JSON decoding error: " ++ err
619− JSON.ISuccess x -> pure x
539+ JSON.Success x -> pure x
621-- | Compatible with the same set of types as @a@. Note that
542-- | Compatible with the same set of types as @a@. Note that
622-- modifying the 'IORef' does not have any effects outside
543-- modifying the 'IORef' does not have any effects outside
@@ -633,33 +554,20 @@ instance FromField a => FromField (MVar a) where
633type Compat = PQ.Oid -> Bool
554type Compat = PQ.Oid -> Bool
635okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
556okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
636−okText = eq TI.nameOid \/ eq TI.textOid \/ eq TI.charOid
557+okText = $( mkCompats [ TI.name, TI.text, TI.char,
637− \/ eq TI.bpcharOid \/ eq TI.varcharOid
558+ TI.bpchar, TI.varchar ] )
638−okText' = eq TI.nameOid \/ eq TI.textOid \/ eq TI.charOid
559+okText' = $( mkCompats [ TI.name, TI.text, TI.char,
639− \/ eq TI.bpcharOid \/ eq TI.varcharOid \/ eq TI.unknownOid
560+ TI.bpchar, TI.varchar, TI.unknown ] )
640−okBinary = eq TI.byteaOid
561+okBinary = (== $( inlineTypoid TI.bytea ))
641−ok16 = eq TI.int2Oid
562+ok16 = (== $( inlineTypoid TI.int2 ))
642−ok32 = eq TI.int2Oid \/ eq TI.int4Oid
563+ok32 = $( mkCompats [TI.int2,TI.int4] )
643−ok64 = eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid
564+ok64 = $( mkCompats [TI.int2,TI.int4,TI.int8] )
644#if WORD_SIZE_IN_BITS < 64
565#if WORD_SIZE_IN_BITS < 64
645okInt = ok32
566okInt = ok32
647okInt = ok64
568okInt = ok64
650−-- | eq and \/ are used to imlement what Macro stuff did,
651−-- i.e. mkCompats and inlineTypoid
652−eq :: PQ.Oid -> PQ.Oid -> Bool
657−(\/) :: (PQ.Oid -> Bool)
660−f \/ g = \x -> f x || g x
663doFromField :: forall a . (Typeable a)
571doFromField :: forall a . (Typeable a)
664 => Field -> Compat -> (ByteString -> Conversion a)
572 => Field -> Compat -> (ByteString -> Conversion a)
665 -> Maybe ByteString -> Conversion a
573 -> Maybe ByteString -> Conversion a
@@ -684,22 +592,10 @@ returnError mkErr f msg = do
684 (show (typeOf (undefined :: a)))
592 (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)
696−attoFieldParser :: forall a. (Typeable a)
595+atto :: forall a. (Typeable a)
697− => (PQ.Oid -> Bool)
596+ => Compat -> Parser a -> Field -> Maybe ByteString
698− -- ^ Predicate for whether the postgresql type oid is compatible with this parser
597+ -> Conversion a
700− -- ^ An attoparsec parser.
702−attoFieldParser types p0 f dat = doFromField f types (go p0) dat
598+atto types p0 f dat = doFromField f types (go p0) dat
704 go :: Parser a -> ByteString -> Conversion a
600 go :: Parser a -> ByteString -> Conversion a