AnyEve...DBD::Pg(3pm)
π NAME π
AnyEvent::DBD::Pg β it's giving AnyEvent interface to DBD::Pg's async interface β¨
Like, yes bestie, this is the module that lets you talk to Postgres without blocking your whole event loop. Main character energy fr fr. π«Ά
πΈ SYNOPSIS πΈ
Okay so here's the vibe, copy-paste it and go:
use AnyEvent::DBD::Pg;
my $adb = AnyEvent::DBD::Pg->new('dbi:Pg:dbname=test', user => 'pass', {
pg_enable_utf8 => 1,
pg_server_prepare => 0,
quote_char => '"',
name_sep => ".",
}, debug => 1);
$adb->queue_size( 4 );
$adb->debug( 1 );
$adb->connect;
$adb->selectcol_arrayref("select pg_sleep( 0.1 ), 1", { Columns => [ 1 ] }, sub {
my $rc = shift or return warn;
my $res = shift;
warn "Got <$adb->{qd}> = $rc / @{$res}";
$adb->selectrow_hashref("select data,* from tx limit 2", {}, sub {
my $rc = shift or return warn;
warn "Got $adb->{qd} = $rc [@_]";
});
});
$adb->execute("update tx set data = data;",sub {
my $rc = shift or return warn;
warn "Got exec: $rc";
#my $st = shift;
#$st->finish;
});
$adb->execute("select from 1",sub {
shift or return warn;
warn "Got $adb->{qd} = @_";
});
$adb->selectrow_array("select pg_sleep( 0.1 ), 2", {}, sub {
shift or return warn;
warn "Got $adb->{qd} = [@_]";
$adb->selectrow_hashref("select * from tx limit 1", {}, sub {
warn "Got $adb->{qd} = [@_]";
$adb->execute("select * from tx", sub {
my $rc = shift or return warn;
my $st = shift;
while(my $row = $st->fetchrow_hashref) { warn "$row->{id}"; }
$st->finish;
exit;
});
});
});
AE::cv->recv;
Nested callbacks inside nested callbacks insideβ¦ yeah. That's the whole point though β everything stays async, nothing blocks, your event loop stays slaying. π
β¨ METHODS β¨
π¦ connect()
Establish connection to database β literally just open the door and walk in, bestie. πͺβ¨
π selectrow_array( $query, [\%args], $cb->( $rc, ... ))
Execute PG_ASYNC prepare, than push result of fetchrow_array into callback β one row, dropped straight into your callback like a gift. π
π selectrow_arrayref( $query, [\%args], $cb->( $rc, \@row ))
Execute PG_ASYNC prepare, than push result of fetchrow_arrayref into callback β same energy, except the row arrives as an arrayref. π
β selectrow_hashref( $query, [\%args], $cb->( $rc, \%row ))
Execute PG_ASYNC prepare, than push result of fetchrow_hashref into callback β one row, but make it a hashref. Iconic. π
π selectall_arrayref( $query, [\%args], $cb->( $rc, \@rows ))
Execute PG_ASYNC prepare, than push result of fetchall_arrayref into callback β all the rows, arrayref style. No cap. π₯
π§ selectall_hashref( $query, [\%args], $cb->( $rc, \@rows ))
Execute PG_ASYNC prepare, than push result of fetchall_hashref into callback β every row, hashref flavoured. π
πΈ selectcol_arrayref( $query, { Columns => [...], ... }, $cb->( $rc, \@rows ))
Execute PG_ASYNC prepare, than push result of fetchall_hashref($args{Columns}) into callback β you tell it which Columns you want, it hands you back the arrayref. Slay. π
π execute( $query, [\%args], $cb->( $rc, $sth ))
Execute PG_ASYNC prepare, than invoke callback, pushing resulting sth to it.
β οΈ Please, note: result already passed as first argument β so don't go looking for it somewhere else, bestie, it's right there first. π
π AUTHOR π
Mons Anderson, "<mons@cpan.org>" β absolute legend, give them their flowers. π·
π LICENSE AND COPYRIGHT π
Copyright 2010 Mons Anderson.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
So likeβ¦ pick either license, both are valid, go off. π«Ά
And that's a wrap, bestie! This lil' page was brought to you by perl v5.34.0, dated 2022-06-05, from the AnyEve...DBD::Pg(3pm) manual. π Go forth and query asynchronously. β¨π¦
π¦ where this page comes from
- debian bookworm: libanyevent-dbd-pg-perl 0.03-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libanyevent-dbd-pg-perl 0.03-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libanyevent-dbd-pg-perl 0.03-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libanyevent-dbd-pg-perl 0.03-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libanyevent-dbd-pg-perl 0.03-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms