Pg: Add COPY support
This commit is contained in:
parent
dc752e2a23
commit
d9d2ad0434
5 changed files with 301 additions and 29 deletions
117
FU/Pg.pm
117
FU/Pg.pm
|
|
@ -112,6 +112,11 @@ Inside a transaction that is in an error state. The transaction must be rolled
|
|||
back in order to recover to a usable state. This happens automatically when the
|
||||
transaction object goes out of scope.
|
||||
|
||||
=item active
|
||||
|
||||
Currently executing a query. This state can only be observed during a L<COPY
|
||||
operation|/"COPY support">.
|
||||
|
||||
=item bad
|
||||
|
||||
Connection is dead or otherwise unusable.
|
||||
|
|
@ -155,10 +160,11 @@ executing the query, but I<before> the query results have been returned.
|
|||
|
||||
The subroutine is (currently) only called for queries executed through C<<
|
||||
$conn->exec >>, C<< $conn->q >>, C<< $conn->Q >> and their C<$txn> variants;
|
||||
internal queries performed by this module (such as for transaction management,
|
||||
querying type information, etc) do not trigger the callback. Statements that
|
||||
result in an error being thrown during or before execution are also not
|
||||
traceable this way. This behavior might change in the future.
|
||||
C<< $conn->copy >> statements and internal queries performed by this module
|
||||
(such as for transaction management, querying type information, etc) do not
|
||||
trigger the callback. Statements that result in an error being thrown during or
|
||||
before execution are also not traceable this way. This behavior might change in
|
||||
the future.
|
||||
|
||||
=item $conn->disconnect
|
||||
|
||||
|
|
@ -519,6 +525,11 @@ current implementation does not track subtransactions that closely)
|
|||
|
||||
A subtransaction is in error state and awaiting to be rolled back.
|
||||
|
||||
=item active
|
||||
|
||||
Currently executing a query. This state can only be observed during a L<COPY
|
||||
operation|/"COPY support">.
|
||||
|
||||
=item bad
|
||||
|
||||
Connection is dead or otherwise unusable.
|
||||
|
|
@ -740,6 +751,71 @@ I<TODO:> Methods to convert between the various formats.
|
|||
|
||||
I<TODO:> Methods to query type info.
|
||||
|
||||
|
||||
=head2 COPY support
|
||||
|
||||
You can use L<COPY
|
||||
statements|https://www.postgresql.org/docs/current/sql-copy.html> for efficient
|
||||
bulk data transfers between your application and the PostgreSQL server:
|
||||
|
||||
=over
|
||||
|
||||
=item $copy = $conn->copy($statement)
|
||||
|
||||
=item $copy = $txn->copy($statement)
|
||||
|
||||
Execute C<$statement> and return a C<FU::Pg::copy> object that lets you
|
||||
transfer data to or from Postgres.
|
||||
|
||||
It is not possible to execute any other queries on the same connection while a
|
||||
copy operation is in progress. When used on a transaction object, C<$txn> must
|
||||
be kept alive long enough to finish the copy operation.
|
||||
|
||||
=back
|
||||
|
||||
A C<$copy> object supports the following methods:
|
||||
|
||||
=over
|
||||
|
||||
=item $copy->is_binary
|
||||
|
||||
Returns true if the transfer is performed in the binary format, false for text.
|
||||
|
||||
=item $copy->write($data)
|
||||
|
||||
Send C<$data> to the server. An error is thrown if this is not a C<COPY FROM
|
||||
STDIN> operation. An error may be thrown if C<$data> is not a valid format
|
||||
understood by Postgres, but such errors can also be deferred to C<close()>.
|
||||
|
||||
C<$data> is interpreted as a Perl Unicode string for textual transfers and as a
|
||||
binary string for binary transfers.
|
||||
|
||||
=item $copy->read
|
||||
|
||||
Return the next row read from the Postgres server, or C<undef> if no more data
|
||||
is coming. In the text format, a single line - including trailing newline - is
|
||||
returned as a Perl Unicode string. In the binary format, a single row is
|
||||
returned as a byte string. An error is thrown if this is not a C<COPY TO
|
||||
STDOUT> operation.
|
||||
|
||||
=item $copy->close
|
||||
|
||||
Marks the end of the copy operation. Does not return anything but throws an
|
||||
error if something went wrong.
|
||||
|
||||
It is possible to close a read-copy operation before all data has been
|
||||
consumed, but that causes all data to still be read and discarded during
|
||||
C<close()>. If you really want to interrupt a large read operation, a more
|
||||
efficient approach is to call C<< $conn->close >> and discard the entire
|
||||
connection.
|
||||
|
||||
It is not I<necessary> to call this method, simply letting the C<$copy> object
|
||||
run out of scope will do the trick as well, but in that case errors are
|
||||
silently discarded. An explicit C<close()> is recommended to catch errors.
|
||||
|
||||
=back
|
||||
|
||||
|
||||
=head2 Errors
|
||||
|
||||
All methods can throw an exception on error. When possible, the error message
|
||||
|
|
@ -823,32 +899,17 @@ to it after C<connect()> is always safe:
|
|||
|
||||
=item * Only works with blocking (synchronous) calls, not very suitable for use
|
||||
in asynchronous frameworks unless you know your queries are fast and you have a
|
||||
low-latency connection with the Postgres server.
|
||||
low-latency connection with the Postgres server. This is unlikely to improve in
|
||||
future versions, Perl's async story is somewhat awkward in general, and fully
|
||||
supporting async operation might require a fundamental redesign of how this
|
||||
module works.
|
||||
|
||||
=back
|
||||
=item * LISTEN support is still missing. May be added in a future version, as
|
||||
this seems doable without supporting full async.
|
||||
|
||||
Missing features:
|
||||
|
||||
=over
|
||||
|
||||
=item COPY support
|
||||
|
||||
I hope to implement this someday.
|
||||
|
||||
=item LISTEN support
|
||||
|
||||
Would be nice to have, most likely doable without going full async.
|
||||
|
||||
=item Asynchronous calls
|
||||
|
||||
Probably won't happen. Perl's async story is slightly awkward in general, and
|
||||
fully supporting async operation might require a fundamental redesign of how
|
||||
this module works. It certainly won't I<simplify> the implementation.
|
||||
|
||||
=item Pipelining
|
||||
|
||||
I have some ideas for an API, but doubt I'll ever implement it. Suffers from
|
||||
the same awkwardness and complexity as asynchronous calls.
|
||||
=item * Pipelining support is also missing. I have some ideas for an API, but
|
||||
doubt I'll ever implement it. Suffers from the same awkwardness and complexity
|
||||
as asynchronous calls.
|
||||
|
||||
=back
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue