hasql-1.10.3: Fast PostgreSQL driver with a flexible mapping API
Safe HaskellNone
LanguageHaskell2010

Hasql.Encoders

Description

A DSL for declaration of statement parameter encoders.

For compactness of names all the types defined here imply being an encoder. E.g., the Array type is an encoder of arrays, not the data-structure itself.

Synopsis

Parameters product

data Params a Source #

Encoder of some representation of a parameters product.

Has instances of Contravariant, Divisible and Monoid, which you can use to compose multiple parameters together. E.g.,

someParamsEncoder :: Params (Int64, Maybe Text)
someParamsEncoder =
  (fst >$< param (nonNullable int8)) <>
  (snd >$< param (nullable text))

As a general solution for tuples of any arity, instead of fst and snd, consider the functions of the contrazip family from the "contravariant-extras" package. E.g., here's how you can achieve the same as the above:

someParamsEncoder :: Params (Int64, Maybe Text)
someParamsEncoder =
  contrazip2 (param (nonNullable int8)) (param (nullable text))

Here's how you can implement encoders for custom composite types:

data Person = Person { name :: Text, gender :: Gender, age :: Int }

data Gender = Male | Female

personParams :: Params Person
personParams =
  (name >$< param (nonNullable text)) <>
  (gender >$< param (nonNullable genderValue)) <>
  (fromIntegral . age >$< param (nonNullable int8))

genderValue :: Value Gender
genderValue = enum Nothing (Just "gender") genderText where
  genderText gender = case gender of
    Male -> "male"
    Female -> "female"

Instances

Instances details
Contravariant Params Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Params

Methods

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

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

Divisible Params Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Params

Methods

divide :: (a -> (b, c)) -> Params b -> Params c -> Params a Source #

conquer :: Params a Source #

Monoid (Params a) Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Params

Methods

mempty :: Params a #

mappend :: Params a -> Params a -> Params a #

mconcat :: [Params a] -> Params a #

Semigroup (Params a) Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Params

Methods

(<>) :: Params a -> Params a -> Params a #

sconcat :: NonEmpty (Params a) -> Params a #

stimes :: Integral b => b -> Params a -> Params a #

noParams :: Params () Source #

No parameters. Same as mempty and conquered.

param :: NullableOrNot Value a -> Params a Source #

Lift a single parameter encoder, with its nullability specified, associating it with a single placeholder.

Nullability

data NullableOrNot (encoder :: Type -> Type) a Source #

Extensional specification of nullability over a generic encoder.

nonNullable :: encoder a -> NullableOrNot encoder a Source #

Specify that an encoder produces a non-nullable value.

nullable :: encoder a -> NullableOrNot encoder (Maybe a) Source #

Specify that an encoder produces a nullable value.

Value

data Value a Source #

Value encoder.

Instances

Instances details
Contravariant Value Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Value

Methods

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

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

bool :: Value Bool Source #

Encoder of BOOL values.

int2 :: Value Int16 Source #

Encoder of INT2 values.

int4 :: Value Int32 Source #

Encoder of INT4 values.

int8 :: Value Int64 Source #

Encoder of INT8 values.

float4 :: Value Float Source #

Encoder of FLOAT4 values.

float8 :: Value Double Source #

Encoder of FLOAT8 values.

numeric :: Value Scientific Source #

Encoder of NUMERIC values.

char :: Value Char Source #

Encoder of CHAR values.

Note that it supports Unicode values and identifies itself under the TEXT OID because of that.

text :: Value Text Source #

Encoder of TEXT values.

varchar :: Value Text Source #

Encoder of VARCHAR values.

bpchar :: Value Text Source #

Encoder of BPCHAR or CHAR(n), CHARACTER(n) values.

bytea :: Value ByteString Source #

Encoder of BYTEA values.

date :: Value Day Source #

Encoder of DATE values.

timestamp :: Value LocalTime Source #

Encoder of TIMESTAMP values.

timestamptz :: Value UTCTime Source #

Encoder of TIMESTAMPTZ values.

time :: Value TimeOfDay Source #

Encoder of TIME values.

timetz :: Value (TimeOfDay, TimeZone) Source #

Encoder of TIMETZ values.

interval :: Value DiffTime Source #

Encoder of INTERVAL values.

uuid :: Value UUID Source #

Encoder of UUID values.

inet :: Value IPRange Source #

Encoder of INET values.

macaddr :: Value (Word8, Word8, Word8, Word8, Word8, Word8) Source #

Encoder of MACADDR values.

Represented as a 6-tuple of Word8 values in big endian order. If you use ip library you can convert using its toOctets.

toOctets >$< macaddr

json :: Value Value Source #

Encoder of JSON values from JSON AST.

jsonBytes :: Value ByteString Source #

Encoder of JSON values from raw JSON.

jsonLazyBytes :: Value ByteString Source #

Encoder of JSON values from raw JSON as lazy ByteString.

jsonb :: Value Value Source #

Encoder of JSONB values from JSON AST.

jsonbBytes :: Value ByteString Source #

Encoder of JSONB values from raw JSON.

jsonbLazyBytes :: Value ByteString Source #

Encoder of JSONB values from raw JSON as lazy ByteString.

int4range :: Value (Range Int32) Source #

Encoder of INT4RANGE values.

int8range :: Value (Range Int64) Source #

Encoder of INT8RANGE values.

numrange :: Value (Range Scientific) Source #

Encoder of NUMRANGE values.

tsrange :: Value (Range LocalTime) Source #

Encoder of TSRANGE values.

tstzrange :: Value (Range UTCTime) Source #

Encoder of TSTZRANGE values.

daterange :: Value (Range Day) Source #

Encoder of DATERANGE values.

int4multirange :: Value (Multirange Int32) Source #

Encoder of INT4MULTIRANGE values.

int8multirange :: Value (Multirange Int64) Source #

Encoder of INT8MULTIRANGE values.

nummultirange :: Value (Multirange Scientific) Source #

Encoder of NUMMULTIRANGE values.

tsmultirange :: Value (Multirange LocalTime) Source #

Encoder of TSMULTIRANGE values.

tstzmultirange :: Value (Multirange UTCTime) Source #

Encoder of TSTZMULTIRANGE values.

datemultirange :: Value (Multirange Day) Source #

Encoder of DATEMULTIRANGE values.

citext :: Value Text Source #

Encoder of CITEXT values.

Requires the citext extension to be installed in the database.

name :: Value Text Source #

Encoder of NAME values.

oid :: Value Int32 Source #

Encoder of OID values.

foldableArray :: Foldable foldable => NullableOrNot Value element -> Value (foldable element) Source #

Lift a value encoder of element into a unidimensional array encoder of a foldable value.

This function is merely a shortcut to the following expression:

(array . dimension foldl' . element)

You can use it like this:

vectorOfInts :: Value (Vector Int64)
vectorOfInts = foldableArray (nonNullable int8)

Please notice that in case of multidimensional arrays nesting foldableArray encoder won't work. You have to explicitly construct the array encoder using array.

array :: Array a -> Value a Source #

Lift an array encoder into a value encoder.

hstore :: Foldable foldable => Value (foldable (Text, Maybe Text)) Source #

Encoder of HSTORE values from a foldable container of key-value pairs.

The value part can be Nothing to represent a NULL value in the hstore.

enum Source #

Arguments

:: Maybe Text

Schema name where the enum type is defined.

-> Text

Enum type name.

-> (a -> Text)

Mapping function from value to enum label.

-> Value a 

Given a function which maps a value into a textual enum label used on the DB side, produces an encoder of that value for a named enum type.

composite Source #

Arguments

:: Maybe Text

Schema name where the composite type is defined.

-> Text

Composite type name.

-> Composite a 
-> Value a 

Lift a composite encoder into a value encoder for named composite types.

This function is for named composite types where the type name is known. If you need to encode an anonymous composite type (like those created with the ROW constructor), PostgreSQL itself does not support that.

custom Source #

Arguments

:: Maybe Text

Schema name where the type is defined.

-> Text

Type name.

-> Maybe (Word32, Word32)

Possible static OIDs for the type. The first is for scalar values the second is for arrays.

When unspecified, the OIDs will be automatically determined at runtime by looking up by name.

-> [(Maybe Text, Text)]

Other named types whose OIDs are needed for serializing.

E.g., when encoding composite types Postgres requires specifying OIDs of all of its fields.

When any of the requested types is missing in the database an error will be raised upon the statement execution.

-> (((Maybe Text, Text) -> (Word32, Word32)) -> a -> ByteString)

Serialization function in the context of resolved OIDs of the types requested in the previous parameter.

It's safe to assume that all of the requested types will be present. In case you run the provided lookup function with unmentioned type names it will produce OID of 0 for them, standing for unknown type in Postgres.

-> (a -> Text)

Render function for error messages.

-> Value a 

Low level API for defining custom value encoders.

unknown :: Value ByteString Source #

Deprecated: Use custom instead.

Identifies the value with the PostgreSQL's "unknown" type, thus leaving it up to Postgres to infer the actual type of the value.

The value transmitted is any value encoded in the Postgres' Text data format. For reference, see the Formats and Format Codes section of the Postgres' documentation.

Warning: Do not use this as part of composite encoders like array since it is the only encoder that doesn't use the binary format.

Array

data Array a Source #

Generic array encoder.

Here's an example of its usage:

someParamsEncoder :: Params [[Int64]]
someParamsEncoder = param (nonNullable (array (dimension foldl' (dimension foldl' (element (nonNullable int8))))))

Please note that the PostgreSQL IN keyword does not accept an array, but rather a syntactical list of values, thus this encoder is not suited for that. Use a value = ANY($1) condition instead.

Instances

Instances details
Contravariant Array Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Array

Methods

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

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

element :: NullableOrNot Value a -> Array a Source #

Lifts a Value encoder into an Array encoder.

dimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c Source #

Encoder of an array dimension, which thus provides support for multidimensional arrays.

Accepts:

  • An implementation of the left-fold operation, such as Data.Foldable.foldl', which determines the input value.
  • A component encoder, which can be either another dimension or element.

Composite

data Composite a Source #

Composite or row-types encoder.

Instances

Instances details
Contravariant Composite Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Composite

Methods

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

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

Divisible Composite Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Composite

Methods

divide :: (a -> (b, c)) -> Composite b -> Composite c -> Composite a Source #

conquer :: Composite a Source #

Monoid (Composite a) Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Composite

Semigroup (Composite a) Source # 
Instance details

Defined in Hasql.Codecs.Encoders.Composite

Methods

(<>) :: Composite a -> Composite a -> Composite a #

sconcat :: NonEmpty (Composite a) -> Composite a #

stimes :: Integral b => b -> Composite a -> Composite a #

field :: NullableOrNot Value a -> Composite a Source #

Single field of a row-type.