PostgreSQL.Binary.Decoder

postgresql-binary-0.9.3: Encoders and decoders for the PostgreSQL's binary format

Safe Haskell | None
Language | Haskell2010

PostgreSQL.Binary.Decoder

Contents

- Primitive
- Textual
- Misc
- Time
- Exotic
  - Array
  - Composite
  - HStore

Synopsis

- type Decoder = BinaryParser
- run :: BinaryParser a -> ByteString -> Either Text a
- int :: (Integral a, Bits a) => Decoder a
- float4 :: Decoder Float
- float8 :: Decoder Double
- bool :: Decoder Bool
- bytea_strict :: Decoder ByteString
- bytea_lazy :: Decoder LazyByteString
- text_strict :: Decoder Text
- text_lazy :: Decoder LazyText
- char :: Decoder Char
- fn :: (ByteString -> Either Text a) -> Decoder a
- numeric :: Decoder Scientific
- uuid :: Decoder UUID
- inet :: Decoder Inet
- json_ast :: Decoder Value
- json_bytes :: (ByteString -> Either Text a) -> Decoder a
- jsonb_ast :: Decoder Value
- jsonb_bytes :: (ByteString -> Either Text a) -> Decoder a
- date :: Decoder Day
- time_int :: Decoder TimeOfDay
- time_float :: Decoder TimeOfDay
- timetz_int :: Decoder (TimeOfDay, TimeZone)
- timetz_float :: Decoder (TimeOfDay, TimeZone)
- timestamp_int :: Decoder LocalTime
- timestamp_float :: Decoder LocalTime
- timestamptz_int :: Decoder UTCTime
- timestamptz_float :: Decoder UTCTime
- interval_int :: Decoder DiffTime
- interval_float :: Decoder DiffTime
- data ArrayDecoder a
- array :: ArrayDecoder a -> Decoder a
- arrayDimension :: (forall m. Monad m => Int -> m a -> m b) -> ArrayDecoder a -> ArrayDecoder b
- arrayValue :: Decoder a -> ArrayDecoder (Maybe a)
- arrayNonNullValue :: Decoder a -> ArrayDecoder a
- data CompositeDecoder a
- composite :: CompositeDecoder a -> Decoder a
- compositeValue :: Decoder a -> CompositeDecoder (Maybe a)
- compositeNonNullValue :: Decoder a -> CompositeDecoder a
- hstore :: (forall m. Monad m => Int -> m (k, Maybe v) -> m r) -> Decoder k -> Decoder v -> Decoder r
- enum :: (Text -> Maybe a) -> Decoder a

# Documentation

type Decoder = BinaryParser #

run :: BinaryParser a -> ByteString -> Either Text a #

Apply a parser to bytes.

# Primitive

int :: (Integral a, Bits a) => Decoder a #

float4 :: Decoder Float #

float8 :: Decoder Double #

bool :: Decoder Bool #

bytea_strict :: Decoder ByteString #

BYTEA or any other type in its undecoded form.

bytea_lazy :: Decoder LazyByteString #

BYTEA or any other type in its undecoded form.

# Textual

text_strict :: Decoder Text #

Any of the variable-length character types: BPCHAR, VARCHAR, NAME and TEXT.

text_lazy :: Decoder LazyText #

Any of the variable-length character types: BPCHAR, VARCHAR, NAME and TEXT.

char :: Decoder Char #

A UTF-8-decoded char.

# Misc

fn :: (ByteString -> Either Text a) -> Decoder a #

Lifts a custom decoder implementation.

numeric :: Decoder Scientific #

uuid :: Decoder UUID #

inet :: Decoder Inet #

json_ast :: Decoder Value #

json_bytes :: (ByteString -> Either Text a) -> Decoder a #

Given a function, which parses a plain UTF-8 JSON string encoded as a byte-array, produces a decoder.

jsonb_ast :: Decoder Value #

jsonb_bytes :: (ByteString -> Either Text a) -> Decoder a #

Given a function, which parses a plain UTF-8 JSON string encoded as a byte-array, produces a decoder.

For those wondering, yes, JSONB is encoded as plain JSON string in the binary format of Postgres. Sad, but true.

# Time

date :: Decoder Day #

`DATE` values decoding.

time_int :: Decoder TimeOfDay #

`TIME` values decoding for servers, which have `integer_datetimes` enabled.

time_float :: Decoder TimeOfDay #

`TIME` values decoding for servers, which don't have `integer_datetimes` enabled.

timetz_int :: Decoder (TimeOfDay, TimeZone) #

`TIMETZ` values decoding for servers, which have `integer_datetimes` enabled.

timetz_float :: Decoder (TimeOfDay, TimeZone) #

`TIMETZ` values decoding for servers, which don't have `integer_datetimes` enabled.

timestamp_int :: Decoder LocalTime #

`TIMESTAMP` values decoding for servers, which have `integer_datetimes` enabled.

timestamp_float :: Decoder LocalTime #

`TIMESTAMP` values decoding for servers, which don't have `integer_datetimes` enabled.

timestamptz_int :: Decoder UTCTime #

`TIMESTAMP` values decoding for servers, which have `integer_datetimes` enabled.

timestamptz_float :: Decoder UTCTime #

`TIMESTAMP` values decoding for servers, which don't have `integer_datetimes` enabled.

interval_int :: Decoder DiffTime #

`INTERVAL` values decoding for servers, which don't have `integer_datetimes` enabled.

interval_float :: Decoder DiffTime #

`INTERVAL` values decoding for servers, which have `integer_datetimes` enabled.

# Exotic

## Array

data ArrayDecoder a #

An efficient generic array decoder, which constructs the result value in place while parsing.

Here's how you can use it to produce a specific array value decoder:

    x :: Decoder [ [ Text ] ]
    x =
      array (arrayDimension replicateM (fmap catMaybes (arrayDimension replicateM (arrayValue text))))

Instances

Functor ArrayDecoder # |
Methods fmap :: (a -> b) -> ArrayDecoder a -> ArrayDecoder b # (<$) :: a -> ArrayDecoder b -> ArrayDecoder a #

array :: ArrayDecoder a -> Decoder a #

Unlift an `ArrayDecoder` to a value `Decoder`.

arrayDimension :: (forall m. Monad m => Int -> m a -> m b) -> ArrayDecoder a -> ArrayDecoder b #

A function for parsing a dimension of an array. Provides support for multi-dimensional arrays.

Accepts:

- An implementation of the `replicateM` function (`Control.Monad.`replicateM``, `Data.Vector.`replicateM``), which determines the output value.
- A decoder of its components, which can be either another `arrayDimension` or `arrayValue`.

arrayValue :: Decoder a -> ArrayDecoder (Maybe a) #

Lift a value `Decoder` into `ArrayDecoder` for parsing of nullable leaf values.

arrayNonNullValue :: Decoder a -> ArrayDecoder a #

Lift a value `Decoder` into `ArrayDecoder` for parsing of non-nullable leaf values.

## Composite

data CompositeDecoder a #

Instances

Monad CompositeDecoder # |
Methods (>>=) :: CompositeDecoder a -> (a -> CompositeDecoder b) -> CompositeDecoder b # (>>) :: CompositeDecoder a -> CompositeDecoder b -> CompositeDecoder b # return :: a -> CompositeDecoder a # fail :: String -> CompositeDecoder a #
Functor CompositeDecoder # |
Methods fmap :: (a -> b) -> CompositeDecoder a -> CompositeDecoder b # (<$) :: a -> CompositeDecoder b -> CompositeDecoder a #
Applicative CompositeDecoder # |
Methods pure :: a -> CompositeDecoder a # (<*>) :: CompositeDecoder (a -> b) -> CompositeDecoder a -> CompositeDecoder b # (*>) :: CompositeDecoder a -> CompositeDecoder b -> CompositeDecoder b # (<*) :: CompositeDecoder a -> CompositeDecoder b -> CompositeDecoder a #

composite :: CompositeDecoder a -> Decoder a #

Unlift a `CompositeDecoder` to a value `Decoder`.

compositeValue :: Decoder a -> CompositeDecoder (Maybe a) #

Lift a value `Decoder` into `CompositeDecoder`.

compositeNonNullValue :: Decoder a -> CompositeDecoder a #

Lift a non-nullable value `Decoder` into `CompositeDecoder`.

## HStore

hstore :: (forall m. Monad m => Int -> m (k, Maybe v) -> m r) -> Decoder k -> Decoder v -> Decoder r #

A function for generic in place parsing of an HStore value.

Accepts:

- An implementation of the `replicateM` function (`Control.Monad.`replicateM``, `Data.Vector.`replicateM``), which determines how to produce the final datastructure from the rows.
- A decoder for keys.
- A decoder for values.

Here's how you can use it to produce a parser to list:

    hstoreAsList :: Decoder [ ( Text , Maybe Text ) ]
    hstoreAsList =
      hstore replicateM text text

##

enum :: (Text -> Maybe a) -> Decoder a #

Given a partial mapping from text to value, produces a decoder of that value.