@@ -2,7 +2,6 @@
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
5−{-# LANGUAGE PolyKinds #-}
8Module: Database.PostgreSQL.Simple.FromField
7Module: Database.PostgreSQL.Simple.FromField
@@ -107,27 +106,26 @@ module Database.PostgreSQL.Simple.FromField
107 , PQ.Oid(..)
106 , PQ.Oid(..)
108 , PQ.Format(..)
107 , PQ.Format(..)
109 , pgArrayFieldParser
108 , pgArrayFieldParser
112 , optionalField
110 , optionalField
113 , fromJSONField
111 , fromJSONField
114− , fromFieldJSONByteString
117#include "MachDeps.h"
114#include "MachDeps.h"
119−import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
116+import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
120import Control.Concurrent.MVar (MVar, newMVar)
117import Control.Concurrent.MVar (MVar, newMVar)
121−import Control.Exception (Exception (toException, fromException))
118+import Control.Exception (Exception)
122import qualified Data.Aeson as JSON
119import qualified Data.Aeson as JSON
120+import qualified Data.Aeson.Internal as JSON
121+import qualified Data.Aeson.Parser as JSON (value')
123import Data.Attoparsec.ByteString.Char8 hiding (Result)
122import Data.Attoparsec.ByteString.Char8 hiding (Result)
124import Data.ByteString (ByteString)
123import Data.ByteString (ByteString)
125import qualified Data.ByteString.Char8 as B
124import qualified Data.ByteString.Char8 as B
126−import Data.Functor.Identity (Identity(Identity))
127import Data.Int (Int16, Int32, Int64)
125import Data.Int (Int16, Int32, Int64)
128import Data.IORef (IORef, newIORef)
126import Data.IORef (IORef, newIORef)
129import Data.Ratio (Ratio)
127import Data.Ratio (Ratio)
130−import Data.Time.Compat ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay, CalendarDiffTime )
128+import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
131import Data.Typeable (Typeable, typeOf)
129import Data.Typeable (Typeable, typeOf)
132import Data.Vector (Vector)
130import Data.Vector (Vector)
133import Data.Vector.Mutable (IOVector)
131import Data.Vector.Mutable (IOVector)
@@ -154,8 +152,6 @@ import qualified Data.UUID.Types as UUID
154import Data.Scientific (Scientific)
152import Data.Scientific (Scientific)
155import GHC.Real (infinity, notANumber)
153import GHC.Real (infinity, notANumber)
157−import qualified Data.Aeson.Types as JSON
159-- | Exception thrown if conversion from a SQL value to a Haskell
155-- | Exception thrown if conversion from a SQL value to a Haskell
160-- value fails.
156-- value fails.
161data ResultError = Incompatible { errSQLType :: String
157data ResultError = Incompatible { errSQLType :: String
@@ -182,9 +178,7 @@ data ResultError = Incompatible { errSQLType :: String
182 -- between metadata and actual data in a row).
178 -- between metadata and actual data in a row).
183 deriving (Eq, Show, Typeable)
179 deriving (Eq, Show, Typeable)
185−instance Exception ResultError where
181+instance Exception ResultError
186− toException = postgresqlExceptionToException
187− fromException = postgresqlExceptionFromException
189left :: Exception a => a -> Conversion b
183left :: Exception a => a -> Conversion b
190left = conversionError
184left = conversionError
@@ -250,11 +244,9 @@ tableOid Field{..} = toMaybeOid (unsafeDupablePerformIO (PQ.ftable result column
250 then Nothing
244 then Nothing
251 else Just x
245 else Just x
253−-- | If the column has a table associated with it, this returns the
247+-- | If the column has a table associated with it, this returns the number
254−-- number of the associated table column. Table columns have
248+-- 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@.
249+-- to libpq's @PQftablecol@.
259tableColumn :: Field -> Int
251tableColumn :: Field -> Int
260tableColumn Field{..} = fromCol (unsafeDupablePerformIO (PQ.ftablecol result column))
252tableColumn Field{..} = fromCol (unsafeDupablePerformIO (PQ.ftablecol result column))
@@ -273,12 +265,6 @@ instance FromField () where
273 | typeOid f /= TI.voidOid = returnError Incompatible f ""
265 | typeOid f /= TI.voidOid = returnError Incompatible f ""
274 | otherwise = pure ()
266 | 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
268-- | For dealing with null values. Compatible with any postgresql type
283-- compatible with type @a@. Note that the type is not checked if
269-- 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
270-- the value is null, although it is inadvisable to rely on this
@@ -325,11 +311,11 @@ instance FromField Char where
327instance FromField Int16 where
313instance FromField Int16 where
328− fromField = attoFieldParser ok16 $ signed decimal
314+ fromField = atto ok16 $ signed decimal
330-- | int2, int4
316-- | int2, int4
331instance FromField Int32 where
317instance FromField Int32 where
332− fromField = attoFieldParser ok32 $ signed decimal
318+ fromField = atto ok32 $ signed decimal
334#if WORD_SIZE_IN_BITS < 64
320#if WORD_SIZE_IN_BITS < 64
335-- | int2, int4, and if compiled as 64-bit code, int8 as well.
321-- | int2, int4, and if compiled as 64-bit code, int8 as well.
@@ -339,36 +325,36 @@ instance FromField Int32 where
339-- This library was compiled as 64-bit code.
325-- This library was compiled as 64-bit code.
341instance FromField Int where
327instance FromField Int where
342− fromField = attoFieldParser okInt $ signed decimal
328+ fromField = atto okInt $ signed decimal
344-- | int2, int4, int8
330-- | int2, int4, int8
345instance FromField Int64 where
331instance FromField Int64 where
346− fromField = attoFieldParser ok64 $ signed decimal
332+ fromField = atto ok64 $ signed decimal
348-- | int2, int4, int8
334-- | int2, int4, int8
349instance FromField Integer where
335instance FromField Integer where
350− fromField = attoFieldParser ok64 $ signed decimal
336+ fromField = atto ok64 $ signed decimal
352-- | int2, float4 (Uses attoparsec's 'double' routine, for
338-- | int2, float4 (Uses attoparsec's 'double' routine, for
353-- better accuracy convert to 'Scientific' or 'Rational' first)
339-- better accuracy convert to 'Scientific' or 'Rational' first)
354instance FromField Float where
340instance FromField Float where
355− fromField = attoFieldParser ok (realToFrac <$> pg_double)
341+ fromField = atto ok (realToFrac <$> pg_double)
356 where ok = eq TI.float4Oid \/ eq TI.int2Oid
342 where ok = eq TI.float4Oid \/ eq TI.int2Oid
358-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
344-- | int2, int4, float4, float8 (Uses attoparsec's 'double' routine, for
359-- better accuracy convert to 'Scientific' or 'Rational' first)
345-- better accuracy convert to 'Scientific' or 'Rational' first)
360instance FromField Double where
346instance FromField Double where
361− fromField = attoFieldParser ok pg_double
347+ fromField = atto ok pg_double
362 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
348 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid
364-- | int2, int4, int8, float4, float8, numeric
350-- | int2, int4, int8, float4, float8, numeric
365instance FromField (Ratio Integer) where
351instance FromField (Ratio Integer) where
366− fromField = attoFieldParser ok pg_rational
352+ 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
353 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
369-- | int2, int4, int8, float4, float8, numeric
355-- | int2, int4, int8, float4, float8, numeric
370instance FromField Scientific where
356instance FromField Scientific where
371− fromField = attoFieldParser ok rational
357+ 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
358 where ok = eq TI.float4Oid \/ eq TI.float8Oid \/ eq TI.int2Oid \/ eq TI.int4Oid \/ eq TI.int8Oid \/ eq TI.numericOid
374unBinary :: Binary t -> t
360unBinary :: Binary t -> t
@@ -396,7 +382,7 @@ instance FromField SB.ByteString where
398instance FromField PQ.Oid where
384instance FromField PQ.Oid where
399− fromField f dat = PQ.Oid <$> attoFieldParser (== TI.oidOid) decimal f dat
385+ fromField f dat = PQ.Oid <$> atto (== TI.oidOid) decimal f dat
401-- | bytea, name, text, \"char\", bpchar, varchar, unknown
387-- | bytea, name, text, \"char\", bpchar, varchar, unknown
402instance FromField LB.ByteString where
388instance FromField LB.ByteString where
@@ -489,15 +475,6 @@ instance FromField LocalTimestamp where
489instance FromField Date where
475instance FromField Date where
490 fromField = ff TI.dateOid "Date" parseDate
476 fromField = ff TI.dateOid "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)
478ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
502 -> Field -> Maybe B8.ByteString -> Conversion a
479 -> Field -> Maybe B8.ByteString -> Conversion a
503ff compatOid hsType parseBS f mstr =
480ff compatOid hsType parseBS f mstr =
@@ -576,13 +553,11 @@ instance FromField UUID where
576-- | json, jsonb
553-- | json, jsonb
577instance FromField JSON.Value where
554instance FromField JSON.Value where
578 fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
555 fromField f mbs = parseBS =<< fromFieldJSONByteString f mbs
579− where parseBS bs = case JSON.eitherDecodeStrict' bs of
556+ where parseBS bs = case parseOnly (JSON.value' <* endOfInput) bs of
580 Left err -> returnError ConversionFailed f err
557 Left err -> returnError ConversionFailed f err
581 Right val -> pure val
558 Right val -> pure val
583-- | Return the JSON ByteString directly
560-- | Return the JSON ByteString directly
586fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
561fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
587fromFieldJSONByteString f mbs =
562fromFieldJSONByteString f mbs =
588 if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
563 if typeOid f /= TI.jsonOid && typeOid f /= TI.jsonbOid
@@ -684,22 +659,10 @@ returnError mkErr f msg = do
684 (show (typeOf (undefined :: a)))
659 (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)
662+atto :: forall a. (Typeable a)
697− => (PQ.Oid -> Bool)
663+ => Compat -> Parser a -> Field -> Maybe ByteString
698− -- ^ Predicate for whether the postgresql type oid is compatible with this parser
664+ -> Conversion a
700− -- ^ An attoparsec parser.
702−attoFieldParser types p0 f dat = doFromField f types (go p0) dat
665+atto types p0 f dat = doFromField f types (go p0) dat
704 go :: Parser a -> ByteString -> Conversion a
667 go :: Parser a -> ByteString -> Conversion a