Erlang PostgreSQL Database Client

* Connect

  {ok, C} = epgsql_wg:connect(Host, [Username], [Password], Opts).

  Host      - host to connect to.
  Username  - username to connect as, defaults to $USER.
  Password  - optional password to authenticate with.
  Opts      - property list of extra options. Supported properties:

    + {database, String}
    + {port,     Integer}
    + {ssl,      Atom}       true | false | required
    + {ssl_opts, List}       see ssl application docs in OTP
    + {timeout,  Integer}    milliseconds, defaults to 5000
    + {async,    Pid}        see Asynchronous Messages section

  {ok, C} = epgsql_wg:connect("localhost", "username", [{database, "test_db"}]).
  ok = epgsql_wg:close(C).

  The timeout parameter will trigger an {error, timeout} result when the
  server fails to respond within Timeout milliseconds. This timeout applies
  to the initial connection attempt and any subsequent queries.

* Simple Query

  {ok, Columns, Rows}        = epgsql_wg:squery(C, "select ...").
  {ok, Count}                = epgsql_wg:squery(C, "update ...").
  {ok, Count, Columns, Rows} = epgsql_wg:squery(C, "insert ... returning ...").

  {error, Error}             = epgsql_wg:squery(C, "invalid SQL").

  Columns       - list of column records, see epgsql_wg.hrl for definition.
  Rows          - list of tuples, one for each row.
  Count         - integer count of rows inserted/updated/etc

  The simple query protocol returns all columns as text (Erlang binaries)
  and does not support binding parameters.

* Extended Query

  {ok, Columns, Rows}        = epgsql_wg:equery(C, "select ...", [Parameters]).
  {ok, Count}                = epgsql_wg:equery(C, "update ...", [Parameters]).
  {ok, Count, Columns, Rows} = epgsql_wg:equery(C, "insert ... returning ...", [Parameters]).

  {error, Error}             = epgsql_wg:equery(C, "invalid SQL", [Parameters]).

  Parameters    - optional list of values to be bound to $1, $2, $3, etc.

  The extended query protocol combines parse, bind, and execute using
  the unnamed prepared statement and portal. A "select" statement returns
  {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  {ok, Count, Columns, Rows} when a "returning" clause is present. When
  an error occurs, all statements result in {error, #epgsql_wg_error{}}.

  PostgreSQL's binary format is used to return integers as Erlang
  integers, floats as floats, bytea/text/varchar columns as binaries,
  bools as true/false, etc. For details see epgsql_wg_binary.erl and the
  Data Representation section below.

* Parse/Bind/Execute

  {ok, Statement} = epgsql_wg:parse(C, [StatementName], Sql, [ParameterTypes]).

  StatementName   - optional, reusable, name for the prepared statement.
  ParameterTypes  - optional list of PostgreSQL types for each parameter.

  For valid type names see epgsql_wg_types.erl.

  ok = epgsql_wg:bind(C, Statement, [PortalName], ParameterValues).

  PortalName      - optional name for the result portal.

  {ok | partial, Rows} = epgsql_wg:execute(C, Statement, [PortalName], [MaxRows]).
  {ok, Count}          = epgsql_wg:execute(C, Statement, [PortalName]).
  {ok, Count, Rows}    = epgsql_wg:execute(C, Statement, [PortalName]).

  PortalName      - optional portal name used in bind/4.
  MaxRows         - maximum number of rows to return (0 for all rows).

  execute returns {partial, Rows} when more rows are available.

  ok = epgsql_wg:close(C, Statement).
  ok = epgsql_wg:close(C, statement | portal, Name).
  ok = epgsql_wg:sync(C).

  All functions return {error, Error} when an error occurs.

* Data Representation

  null        = null
  bool        = true | false
  char        = $A | binary
  intX        = 1
  floatX      = 1.0
  date        = {Year, Month, Day}
  time        = {Hour, Minute, Second.Microsecond}
  timetz      = {time, Timezone}
  timestamp   = {date, time}
  timestamptz = {date, time}
  interval    = {time, Days, Months}
  text        = <<"a">>
  varchar     = <<"a">>
  bytea       = <<1, 2>>
  array       = [1, 2, 3]

  record      = {int2, time, text, ...} (decode only)

* Errors

  Errors originating from the PostgreSQL backend are returned as {error, #epgsql_wg_error{}},
  see epgsql_wg.hrl for the record definition. epgsql_wg functions may also return
  {error, What} where What is one of the following:

  {unsupported_auth_method, Method}     - required auth method is unsupported
  timeout                               - request timed out
  closed                                - connection was closed
  sync_required                         - error occured and epgsql_wg:sync must be called

* Asynchronous Messages

  PostgreSQL may deliver two types of asynchronous message: "notices" in response
  to notice and warning messages generated by the server, and "notifications" which
  are generated by the LISTEN/NOTIFY mechanism.

  Passing the {async, Pid} option to epgsql_wg:connect will result in these async
  messages being sent to the specified process, otherwise they will be dropped.

  Message formats:

    {pgsql, Connection, {notification, Channel, Pid, Payload}}

      Connection  - connection the notification occured on

      Channel     - channel the notification occured on
      Pid         - database session pid that sent notification
      Payload     - optional payload, only available from PostgreSQL >= 9.0

    {pgsql, Connection, {notice, Error}}

      Connection  - connection the notice occured on
      Error       - an #epgsql_wg_error{} record, see epgsql_wg.hrl

