Postgrex v0.13.0 Postgrex.Extension behaviour

An extension knows how to encode and decode Postgres types to and from Elixir values.

Custom extensions can be enabled via Postgrex.Types.define/3.

For example to support label trees using the text encoding format:

defmodule MyApp.LTree do
  @behaviour Postgrex.Extension

  # It can be memory efficient to copy the decoded binary because a
  # reference counted binary that points to a larger binary will be passed
  # to the decode/4 callback. Copying the binary can allow the larger
  # binary to be garbage collected sooner if the copy is going to be kept
  # for a longer period of time. See `:binary.copy/1` for more
  # information.
  def init(opts) when opts in [:reference, :copy], do: opts

  # Use this extension when `type` from %Postgrex.TypeInfo{} is "ltree"
  def matching(_opts), do: [type: "ltree"]

  # Use the text format, "ltree" does not have a binary format.
  def format(_opts), do: :text

  # Use quoted expression to encode a string that is the same as
  # postgresql's ltree text format. The quoted expression should contain
  # clauses that match those of a `case` or `fn`. Encoding matches on the
  # value and returns encoded `iodata()`. The first 4 bytes in the
  # `iodata()` must be the byte size of the rest of the encoded data, as a
  # signed 32bit big endian integer.
  def encode(_opts) do
    quote do
      bin when is_binary(bin) ->
        [<<byte_size(bin) :: signed-size(32)>> | bin]
    end
  end

  # Use quoted expression to decode the data to a string. Decoding matches
  # on an encoded binary with the same signed 32bit big endian integer
  # length header.
  def decode(:reference) do
    quote do
      <<len::signed-size(32), bin::binary-size(len)>> ->
        bin
    end
  end
  def decode(:copy) do
    quote do
      <<len::signed-size(32), bin::binary-size(len)>> ->
        :binary.copy(bin)
    end
  end
end

This example could be used in a custom types module:

Postgrex.Types.define(MyApp.Types, [{MyApp.LTree, :copy}])

Summary

Callbacks

Returns a quoted list of clauses that decode a binary to an Elixir value

Returns a quoted list of clauses that encode an Elixir value to iodata

Returns the format the type should be encoded as. See http://www.postgresql.org/docs/9.4/static/protocol-overview.html#PROTOCOL-FORMAT-CODES

Should perform any initialization of the extension. The function receives the user options. The state returned from this function will be passed to other callbacks

Specifies the types the extension matches, see Postgrex.TypeInfo for specification of the fields

Types

state()
state :: term
t()
t :: module

Callbacks

decode(state)
decode(state) :: Macro.expr

Returns a quoted list of clauses that decode a binary to an Elixir value.

The pattern must use binary syntax and decode a fixed length using the signed 32 bit big endian integer byte length header.

def decode(_) do
  quote do
    # length header is in bytes
    <<len :: signed-32, integer :: signed-size(len)-unit(8)>> ->
      integer
  end
end
encode(state)
encode(state) :: Macro.expr

Returns a quoted list of clauses that encode an Elixir value to iodata.

It must use a signed 32 bit big endian integer byte length header.

def encode(_) do
  quote do
    integer ->
      <<8 :: signed-32, integer :: signed-64>>
  end
end
format(state)
format(state) :: :binary | :text

Returns the format the type should be encoded as. See http://www.postgresql.org/docs/9.4/static/protocol-overview.html#PROTOCOL-FORMAT-CODES.

init(arg0)
init(Keyword.t) :: state

Should perform any initialization of the extension. The function receives the user options. The state returned from this function will be passed to other callbacks.

matching(state)
matching(state) :: [type: String.t, send: String.t, receive: String.t, input: String.t, output: String.t]

Specifies the types the extension matches, see Postgrex.TypeInfo for specification of the fields.