| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Database.PostgreSQL.PQTypes.Deriving
Synopsis
- newtype SQLEnum a = SQLEnum a
- class (Enum a, Bounded a, Enum (EnumBase a), Ord (EnumBase a)) => EnumEncoding a where
- type EnumBase a
- encodeEnum :: a -> EnumBase a
- decodeEnum :: EnumBase a -> Either [(EnumBase a, EnumBase a)] a
- decodeEnumMap :: Map (EnumBase a) a
- newtype SQLEnumAsText a = SQLEnumAsText a
- class (Enum a, Bounded a) => EnumAsTextEncoding a where
- encodeEnumAsText :: a -> Text
- decodeEnumAsText :: Text -> Either [Text] a
- decodeEnumAsTextMap :: Map Text a
- isInjective :: (Enum a, Bounded a, Eq a, Eq b) => (a -> b) -> Bool
Helpers, to be used with deriving via (-XDerivingVia).
Helper newtype to be used with deriving via to derive (PQFormat, ToSQL,
FromSQL) instances for enums, given an instance of EnumEncoding.
Hint: non-trivial Enum instances can be derived using the 'generic-data'
package!
>>>:{data Colours = Blue | Black | Red | Mauve | Orange deriving (Eq, Show, Enum, Bounded) instance EnumEncoding Colours where type EnumBase Colours = Int16 encodeEnum = \case Blue -> 1 Black -> 7 Red -> 2 Mauve -> 6 Orange -> 3 :}
Note: To get SQL-specific instances use DerivingVia:
data Colours = ... ... deriving (PQFormat, ToSQL, FromSQL) via SQLEnum Colours
>>>isInjective (encodeEnum @Colours)True
>>>decodeEnum @Colours 7Right Black
>>>decodeEnum @Colours 42Left [(1,3),(6,7)]
Constructors
| SQLEnum a |
Instances
| PQFormat (EnumBase a) => PQFormat (SQLEnum a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
| (EnumEncoding a, Storable (PQBase (EnumBase a)), PQFormat (EnumBase a), FromSQL (EnumBase a), Show (EnumBase a), Typeable (EnumBase a)) => FromSQL (SQLEnum a) Source # | |
| (EnumEncoding a, PQFormat (EnumBase a), ToSQL (EnumBase a)) => ToSQL (SQLEnum a) Source # | |
| type PQBase (SQLEnum a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
| type PQDest (SQLEnum a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
class (Enum a, Bounded a, Enum (EnumBase a), Ord (EnumBase a)) => EnumEncoding a where Source #
Minimal complete definition
Methods
encodeEnum :: a -> EnumBase a Source #
Encode a as a base type.
decodeEnum :: EnumBase a -> Either [(EnumBase a, EnumBase a)] a Source #
Decode base type to an a. If the conversion fails, a list of valid
ranges is returned instead.
Note: The default implementation looks up values in decodeEnumMap and
can be overwritten for performance if necessary.
decodeEnumMap :: Map (EnumBase a) a Source #
Include the inverse map as a top-level part of the EnumEncoding
instance to ensure it is only computed once by GHC.
newtype SQLEnumAsText a Source #
A special case of SQLEnum, where the enum is to be encoded as text
(SQLEnum can't be used because of the Enum constraint on the domain of
encodeEnum).
>>>:{data Person = Alfred | Bertrand | Charles deriving (Eq, Show, Enum, Bounded) instance EnumAsTextEncoding Person where encodeEnumAsText = \case Alfred -> "alfred" Bertrand -> "bertrand" Charles -> "charles" :}
Note: To get SQL-specific instances use DerivingVia:
data Person = ... ... deriving (PQFormat, ToSQL, FromSQL) via SQLEnumAsText Person
>>>isInjective (encodeEnumAsText @Person)True
>>>decodeEnumAsText @Person "bertrand"Right Bertrand
>>>decodeEnumAsText @Person "batman"Left ["alfred","bertrand","charles"]
Constructors
| SQLEnumAsText a |
Instances
| EnumAsTextEncoding a => PQFormat (SQLEnumAsText a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
| EnumAsTextEncoding a => FromSQL (SQLEnumAsText a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving Associated Types type PQBase (SQLEnumAsText a) # Methods fromSQL :: Maybe (PQBase (SQLEnumAsText a)) -> IO (SQLEnumAsText a) # | |
| EnumAsTextEncoding a => ToSQL (SQLEnumAsText a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving Associated Types type PQDest (SQLEnumAsText a) # Methods toSQL :: SQLEnumAsText a -> ParamAllocator -> (Ptr (PQDest (SQLEnumAsText a)) -> IO r) -> IO r # | |
| type PQBase (SQLEnumAsText a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
| type PQDest (SQLEnumAsText a) Source # | |
Defined in Database.PostgreSQL.PQTypes.Deriving | |
class (Enum a, Bounded a) => EnumAsTextEncoding a where Source #
Minimal complete definition
Methods
encodeEnumAsText :: a -> Text Source #
Encode a as Text.
decodeEnumAsText :: Text -> Either [Text] a Source #
Decode Text to an a. If the conversion fails, a list of valid values
is returned instead.
Note: The default implementation looks up values in decodeEnumAsTextMap
and can be overwritten for performance if necessary.
decodeEnumAsTextMap :: Map Text a Source #
Include the inverse map as a top-level part of the SQLEnumTextEncoding
instance to ensure it is only computed once by GHC.