Protocol::Database::PostgreSQL - support for the PostgreSQL wire protocol
use strict; use warnings; use mro; package Example::PostgreSQL::Client; sub new { bless { @_[1..$#_] }, $_[0] } sub protocol { my ($self) = @_; $self->{protocol} //= Protocol::Database::PostgresQL->new( outgoing => $self->outgoing, ) } # Any received packets will arrive here sub incoming { shift->{incoming} //= Ryu::Source->new } # Anything we want to send goes here sub outgoing { shift->{outgoing} //= Ryu::Source->new } ... # We raise events on our incoming source in this example - # if you prefer to handle each message as it's extracted you # could add that directly in the loop $self->incoming ->switch_str( sub { $_->type }, authentication_request => sub { ... }, sub { warn 'unknown message - ' . $_->type } ); # When there's something to write, we'll get an event here $self->outgoing ->each(sub { $sock->write($_) }); while(1) { $sock->read(my $buf, 1_000_000); while(my $msg = $self->protocol->extract_message(\$buf)) { $self->incoming->emit($msg); } }
Provides protocol-level support for PostgreSQL 7.4+, as defined in http://www.postgresql.org/docs/current/static/protocol.html.
The short answer: don't.
Use Database::Async::Engine::PostgreSQL instead, unless you're writing a driver for talking to PostgreSQL (or compatible) systems.
This distribution provides the abstract protocol handling, meaning that it understands the packets that make up the PostgreSQL communication protocol, but it does not attempt to send or receive those packets itself. You need to provide the transport layer (typically this would involve TCP or Unix sockets).
Possible states:
Unconnected - we have a valid instantiated PostgreSQL object, but no connection yet.
Connected - transport layer has made a connection for us
AuthRequested - the server has challenged us to identify
Authenticated - we have successfully identified with the server
Idle - session is active and ready for commands
Parsing - a statement has been passed to the server for parsing
Describing - the indicated statement is being described, called after the transport layer has sent the Describe request
Binding - parameters for a given query have been transmitted
Executing - we have sent a request to execute
ShuttingDown - terminate request sent
CopyIn - the server is expecting data for a COPY command
The "type" in Protocol::Database::Backend for incoming messages can currently include the following:
send_request - Called each time there is a new message to be sent to the other side of the connection.
send_request
authenticated - Called when authentication is complete
authenticated
copy_data - we have received data from an ongoing COPY request
copy_data
copy_complete - the active COPY request has completed
copy_complete
For the client, the following additional callbacks are available:
request_ready - the server is ready for the next request
request_ready
bind_complete - a Bind request has completed
bind_complete
close_complete - the Close request has completed
close_complete
command_complete - the requested command has finished, this will typically be followed by an on_request_ready event
command_complete
copy_in_response - indicates that the server is ready to receive COPY data
copy_in_response
copy_out_response - indicates that the server is ready to send COPY data
copy_out_response
copy_both_response - indicates that the server is ready to exchange COPY data (for replication)
copy_both_response
data_row - data from the current query
data_row
empty_query - special-case response when sent an empty query, can be used for 'ping'. Typically followed by on_request_ready
empty_query
error - server has raised an error
error
function_call_result - results from a function call
function_call_result
no_data - indicate that a query returned no data, typically followed by on_request_ready
no_data
notice - server has sent us a notice
notice
notification - server has sent us a NOTIFY
notification
parameter_description - parameters are being described
parameter_description
parameter_status - parameter status...
parameter_status
parse_complete - parsing is done
parse_complete
portal_suspended - the portal has been suspended, probably hit the row limit
portal_suspended
ready_for_query - we're ready for queries
ready_for_query
row_description - descriptive information about the rows we're likely to be seeing shortly
row_description
And there are also these potential events back from the server:
copy_fail - the frontend is indicating that the copy has failed
copy_fail
describe - request for something to be described
describe
execute - request execution of a given portal
execute
flush - request flush
flush
function_call - request execution of a given function
function_call
parse - request to parse something
parse
password - password information
password
query - simple query request
query
ssl_request - we have an SSL request
ssl_request
startup_message - we have an SSL request
startup_message
sync - sync request
sync
terminate - termination request
terminate
Instantiate a new object. Blesses an empty hashref and calls "configure", subclasses can bypass this entirely and just call "configure" directly after instantiation.
Does the real preparation for the object.
Bind parameters to an existing prepared statement.
Describe expected SQL results
Execute either a named or anonymous portal (prepared statement with bind vars)
Parse SQL for a prepared statement
Password data, possibly encrypted depending on what the server specified.
Initial client response for SASL authentication
Simple query
Initial mesage informing the server which database and user we want
Synchonise after a prepared statement has finished execution.
Returns true if we are authenticated (and can start sending real data).
Returns true if this is the first message, as per http://developer.postgresql.org/pgdocs/postgres/protocol-overview.html:
"For historical reasons, the very first message sent by the client (the startup message) has no initial message-type byte."
Send a message.
Returns the method name for the given frontend type.
Returns true if the given frontend type is one that we know how to handle.
Creates a new message of the given type.
Handle an incoming message from the server.
Returns the length of the given message.
Send a simple query to the server - only supports plain queries (no bind parameters).
Send copy data to the server.
Indicate that the COPY data from the client is complete.
Accessor for current backend state.
Returns true if we're ready to send more data to the server.
Send COPY data to the server. Takes an arrayref and replaces any reserved characters with quoted versions.
Construct a new message.
Some PostgreSQL-related modules - plenty of things build on these so have a look at the relevant reverse deps if you're after something higher level:
DBD::Pg - uses the official library and (unlike this module) provides full support for DBI
Pg::PQ - another libpq wrapper
Postgres - quite an old (1998) libpq binding
Pg - slightly less old (2000) libpq binding
DBD::PgPP - provides another pure-Perl implemmentation, with the focus on DBI compatibility
Other related database protocols:
Protocol::MySQL - Oracle's popular database product
Protocol::TDS - the tabular data stream protocol, mainly of interest for SQL Server users
Tom Molesworth <TEAM@cpan.org>
Copyright Tom Molesworth 2010-2019. Licensed under the same terms as Perl itself.
To install Protocol::Database::PostgreSQL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Protocol::Database::PostgreSQL
CPAN shell
perl -MCPAN -e shell install Protocol::Database::PostgreSQL
For more information on module installation, please visit the detailed CPAN module installation guide.