hpgsql
Safe HaskellNone
LanguageHaskell2010

Hpgsql.Encoding

Description

Encoding and decoding fields and rows

This module contains a collection of functions, classes, and instances that can help you build encoders and decoders for your Haskell types.

Here's an example:

data Person = Person { name :: Text, born :: Day, heightMeters :: Double }
  deriving stock Generic
  deriving anyclass FromPgRow

persons :: [Person] <- query conn "SELECT * FROM persons"

Note that Hpgsql's RowDecoder does not have a Monad instance because that allows it to type check query results and field counts even when queries return zero rows. If you need to write a row decoder that is monadic (because decoding can change depending on the values of fields), check Hpgsql.Encoding.RowDecoderMonadic.

Synopsis

Decoding

class FromPgField a where Source #

Instances

Instances details
FromPgField Value Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField ByteString Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField ByteString Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Int16 Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Int32 Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Int64 Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Oid Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField PgJson Source # 
Instance details

Defined in Hpgsql.Types

FromPgField Scientific Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Text Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Text Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Day Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField UTCTime Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField CalendarDiffTime Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField LocalTime Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField TimeOfDay Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField ZonedTime Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField UUID Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Integer Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField String Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField () Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Bool Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Char Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Double Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Float Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField Int Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField (CI Text) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

FromPgField (CI Text) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

FromPgField (CI String) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

FromPgField (Ratio Integer) Source # 
Instance details

Defined in Hpgsql.Encoding

(Generic a, EnumDecoder (Rep a)) => FromPgField (LowerCasedPgEnum a) Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField (Unbounded Day) Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField (Unbounded UTCTime) Source # 
Instance details

Defined in Hpgsql.Encoding

FromPgField (Unbounded ZonedTime) Source # 
Instance details

Defined in Hpgsql.Encoding

FromJSON a => FromPgField (Aeson a) Source # 
Instance details

Defined in Hpgsql.Types

FromPgField a => FromPgField (PGArray a) Source # 
Instance details

Defined in Hpgsql.Types

FromPgField a => FromPgField (Vector (Vector a)) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

fieldDecoder :: FieldDecoder (Vector (Vector a)) Source #

FromPgField a => FromPgField (Vector a) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

fieldDecoder :: FieldDecoder (Vector a) Source #

FromPgField a => FromPgField (Maybe a) Source # 
Instance details

Defined in Hpgsql.Encoding

data FieldDecoder a Source #

A decoder for a single field/column.

Instances

Instances details
Functor FieldDecoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

fmap :: (a -> b) -> FieldDecoder a -> FieldDecoder b #

(<$) :: a -> FieldDecoder b -> FieldDecoder a #

Semigroup (FieldDecoder a) Source #

`f1 <> f2` produces a FieldDecoder that tries f1 first, and if that fails it tries f2.

Instance details

Defined in Hpgsql.Encoding

data FieldInfo Source #

Constructors

FieldInfo 

Fields

class FromPgRow a where Source #

Minimal complete definition

Nothing

Methods

rowDecoder :: RowDecoder a Source #

default rowDecoder :: (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a Source #

Instances

Instances details
FromPgField a => FromPgRow (Only a) Source # 
Instance details

Defined in Hpgsql.Encoding

(FromPgRow a, FromPgRow b) => FromPgRow (a :. b) Source # 
Instance details

Defined in Hpgsql.Types

Methods

rowDecoder :: RowDecoder (a :. b) Source #

(FromPgField a, FromPgField b) => FromPgRow (a, b) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b) Source #

(FromPgField a, FromPgField b, FromPgField c) => FromPgRow (a, b, c) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d) => FromPgRow (a, b, c, d) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e) => FromPgRow (a, b, c, d, e) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f) => FromPgRow (a, b, c, d, e, f) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g) => FromPgRow (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h) => FromPgRow (a, b, c, d, e, f, g, h) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i) => FromPgRow (a, b, c, d, e, f, g, h, i) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h, i) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j) => FromPgRow (a, b, c, d, e, f, g, h, i, j) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h, i, j) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h, i, j, k) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h, i, j, k, l) Source #

(FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l, FromPgField m) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l, m) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowDecoder :: RowDecoder (a, b, c, d, e, f, g, h, i, j, k, l, m) Source #

data RowDecoder a Source #

Constructors

RowDecoder 

Fields

Instances

Instances details
Applicative RowDecoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

pure :: a -> RowDecoder a #

(<*>) :: RowDecoder (a -> b) -> RowDecoder a -> RowDecoder b #

liftA2 :: (a -> b -> c) -> RowDecoder a -> RowDecoder b -> RowDecoder c #

(*>) :: RowDecoder a -> RowDecoder b -> RowDecoder b #

(<*) :: RowDecoder a -> RowDecoder b -> RowDecoder a #

Functor RowDecoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

fmap :: (a -> b) -> RowDecoder a -> RowDecoder b #

(<$) :: a -> RowDecoder b -> RowDecoder a #

(TypeError ('Text "RowDecoder does not have a Monad instance in Hpgsql because Hpgsql type-checks the result types of queries before having access to even the first data row. Use the Applicative class to write your instances or use the Monadic decoding variants.") :: Constraint) => Monad RowDecoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

(>>=) :: RowDecoder a -> (a -> RowDecoder b) -> RowDecoder b #

(>>) :: RowDecoder a -> RowDecoder b -> RowDecoder b #

return :: a -> RowDecoder a #

Generic (RowDecoder a) Source # 
Instance details

Defined in Hpgsql.Encoding

Associated Types

type Rep (RowDecoder a) 
Instance details

Defined in Hpgsql.Encoding

type Rep (RowDecoder a)

Methods

from :: RowDecoder a -> Rep (RowDecoder a) x #

to :: Rep (RowDecoder a) x -> RowDecoder a #

type Rep (RowDecoder a) Source # 
Instance details

Defined in Hpgsql.Encoding

type Rep (RowDecoder a)

nullableField :: FieldDecoder a -> FieldDecoder (Maybe a) Source #

A FieldDecoder that accepts and decodes SQL NULLs into Nothing values for a given decoder.

genericFromPgRow :: (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a Source #

Derives FromPgRow generically.

Encoding

Keep in mind that Haskell's Int is an Int64 on most hardware, which is a mismatch for the commonly used 32-bit integer PostgreSQL type. This is not a problem when decoding because Hpgsql can decode integer into Haskell's Int, but when encoding PostgreSQL will understandably not accept a larger type.

class ToPgField a where Source #

Instances

Instances details
ToPgField Value Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField ByteString Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField ByteString Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Int16 Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Int32 Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Int64 Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Oid Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Scientific Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Text Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Text Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Day Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField NominalDiffTime Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField UTCTime Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField CalendarDiffTime Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField LocalTime Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField TimeOfDay Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField ZonedTime Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField UUID Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Integer Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField String Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Bool Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Char Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Double Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Float Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField Int Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField (CI Text) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

ToPgField (CI Text) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

ToPgField (CI String) Source #

This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default connection option).

Instance details

Defined in Hpgsql.Encoding

ToPgField (Ratio Integer) Source # 
Instance details

Defined in Hpgsql.Encoding

(Generic a, EnumEncoder (Rep a)) => ToPgField (LowerCasedPgEnum a) Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField (Unbounded Day) Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField (Unbounded UTCTime) Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField (Unbounded ZonedTime) Source # 
Instance details

Defined in Hpgsql.Encoding

ToJSON a => ToPgField (Aeson a) Source # 
Instance details

Defined in Hpgsql.Types

ToPgField a => ToPgField (PGArray a) Source # 
Instance details

Defined in Hpgsql.Types

ToPgField a => ToPgField (Vector a) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

fieldEncoder :: FieldEncoder (Vector a) Source #

ToPgField a => ToPgField (Maybe a) Source # 
Instance details

Defined in Hpgsql.Encoding

data FieldEncoder a Source #

Instances

Instances details
Contravariant FieldEncoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

contramap :: (a' -> a) -> FieldEncoder a -> FieldEncoder a' #

(>$) :: b -> FieldEncoder b -> FieldEncoder a #

class ToPgRow a where Source #

Minimal complete definition

Nothing

Methods

rowEncoder :: RowEncoder a Source #

default rowEncoder :: (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a Source #

Instances

Instances details
ToPgRow () Source # 
Instance details

Defined in Hpgsql.Encoding

ToPgField a => ToPgRow (Only a) Source # 
Instance details

Defined in Hpgsql.Encoding

(ToPgRow a, ToPgRow b) => ToPgRow (a :. b) Source # 
Instance details

Defined in Hpgsql.Types

Methods

rowEncoder :: RowEncoder (a :. b) Source #

(ToPgField a, ToPgField b) => ToPgRow (a, b) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b) Source #

(ToPgField a, ToPgField b, ToPgField c) => ToPgRow (a, b, c) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d) => ToPgRow (a, b, c, d) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e) => ToPgRow (a, b, c, d, e) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f) => ToPgRow (a, b, c, d, e, f) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g) => ToPgRow (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f, g) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h) => ToPgRow (a, b, c, d, e, f, g, h) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f, g, h) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i) => ToPgRow (a, b, c, d, e, f, g, h, i) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f, g, h, i) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j) => ToPgRow (a, b, c, d, e, f, g, h, i, j) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f, g, h, i, j) Source #

(ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j, ToPgField k) => ToPgRow (a, b, c, d, e, f, g, h, i, j, k) Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

rowEncoder :: RowEncoder (a, b, c, d, e, f, g, h, i, j, k) Source #

data RowEncoder a Source #

Constructors

RowEncoder 

Fields

Instances

Instances details
Contravariant RowEncoder Source # 
Instance details

Defined in Hpgsql.Encoding

Methods

contramap :: (a' -> a) -> RowEncoder a -> RowEncoder a' #

(>$) :: b -> RowEncoder b -> RowEncoder a #

newtype EncodingContext Source #

Constructors

EncodingContext 

Fields

  • typeInfoCache :: TypeInfoCache

    A map with all builtin PostgreSQL types plus user-defined types, unless you specify custom connection options.

genericToPgRow :: (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a Source #

PostgreSQL enums

newtype LowerCasedPgEnum a Source #

For the very common case of a Haskell enum matching a custom postgres enum type that has its values all as lower case strings, this newtype can help you derive instances as such:

data Mood = Sad | Ok | Happy
  deriving stock (Generic)
  deriving (FromPgField, ToPgField) via (LowerCasedPgEnum Mood)

And this would match the Postgres equivalent:

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');

If you run into PostgreSQL type inference problems with this, you can write instances manually with genericEnumFieldDecoder, genericEnumFieldEncoder, typeFieldEncoder, and typeFieldDecoder.

Constructors

LowerCasedPgEnum a 

Instances

Instances details
(Generic a, EnumDecoder (Rep a)) => FromPgField (LowerCasedPgEnum a) Source # 
Instance details

Defined in Hpgsql.Encoding

(Generic a, EnumEncoder (Rep a)) => ToPgField (LowerCasedPgEnum a) Source # 
Instance details

Defined in Hpgsql.Encoding

genericEnumFieldDecoder Source #

Arguments

:: (Generic a, EnumDecoder (Rep a)) 
=> (Text -> Text)

A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres

-> FieldDecoder a 

One of the functions behind LowerCasedPgEnum, but you can decide how to map your type's constructor names arbitrarily, which can be useful if you're not using lowercase values in your postgres enums.

genericEnumFieldEncoder Source #

Arguments

:: (Generic a, EnumEncoder (Rep a)) 
=> (Text -> Text)

A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres

-> a 
-> ByteString 

One of the functions behind LowerCasedPgEnum, but you can decide how to map your type's constructor names arbitrarily, which can be useful if you're not using lowercase values in your postgres enums.

PostgreSQL composite types

compositeTypeDecoder :: RowDecoder a -> FieldDecoder a Source #

Allows you to create a FieldDecoder for composite types. For a type such as:

CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);

You can define a Haskell type as such:

data IntAndBool = IntAndBool Int Bool

instance FromPgField IntAndBool where
  fieldDecoder = compositeTypeDecoder rowDecoder <&> \(i, b) -> IntAndBool i b

compositeTypeEncoder :: RowEncoder a -> FieldEncoder a Source #

Allows you to create a FieldEncoder for composite types. For a type such as:

CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);

You can define a Haskell type as such:

data IntAndBool = IntAndBool Int Bool

instance ToPgField IntAndBool where
  fieldEncoder = typeFieldEncoder (typeOidWithName "int_and_bool")
    $ compositeTypeEncoder $ contramap (\(IntAndBool i b) -> (fromIntegral i :: Int32, b)) rowEncoder

Driving PostgreSQL type inference

typeFieldDecoder :: (FieldInfo -> Bool) -> FieldDecoder a -> FieldDecoder a Source #

Allows you to specify a type (and other checks, possibly) for a FieldDecoder. This can be useful to ensure you're not accidentally decoding a different type.

data MyEnum = Val1 | Val2 | Val3
myEnumFieldDecoderWithTypeInfoCheck :: FieldDecoder MyEnum
myEnumFieldDecoderWithTypeInfoCheck =
  let convert = \case
        "val1" -> Val1
        "val2" -> Val2
        "val3" -> Val3
        _ -> error "Invalid value for MyEnum"
   in typeFieldDecoder
        (typeMustBeNamed "my_enum")
        $ convert <$> rawBytesFieldDecoder

This will work unless you use non-default flags in your connection options.

typeFieldEncoder :: (EncodingContext -> Maybe Oid) -> FieldEncoder a -> FieldEncoder a Source #

Allows you to specify a type for a FieldEncoder. This can be useful to avoid letting postgres infer types itself, which can cause errors. For example:

data MyEnum = Val1 | Val2 | Val3
myEnumFieldDecoderWithTypeInfoCheck :: FieldEncoder MyEnum
myEnumFieldDecoderWithTypeInfoCheck =
  let convert = \case
        Val1 -> "val1" :: Text
        Val2 -> "val2"
        Val3 -> "val3"
   in typeFieldEncoder
        (typeOidWithName "my_enum")
        $ contramap convert fieldEncoder

This will work unless you use non-default flags in your connection options.

Others

rawBytesFieldDecoder :: FieldDecoder ByteString Source #

A decoder that accepts any PG type and returns the object's postgres' binary representation as a ByteString.

untypedFieldEncoder :: (EncodingContext -> a -> BinaryField) -> FieldEncoder a Source #

Returns a FieldEncoder that is sent without a type OID in queries. This means postgres will try to infer the type of these arguments. Check typedFieldEncoder if you're interested in encoding your custom types, you probably don't need this.

toPgVectorField :: (Foldable f, ToPgField a) => EncodingContext -> f a -> BinaryField Source #

Returns a field-encoding function for a vector-like Foldable (e.g. Lists and Vector itself).

arrayField :: forall a f. Monoid (f a) => (forall (m :: Type -> Type). Monad m => Int -> m a -> m (f a)) -> FieldDecoder a -> FieldDecoder (f a) Source #

A FieldDecoder that accepts and decodes Postgres arrays.