{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

module Database.Persist.Postgresql.Internal
    ( P(..)
    , PgInterval(..)
    , getGetter
    ) where

import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.FromField as PGFF
import qualified Database.PostgreSQL.Simple.Internal as PG
import qualified Database.PostgreSQL.Simple.ToField as PGTF
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as PS
import qualified Database.PostgreSQL.Simple.Types as PG

import qualified Blaze.ByteString.Builder.Char8 as BBB
import qualified Data.Attoparsec.ByteString.Char8 as P
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Char8 as B8
import Data.Char (ord)
import Data.Data (Typeable)
import Data.Fixed (Fixed(..), Pico)
import Data.Int (Int64)
import qualified Data.IntMap as I
import Data.Maybe (fromMaybe)
import Data.String.Conversions.Monomorphic (toStrictByteString)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Time (NominalDiffTime, localTimeToUTC, utc)

import Database.Persist.Sql

-- | Newtype used to avoid orphan instances for @postgresql-simple@ classes.
--
-- @since 2.13.2.0
newtype P = P { P -> PersistValue
unP :: PersistValue }

instance PGTF.ToField P where
    toField :: P -> Action
toField (P (PersistText Text
t))        = Text -> Action
forall a. ToField a => a -> Action
PGTF.toField Text
t
    toField (P (PersistByteString ByteString
bs)) = Binary ByteString -> Action
forall a. ToField a => a -> Action
PGTF.toField (ByteString -> Binary ByteString
forall a. a -> Binary a
PG.Binary ByteString
bs)
    toField (P (PersistInt64 Int64
i))       = Int64 -> Action
forall a. ToField a => a -> Action
PGTF.toField Int64
i
    toField (P (PersistDouble Double
d))      = Double -> Action
forall a. ToField a => a -> Action
PGTF.toField Double
d
    toField (P (PersistRational Rational
r))    = Builder -> Action
PGTF.Plain (Builder -> Action) -> Builder -> Action
forall a b. (a -> b) -> a -> b
$
                                         String -> Builder
BBB.fromString (String -> Builder) -> String -> Builder
forall a b. (a -> b) -> a -> b
$
                                         Pico -> String
forall a. Show a => a -> String
show (Rational -> Pico
forall a. Fractional a => Rational -> a
fromRational Rational
r :: Pico) --  FIXME: Too Ambigous, can not select precision without information about field
    toField (P (PersistBool Bool
b))        = Bool -> Action
forall a. ToField a => a -> Action
PGTF.toField Bool
b
    toField (P (PersistDay Day
d))         = Day -> Action
forall a. ToField a => a -> Action
PGTF.toField Day
d
    toField (P (PersistTimeOfDay TimeOfDay
t))   = TimeOfDay -> Action
forall a. ToField a => a -> Action
PGTF.toField TimeOfDay
t
    toField (P (PersistUTCTime UTCTime
t))     = UTCTime -> Action
forall a. ToField a => a -> Action
PGTF.toField UTCTime
t
    toField (P PersistValue
PersistNull)            = Null -> Action
forall a. ToField a => a -> Action
PGTF.toField Null
PG.Null
    toField (P (PersistList [PersistValue]
l))        = Text -> Action
forall a. ToField a => a -> Action
PGTF.toField (Text -> Action) -> Text -> Action
forall a b. (a -> b) -> a -> b
$ [PersistValue] -> Text
listToJSON [PersistValue]
l
    toField (P (PersistMap [(Text, PersistValue)]
m))         = Text -> Action
forall a. ToField a => a -> Action
PGTF.toField (Text -> Action) -> Text -> Action
forall a b. (a -> b) -> a -> b
$ [(Text, PersistValue)] -> Text
mapToJSON [(Text, PersistValue)]
m
    toField (P (PersistLiteral_ LiteralType
DbSpecific ByteString
s))  = Unknown -> Action
forall a. ToField a => a -> Action
PGTF.toField (ByteString -> Unknown
Unknown ByteString
s)
    toField (P (PersistLiteral_ LiteralType
Unescaped ByteString
l))     = UnknownLiteral -> Action
forall a. ToField a => a -> Action
PGTF.toField (ByteString -> UnknownLiteral
UnknownLiteral ByteString
l)
    toField (P (PersistLiteral_ LiteralType
Escaped ByteString
e)) = Unknown -> Action
forall a. ToField a => a -> Action
PGTF.toField (ByteString -> Unknown
Unknown ByteString
e)
    toField (P (PersistArray [PersistValue]
a))       = PGArray P -> Action
forall a. ToField a => a -> Action
PGTF.toField (PGArray P -> Action) -> PGArray P -> Action
forall a b. (a -> b) -> a -> b
$ [P] -> PGArray P
forall a. [a] -> PGArray a
PG.PGArray ([P] -> PGArray P) -> [P] -> PGArray P
forall a b. (a -> b) -> a -> b
$ PersistValue -> P
P (PersistValue -> P) -> [PersistValue] -> [P]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [PersistValue]
a
    toField (P (PersistObjectId ByteString
_))    =
        String -> Action
forall a. HasCallStack => String -> a
error String
"Refusing to serialize a PersistObjectId to a PostgreSQL value"

instance PGFF.FromField P where
    fromField :: FieldParser P
fromField Field
field Maybe ByteString
mdata = (PersistValue -> P) -> Conversion PersistValue -> Conversion P
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PersistValue -> P
P (Conversion PersistValue -> Conversion P)
-> Conversion PersistValue -> Conversion P
forall a b. (a -> b) -> a -> b
$ case Maybe ByteString
mdata of
      -- If we try to simply decode based on oid, we will hit unexpected null
      -- errors.
      Maybe ByteString
Nothing -> PersistValue -> Conversion PersistValue
forall (f :: * -> *) a. Applicative f => a -> f a
pure PersistValue
PersistNull
      Maybe ByteString
data' -> Oid -> Getter PersistValue
getGetter (Field -> Oid
PGFF.typeOid Field
field) Field
field Maybe ByteString
data'

newtype Unknown = Unknown { Unknown -> ByteString
unUnknown :: ByteString }
  deriving (Unknown -> Unknown -> Bool
(Unknown -> Unknown -> Bool)
-> (Unknown -> Unknown -> Bool) -> Eq Unknown
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Unknown -> Unknown -> Bool
$c/= :: Unknown -> Unknown -> Bool
== :: Unknown -> Unknown -> Bool
$c== :: Unknown -> Unknown -> Bool
Eq, Int -> Unknown -> ShowS
[Unknown] -> ShowS
Unknown -> String
(Int -> Unknown -> ShowS)
-> (Unknown -> String) -> ([Unknown] -> ShowS) -> Show Unknown
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Unknown] -> ShowS
$cshowList :: [Unknown] -> ShowS
show :: Unknown -> String
$cshow :: Unknown -> String
showsPrec :: Int -> Unknown -> ShowS
$cshowsPrec :: Int -> Unknown -> ShowS
Show, ReadPrec [Unknown]
ReadPrec Unknown
Int -> ReadS Unknown
ReadS [Unknown]
(Int -> ReadS Unknown)
-> ReadS [Unknown]
-> ReadPrec Unknown
-> ReadPrec [Unknown]
-> Read Unknown
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Unknown]
$creadListPrec :: ReadPrec [Unknown]
readPrec :: ReadPrec Unknown
$creadPrec :: ReadPrec Unknown
readList :: ReadS [Unknown]
$creadList :: ReadS [Unknown]
readsPrec :: Int -> ReadS Unknown
$creadsPrec :: Int -> ReadS Unknown
Read, Eq Unknown
Eq Unknown
-> (Unknown -> Unknown -> Ordering)
-> (Unknown -> Unknown -> Bool)
-> (Unknown -> Unknown -> Bool)
-> (Unknown -> Unknown -> Bool)
-> (Unknown -> Unknown -> Bool)
-> (Unknown -> Unknown -> Unknown)
-> (Unknown -> Unknown -> Unknown)
-> Ord Unknown
Unknown -> Unknown -> Bool
Unknown -> Unknown -> Ordering
Unknown -> Unknown -> Unknown
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Unknown -> Unknown -> Unknown
$cmin :: Unknown -> Unknown -> Unknown
max :: Unknown -> Unknown -> Unknown
$cmax :: Unknown -> Unknown -> Unknown
>= :: Unknown -> Unknown -> Bool
$c>= :: Unknown -> Unknown -> Bool
> :: Unknown -> Unknown -> Bool
$c> :: Unknown -> Unknown -> Bool
<= :: Unknown -> Unknown -> Bool
$c<= :: Unknown -> Unknown -> Bool
< :: Unknown -> Unknown -> Bool
$c< :: Unknown -> Unknown -> Bool
compare :: Unknown -> Unknown -> Ordering
$ccompare :: Unknown -> Unknown -> Ordering
$cp1Ord :: Eq Unknown
Ord)

instance PGFF.FromField Unknown where
    fromField :: FieldParser Unknown
fromField Field
f Maybe ByteString
mdata =
      case Maybe ByteString
mdata of
        Maybe ByteString
Nothing  -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion Unknown
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
PGFF.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
PGFF.UnexpectedNull Field
f String
"Database.Persist.Postgresql/PGFF.FromField Unknown"
        Just ByteString
dat -> Unknown -> Conversion Unknown
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Unknown
Unknown ByteString
dat)

instance PGTF.ToField Unknown where
    toField :: Unknown -> Action
toField (Unknown ByteString
a) = ByteString -> Action
PGTF.Escape ByteString
a

newtype UnknownLiteral = UnknownLiteral { UnknownLiteral -> ByteString
unUnknownLiteral :: ByteString }
  deriving (UnknownLiteral -> UnknownLiteral -> Bool
(UnknownLiteral -> UnknownLiteral -> Bool)
-> (UnknownLiteral -> UnknownLiteral -> Bool) -> Eq UnknownLiteral
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UnknownLiteral -> UnknownLiteral -> Bool
$c/= :: UnknownLiteral -> UnknownLiteral -> Bool
== :: UnknownLiteral -> UnknownLiteral -> Bool
$c== :: UnknownLiteral -> UnknownLiteral -> Bool
Eq, Int -> UnknownLiteral -> ShowS
[UnknownLiteral] -> ShowS
UnknownLiteral -> String
(Int -> UnknownLiteral -> ShowS)
-> (UnknownLiteral -> String)
-> ([UnknownLiteral] -> ShowS)
-> Show UnknownLiteral
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UnknownLiteral] -> ShowS
$cshowList :: [UnknownLiteral] -> ShowS
show :: UnknownLiteral -> String
$cshow :: UnknownLiteral -> String
showsPrec :: Int -> UnknownLiteral -> ShowS
$cshowsPrec :: Int -> UnknownLiteral -> ShowS
Show, ReadPrec [UnknownLiteral]
ReadPrec UnknownLiteral
Int -> ReadS UnknownLiteral
ReadS [UnknownLiteral]
(Int -> ReadS UnknownLiteral)
-> ReadS [UnknownLiteral]
-> ReadPrec UnknownLiteral
-> ReadPrec [UnknownLiteral]
-> Read UnknownLiteral
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [UnknownLiteral]
$creadListPrec :: ReadPrec [UnknownLiteral]
readPrec :: ReadPrec UnknownLiteral
$creadPrec :: ReadPrec UnknownLiteral
readList :: ReadS [UnknownLiteral]
$creadList :: ReadS [UnknownLiteral]
readsPrec :: Int -> ReadS UnknownLiteral
$creadsPrec :: Int -> ReadS UnknownLiteral
Read, Eq UnknownLiteral
Eq UnknownLiteral
-> (UnknownLiteral -> UnknownLiteral -> Ordering)
-> (UnknownLiteral -> UnknownLiteral -> Bool)
-> (UnknownLiteral -> UnknownLiteral -> Bool)
-> (UnknownLiteral -> UnknownLiteral -> Bool)
-> (UnknownLiteral -> UnknownLiteral -> Bool)
-> (UnknownLiteral -> UnknownLiteral -> UnknownLiteral)
-> (UnknownLiteral -> UnknownLiteral -> UnknownLiteral)
-> Ord UnknownLiteral
UnknownLiteral -> UnknownLiteral -> Bool
UnknownLiteral -> UnknownLiteral -> Ordering
UnknownLiteral -> UnknownLiteral -> UnknownLiteral
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: UnknownLiteral -> UnknownLiteral -> UnknownLiteral
$cmin :: UnknownLiteral -> UnknownLiteral -> UnknownLiteral
max :: UnknownLiteral -> UnknownLiteral -> UnknownLiteral
$cmax :: UnknownLiteral -> UnknownLiteral -> UnknownLiteral
>= :: UnknownLiteral -> UnknownLiteral -> Bool
$c>= :: UnknownLiteral -> UnknownLiteral -> Bool
> :: UnknownLiteral -> UnknownLiteral -> Bool
$c> :: UnknownLiteral -> UnknownLiteral -> Bool
<= :: UnknownLiteral -> UnknownLiteral -> Bool
$c<= :: UnknownLiteral -> UnknownLiteral -> Bool
< :: UnknownLiteral -> UnknownLiteral -> Bool
$c< :: UnknownLiteral -> UnknownLiteral -> Bool
compare :: UnknownLiteral -> UnknownLiteral -> Ordering
$ccompare :: UnknownLiteral -> UnknownLiteral -> Ordering
$cp1Ord :: Eq UnknownLiteral
Ord, Typeable)

instance PGFF.FromField UnknownLiteral where
    fromField :: FieldParser UnknownLiteral
fromField Field
f Maybe ByteString
mdata =
      case Maybe ByteString
mdata of
        Maybe ByteString
Nothing  -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion UnknownLiteral
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
PGFF.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
PGFF.UnexpectedNull Field
f String
"Database.Persist.Postgresql/PGFF.FromField UnknownLiteral"
        Just ByteString
dat -> UnknownLiteral -> Conversion UnknownLiteral
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> UnknownLiteral
UnknownLiteral ByteString
dat)

instance PGTF.ToField UnknownLiteral where
    toField :: UnknownLiteral -> Action
toField (UnknownLiteral ByteString
a) = Builder -> Action
PGTF.Plain (Builder -> Action) -> Builder -> Action
forall a b. (a -> b) -> a -> b
$ ByteString -> Builder
BB.byteString ByteString
a

type Getter a = PGFF.FieldParser a

convertPV :: PGFF.FromField a => (a -> b) -> Getter b
convertPV :: (a -> b) -> Getter b
convertPV a -> b
f = ((a -> b) -> Conversion a -> Conversion b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Conversion a -> Conversion b)
-> (Maybe ByteString -> Conversion a)
-> Maybe ByteString
-> Conversion b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Maybe ByteString -> Conversion a)
 -> Maybe ByteString -> Conversion b)
-> (Field -> Maybe ByteString -> Conversion a) -> Getter b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Maybe ByteString -> Conversion a
forall a. FromField a => FieldParser a
PGFF.fromField

builtinGetters :: I.IntMap (Getter PersistValue)
builtinGetters :: IntMap (Getter PersistValue)
builtinGetters = [(Int, Getter PersistValue)] -> IntMap (Getter PersistValue)
forall a. [(Int, a)] -> IntMap a
I.fromList
    [ (TypeInfo -> Int
k TypeInfo
PS.bool,        (Bool -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Bool -> PersistValue
PersistBool)
    , (TypeInfo -> Int
k TypeInfo
PS.bytea,       (Binary ByteString -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Binary ByteString -> ByteString)
-> Binary ByteString
-> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Binary ByteString -> ByteString
forall a. Binary a -> a
unBinary))
    , (TypeInfo -> Int
k TypeInfo
PS.char,        (Text -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Text -> PersistValue
PersistText)
    , (TypeInfo -> Int
k TypeInfo
PS.name,        (Text -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Text -> PersistValue
PersistText)
    , (TypeInfo -> Int
k TypeInfo
PS.int8,        (Int64 -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Int64 -> PersistValue
PersistInt64)
    , (TypeInfo -> Int
k TypeInfo
PS.int2,        (Int64 -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Int64 -> PersistValue
PersistInt64)
    , (TypeInfo -> Int
k TypeInfo
PS.int4,        (Int64 -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Int64 -> PersistValue
PersistInt64)
    , (TypeInfo -> Int
k TypeInfo
PS.text,        (Text -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Text -> PersistValue
PersistText)
    , (TypeInfo -> Int
k TypeInfo
PS.xml,         (Unknown -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (TypeInfo -> Int
k TypeInfo
PS.float4,      (Double -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Double -> PersistValue
PersistDouble)
    , (TypeInfo -> Int
k TypeInfo
PS.float8,      (Double -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Double -> PersistValue
PersistDouble)
    , (TypeInfo -> Int
k TypeInfo
PS.money,       (Rational -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Rational -> PersistValue
PersistRational)
    , (TypeInfo -> Int
k TypeInfo
PS.bpchar,      (Text -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Text -> PersistValue
PersistText)
    , (TypeInfo -> Int
k TypeInfo
PS.varchar,     (Text -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Text -> PersistValue
PersistText)
    , (TypeInfo -> Int
k TypeInfo
PS.date,        (Day -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Day -> PersistValue
PersistDay)
    , (TypeInfo -> Int
k TypeInfo
PS.time,        (TimeOfDay -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV TimeOfDay -> PersistValue
PersistTimeOfDay)
    , (TypeInfo -> Int
k TypeInfo
PS.timestamp,   (LocalTime -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (UTCTime -> PersistValue
PersistUTCTime(UTCTime -> PersistValue)
-> (LocalTime -> UTCTime) -> LocalTime -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeZone -> LocalTime -> UTCTime
localTimeToUTC TimeZone
utc))
    , (TypeInfo -> Int
k TypeInfo
PS.timestamptz, (UTCTime -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV UTCTime -> PersistValue
PersistUTCTime)
    , (TypeInfo -> Int
k TypeInfo
PS.interval,    (PgInterval -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistLiteralEscaped (ByteString -> PersistValue)
-> (PgInterval -> ByteString) -> PgInterval -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgInterval -> ByteString
pgIntervalToBs))
    , (TypeInfo -> Int
k TypeInfo
PS.bit,         (Int64 -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Int64 -> PersistValue
PersistInt64)
    , (TypeInfo -> Int
k TypeInfo
PS.varbit,      (Int64 -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Int64 -> PersistValue
PersistInt64)
    , (TypeInfo -> Int
k TypeInfo
PS.numeric,     (Rational -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV Rational -> PersistValue
PersistRational)
    , (TypeInfo -> Int
k TypeInfo
PS.void,        \Field
_ Maybe ByteString
_ -> PersistValue -> Conversion PersistValue
forall (m :: * -> *) a. Monad m => a -> m a
return PersistValue
PersistNull)
    , (TypeInfo -> Int
k TypeInfo
PS.json,        (Unknown -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (TypeInfo -> Int
k TypeInfo
PS.jsonb,       (Unknown -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (TypeInfo -> Int
k TypeInfo
PS.unknown,     (Unknown -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))

    -- Array types: same order as above.
    -- The OIDs were taken from pg_type.
    , (Int
1000,             (Bool -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Bool -> PersistValue
PersistBool)
    , (Int
1001,             (Binary ByteString -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Binary ByteString -> ByteString)
-> Binary ByteString
-> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Binary ByteString -> ByteString
forall a. Binary a -> a
unBinary))
    , (Int
1002,             (Text -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Text -> PersistValue
PersistText)
    , (Int
1003,             (Text -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Text -> PersistValue
PersistText)
    , (Int
1016,             (Int64 -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Int64 -> PersistValue
PersistInt64)
    , (Int
1005,             (Int64 -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Int64 -> PersistValue
PersistInt64)
    , (Int
1007,             (Int64 -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Int64 -> PersistValue
PersistInt64)
    , (Int
1009,             (Text -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Text -> PersistValue
PersistText)
    , (Int
143,              (Unknown -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (Int
1021,             (Double -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Double -> PersistValue
PersistDouble)
    , (Int
1022,             (Double -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Double -> PersistValue
PersistDouble)
    , (Int
1023,             (UTCTime -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf UTCTime -> PersistValue
PersistUTCTime)
    , (Int
1024,             (UTCTime -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf UTCTime -> PersistValue
PersistUTCTime)
    , (Int
791,              (Rational -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Rational -> PersistValue
PersistRational)
    , (Int
1014,             (Text -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Text -> PersistValue
PersistText)
    , (Int
1015,             (Text -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Text -> PersistValue
PersistText)
    , (Int
1182,             (Day -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Day -> PersistValue
PersistDay)
    , (Int
1183,             (TimeOfDay -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf TimeOfDay -> PersistValue
PersistTimeOfDay)
    , (Int
1115,             (UTCTime -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf UTCTime -> PersistValue
PersistUTCTime)
    , (Int
1185,             (UTCTime -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf UTCTime -> PersistValue
PersistUTCTime)
    , (Int
1187,             (PgInterval -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistLiteralEscaped (ByteString -> PersistValue)
-> (PgInterval -> ByteString) -> PgInterval -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgInterval -> ByteString
pgIntervalToBs))
    , (Int
1561,             (Int64 -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Int64 -> PersistValue
PersistInt64)
    , (Int
1563,             (Int64 -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Int64 -> PersistValue
PersistInt64)
    , (Int
1231,             (Rational -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf Rational -> PersistValue
PersistRational)
    -- no array(void) type
    , (Int
2951,             (Unknown -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistLiteralEscaped (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (Int
199,              (Unknown -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    , (Int
3807,             (Unknown -> PersistValue) -> Getter PersistValue
forall a.
(FromField a, Typeable a) =>
(a -> PersistValue) -> Getter PersistValue
listOf (ByteString -> PersistValue
PersistByteString (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown))
    -- no array(unknown) either
    ]
    where
        k :: TypeInfo -> Int
k (TypeInfo -> Oid
PGFF.typoid -> Oid
i) = Oid -> Int
PG.oid2int Oid
i
        -- A @listOf f@ will use a @PGArray (Maybe T)@ to convert
        -- the values to Haskell-land.  The @Maybe@ is important
        -- because the usual way of checking NULLs
        -- (c.f. withStmt') won't check for NULL inside
        -- arrays---or any other compound structure for that matter.
        listOf :: (a -> PersistValue) -> Getter PersistValue
listOf a -> PersistValue
f = (PGArray (Maybe a) -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV ([PersistValue] -> PersistValue
PersistList ([PersistValue] -> PersistValue)
-> (PGArray (Maybe a) -> [PersistValue])
-> PGArray (Maybe a)
-> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> PersistValue) -> [Maybe a] -> [PersistValue]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> PersistValue) -> Maybe a -> PersistValue
forall a. (a -> PersistValue) -> Maybe a -> PersistValue
nullable a -> PersistValue
f) ([Maybe a] -> [PersistValue])
-> (PGArray (Maybe a) -> [Maybe a])
-> PGArray (Maybe a)
-> [PersistValue]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGArray (Maybe a) -> [Maybe a]
forall a. PGArray a -> [a]
PG.fromPGArray)
          where nullable :: (a -> PersistValue) -> Maybe a -> PersistValue
nullable = PersistValue -> (a -> PersistValue) -> Maybe a -> PersistValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PersistValue
PersistNull

-- | Get the field parser corresponding to the given 'PG.Oid'.
--
-- For example, pass in the 'PG.Oid' of 'PS.bool', and you will get back a
-- field parser which parses boolean values in the table into 'PersistBool's.
--
-- @since 2.13.2.0
getGetter :: PG.Oid -> Getter PersistValue
getGetter :: Oid -> Getter PersistValue
getGetter Oid
oid
  = Getter PersistValue
-> Maybe (Getter PersistValue) -> Getter PersistValue
forall a. a -> Maybe a -> a
fromMaybe Getter PersistValue
defaultGetter (Maybe (Getter PersistValue) -> Getter PersistValue)
-> Maybe (Getter PersistValue) -> Getter PersistValue
forall a b. (a -> b) -> a -> b
$ Int -> IntMap (Getter PersistValue) -> Maybe (Getter PersistValue)
forall a. Int -> IntMap a -> Maybe a
I.lookup (Oid -> Int
PG.oid2int Oid
oid) IntMap (Getter PersistValue)
builtinGetters
  where defaultGetter :: Getter PersistValue
defaultGetter = (Unknown -> PersistValue) -> Getter PersistValue
forall a b. FromField a => (a -> b) -> Getter b
convertPV (ByteString -> PersistValue
PersistLiteralEscaped (ByteString -> PersistValue)
-> (Unknown -> ByteString) -> Unknown -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unknown -> ByteString
unUnknown)

unBinary :: PG.Binary a -> a
unBinary :: Binary a -> a
unBinary (PG.Binary a
x) = a
x

-- | Represent Postgres interval using NominalDiffTime
--
-- @since 2.11.0.0
newtype PgInterval = PgInterval { PgInterval -> NominalDiffTime
getPgInterval :: NominalDiffTime }
  deriving (PgInterval -> PgInterval -> Bool
(PgInterval -> PgInterval -> Bool)
-> (PgInterval -> PgInterval -> Bool) -> Eq PgInterval
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgInterval -> PgInterval -> Bool
$c/= :: PgInterval -> PgInterval -> Bool
== :: PgInterval -> PgInterval -> Bool
$c== :: PgInterval -> PgInterval -> Bool
Eq, Int -> PgInterval -> ShowS
[PgInterval] -> ShowS
PgInterval -> String
(Int -> PgInterval -> ShowS)
-> (PgInterval -> String)
-> ([PgInterval] -> ShowS)
-> Show PgInterval
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgInterval] -> ShowS
$cshowList :: [PgInterval] -> ShowS
show :: PgInterval -> String
$cshow :: PgInterval -> String
showsPrec :: Int -> PgInterval -> ShowS
$cshowsPrec :: Int -> PgInterval -> ShowS
Show)

pgIntervalToBs :: PgInterval -> ByteString
pgIntervalToBs :: PgInterval -> ByteString
pgIntervalToBs = String -> ByteString
forall a. ConvertibleStrings a ByteString => a -> ByteString
toStrictByteString (String -> ByteString)
-> (PgInterval -> String) -> PgInterval -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NominalDiffTime -> String
forall a. Show a => a -> String
show (NominalDiffTime -> String)
-> (PgInterval -> NominalDiffTime) -> PgInterval -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgInterval -> NominalDiffTime
getPgInterval

instance PGTF.ToField PgInterval where
    toField :: PgInterval -> Action
toField (PgInterval NominalDiffTime
t) = NominalDiffTime -> Action
forall a. ToField a => a -> Action
PGTF.toField NominalDiffTime
t

instance PGFF.FromField PgInterval where
    fromField :: FieldParser PgInterval
fromField Field
f Maybe ByteString
mdata =
      if Field -> Oid
PGFF.typeOid Field
f Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
PS.typoid TypeInfo
PS.interval
        then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgInterval
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
PGFF.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
PGFF.Incompatible Field
f String
""
        else case Maybe ByteString
mdata of
          Maybe ByteString
Nothing  -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgInterval
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
PGFF.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
PGFF.UnexpectedNull Field
f String
""
          Just ByteString
dat -> case Parser NominalDiffTime
-> ByteString -> Either String NominalDiffTime
forall a. Parser a -> ByteString -> Either String a
P.parseOnly (Parser NominalDiffTime
nominalDiffTime Parser NominalDiffTime
-> Parser ByteString () -> Parser NominalDiffTime
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ByteString ()
forall t. Chunk t => Parser t ()
P.endOfInput) ByteString
dat of
            Left String
msg  ->  (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgInterval
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
PGFF.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
PGFF.ConversionFailed Field
f String
msg
            Right NominalDiffTime
t   -> PgInterval -> Conversion PgInterval
forall (m :: * -> *) a. Monad m => a -> m a
return (PgInterval -> Conversion PgInterval)
-> PgInterval -> Conversion PgInterval
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> PgInterval
PgInterval NominalDiffTime
t

      where
        toPico :: Integer -> Pico
        toPico :: Integer -> Pico
toPico = Integer -> Pico
forall k (a :: k). Integer -> Fixed a
MkFixed

        -- Taken from Database.PostgreSQL.Simple.Time.Internal.Parser
        twoDigits :: P.Parser Int
        twoDigits :: Parser Int
twoDigits = do
          Char
a <- Parser Char
P.digit
          Char
b <- Parser Char
P.digit
          let c2d :: Char -> Int
c2d Char
c = Char -> Int
ord Char
c Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
15
          Int -> Parser Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Parser Int) -> Int -> Parser Int
forall a b. (a -> b) -> a -> b
$! Char -> Int
c2d Char
a Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
c2d Char
b

        -- Taken from Database.PostgreSQL.Simple.Time.Internal.Parser
        seconds :: P.Parser Pico
        seconds :: Parser Pico
seconds = do
          Int
real <- Parser Int
twoDigits
          Maybe Char
mc <- Parser (Maybe Char)
P.peekChar
          case Maybe Char
mc of
            Just Char
'.' -> do
              ByteString
t <- Parser Char
P.anyChar Parser Char
-> Parser ByteString ByteString -> Parser ByteString ByteString
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser ByteString ByteString
P.takeWhile1 Char -> Bool
P.isDigit
              Pico -> Parser Pico
forall (m :: * -> *) a. Monad m => a -> m a
return (Pico -> Parser Pico) -> Pico -> Parser Pico
forall a b. (a -> b) -> a -> b
$! Int64 -> ByteString -> Pico
parsePicos (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
real) ByteString
t
            Maybe Char
_ -> Pico -> Parser Pico
forall (m :: * -> *) a. Monad m => a -> m a
return (Pico -> Parser Pico) -> Pico -> Parser Pico
forall a b. (a -> b) -> a -> b
$! Int -> Pico
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
real
         where
          parsePicos :: Int64 -> B8.ByteString -> Pico
          parsePicos :: Int64 -> ByteString -> Pico
parsePicos Int64
a0 ByteString
t = Integer -> Pico
toPico (Int64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64
t' Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
10Int64 -> Int -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n))
            where n :: Int
n  = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
12 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B8.length ByteString
t)
                  t' :: Int64
t' = (Int64 -> Char -> Int64) -> Int64 -> ByteString -> Int64
forall a. (a -> Char -> a) -> a -> ByteString -> a
B8.foldl' (\Int64
a Char
c -> Int64
10 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
a Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
15)) Int64
a0
                                 (Int -> ByteString -> ByteString
B8.take Int
12 ByteString
t)

        parseSign :: P.Parser Bool
        parseSign :: Parser Bool
parseSign = [Parser Bool] -> Parser Bool
forall (f :: * -> *) a. Alternative f => [f a] -> f a
P.choice [Char -> Parser Char
P.char Char
'-' Parser Char -> Parser Bool -> Parser Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Parser Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True, Bool -> Parser Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False]

        -- Db stores it in [-]HHH:MM:SS.[SSSS]
        -- For example, nominalDay is stored as 24:00:00
        interval :: P.Parser (Bool, Int, Int, Pico)
        interval :: Parser (Bool, Int, Int, Pico)
interval = do
            Bool
s  <- Parser Bool
parseSign
            Int
h  <- Parser Int
forall a. Integral a => Parser a
P.decimal Parser Int -> Parser Char -> Parser Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
P.char Char
':'
            Int
m  <- Parser Int
twoDigits Parser Int -> Parser Char -> Parser Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
P.char Char
':'
            Pico
ss <- Parser Pico
seconds
            if Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
60 Bool -> Bool -> Bool
&& Pico
ss Pico -> Pico -> Bool
forall a. Ord a => a -> a -> Bool
<= Pico
60
                then (Bool, Int, Int, Pico) -> Parser (Bool, Int, Int, Pico)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
s, Int
h, Int
m, Pico
ss)
                else String -> Parser (Bool, Int, Int, Pico)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid interval"

        nominalDiffTime :: P.Parser NominalDiffTime
        nominalDiffTime :: Parser NominalDiffTime
nominalDiffTime = do
          (Bool
s, Int
h, Int
m, Pico
ss) <- Parser (Bool, Int, Int, Pico)
interval
          let pico :: Pico
pico   = Pico
ss Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
+ Pico
60 Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
* (Int -> Pico
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m) Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
+ Pico
60 Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
* Pico
60 Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
* (Int -> Pico
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
forall a. Num a => a -> a
abs Int
h))
          NominalDiffTime -> Parser NominalDiffTime
forall (m :: * -> *) a. Monad m => a -> m a
return (NominalDiffTime -> Parser NominalDiffTime)
-> (Pico -> NominalDiffTime) -> Pico -> Parser NominalDiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> NominalDiffTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> NominalDiffTime)
-> (Pico -> Rational) -> Pico -> NominalDiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pico -> Rational
forall a. Real a => a -> Rational
toRational (Pico -> Parser NominalDiffTime) -> Pico -> Parser NominalDiffTime
forall a b. (a -> b) -> a -> b
$ if Bool
s then (-Pico
pico) else Pico
pico

fromPersistValueError :: Text -- ^ Haskell type, should match Haskell name exactly, e.g. "Int64"
                      -> Text -- ^ Database type(s), should appear different from Haskell name, e.g. "integer" or "INT", not "Int".
                      -> PersistValue -- ^ Incorrect value
                      -> Text -- ^ Error message
fromPersistValueError :: Text -> Text -> PersistValue -> Text
fromPersistValueError Text
haskellType Text
databaseType PersistValue
received = [Text] -> Text
T.concat
    [ Text
"Failed to parse Haskell type `"
    , Text
haskellType
    , Text
"`; expected "
    , Text
databaseType
    , Text
" from database, but received: "
    , String -> Text
T.pack (PersistValue -> String
forall a. Show a => a -> String
show PersistValue
received)
    , Text
". Potential solution: Check that your database schema matches your Persistent model definitions."
    ]

instance PersistField PgInterval where
    toPersistValue :: PgInterval -> PersistValue
toPersistValue = ByteString -> PersistValue
PersistLiteralEscaped (ByteString -> PersistValue)
-> (PgInterval -> ByteString) -> PgInterval -> PersistValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgInterval -> ByteString
pgIntervalToBs
    fromPersistValue :: PersistValue -> Either Text PgInterval
fromPersistValue (PersistLiteral_ LiteralType
DbSpecific ByteString
bs) =
        PersistValue -> Either Text PgInterval
forall a. PersistField a => PersistValue -> Either Text a
fromPersistValue (ByteString -> PersistValue
PersistLiteralEscaped ByteString
bs)
    fromPersistValue x :: PersistValue
x@(PersistLiteral_ LiteralType
Escaped ByteString
bs) =
      case Parser NominalDiffTime
-> ByteString -> Either String NominalDiffTime
forall a. Parser a -> ByteString -> Either String a
P.parseOnly (Parser NominalDiffTime -> Parser NominalDiffTime
forall a. Num a => Parser a -> Parser a
P.signed Parser NominalDiffTime
forall a. Fractional a => Parser a
P.rational Parser NominalDiffTime -> Parser Char -> Parser NominalDiffTime
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
P.char Char
's' Parser NominalDiffTime
-> Parser ByteString () -> Parser NominalDiffTime
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ByteString ()
forall t. Chunk t => Parser t ()
P.endOfInput) ByteString
bs of
        Left String
_  -> Text -> Either Text PgInterval
forall a b. a -> Either a b
Left (Text -> Either Text PgInterval) -> Text -> Either Text PgInterval
forall a b. (a -> b) -> a -> b
$ Text -> Text -> PersistValue -> Text
fromPersistValueError Text
"PgInterval" Text
"Interval" PersistValue
x
        Right NominalDiffTime
i -> PgInterval -> Either Text PgInterval
forall a b. b -> Either a b
Right (PgInterval -> Either Text PgInterval)
-> PgInterval -> Either Text PgInterval
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> PgInterval
PgInterval NominalDiffTime
i
    fromPersistValue PersistValue
x = Text -> Either Text PgInterval
forall a b. a -> Either a b
Left (Text -> Either Text PgInterval) -> Text -> Either Text PgInterval
forall a b. (a -> b) -> a -> b
$ Text -> Text -> PersistValue -> Text
fromPersistValueError Text
"PgInterval" Text
"Interval" PersistValue
x

instance PersistFieldSql PgInterval where
  sqlType :: Proxy PgInterval -> SqlType
sqlType Proxy PgInterval
_ = Text -> SqlType
SqlOther Text
"interval"