Skip to content

Commit 877d44b

Browse files
committed
Merge branch 'kaare-master'
2 parents 07226c7 + 50763db commit 877d44b

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

README.pod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ string Pg can understand.
118118
#prepare an insertion of an array field;
119119
$sth.execute($sth.pg-array-str(@data));
120120

121+
=head3 B<pg_notifies>
122+
123+
$ret = $dbh.pg_notifies;
124+
125+
Looks for any asynchronous notifications received and returns a pg-notify object that looks like this
126+
127+
class pg-notify {
128+
has Str $.relname; # Channel Name
129+
has int32 $.be_pid; # Backend pid
130+
has Str $.extra; # Payload
131+
}
132+
133+
or nothing if there is no pending notifications.
134+
135+
The payload is optional and will always be an empty string for Postgres servers less than version 9.0.
136+
121137
=head2 SQLite
122138

123139
Supports basic CRUD operations and prepared statements with placeholders

lib/DBDish/Pg/Connection.pm6

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ method ping {
115115
}
116116
}
117117

118+
method pg-notifies {
119+
return $!pg_conn.pg-notifies;
120+
}
121+
118122
method _disconnect() {
119123
.PQfinish with $!pg_conn;
120124
$!pg_conn = Nil;

lib/DBDish/Pg/Native.pm6

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class PGresult is export is repr('CPointer') {
8585
}
8686
}
8787

88+
class pg-notify {
89+
has Str $.relname; # char* relname
90+
has int32 $.be_pid; # int be_pid
91+
has Str $.extra; # char* extra
92+
}
93+
8894
class PGconn is export is repr('CPointer') {
8995
method PQexec(str --> PGresult) is native(LIB) { * }
9096
method PQexecPrepared(
@@ -115,6 +121,28 @@ class PGconn is export is repr('CPointer') {
115121
die "Can't allocate memory!"
116122
}
117123
}
124+
method pg-notifies {
125+
126+
class PGnotify is repr('CStruct') {
127+
has Str $.relname; # char* relname
128+
has int32 $.be_pid; # int be_pid
129+
has Str $.extra; # char* extra
130+
}
131+
sub PQnotifies(PGconn) returns Pointer is native(LIB) is export { * }
132+
133+
my \ptr = PQnotifies(self);
134+
LEAVE { PQfreemem(ptr) if ptr }
135+
with ptr {
136+
with my PGnotify $pgnote = nativecast(PGnotify, $_) {
137+
my $note = pg-notify.new(
138+
relname => $_.relname,
139+
be_pid => $_.be_pid,
140+
extra => $_.extra,
141+
);
142+
return $note;
143+
}
144+
}
145+
}
118146

119147
method new(Str $conninfo) { # Our constructor
120148
sub PQconnectdb(str --> PGconn) is native(LIB) { * };

t/36-pg-native.t

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
v6;
2+
use Test;
3+
use DBIish;
4+
5+
plan 13;
6+
7+
my %con-parms;
8+
9+
# If env var set, no parameter needed.
10+
%con-parms<dbname> = 'dbdishtest' unless %*ENV<PGDATABASE>;
11+
%con-parms<user> = 'postgres' unless %*ENV<PGUSER>;
12+
13+
my $dbh;
14+
15+
try {
16+
$dbh = DBIish.connect('Pg', |%con-parms);
17+
CATCH {
18+
when X::DBIish::LibraryMissing | X::DBDish::ConnectionFailed {
19+
diag "$_\nCan't continue.";
20+
}
21+
default { .throw; }
22+
}
23+
}
24+
without $dbh {
25+
skip-rest 'prerequisites failed';
26+
exit;
27+
}
28+
29+
ok $dbh, 'Connected';
30+
lives-ok { $dbh.do('LISTEN test') }, 'Listen to test';
31+
my $note = $dbh.pg-notifies;
32+
isa-ok $note, Any, 'No notification';
33+
lives-ok { $dbh.do('NOTIFY test') }, 'Notify test';
34+
lives-ok { $dbh.do("NOTIFY test, 'Payload'") }, 'Notify test w/payload';
35+
$note = $dbh.pg-notifies;
36+
isa-ok $note, 'DBDish::Pg::Native::pg-notify', 'A notification received';
37+
is $note.relname, 'test', 'Test channel';
38+
isa-ok $note.be_pid, Int, 'Pid';
39+
is $note.extra, '', 'No extras';
40+
$note = $dbh.pg-notifies;
41+
isa-ok $note, 'DBDish::Pg::Native::pg-notify', 'A notification received';
42+
is $note.relname, 'test', 'Test channel';
43+
isa-ok $note.be_pid, Int, 'Pid';
44+
is $note.extra, 'Payload', 'w/ extras';

0 commit comments

Comments
 (0)