<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">[![Actions Status](https://github.com/Leont/protocol-postgres/workflows/test/badge.svg)](https://github.com/Leont/protocol-postgres/actions)

Name
====

Protocol::Postgres - a sans-io postgresql client

Synopsis
========

```raku
use v6.d;
use Protocol::Postgres;

my $socket = await IO::Socket::Async.connect($host, $port);
my $client = Protocol::Postgres::Client.new;
$socket.Supply(:bin).act({ $client.incoming-data($^data) });
$client.outbound-data.act({ $socket.write($^data) });

await $client.startup($user, $database, $password);

my $resultset = await $client.query('SELECT * FROM foo WHERE id = $1', 42);
react {
	whenever $resultset.hash-rows -&gt; (:$name, :$description) {
		say "$name is $description";
	}
}
```

Description
===========

Protocol::Postgres is sans-io implementation of (the client side of) the postgresql protocol. It is typically used through the `Protocol::Postgres::Client` class.

Client
======

`Protocol::Postgres::Client` has the following methods

new(--&gt; Protocol::Postgres::Client)
-----------------------------------

This creates a new postgres client. It supports one optional named argument:

  * TypeMap :$typemap = TypeMap::Simple

    This is the typemap that is used to translate between Raku's and Postgres' typesystem. The default mapping supports common built-in types such as strings, numbers, bools, dates, datetimes and blobs.

outgoing-data(--&gt; Supply)
-------------------------

This returns a `Supply` of `Blob`s to be written to the server.

incoming-data(Blob --&gt; Nil)
---------------------------

This consumes bytes received from the server.

startup($user, $database?, $password? --&gt; Promise)
--------------------------------------------------

This starts the handshake to the server. `$database` may be left undefined, the server will use `$user` as database name. If a `$password` is defined, any of clearnext, md5 or SCRAM-SHA-256 based authentication is supported.

The resulting promise will finish when the connection is ready for queries.

query($query, @bind-values --&gt; Promise)
---------------------------------------

This will issue a query with the given bind values, and return a promise to the result.

For fetching queries such as `SELECT` the result will be a `ResultSet` object, for manipulation (e.g. `INSERT`) and definition (e.g. `CREATE`) queries it will result in the value `True`.

Both the input types and the output types will be typemapped between Raku types and Postgres types using the typemapper.

query-multiple($query --&gt; Supply[ResultSet])
--------------------------------------------

This will issue a complex query that may contain multiple statements, but can not use bind values. It will return a `Supply` to the results of each query.

prepare($query --&gt; Promise[PreparedStatement])
----------------------------------------------

This prepares the query, and returns a Promise to the PreparedStatement object.

startTls(--&gt; Blob)
------------------

This will return the marker that should be written to the server to start upgrading the connection to use TLS. If the server responds with a single `S` byte the proposal is accepted and the client is expected to initiate the TLS handshake. If the server responds with an `N` it is rejected, and the connection proceeds in cleartext.

terminate(--&gt; Nil)
------------------

This sends a message to the server to terminate the connection

notifications(--&gt; Supply[Notification])
---------------------------------------

This returns a supply with all notifications that the current connection is subscribed to. Channels can be subscribed using the `LISTEN` command, and messages can be sent using the `NOTIFY` command.

process-id(--&gt; Int)
-------------------

This returns the process id of the backend of this connection. This is useful for debugging purposes and for notifications.

get-parameter(Str $name --&gt; Str)
--------------------------------

This returns various parameters, currently known parameters are: `server_version`, `server_encoding`, `client_encoding`, `application_name`, `default_transaction_read_only`, `in_hot_standby`, `is_superuser`, `session_authorization`, `DateStyle`, `IntervalStyle`, `TimeZone`, `integer_datetimes`, and `standard_conforming_strings`.

ResultSet
=========

A `Protocol::Postgres::ResultSet` represents the results of a query, if any.

columns(--&gt; List)
-----------------

This returns the column names for this resultset.

rows(--&gt; Supply[List])
----------------------

This returns a Supply of rows. Each row is a list of values.

hash-rows(--&gt; Supply[Hash])
---------------------------

This returns a Supply of rows. Each row is a hash with the column names as keys and the row values as values.

PreparedStatement
=================

A `Protocol::Postgres::PreparedStatement` represents a prepated statement. Its reason of existence is to call `execute` on it.

execute(@arguments --&gt; Promise[ResultSet])
------------------------------------------

This runs the prepared statement, much like the `query` method would have done.

close()
-------

This closes the prepared statement.

Notification
============

`Protocol::Postgres::Notification` has the following methods:

sender(--&gt; Int)
---------------

This is the process-id of the sender

channel(--&gt; Str)
----------------

This is the name of the channel that the notification was sent on

payload(--&gt; Str)
----------------

This is the payload of the notification

Todo
====

  * Implement the copy protocol

Author
======

Leon Timmermans &lt;fawaka@gmail.com&gt;

Copyright and License
=====================

Copyright 2022 Leon Timmermans

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

</pre></body></html>