Skip to content

Latest commit

 

History

History
88 lines (67 loc) · 1.87 KB

README.md

File metadata and controls

88 lines (67 loc) · 1.87 KB

PGWire

CI

The PostgreSQL backend protocol for Elixir. Allows the creation of Postgres compatible TCP servers.

Usage

  1. Implement the protocol behaviour
defmodule PGEcho.Protocol do
  use PGWire.Protocol

  alias PGWire.{Query, Protocol}
  alias PGWire.Authentication, as: A

  @impl true
  def init(_opts) do
    {:ok, %{}}
  end

  @impl true
  def handle_authentication(%A{user: user, password: pass} = a, state) do
    if user == "hydra" and pass == "pass",
      do: {:ok, [], state},
      else: {:error, :not_authenticated, state}
  end

  @impl true
  def handle_query(%Query{statement: statement} = q, state) do
    msgs = encode_and_complete(q, [%{"echo" => statement}])

    {:ok, msgs, state}
  end

  defp encode_and_complete(query, rows) do
    [
      Protocol.encode_descriptor(rows),
      Protocol.encode_data(rows),
      Protocol.complete(query, length(rows)),
      Protocol.ready()
    ]
  end
end
  1. Start the PGWire tcp server:
iex> PGWire.start_link(PGEcho.Protocol, port: 5432)
{:ok, #PID<0.251.0>}
  1. Start a client and send a query;
➜ psql -U hydra -h localhost -c 'SELECT * FROM mytable'

         echo
-----------------------
 SELECT * FROM mytable
(1 row)

Features

  • [ x ] Simple query
  • [ x ] Notify / Listen
  • [ X ] Emulation of pg_catalog (pg_types)
  • Extended query
  • Copy

Installation

If available in Hex, the package can be installed by adding pg_wire to your list of dependencies in mix.exs:

def deps do
  [
    {:pg_wire, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/pg_wire.