Pg-PQ

 view release on metacpan or  search on metacpan

PQ.xs  view on Meta::CPAN

    RETVAL

PGresult *PQgetResult(PGconn *conn);
ALIAS:
    result = 0

int PQconsumeInput(PGconn *conn);

int PQisBusy(PGconn *conn)
ALIAS:
    busy = 0

int PQsetnonblocking(PGconn *conn, int arg)

int PQisnonblocking(PGconn *conn);

int nonBlocking(PGconn *conn, SV *nb = &PL_sv_undef)
CODE:
    if (SvOK(nb))
        PQsetnonblocking(conn, SvIV(nb));
    RETVAL = PQisnonblocking(conn);

lib/Pg/PQ.pm  view on Meta::CPAN


  use Pg::PQ qw(:pgres_polling);

  my $dbc = Pg::PQ::Conn->new(dbname => 'test',
                              host => 'dbserver');

  $dbc->sendQuery("select * from foo");

  while (1) {
    $dbc->consumeInput;
    last unless $dbc->busy
    # do something else
    ...
  }

  my $res = $dbc->result;
  my @rows = $res->rows;

  print "query result:\n", Dumper \@rows;

=head1 DESCRIPTION

lib/Pg/PQ.pm  view on Meta::CPAN

Using C<sendQuery> and C<result> solves one of L</exec>'s problems: if
a command string contains multiple SQL commands, the results of those
commands can be obtained individually.

This allows a simple form of overlapped processing, by the way: the
client can be handling the results of one command while the server is
still working on later queries in the same command string.

However, calling C<result> will still cause the client to block until the
server completes the next SQL command. This can be avoided by proper
use of the C<consumeInput> and L</busy> methods described next.

=item $ok = $dbc->consumeInput

If input is available from the server, consume it.

C<consumeInput> normally returns 1 indicating "no error", but returns
0 if there was some kind of trouble (in which case L</errorMessage> can
be consulted). Note that the result does not say whether any input
data was actually collected.

After calling C<consumeInput>, the application can check L</busy>
and/or L</notifies> to see if their state has changed.

C<consumeInput> can be called even if the application is not prepared
to deal with a result or notification just yet. The method will read
available data and save it in a buffer, thereby causing a C<select>
read-ready indication to go away.

=item $ok = $dbc->busy

Returns 1 if a command is busy, that is, L</result> would block waiting
for input. A 0 return indicates that C<result> can be called with
assurance of not blocking.

C<busy> will not itself attempt to read data from the server;
therefore L</consumeInput> must be invoked first, or the busy state
will never end.

=item $nb = $dbc->nonBlocking

=item $dbc->nonBlocking($bool)

This methods get and sets the non blocking status of the database connection.

=item $dbc->flush

lib/Pg/PQ.pm  view on Meta::CPAN


After some query is dispatched to the database using any of the
asynchronous send methods (C<sendQuery>, C<sendPrepare>,
C<sendQueryPrepared>, C<sendDescribePrepared> or
C<sendDescribePortal>) one of the conditions will be input available
from the server, which in terms of C<select> means readable data on
the file descriptor identified by C<socket>.

When the main loop detects input ready, it should call C<consumeInput>
to read the input. It can then call C<isBusy>, followed by C<result>
if C<busy> returns false (0).

It can also call C<notifies> to detect C<NOTIFY> messages (see Section
31.7 of the PostgreSQL documentation).

A client that uses C<sendQuery>/C<result> can also attempt to cancel a
command that is still being processed by the server (see Section 31.5
of the PostgreSQL documentation). But regardless of the return value
of C<cancel>, the application must continue with the normal
result-reading sequence using C<result>. A successful cancellation
will simply cause the command to terminate sooner than it would have

t/Pg-PQ.t  view on Meta::CPAN


$res = $conn->execPrepared(sth2 => 12);
tuples_ok($res, 3, "select prepared");

is_deeply([$res->rows], [[13, 169], [14, 196], [8378, 70190884]], "rows");
is_deeply([$res->columns], [[13, 14, 8378], [169, 196, 70190884]], "columns");

ok($conn->sendQueryPrepared(sth2 => 12));
while (1) {
    $conn->consumeInput;
    last unless $conn->busy;
    sleep 1;
}

$res = $conn->result;
tuples_ok($res, 3, "select send prepared");

is_deeply([$res->rows], [[13, 169], [14, 196], [8378, 70190884]], "send rows");
is_deeply([$res->columns], [[13, 14, 8378], [169, 196, 70190884]], "send columns");




( run in 0.308 second using v1.01-cache-2.11-cpan-87723dcf8b7 )