{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PolyKinds #-}
module Database.PostgreSQL.Simple.FromField
(
FromField(..)
, FieldParser
, Conversion()
, runConversion
, conversionMap
, conversionError
, ResultError(..)
, returnError
, Field
, typename
, TypeInfo(..)
, Attribute(..)
, typeInfo
, typeInfoByOid
, name
, tableOid
, tableColumn
, format
, typeOid
, PQ.Oid(..)
, PQ.Format(..)
, pgArrayFieldParser
, attoFieldParser
, optionalField
, fromJSONField
, fromFieldJSONByteString
) where
#include "MachDeps.h"
import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
import Control.Concurrent.MVar (MVar, newMVar)
import Control.Exception (Exception)
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Internal as JSON
import qualified Data.Aeson.Parser as JSON (value')
import Data.Attoparsec.ByteString.Char8 hiding (Result)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Functor.Identity (Identity(Identity))
import Data.Int (Int16, Int32, Int64)
import Data.IORef (IORef, newIORef)
import Data.Ratio (Ratio)
import Data.Time.Compat ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay, CalendarDiffTime )
import Data.Typeable (Typeable, typeOf)
import Data.Vector (Vector)
import Data.Vector.Mutable (IOVector)
import qualified Data.Vector as V
import Database.PostgreSQL.Simple.Internal
import Database.PostgreSQL.Simple.Compat
import Database.PostgreSQL.Simple.Ok
import Database.PostgreSQL.Simple.Types
import Database.PostgreSQL.Simple.TypeInfo as TI
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
import Database.PostgreSQL.Simple.Time
import Database.PostgreSQL.Simple.Arrays as Arrays
import qualified Database.PostgreSQL.LibPQ as PQ
import qualified Data.ByteString as SB
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as ST
import qualified Data.Text.Encoding as ST
import qualified Data.Text.Lazy as LT
import Data.CaseInsensitive (CI)
import qualified Data.CaseInsensitive as CI
import Data.UUID.Types (UUID)
import qualified Data.UUID.Types as UUID
import Data.Scientific (Scientific)
import GHC.Real (infinity, notANumber)
data ResultError = Incompatible { ResultError -> String
errSQLType :: String
, ResultError -> Maybe Oid
errSQLTableOid :: Maybe PQ.Oid
, ResultError -> String
errSQLField :: String
, ResultError -> String
errHaskellType :: String
, ResultError -> String
errMessage :: String }
| UnexpectedNull { errSQLType :: String
, errSQLTableOid :: Maybe PQ.Oid
, errSQLField :: String
, errHaskellType :: String
, errMessage :: String }
| ConversionFailed { errSQLType :: String
, errSQLTableOid :: Maybe PQ.Oid
, errSQLField :: String
, errHaskellType :: String
, errMessage :: String }
deriving (ResultError -> ResultError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ResultError -> ResultError -> Bool
$c/= :: ResultError -> ResultError -> Bool
== :: ResultError -> ResultError -> Bool
$c== :: ResultError -> ResultError -> Bool
Eq, Int -> ResultError -> ShowS
[ResultError] -> ShowS
ResultError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ResultError] -> ShowS
$cshowList :: [ResultError] -> ShowS
show :: ResultError -> String
$cshow :: ResultError -> String
showsPrec :: Int -> ResultError -> ShowS
$cshowsPrec :: Int -> ResultError -> ShowS
Show, Typeable)
instance Exception ResultError
left :: Exception a => a -> Conversion b
left :: forall a b. Exception a => a -> Conversion b
left = forall a b. Exception a => a -> Conversion b
conversionError
type FieldParser a = Field -> Maybe ByteString -> Conversion a
class FromField a where
fromField :: FieldParser a
typename :: Field -> Conversion ByteString
typename :: Field -> Conversion ByteString
typename Field
field = TypeInfo -> ByteString
typname forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Field -> Conversion TypeInfo
typeInfo Field
field
typeInfo :: Field -> Conversion TypeInfo
typeInfo :: Field -> Conversion TypeInfo
typeInfo Field{Result
Oid
Column
column :: Field -> Column
result :: Field -> Result
typeOid :: Oid
column :: Column
result :: Result
typeOid :: Field -> Oid
..} = forall a. (Connection -> IO (Ok a)) -> Conversion a
Conversion forall a b. (a -> b) -> a -> b
$ \Connection
conn -> do
forall a. a -> Ok a
Ok forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Connection -> Oid -> IO TypeInfo
getTypeInfo Connection
conn Oid
typeOid)
typeInfoByOid :: PQ.Oid -> Conversion TypeInfo
typeInfoByOid :: Oid -> Conversion TypeInfo
typeInfoByOid Oid
oid = forall a. (Connection -> IO (Ok a)) -> Conversion a
Conversion forall a b. (a -> b) -> a -> b
$ \Connection
conn -> do
forall a. a -> Ok a
Ok forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Connection -> Oid -> IO TypeInfo
getTypeInfo Connection
conn Oid
oid)
name :: Field -> Maybe ByteString
name :: Field -> Maybe ByteString
name Field{Result
Oid
Column
typeOid :: Oid
column :: Column
result :: Result
column :: Field -> Column
result :: Field -> Result
typeOid :: Field -> Oid
..} = forall a. IO a -> a
unsafeDupablePerformIO (Result -> Column -> IO (Maybe ByteString)
PQ.fname Result
result Column
column)
tableOid :: Field -> Maybe PQ.Oid
tableOid :: Field -> Maybe Oid
tableOid Field{Result
Oid
Column
typeOid :: Oid
column :: Column
result :: Result
column :: Field -> Column
result :: Field -> Result
typeOid :: Field -> Oid
..} = Oid -> Maybe Oid
toMaybeOid (forall a. IO a -> a
unsafeDupablePerformIO (Result -> Column -> IO Oid
PQ.ftable Result
result Column
column))
where
toMaybeOid :: Oid -> Maybe Oid
toMaybeOid Oid
x
= if Oid
x forall a. Eq a => a -> a -> Bool
== Oid
PQ.invalidOid
then forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just Oid
x
tableColumn :: Field -> Int
tableColumn :: Field -> Int
tableColumn Field{Result
Oid
Column
typeOid :: Oid
column :: Column
result :: Result
column :: Field -> Column
result :: Field -> Result
typeOid :: Field -> Oid
..} = forall {b}. Num b => Column -> b
fromCol (forall a. IO a -> a
unsafeDupablePerformIO (Result -> Column -> IO Column
PQ.ftablecol Result
result Column
column))
where
fromCol :: Column -> b
fromCol (PQ.Col CInt
x) = forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
x
format :: Field -> PQ.Format
format :: Field -> Format
format Field{Result
Oid
Column
typeOid :: Oid
column :: Column
result :: Result
column :: Field -> Column
result :: Field -> Result
typeOid :: Field -> Oid
..} = forall a. IO a -> a
unsafeDupablePerformIO (Result -> Column -> IO Format
PQ.fformat Result
result Column
column)
instance FromField () where
fromField :: FieldParser ()
fromField Field
f Maybe ByteString
_bs
| Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
TI.voidOid = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
| Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
instance (FromField a) => FromField (Const a b) where
fromField :: FieldParser (Const a b)
fromField Field
f Maybe ByteString
bs = forall {k} a (b :: k). a -> Const a b
Const forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
bs
instance (FromField a) => FromField (Identity a) where
fromField :: FieldParser (Identity a)
fromField Field
f Maybe ByteString
bs = forall a. a -> Identity a
Identity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
bs
instance FromField a => FromField (Maybe a) where
fromField :: FieldParser (Maybe a)
fromField = forall a. FieldParser a -> FieldParser (Maybe a)
optionalField forall a. FromField a => FieldParser a
fromField
optionalField :: FieldParser a -> FieldParser (Maybe a)
optionalField :: forall a. FieldParser a -> FieldParser (Maybe a)
optionalField FieldParser a
p Field
f Maybe ByteString
mv =
case Maybe ByteString
mv of
Maybe ByteString
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
Just ByteString
_ -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser a
p Field
f Maybe ByteString
mv
{-# INLINE optionalField #-}
instance FromField Null where
fromField :: FieldParser Null
fromField Field
_ Maybe ByteString
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure Null
Null
fromField Field
f (Just ByteString
_) = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
"data is not null"
instance FromField Bool where
fromField :: FieldParser Bool
fromField Field
f Maybe ByteString
bs
| Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
TI.boolOid = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
| Maybe ByteString
bs forall a. Eq a => a -> a -> Bool
== forall a. Maybe a
Nothing = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
| Maybe ByteString
bs forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just ByteString
"t" = forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
| Maybe ByteString
bs forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just ByteString
"f" = forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
| Bool
otherwise = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
""
instance FromField Char where
fromField :: FieldParser Char
fromField Field
f Maybe ByteString
bs0 =
if (Oid -> Oid -> Bool
eq Oid
TI.charOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.bpcharOid) (Field -> Oid
typeOid Field
f)
then case Maybe ByteString
bs0 of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
bs -> if ByteString -> Int
B.length ByteString
bs forall a. Eq a => a -> a -> Bool
/= Int
1
then forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
"length not 1"
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (ByteString -> Char
B.head ByteString
bs)
else forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
instance FromField Int16 where
fromField :: FieldParser Int16
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok16 forall a b. (a -> b) -> a -> b
$ forall a. Num a => Parser a -> Parser a
signed forall a. Integral a => Parser a
decimal
instance FromField Int32 where
fromField :: FieldParser Int32
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok32 forall a b. (a -> b) -> a -> b
$ forall a. Num a => Parser a -> Parser a
signed forall a. Integral a => Parser a
decimal
#if WORD_SIZE_IN_BITS < 64
#else
#endif
instance FromField Int where
fromField :: FieldParser Int
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
okInt forall a b. (a -> b) -> a -> b
$ forall a. Num a => Parser a -> Parser a
signed forall a. Integral a => Parser a
decimal
instance FromField Int64 where
fromField :: FieldParser Int64
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok64 forall a b. (a -> b) -> a -> b
$ forall a. Num a => Parser a -> Parser a
signed forall a. Integral a => Parser a
decimal
instance FromField Integer where
fromField :: FieldParser Integer
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok64 forall a b. (a -> b) -> a -> b
$ forall a. Num a => Parser a -> Parser a
signed forall a. Integral a => Parser a
decimal
instance FromField Float where
fromField :: FieldParser Float
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok (forall a b. (Real a, Fractional b) => a -> b
realToFrac forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Double
pg_double)
where ok :: Oid -> Bool
ok = Oid -> Oid -> Bool
eq Oid
TI.float4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int2Oid
instance FromField Double where
fromField :: FieldParser Double
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok Parser Double
pg_double
where ok :: Oid -> Bool
ok = Oid -> Oid -> Bool
eq Oid
TI.float4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.float8Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int2Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int4Oid
instance FromField (Ratio Integer) where
fromField :: FieldParser Rational
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok Parser Rational
pg_rational
where ok :: Oid -> Bool
ok = Oid -> Oid -> Bool
eq Oid
TI.float4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.float8Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int2Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int8Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.numericOid
instance FromField Scientific where
fromField :: FieldParser Scientific
fromField = forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
ok forall a. Fractional a => Parser a
rational
where ok :: Oid -> Bool
ok = Oid -> Oid -> Bool
eq Oid
TI.float4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.float8Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int2Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int8Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.numericOid
unBinary :: Binary t -> t
unBinary :: forall t. Binary t -> t
unBinary (Binary t
x) = t
x
pg_double :: Parser Double
pg_double :: Parser Double
pg_double
= (ByteString -> Parser ByteString
string ByteString
"NaN" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure ( Double
0 forall a. Fractional a => a -> a -> a
/ Double
0))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ByteString -> Parser ByteString
string ByteString
"Infinity" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure ( Double
1 forall a. Fractional a => a -> a -> a
/ Double
0))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ByteString -> Parser ByteString
string ByteString
"-Infinity" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure (-Double
1 forall a. Fractional a => a -> a -> a
/ Double
0))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Double
double
pg_rational :: Parser Rational
pg_rational :: Parser Rational
pg_rational
= (ByteString -> Parser ByteString
string ByteString
"NaN" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Rational
notANumber )
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ByteString -> Parser ByteString
string ByteString
"Infinity" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Rational
infinity )
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ByteString -> Parser ByteString
string ByteString
"-Infinity" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure (-Rational
infinity))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Fractional a => Parser a
rational
instance FromField SB.ByteString where
fromField :: FieldParser ByteString
fromField Field
f Maybe ByteString
dat = if Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
== Oid
TI.byteaOid
then forall t. Binary t -> t
unBinary forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat
else forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
okText' forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ByteString
dat
instance FromField PQ.Oid where
fromField :: FieldParser Oid
fromField Field
f Maybe ByteString
dat = CUInt -> Oid
PQ.Oid forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser (forall a. Eq a => a -> a -> Bool
== Oid
TI.oidOid) forall a. Integral a => Parser a
decimal Field
f Maybe ByteString
dat
instance FromField LB.ByteString where
fromField :: FieldParser ByteString
fromField Field
f Maybe ByteString
dat = [ByteString] -> ByteString
LB.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[]) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat
unescapeBytea :: Field -> SB.ByteString
-> Conversion (Binary SB.ByteString)
unescapeBytea :: Field -> ByteString -> Conversion (Binary ByteString)
unescapeBytea Field
f ByteString
str' = case forall a. IO a -> a
unsafeDupablePerformIO (ByteString -> IO (Maybe ByteString)
PQ.unescapeBytea ByteString
str') of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
"unescapeBytea failed"
Just ByteString
str -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Binary a
Binary ByteString
str)
instance FromField (Binary SB.ByteString) where
fromField :: FieldParser (Binary ByteString)
fromField Field
f Maybe ByteString
dat = case Field -> Format
format Field
f of
Format
PQ.Text -> forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
okBinary (Field -> ByteString -> Conversion (Binary ByteString)
unescapeBytea Field
f) Maybe ByteString
dat
Format
PQ.Binary -> forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
okBinary (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Binary a
Binary) Maybe ByteString
dat
instance FromField (Binary LB.ByteString) where
fromField :: FieldParser (Binary ByteString)
fromField Field
f Maybe ByteString
dat = forall a. a -> Binary a
Binary forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
LB.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Binary t -> t
unBinary forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat
instance FromField ST.Text where
fromField :: FieldParser Text
fromField Field
f = forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
okText forall a b. (a -> b) -> a -> b
$ (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. Exception a => a -> Conversion b
left forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
ST.decodeUtf8')
instance FromField LT.Text where
fromField :: FieldParser Text
fromField Field
f Maybe ByteString
dat = Text -> Text
LT.fromStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat
instance FromField (CI ST.Text) where
fromField :: FieldParser (CI Text)
fromField Field
f Maybe ByteString
mdat = do
ByteString
typ <- Field -> Conversion ByteString
typename Field
f
if ByteString
typ forall a. Eq a => a -> a -> Bool
/= ByteString
"citext"
then forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
else case Maybe ByteString
mdat of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
dat -> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. Exception a => a -> Conversion b
left (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. FoldCase s => s -> CI s
CI.mk)
(ByteString -> Either UnicodeException Text
ST.decodeUtf8' ByteString
dat)
instance FromField (CI LT.Text) where
fromField :: FieldParser (CI Text)
fromField Field
f Maybe ByteString
mdat = do
ByteString
typ <- Field -> Conversion ByteString
typename Field
f
if ByteString
typ forall a. Eq a => a -> a -> Bool
/= ByteString
"citext"
then forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
else case Maybe ByteString
mdat of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
dat -> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. Exception a => a -> Conversion b
left (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. FoldCase s => s -> CI s
CI.mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.fromStrict)
(ByteString -> Either UnicodeException Text
ST.decodeUtf8' ByteString
dat)
instance FromField [Char] where
fromField :: FieldParser String
fromField Field
f Maybe ByteString
dat = Text -> String
ST.unpack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat
instance FromField UTCTime where
fromField :: FieldParser UTCTime
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestamptzOid String
"UTCTime" ByteString -> Either String UTCTime
parseUTCTime
instance FromField ZonedTime where
fromField :: FieldParser ZonedTime
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestamptzOid String
"ZonedTime" ByteString -> Either String ZonedTime
parseZonedTime
instance FromField LocalTime where
fromField :: FieldParser LocalTime
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestampOid String
"LocalTime" ByteString -> Either String LocalTime
parseLocalTime
instance FromField Day where
fromField :: FieldParser Day
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.dateOid String
"Day" ByteString -> Either String Day
parseDay
instance FromField TimeOfDay where
fromField :: FieldParser TimeOfDay
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timeOid String
"TimeOfDay" ByteString -> Either String TimeOfDay
parseTimeOfDay
instance FromField UTCTimestamp where
fromField :: FieldParser UTCTimestamp
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestamptzOid String
"UTCTimestamp" ByteString -> Either String UTCTimestamp
parseUTCTimestamp
instance FromField ZonedTimestamp where
fromField :: FieldParser ZonedTimestamp
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestamptzOid String
"ZonedTimestamp" ByteString -> Either String ZonedTimestamp
parseZonedTimestamp
instance FromField LocalTimestamp where
fromField :: FieldParser LocalTimestamp
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.timestampOid String
"LocalTimestamp" ByteString -> Either String LocalTimestamp
parseLocalTimestamp
instance FromField Date where
fromField :: FieldParser Date
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.dateOid String
"Date" ByteString -> Either String Date
parseDate
instance FromField CalendarDiffTime where
fromField :: FieldParser CalendarDiffTime
fromField = forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
TI.intervalOid String
"CalendarDiffTime" ByteString -> Either String CalendarDiffTime
parseCalendarDiffTime
ff :: PQ.Oid -> String -> (B8.ByteString -> Either String a)
-> Field -> Maybe B8.ByteString -> Conversion a
ff :: forall a.
Oid
-> String
-> (ByteString -> Either String a)
-> Field
-> Maybe ByteString
-> Conversion a
ff Oid
compatOid String
hsType ByteString -> Either String a
parseBS Field
f Maybe ByteString
mstr =
if Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
compatOid
then forall {a} {t} {b}.
Exception a =>
(String -> Maybe Oid -> String -> String -> t -> a)
-> t -> Conversion b
err String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible String
""
else case Maybe ByteString
mstr of
Maybe ByteString
Nothing -> forall {a} {t} {b}.
Exception a =>
(String -> Maybe Oid -> String -> String -> t -> a)
-> t -> Conversion b
err String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull String
""
Just ByteString
str -> case ByteString -> Either String a
parseBS ByteString
str of
Left String
msg -> forall {a} {t} {b}.
Exception a =>
(String -> Maybe Oid -> String -> String -> t -> a)
-> t -> Conversion b
err String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed String
msg
Right a
val -> forall (m :: * -> *) a. Monad m => a -> m a
return a
val
where
err :: (String -> Maybe Oid -> String -> String -> t -> a)
-> t -> Conversion b
err String -> Maybe Oid -> String -> String -> t -> a
errC t
msg = do
ByteString
typnam <- Field -> Conversion ByteString
typename Field
f
forall a b. Exception a => a -> Conversion b
left forall a b. (a -> b) -> a -> b
$ String -> Maybe Oid -> String -> String -> t -> a
errC (ByteString -> String
B8.unpack ByteString
typnam)
(Field -> Maybe Oid
tableOid Field
f)
(forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ByteString -> String
B8.unpack (Field -> Maybe ByteString
name Field
f))
String
hsType
t
msg
{-# INLINE ff #-}
instance (FromField a, FromField b) => FromField (Either a b) where
fromField :: FieldParser (Either a b)
fromField Field
f Maybe ByteString
dat = (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
dat)
instance (FromField a, Typeable a) => FromField (PGArray a) where
fromField :: FieldParser (PGArray a)
fromField = forall a. Typeable a => FieldParser a -> FieldParser (PGArray a)
pgArrayFieldParser forall a. FromField a => FieldParser a
fromField
pgArrayFieldParser :: Typeable a => FieldParser a -> FieldParser (PGArray a)
pgArrayFieldParser :: forall a. Typeable a => FieldParser a -> FieldParser (PGArray a)
pgArrayFieldParser FieldParser a
fieldParser Field
f Maybe ByteString
mdat = do
TypeInfo
info <- Field -> Conversion TypeInfo
typeInfo Field
f
case TypeInfo
info of
TI.Array{} ->
case Maybe ByteString
mdat of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
dat -> do
case forall a. Parser a -> ByteString -> Either String a
parseOnly (forall a.
FieldParser a -> TypeInfo -> Field -> Parser (Conversion [a])
fromArray FieldParser a
fieldParser TypeInfo
info Field
f) ByteString
dat of
Left String
err -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
err
Right Conversion [a]
conv -> forall a. [a] -> PGArray a
PGArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Conversion [a]
conv
TypeInfo
_ -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
fromArray :: FieldParser a -> TypeInfo -> Field -> Parser (Conversion [a])
fromArray :: forall a.
FieldParser a -> TypeInfo -> Field -> Parser (Conversion [a])
fromArray FieldParser a
fieldParser TypeInfo
typInfo Field
f = forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ArrayFormat -> Conversion a
parseIt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> Parser [ArrayFormat]
array Char
delim
where
delim :: Char
delim = TypeInfo -> Char
typdelim (TypeInfo -> TypeInfo
typelem TypeInfo
typInfo)
fElem :: Field
fElem = Field
f{ typeOid :: Oid
typeOid = TypeInfo -> Oid
typoid (TypeInfo -> TypeInfo
typelem TypeInfo
typInfo) }
parseIt :: ArrayFormat -> Conversion a
parseIt ArrayFormat
item =
FieldParser a
fieldParser Field
f' forall a b. (a -> b) -> a -> b
$ if ArrayFormat
item forall a. Eq a => a -> a -> Bool
== ByteString -> ArrayFormat
Arrays.Plain ByteString
"NULL" then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just ByteString
item'
where
item' :: ByteString
item' = Char -> ArrayFormat -> ByteString
fmt Char
delim ArrayFormat
item
f' :: Field
f' | Arrays.Array [ArrayFormat]
_ <- ArrayFormat
item = Field
f
| Bool
otherwise = Field
fElem
instance (FromField a, Typeable a) => FromField (Vector a) where
fromField :: FieldParser (Vector a)
fromField Field
f Maybe ByteString
v = forall a. [a] -> Vector a
V.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. PGArray a -> [a]
fromPGArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
v
instance (FromField a, Typeable a) => FromField (IOVector a) where
fromField :: FieldParser (IOVector a)
fromField Field
f Maybe ByteString
v = forall a. IO a -> Conversion a
liftConversion forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
PrimMonad m =>
Vector a -> m (MVector (PrimState m) a)
V.unsafeThaw forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
v
instance FromField UUID where
fromField :: FieldParser UUID
fromField Field
f Maybe ByteString
mbs =
if Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
TI.uuidOid
then forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
else case Maybe ByteString
mbs of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
bs ->
case ByteString -> Maybe UUID
UUID.fromASCIIBytes ByteString
bs of
Maybe UUID
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
"Invalid UUID"
Just UUID
uuid -> forall (f :: * -> *) a. Applicative f => a -> f a
pure UUID
uuid
instance FromField JSON.Value where
fromField :: FieldParser Value
fromField Field
f Maybe ByteString
mbs = ByteString -> Conversion Value
parseBS forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FieldParser ByteString
fromFieldJSONByteString Field
f Maybe ByteString
mbs
where parseBS :: ByteString -> Conversion Value
parseBS ByteString
bs = case forall a. Parser a -> ByteString -> Either String a
parseOnly (Parser ByteString Value
JSON.value' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall t. Chunk t => Parser t ()
endOfInput) ByteString
bs of
Left String
err -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
err
Right Value
val -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString
fromFieldJSONByteString :: FieldParser ByteString
fromFieldJSONByteString Field
f Maybe ByteString
mbs =
if Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
TI.jsonOid Bool -> Bool -> Bool
&& Field -> Oid
typeOid Field
f forall a. Eq a => a -> a -> Bool
/= Oid
TI.jsonbOid
then forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
""
else case Maybe ByteString
mbs of
Maybe ByteString
Nothing -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
Just ByteString
bs -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bs
fromJSONField :: (JSON.FromJSON a, Typeable a) => FieldParser a
fromJSONField :: forall a. (FromJSON a, Typeable a) => FieldParser a
fromJSONField Field
f Maybe ByteString
mbBs = do
Value
value <- forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
mbBs
case forall a. FromJSON a => Value -> IResult a
JSON.ifromJSON Value
value of
JSON.IError JSONPath
path String
err -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f forall a b. (a -> b) -> a -> b
$
String
"JSON decoding error: " forall a. [a] -> [a] -> [a]
++ (JSONPath -> ShowS
JSON.formatError JSONPath
path String
err)
JSON.ISuccess a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
instance FromField a => FromField (IORef a) where
fromField :: FieldParser (IORef a)
fromField Field
f Maybe ByteString
v = forall a. IO a -> Conversion a
liftConversion forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (IORef a)
newIORef forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
v
instance FromField a => FromField (MVar a) where
fromField :: FieldParser (MVar a)
fromField Field
f Maybe ByteString
v = forall a. IO a -> Conversion a
liftConversion forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> IO (MVar a)
newMVar forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. FromField a => FieldParser a
fromField Field
f Maybe ByteString
v
type Compat = PQ.Oid -> Bool
okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
okText :: Oid -> Bool
okText = Oid -> Oid -> Bool
eq Oid
TI.nameOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.textOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.charOid
(Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.bpcharOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.varcharOid
okText' :: Oid -> Bool
okText' = Oid -> Oid -> Bool
eq Oid
TI.nameOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.textOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.charOid
(Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.bpcharOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.varcharOid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.unknownOid
okBinary :: Oid -> Bool
okBinary = Oid -> Oid -> Bool
eq Oid
TI.byteaOid
ok16 :: Oid -> Bool
ok16 = Oid -> Oid -> Bool
eq Oid
TI.int2Oid
ok32 :: Oid -> Bool
ok32 = Oid -> Oid -> Bool
eq Oid
TI.int2Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int4Oid
ok64 :: Oid -> Bool
ok64 = Oid -> Oid -> Bool
eq Oid
TI.int2Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int4Oid (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Oid -> Bool
eq Oid
TI.int8Oid
#if WORD_SIZE_IN_BITS < 64
okInt = ok32
#else
okInt :: Oid -> Bool
okInt = Oid -> Bool
ok64
#endif
eq :: PQ.Oid -> PQ.Oid -> Bool
eq :: Oid -> Oid -> Bool
eq = forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE eq #-}
infixr 2 \/
(\/) :: (PQ.Oid -> Bool)
-> (PQ.Oid -> Bool)
-> (PQ.Oid -> Bool)
Oid -> Bool
f \/ :: (Oid -> Bool) -> (Oid -> Bool) -> Oid -> Bool
\/ Oid -> Bool
g = \Oid
x -> Oid -> Bool
f Oid
x Bool -> Bool -> Bool
|| Oid -> Bool
g Oid
x
{-# INLINE (\/) #-}
doFromField :: forall a . (Typeable a)
=> Field -> Compat -> (ByteString -> Conversion a)
-> Maybe ByteString -> Conversion a
doFromField :: forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
isCompat ByteString -> Conversion a
cvt (Just ByteString
bs)
| Oid -> Bool
isCompat (Field -> Oid
typeOid Field
f) = ByteString -> Conversion a
cvt ByteString
bs
| Bool
otherwise = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Incompatible Field
f String
"types incompatible"
doFromField Field
f Oid -> Bool
_ ByteString -> Conversion a
_ Maybe ByteString
_ = forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
UnexpectedNull Field
f String
""
returnError :: forall a err . (Typeable a, Exception err)
=> (String -> Maybe PQ.Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError :: forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> err
mkErr Field
f String
msg = do
ByteString
typnam <- Field -> Conversion ByteString
typename Field
f
forall a b. Exception a => a -> Conversion b
left forall a b. (a -> b) -> a -> b
$ String -> Maybe Oid -> String -> String -> String -> err
mkErr (ByteString -> String
B.unpack ByteString
typnam)
(Field -> Maybe Oid
tableOid Field
f)
(forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ByteString -> String
B.unpack (Field -> Maybe ByteString
name Field
f))
(forall a. Show a => a -> String
show (forall a. Typeable a => a -> TypeRep
typeOf (forall a. HasCallStack => a
undefined :: a)))
String
msg
attoFieldParser :: forall a. (Typeable a)
=> (PQ.Oid -> Bool)
-> Parser a
-> FieldParser a
attoFieldParser :: forall a. Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
attoFieldParser Oid -> Bool
types Parser a
p0 Field
f Maybe ByteString
dat = forall a.
Typeable a =>
Field
-> (Oid -> Bool)
-> (ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion a
doFromField Field
f Oid -> Bool
types (Parser a -> ByteString -> Conversion a
go Parser a
p0) Maybe ByteString
dat
where
go :: Parser a -> ByteString -> Conversion a
go :: Parser a -> ByteString -> Conversion a
go Parser a
p ByteString
s =
case forall a. Parser a -> ByteString -> Either String a
parseOnly Parser a
p ByteString
s of
Left String
err -> forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
returnError String -> Maybe Oid -> String -> String -> String -> ResultError
ConversionFailed Field
f String
err
Right a
v -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v