Async/Storage.pm

filter: Statementstmt 50.8% 159 / 313 Branchbran 40.0% 36 / 90 Conditioncond 46.3% 19 / 41 MC/DCmcdc 16.0% 4 / 25 Subroutinesub 53.0% 43 / 81 Podpod 100% 26 / 26 totaltotal 49.8% 287 / 576 CC 63 SCAR 16.3
CC
63
Cov
48%
__ANON__
30.0
_query_async_pinned
12.0
__ANON__
12.0
CRAP 5.1
↑ Summary Async/ConnectInfo.pm »
1 package DBIO::PostgreSQL::Async::Storage;
2 # ABSTRACT: Async PostgreSQL storage driver using EV::Pg
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::PostgreSQL::SQLMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DBIO::PostgreSQL::Async::ConnectInfo 'conninfo_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use DBIO::PostgreSQL::Async::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use DBIO::PostgreSQL::Async::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 sub new {
18 3 ▶ my ($class, $schema, $args) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
19 my $self = bless {
20 schema => $schema,
21 pool => undef,
22 connect_info => undef,
23 sql_maker => undef,
24 sql_maker_class => 'DBIO::PostgreSQL::SQLMaker',
25 _prepared_cache => {},
26 _listeners => {},
27 _conninfo_provider => undef,
28 3 ▶ debug => $ENV{DBIO_TRACE} || 0,
Condition50%1/2
$ENV{'DBIO_TRACE'} || 0
l!l
03
MC/DC0%0/1
$ENV{'DBIO_TRACE'} || 0
$ENV{'DBIO_TRACE'}
Truth table50%1/2
A
A= $ENV{'DBIO_TRACE'}
Aresult
11
00
29 debugobj => undef,
30 }, $class;
31 3 $self;
32 }
33  
34  
35 1 ▶sub future_class { 'Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
36  
37  
38 sub connect_info {
39 2 ▶ my ($self, $info) = @_;
Subroutine connect_info: called CC=5 SCAR=18.9
Pod documented
40 2 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 2 0 2
41 2 ▶ $self->disconnect if $self->{pool} || $self->{_listen_pg};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} or $self->{"_listen_pg"} 0 2 2
Condition33%1/3
$self->{"pool"} or $self->{"_listen_pg"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{"pool"} or $self->{"_listen_pg"}
$self->{"pool"} $self->{"_listen_pg"}
Truth table33%1/3
A or B
A= $self->{"pool"}B= $self->{"_listen_pg"}
ABresult
1X1
011
000
42 2 $self->{connect_info} = $info;
43  
44 2 ▶ if ($self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_access_broker_connect_info($info)) { } 0 2 2
45 0 $self->_setup_access_broker($info->[0]);
46 0 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info(
47 $self->_current_async_connect_info($self->access_broker_mode)
48 );
49 0 $self->{_conninfo} = $conninfo;
50 0 $self->{_pool_size} = $pool_size;
51 0 $self->{_opts} = $opts;
52 }
53 else {
54 2 $self->_clear_access_broker;
55 2 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info($info);
56 2 $self->{_conninfo} = $conninfo;
57 2 $self->{_pool_size} = $pool_size;
58 2 $self->{_opts} = $opts;
59 }
60 }
61 2 return $self->{connect_info};
62 }
63  
64 sub _current_async_connect_info {
65 1 ▶ my ($self, $mode) = @_;
Subroutine _current_async_connect_info: called CC=5 SCAR=18.1
66  
67 1 my $connect_info = $self->current_access_broker_connect_info($mode);
68 1 ▶ return [$connect_info, {}] if $connect_info && ref $connect_info eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $connect_info and ref $connect_info eq "HASH" 0 1 1
Condition33%1/3
$connect_info and ref $connect_info eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
$connect_info and ref $connect_info eq "HASH"
$connect_info ref $connect_info eq "HASH"
Truth table33%1/3
A and B
A= $connect_infoB= ref $connect_info eq "HASH"
ABresult
0X0
100
111
69 1 ▶ return $connect_info if $connect_info;
Branch: 1 branch
BranchTrueFalseTotal
if $connect_info 0 1 1
70  
71 1 ▶ return [ $self->{_conninfo}, $self->{_opts} || {} ];
Condition50%1/2
$self->{'_opts'} || {}
l!l
10
MC/DC0%0/1
$self->{'_opts'} || {}
$self->{'_opts'}
Truth table50%1/2
A
A= $self->{'_opts'}
Aresult
11
00
72 }
73  
74 sub _normalize_async_connect_info {
75 2 ▶ my ($self, $info) = @_;
Subroutine _normalize_async_connect_info: called CC=5 SCAR=16.7
76  
77 2 my $conninfo = $info->[0];
78 2 ▶ $conninfo = ref($conninfo) eq 'HASH' ? { %$conninfo } : $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
ref $conninfo eq 'HASH' ? : 2 0 2
79  
80 2 my $opts = $info->[1];
81 2 ▶ $opts = ref($opts) eq 'HASH' ? { %$opts } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq 'HASH' ? : 0 2 2
82  
83 2 my $pool_size = 5;
84 2 ▶ if (ref($conninfo) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conninfo eq "HASH") 2 0 2
85 2 ▶ $pool_size = delete $conninfo->{pool_size} // 5;
Condition50%1/2
delete $conninfo->{"pool_size"} // 5
l!l
02
MC/DC0%0/1
delete $conninfo->{"pool_size"} // 5
delete $conninfo->{"pool_size"}
Truth table50%1/2
A
A= delete $conninfo->{"pool_size"}
Aresult
11
00
86 }
87  
88 2 return ($conninfo, $pool_size, $opts);
89 }
90  
91 sub _async_broker_conninfo {
92 0 ▶ my ($self, $mode) = @_;
Subroutine _async_broker_conninfo: not called CC=1 SCAR=6.9
93 0 my ($conninfo) = $self->_normalize_async_connect_info(
94 $self->_current_async_connect_info($mode)
95 );
96 0 return $conninfo;
97 }
98  
99  
100 sub pool {
101 16 ▶ my $self = shift;
Subroutine pool: called CC=3 SCAR=19.6
Pod documented
102 16 ▶ $self->{pool} ||= do {
Condition33%1/3
$self->{"pool"} ||= do {
l!l&&r!l&&!r
1600
MC/DC0%0/2
$self->{"pool"} ||= do { my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::PostgreSQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::Async::Pool"->new(%args) }
$self->{"pool"} do { my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::PostgreSQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::Async::Pool"->new(%args) }
Truth table33%1/3
A ||= B
A= $self->{"pool"}B= do { my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::PostgreSQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::Async::Pool"->new(%args) }
ABresult
1X1
011
000
103 my %args = (
104 size => $self->{_pool_size},
105 0 ▶ on_error => sub { warn "DBIO::PostgreSQL::Async pool error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 );
107  
108 0 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 0 0 0
109 0 $args{conninfo_provider} = $provider;
110 }
111 else {
112 0 $args{conninfo} = $self->_conninfo_string;
113 }
114  
115 0 DBIO::PostgreSQL::Async::Pool->new(%args);
116 };
117 }
118  
119 sub _conninfo_string {
120 1 ▶ my ($self, $ci) = @_;
Subroutine _conninfo_string: called CC=2 SCAR=7.1
121 1 ▶ $ci = $self->_current_async_connect_info($self->access_broker_mode)->[0]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ci 1 0 1
122 if ! defined $ci;
123 1 return conninfo_string($ci);
124 }
125  
126 # --- SQL Generation ---
127  
128  
129 sub sql_maker {
130 10 ▶ my $self = shift;
Subroutine sql_maker: called CC=2 SCAR=7.0
Pod documented
131 10 ▶ $self->{sql_maker} ||= do {
Condition66%2/3
$self->{"sql_maker"} ||= do {
l!l&&r!l&&!r
730
MC/DC0%0/2
$self->{"sql_maker"} ||= do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "\"", "name_sep", ".", "limit_dialect", "LimitOffset") }
$self->{"sql_maker"} do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "\"", "name_sep", ".", "limit_dialect", "LimitOffset") }
Truth table66%2/3
A ||= B
A= $self->{"sql_maker"}B= do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "\"", "name_sep", ".", "limit_dialect", "LimitOffset") }
ABresult
1X1
011
000
132 3 my $class = $self->{sql_maker_class};
133 3 $class->new(
134 quote_char => '"',
135 name_sep => '.',
136 limit_dialect => 'LimitOffset',
137 );
138 };
139 }
140  
141 sub _generate_sql {
142 0 ▶ my ($self, $op, @args) = @_;
Subroutine _generate_sql: not called CC=2 SCAR=17.9
143 0 my $sm = $self->sql_maker;
144 my $method = {
145 select => 'select',
146 select_single => 'select',
147 insert => 'insert',
148 update => 'update',
149 delete => 'delete',
150 0 ▶ }->{$op} or croak "Unknown operation: $op";
Branch: 1 branch
BranchTrueFalseTotal
my $method = {"select", "select", "select_single", "select", "insert", "insert", "update", "update", "delete", "delete"}->{$op} or croak("Unknown operation: $op") 0 0 0
151  
152 0 return $sm->$method(@args);
153 }
154  
155 # --- Async Query Execution ---
156  
157  
158 sub select_async {
159 2 ▶ my $self = shift;
Subroutine select_async: called CC=1 SCAR=0.0
Pod documented
160 2 return $self->_run_crud('select', $self->_pool_runner, @_);
161 }
162  
163  
164 sub select_single_async {
165 1 ▶ my $self = shift;
Subroutine select_single_async: called CC=1 SCAR=0.0
Pod documented
166 1 return $self->_run_crud('select_single', $self->_pool_runner, @_);
167 }
168  
169  
170 sub insert_async {
171 1 ▶ my $self = shift;
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
172 1 return $self->_run_crud('insert', $self->_pool_runner, @_);
173 }
174  
175  
176 sub update_async {
177 1 ▶ my $self = shift;
Subroutine update_async: called CC=1 SCAR=0.0
Pod documented
178 1 return $self->_run_crud('update', $self->_pool_runner, @_);
179 }
180  
181  
182 sub delete_async {
183 1 ▶ my $self = shift;
Subroutine delete_async: called CC=1 SCAR=0.0
Pod documented
184 1 return $self->_run_crud('delete', $self->_pool_runner, @_);
185 }
186  
187 # Build SQL for a CRUD op and execute it through $runner, a coderef
188 # that takes ($sql, \@bind) and returns a Future of result rows.
189 # All the PG-specific shaping (RETURNING on insert, first-row for
190 # select_single) lives here exactly once, so the pooled path
191 # (_pool_runner) and the pinned txn path (a pinned runner supplied by
192 # TransactionContext) share identical behavior.
193 sub _run_crud {
194 6 ▶ my ($self, $op, $runner, @args) = @_;
Subroutine _run_crud: called CC=8 SCAR=20.9
195  
196 6 my $sm = $self->sql_maker;
197 6 ▶ if ($op eq 'select' || $op eq 'select_single') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq 'select' or $op eq 'select_single') { } 3 3 6
elsif ($op eq 'insert') { } 1 2 3
elsif ($op eq 'update') { } 1 1 2
elsif ($op eq 'delete') { } 1 0 1
Condition100%3/3
$op eq "select" or $op eq "select_single"
l!l&&r!l&&!r
213
MC/DC100%2/2
$op eq "select" or $op eq "select_single"
$op eq "select" $op eq "select_single"
Truth table100%3/3
A or B
A= $op eq "select"B= $op eq "select_single"
ABresult
1X1
011
000
198 3 my ($sql, @bind) = $sm->select(@args);
199 3 my $f = $runner->($sql, \@bind);
200 3 ▶ return $f unless $op eq 'select_single';
Branch: 1 branch
BranchTrueFalseTotal
unless $op eq "select_single" 2 1 3
201 return $f->then(sub {
202 1 ▶ my @rows = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
203 1 ▶ return @rows ? $rows[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 1 0 1
204 1 });
205 }
206 elsif ($op eq 'insert') {
207 1 my ($sql, @bind) = $sm->insert(@args);
208 # PostgreSQL RETURNING for auto-generated columns
209 1 ▶ $sql .= ' RETURNING *' unless $sql =~ /RETURNING/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /RETURNING/i 1 0 1
210 1 return $runner->($sql, \@bind);
211 }
212 elsif ($op eq 'update') {
213 1 my ($sql, @bind) = $sm->update(@args);
214 1 return $runner->($sql, \@bind);
215 }
216 elsif ($op eq 'delete') {
217 1 my ($sql, @bind) = $sm->delete(@args);
218 1 return $runner->($sql, \@bind);
219 }
220 0 croak "Unknown CRUD operation: $op";
221 }
222  
223 # Runner that executes on a freshly-acquired pooled connection and
224 # releases it when done. This is the normal (non-transactional) path.
225 sub _pool_runner {
226 6 ▶ my $self = shift;
Subroutine _pool_runner: called CC=1 SCAR=0.0
227 return sub {
228 6 ▶ my ($sql, $bind) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
229 6 return $self->_query_async($sql, $bind);
230 6 };
231 }
232  
233 # Runner that executes on a pinned connection without releasing it.
234 # Used by TransactionContext so CRUD inside a txn hits the same
235 # connection that BEGIN/COMMIT ran on.
236 sub _pinned_runner {
237 0 ▶ my ($self, $pg) = @_;
Subroutine _pinned_runner: not called CC=1 SCAR=6.9
238 return sub {
239 0 ▶ my ($sql, $bind) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 0 return $self->_query_async_pinned($pg, $sql, $bind);
241 0 };
242 }
243  
244 # Build SQL and run a CRUD op on a pinned connection. Entry point used
245 # by TransactionContext; shares _run_crud's builders/post-processing.
246 sub _run_crud_pinned {
247 0 ▶ my ($self, $op, $pg, @args) = @_;
Subroutine _run_crud_pinned: not called CC=1 SCAR=6.9
248 0 return $self->_run_crud($op, $self->_pinned_runner($pg), @args);
249 }
250  
251 # Low-level async query dispatch
252  
253 sub _query_async {
254 6 ▶ my ($self, $sql, $bind) = @_;
Subroutine _query_async: called CC=3 SCAR=11.4
255 6 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
60
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
256  
257 6 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 6 6
258  
259 return $self->pool->acquire->then(sub {
260 6 ▶ my $pg = shift;
Subroutine __ANON__: called CC=1 SCAR=0.1
261 6 my $f = Future->new;
262 $pg->query_params($sql, $bind, sub {
263 6 ▶ my ($rows, $err) = @_;
Subroutine __ANON__: called CC=3 SCAR=12.0
264 6 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 6 6
265 0 $f->fail($err);
266 } else {
267 6 ▶ $f->done(ref $rows eq 'ARRAY' ? @$rows : $rows);
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 6 0 6
268 }
269 6 $self->pool->release($pg);
270 6 });
271 6 return $f;
272 6 });
273 }
274  
275 # Like _query_async but does NOT release the connection.
276 # Used for queries within a pinned transaction.
277 sub _query_async_pinned {
278 0 ▶ my ($self, $pg, $sql, $bind) = @_;
Subroutine _query_async_pinned: not called CC=3 SCAR=24.8
279 0 ▶ $bind //= [];
Condition0%0/2
$bind //= []
l!l
00
MC/DC0%0/1
$bind //= []
$bind
Truth table0%0/2
A
A= $bind
Aresult
11
00
280  
281 0 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
282  
283 0 my $f = Future->new;
284 $pg->query_params($sql, $bind, sub {
285 0 ▶ my ($rows, $err) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
286 0 ▶ if ($err) { $f->fail($err) } else { $f->done(ref $rows eq 'ARRAY' ? @$rows : $rows) }
Branch: 2 branches
BranchTrueFalseTotal
if ($err) { } 0 0 0
ref $rows eq 'ARRAY' ? : 0 0 0
287 # Do NOT release — connection is pinned for the duration of the txn
288 0 });
289 0 return $f;
290 }
291  
292 sub _debug_query {
293 0 ▶ my ($self, $sql, $bind) = @_;
Subroutine _debug_query: not called CC=2 SCAR=17.9
294 0 ▶ my $bind_str = join(', ', map { defined $_ ? "'$_'" : 'NULL' } @$bind);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
295 0 warn "$sql: $bind_str\n";
296 }
297  
298 # --- Transactions ---
299  
300  
301 sub txn_do_async {
302 0 ▶ my ($self, $coderef, @args) = @_;
Subroutine txn_do_async: not called CC=1 SCAR=6.9
Pod documented
303  
304 return $self->pool->acquire_txn->then(sub {
305 0 ▶ my $pg = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
306  
307 0 my $txn_ctx = DBIO::PostgreSQL::Async::TransactionContext->new(
308 storage => $self,
309 pg => $pg,
310 );
311  
312 0 my $f = Future->new;
313  
314 $pg->query('BEGIN', sub {
315 0 ▶ my (undef, $err) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
316 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
317 0 $self->pool->release($pg);
318 0 $f->fail("BEGIN failed: $err");
319 0 return;
320 }
321  
322 0 my $inner = eval { $coderef->($txn_ctx, @args) };
323 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
324 0 my $error = $@;
325 $pg->query('ROLLBACK', sub {
326 0 ▶ $self->pool->release($pg);
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 0 $f->fail($error);
328 0 });
329 0 return;
330 }
331  
332 # If coderef returned a Future, chain COMMIT/ROLLBACK
333 0 ▶ if (ref $inner && $inner->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inner and $inner->can('then')) { } 0 0 0
Condition0%0/3
ref $inner and $inner->can("then")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $inner and $inner->can("then")
ref $inner $inner->can("then")
Truth table0%0/3
A and B
A= ref $innerB= $inner->can("then")
ABresult
0X0
100
111
334 $inner->then(sub {
335 0 ▶ my @result = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 my $commit_f = Future->new;
337 $pg->query('COMMIT', sub {
338 0 ▶ my (undef, $cerr) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
339 0 $self->pool->release($pg);
340 0 ▶ if ($cerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cerr) { } 0 0 0
341 0 $commit_f->fail("COMMIT failed: $cerr");
342 } else {
343 0 $commit_f->done(@result);
344 }
345 0 });
346 0 return $commit_f;
347 })->catch(sub {
348 0 ▶ my $error = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 my $rb_f = Future->new;
350 $pg->query('ROLLBACK', sub {
351 0 ▶ $self->pool->release($pg);
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 0 $rb_f->fail($error);
353 0 });
354 0 return $rb_f;
355 0 ▶ })->on_done(sub { $f->done(@_) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 ▶ ->on_fail(sub { $f->fail(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 } else {
358 # Coderef returned a plain value — commit immediately
359 $pg->query('COMMIT', sub {
360 0 ▶ my (undef, $cerr) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
361 0 $self->pool->release($pg);
362 0 ▶ if ($cerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cerr) { } 0 0 0
363 0 $f->fail("COMMIT failed: $cerr");
364 } else {
365 0 $f->done($inner);
366 }
367 0 });
368 }
369 0 });
370  
371 0 return $f;
372 0 });
373 }
374  
375 # --- Pipeline Mode ---
376  
377  
378 sub pipeline {
379 0 ▶ my ($self, $coderef) = @_;
Subroutine pipeline: not called CC=1 SCAR=6.9
Pod documented
380  
381 return $self->pool->acquire->then(sub {
382 0 ▶ my $pg = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
383 0 $pg->enter_pipeline;
384  
385 0 my $result = eval { $coderef->($self) };
386 0 my $err = $@;
387  
388 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
389 0 $pg->exit_pipeline;
390 0 $self->pool->release($pg);
391 0 return Future->fail($err);
392 }
393  
394 # Sync the pipeline — callback fires when all results are in
395 0 my $f = Future->new;
396 $pg->pipeline_sync(sub {
397 0 ▶ $pg->exit_pipeline;
Subroutine __ANON__: not called CC=3 SCAR=24.8
398 0 $self->pool->release($pg);
399 0 ▶ if (ref $result && $result->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result and $result->can('then')) { } 0 0 0
Condition0%0/3
ref $result and $result->can("then")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result and $result->can("then")
ref $result $result->can("then")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("then")
ABresult
0X0
100
111
400 0 ▶ $result->on_done(sub { $f->done(@_) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 ▶ ->on_fail(sub { $f->fail(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
402 } else {
403 0 $f->done($result);
404 }
405 0 });
406  
407 0 return $f;
408 0 });
409 }
410  
411 # --- LISTEN/NOTIFY ---
412  
413  
414 sub listen {
415 2 ▶ my ($self, $channel, $cb) = @_;
Subroutine listen: called CC=3 SCAR=11.0
Pod documented
416  
417 2 $self->{_listeners}{$channel} = $cb;
418  
419 # Use a dedicated connection for LISTEN (not from the pool).
420 # EV::Pg->new returns before the socket is actually connected; query()
421 # dispatched on a not-yet-connected handle throws "not connected".
422 # We buffer LISTEN/UNLISTEN until on_connect fires and then flush.
423 2 ▶ $self->{_listen_pg} ||= do {
Condition66%2/3
$self->{"_listen_pg"} ||= do {
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"_listen_pg"} ||= do { require EV::Pg; $self->{"_listen_pending"} = []; $self->{"_listen_connected"} = 0; my $pg = "EV::Pg"->new("conninfo", $self->_conninfo_string, "keep_alive", 1, "on_connect", sub { $self->{"_listen_connected"} = 1; my $q = delete $self->{"_listen_pending"} || []; $self->{"_listen_pending"} = []; $self->{"_listen_pg"}->query($_, sub { } ) foreach (@$q); } , "on_error", sub { warn "LISTEN connection error: $_[0]\n"; } , "on_notify", sub { my($ch, $payload, $pid) = @_; if (my $handler = $self->{"_listeners"}{$ch}) { &$handler($ch, $payload, $pid); } ; } ); $pg }
$self->{"_listen_pg"} do { require EV::Pg; $self->{"_listen_pending"} = []; $self->{"_listen_connected"} = 0; my $pg = "EV::Pg"->new("conninfo", $self->_conninfo_string, "keep_alive", 1, "on_connect", sub { $self->{"_listen_connected"} = 1; my $q = delete $self->{"_listen_pending"} || []; $self->{"_listen_pending"} = []; $self->{"_listen_pg"}->query($_, sub { } ) foreach (@$q); } , "on_error", sub { warn "LISTEN connection error: $_[0]\n"; } , "on_notify", sub { my($ch, $payload, $pid) = @_; if (my $handler = $self->{"_listeners"}{$ch}) { &$handler($ch, $payload, $pid); } ; } ); $pg }
Truth table66%2/3
A ||= B
A= $self->{"_listen_pg"}B= do { require EV::Pg; $self->{"_listen_pending"} = []; $self->{"_listen_connected"} = 0; my $pg = "EV::Pg"->new("conninfo", $self->_conninfo_string, "keep_alive", 1, "on_connect", sub { $self->{"_listen_connected"} = 1; my $q = delete $self->{"_listen_pending"} || []; $self->{"_listen_pending"} = []; $self->{"_listen_pg"}->query($_, sub { } ) foreach (@$q); } , "on_error", sub { warn "LISTEN connection error: $_[0]\n"; } , "on_notify", sub { my($ch, $payload, $pid) = @_; if (my $handler = $self->{"_listeners"}{$ch}) { &$handler($ch, $payload, $pid); } ; } ); $pg }
ABresult
1X1
011
000
424 1 require EV::Pg;
425 1 $self->{_listen_pending} = [];
426 1 $self->{_listen_connected} = 0;
427 my $pg = EV::Pg->new(
428 conninfo => $self->_conninfo_string,
429 keep_alive => 1,
430 on_connect => sub {
431 1 ▶ $self->{_listen_connected} = 1;
Subroutine __ANON__: called CC=3 SCAR=11.1
432 1 ▶ my $q = delete $self->{_listen_pending} || [];
Condition50%1/2
delete $self->{'_listen_pending'} || []
l!l
10
MC/DC0%0/1
delete $self->{'_listen_pending'} || []
delete $self->{'_listen_pending'}
Truth table50%1/2
A
A= delete $self->{'_listen_pending'}
Aresult
11
00
433 1 $self->{_listen_pending} = [];
434 1 ▶ $self->{_listen_pg}->query($_, sub {}) for @$q;
Subroutine __ANON__: called CC=1 SCAR=0.0
435 },
436 0 ▶ on_error => sub { warn "LISTEN connection error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
437 on_notify => sub {
438 3 ▶ my ($ch, $payload, $pid) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
439 3 ▶ if (my $handler = $self->{_listeners}{$ch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $self->{"_listeners"}{$ch}) 1 2 3
440 1 $handler->($ch, $payload, $pid);
441 }
442 },
443 1 );
444 1 $pg;
445 };
446  
447 2 my $quoted = $self->sql_maker->_quote($channel);
448 2 my $sql = "LISTEN $quoted";
449 2 ▶ if ($self->{_listen_connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_listen_connected'}) { } 1 1 2
450 1 ▶ $self->{_listen_pg}->query($sql, sub {});
Subroutine __ANON__: called CC=1 SCAR=0.0
451 } else {
452 1 push @{ $self->{_listen_pending} }, $sql;
453 }
454 }
455  
456  
457 sub unlisten {
458 1 ▶ my ($self, $channel) = @_;
Subroutine unlisten: called CC=3 SCAR=11.8
Pod documented
459 1 delete $self->{_listeners}{$channel};
460 1 ▶ if ($self->{_listen_pg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_listen_pg"}) 1 0 1
461 1 my $quoted = $self->sql_maker->_quote($channel);
462 1 my $sql = "UNLISTEN $quoted";
463 1 ▶ if ($self->{_listen_connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_listen_connected'}) { } 1 0 1
464 1 ▶ $self->{_listen_pg}->query($sql, sub {});
Subroutine __ANON__: called CC=1 SCAR=0.0
465 } else {
466 0 push @{ $self->{_listen_pending} }, $sql;
467 }
468 }
469 }
470  
471  
472 sub notify {
473 3 ▶ my ($self, $channel, $payload) = @_;
Subroutine notify: called CC=3 SCAR=11.1
Pod documented
474  
475 3 ▶ croak 'Channel name required' unless defined $channel && $channel ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel and $channel ne "" 1 2 3
Condition66%2/3
defined $channel and $channel ne ""
!ll&&!rl&&r
102
MC/DC50%1/2
defined $channel and $channel ne ""
defined $channel $channel ne ""
Truth table66%2/3
A and B
A= defined $channelB= $channel ne ""
ABresult
0X0
100
111
476  
477 return $self->pool->acquire->then(sub {
478 2 ▶ my $pg = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
479 2 my $f = Future->new;
480  
481 # pg_notify() with bind params -- NOTIFY itself takes no placeholders,
482 # and inlining the payload as a string literal invites quoting bugs.
483 $pg->query_params('SELECT pg_notify($1, $2)', [ $channel, $payload // '' ], sub {
484 2 ▶ my ($res, $err) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.4
485 2 $self->pool->release($pg);
486 2 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 2 2
487 0 $f->fail($err);
488 } else {
489 2 $f->done;
490 }
491 2 ▶ });
Condition100%2/2
$payload // ""
l!l
11
MC/DC100%1/1
$payload // ""
$payload
Truth table100%2/2
A
A= $payload
Aresult
11
00
492  
493 2 return $f;
494 2 });
495 }
496  
497 # --- COPY ---
498  
499  
500 sub copy_in {
501 0 ▶ my ($self, $table, $columns, $coderef) = @_;
Subroutine copy_in: not called CC=1 SCAR=6.9
Pod documented
502  
503 0 my $col_list = join(', ', map { $self->sql_maker->_quote($_) } @$columns);
504 0 my $quoted_table = $self->sql_maker->_quote($table);
505 0 my $sql = "COPY $quoted_table ($col_list) FROM STDIN";
506  
507 return $self->pool->acquire->then(sub {
508 0 ▶ my $pg = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
509 0 my $f = Future->new;
510  
511 $pg->query($sql, sub {
512 0 ▶ my ($status, $err) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
513 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
514 0 $self->pool->release($pg);
515 0 $f->fail($err);
516 0 return;
517 }
518  
519 my $put = sub {
520 0 ▶ my $row = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
521 0 ▶ my $line = join("\t", map { defined $_ ? $_ : '\N' } @$row) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
522 0 $pg->put_copy_data($line);
523 0 };
524  
525 0 eval { $coderef->($put) };
526 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
527 0 $pg->put_copy_end($@);
528 0 $self->pool->release($pg);
529 0 $f->fail($@);
530 } else {
531 0 $pg->put_copy_end;
532 0 $self->pool->release($pg);
533 0 $f->done(1);
534 }
535 0 });
536  
537 0 return $f;
538 0 });
539 }
540  
541 # --- Sync Fallbacks ---
542 # These allow sync methods (->all, ->first etc.) to work
543 # by blocking the event loop. Useful for scripts/migrations.
544  
545 sub select {
546 1 ▶ my $self = shift;
Subroutine select: called CC=1 SCAR=0.0
Pod documented
547 1 return $self->select_async(@_)->get;
548 }
549  
550 sub select_single {
551 0 ▶ my $self = shift;
Subroutine select_single: not called CC=1 SCAR=6.9
Pod documented
552 0 return $self->select_single_async(@_)->get;
553 }
554  
555 sub insert {
556 0 ▶ my $self = shift;
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
557 0 return $self->insert_async(@_)->get;
558 }
559  
560 sub update {
561 0 ▶ my $self = shift;
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
562 0 return $self->update_async(@_)->get;
563 }
564  
565 sub delete {
566 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
567 0 return $self->delete_async(@_)->get;
568 }
569  
570 sub txn_do {
571 0 ▶ my $self = shift;
Subroutine txn_do: not called CC=1 SCAR=6.9
Pod documented
572 0 return $self->txn_do_async(@_)->get;
573 }
574  
575 # --- Schema Integration ---
576  
577 0 ▶sub schema { $_[0]->{schema} }
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
578 0 ▶sub debug { $_[0]->{debug} }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
579  
580 0 ▶sub connected { defined $_[0]->{pool} && $_[0]->pool->available > 0 }
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{"pool"} and $_[0]->pool->available > 0 0 0 0
581  
582 sub disconnect {
583 1 ▶ my $self = shift;
Subroutine disconnect: called CC=3 SCAR=12.3
Pod documented
584 1 ▶ if ($self->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pool"}) 1 0 1
585 1 $self->{pool}->shutdown;
586 1 $self->{pool} = undef;
587 }
588 1 ▶ if ($self->{_listen_pg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_listen_pg"}) 0 1 1
589 0 $self->{_listen_pg}->finish;
590 0 $self->{_listen_pg} = undef;
591 }
592 }
593  
594 sub DESTROY {
595 2 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
596 2 ▶ $self->disconnect if $self->{pool};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} 1 1 2
597 }
598  
599 4 1;
600  
601 __END__
602  
603 =pod
604  
605 =encoding UTF-8
606  
607 =head1 NAME
608  
609 DBIO::PostgreSQL::Async::Storage - Async PostgreSQL storage driver using EV::Pg
610  
611 =head1 VERSION
612  
613 version 0.900000
614  
615 =head1 DESCRIPTION
616  
617 Implements L<DBIO::Storage::Async> using L<EV::Pg> — a non-blocking
618 PostgreSQL client that speaks libpq's async protocol directly.
619 No DBI, no DBD::Pg, just raw libpq performance.
620  
621 Features:
622  
623 =over 4
624  
625 =item * Pipeline mode — batch queries in a single network round-trip
626  
627 =item * Prepared statement caching
628  
629 =item * LISTEN/NOTIFY for real-time event streaming
630  
631 =item * COPY IN/OUT for bulk data transfer
632  
633 =item * Connection pooling with transaction pinning
634  
635 =back
636  
637 =head1 METHODS
638  
639 =head2 future_class
640  
641 Returns C<'Future'> — uses L<Future.pm|Future> from CPAN.
642  
643 =head2 connect_info
644  
645 $storage->connect_info([ \%conninfo, \%opts ]);
646  
647 Set connection parameters. C<%conninfo> is passed directly to
648 L<EV::Pg> as libpq connection parameters (host, dbname, user, etc.).
649  
650 =head2 pool
651  
652 Returns the L<DBIO::PostgreSQL::Async::Pool> connection pool.
653 Created lazily on first access.
654  
655 =head2 sql_maker
656  
657 Returns the L<DBIO::PostgreSQL::SQLMaker> instance, configured for PostgreSQL
658 (double-quote quoting, LIMIT/OFFSET dialect).
659  
660 =head2 select_async
661  
662 my $future = $storage->select_async($source, $select, $where, $attrs);
663  
664 Execute a SELECT query asynchronously. Returns a L<Future> that
665 resolves with the result rows (arrayrefs).
666  
667 =head2 select_single_async
668  
669 Like L</select_async> but returns only the first row.
670  
671 =head2 insert_async
672  
673 my $future = $storage->insert_async($source, \%vals);
674  
675 =head2 update_async
676  
677 my $future = $storage->update_async($source, \%vals, \%where);
678  
679 =head2 delete_async
680  
681 my $future = $storage->delete_async($source, \%where);
682  
683 =head2 txn_do_async
684  
685 my $future = $storage->txn_do_async(sub {
686 my ($storage) = @_;
687 # All queries in here use the same connection
688 $storage->insert_async(...)->then(sub { ... });
689 });
690  
691 Acquires a connection from the pool, issues BEGIN, executes the
692 coderef, and issues COMMIT on success or ROLLBACK on Future failure.
693  
694 =head2 pipeline
695  
696 my $future = $storage->pipeline(sub {
697 my ($storage) = @_;
698 my @futures;
699 push @futures, $storage->insert_async('artist', { name => $_ })
700 for @names;
701 return Future->needs_all(@futures);
702 });
703  
704 Execute multiple queries in pipeline mode. All queries are batched
705 and sent in a single network round-trip for maximum throughput.
706  
707 =head2 listen
708  
709 $storage->listen($channel, sub {
710 my ($channel, $payload, $sender_pid) = @_;
711 # Handle notification
712 });
713  
714 Subscribe to PostgreSQL LISTEN/NOTIFY notifications on the given
715 channel. The callback fires each time a notification arrives.
716  
717 =head2 unlisten
718  
719 $storage->unlisten($channel);
720  
721 Unsubscribe from a notification channel.
722  
723 =head2 notify
724  
725 $storage->notify($channel, $payload?);
726  
727 Send a PostgreSQL NOTIFY to the given channel with an optional payload.
728 Returns a L<Future> that resolves when the NOTIFY has been dispatched.
729  
730 Unlike L</listen>, this does not require a dedicated connection — it uses
731 a pooled connection from the normal pool.
732  
733 =head2 copy_in
734  
735 $storage->copy_in($table, \@columns, sub {
736 my ($put) = @_;
737 $put->(['Miles Davis', 'Jazz']);
738 $put->(['John Coltrane', 'Jazz']);
739 });
740  
741 Bulk load data via PostgreSQL COPY FROM STDIN. The callback receives
742 a writer function that accepts arrayrefs of column values.
743  
744 =head1 AUTHOR
745  
746 DBIO Authors
747  
748 =head1 COPYRIGHT AND LICENSE
749  
750 Copyright (C) 2026 DBIO Authors
751  
752 This is free software; you can redistribute it and/or modify it under
753 the same terms as the Perl 5 programming language system itself.
754  
755 =cut
↑ Summary Async/ConnectInfo.pm »