Pg/Cursor.pm

filter: Statementstmt 42.1% 27 / 64 Branchbran 5.5% 1 / 18 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 66.6% 10 / 15 Podpod 44.4% 4 / 9 totaltotal 39.3% 48 / 122 CC 15 SCAR 23.6
CC
15
Cov
36%
next
30.0
close
20.0
each
12.0
CRAP 10.6
« Pg/Util.pm ↑ Summary Pg/Results.pm »
1 package IO::Async::Pg::Cursor;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Async::Pg::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 my $cursor_counter = 0;
10  
11 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
12 4 my ($class, %args) = @_;
13  
14 my $self = bless {
15 name => $args{name} // _generate_name(),
16 batch_size => $args{batch_size} // 1000,
17 conn => $args{conn},
18 4 ▶ _owns_transaction => $args{_owns_transaction} // 0,
Condition71%5/7
$args{"name"} // _generate_name()
l!l&&r!l&&!r
220
$args{"batch_size"} // 1000
l!l
13
$args{"_owns_transaction"} // 0
l!l
04
MC/DC0%0/2
$args{"name"} // _generate_name()
$args{"name"} _generate_name()
MC/DC100%1/1
$args{"batch_size"} // 1000
$args{"batch_size"}
MC/DC0%0/1
$args{"_owns_transaction"} // 0
$args{"_owns_transaction"}
Truth table66%2/3
A // B
A= $args{"name"}B= _generate_name()
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{"batch_size"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"_owns_transaction"}
Aresult
11
00
19 exhausted => 0,
20 closed => 0,
21 buffer => [],
22 }, $class;
23  
24 4 return $self;
25 }
26  
27 2 ▶sub _generate_name {
Subroutine _generate_name: called CC=1 SCAR=0.0
28 2 return "cursor_" . ++$cursor_counter;
29 }
30  
31 # Accessors
32 4 ▶sub name { shift->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
33 2 ▶sub batch_size { shift->{batch_size} }
Subroutine batch_size: called CC=1 SCAR=0.0
Pod undocumented
34 1 ▶sub is_exhausted { shift->{exhausted} }
Subroutine is_exhausted: called CC=1 SCAR=0.0
Pod undocumented
35 0 ▶sub is_closed { shift->{closed} }
Subroutine is_closed: not called CC=1 SCAR=6.9
Pod undocumented
36  
37 # Fetch next batch of rows
38 0 ▶async sub next {
Subroutine next: not called CC=5 SCAR=34.0
Pod documented
39 0 my ($self) = @_;
40  
41 0 ▶ return undef if $self->{exhausted} || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"exhausted"} or $self->{"closed"} 0 0 0
Condition0%0/3
$self->{"exhausted"} or $self->{"closed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"exhausted"} or $self->{"closed"}
$self->{"exhausted"} $self->{"closed"}
Truth table0%0/3
A or B
A= $self->{"exhausted"}B= $self->{"closed"}
ABresult
1X1
011
000
42  
43 0 my $conn = $self->{conn};
44 0 my $name = $self->{name};
45 0 my $batch_size = $self->{batch_size};
46  
47 0 my $result = await $conn->query("FETCH $batch_size FROM $name");
48 0 my $rows = $result->rows;
49  
50 0 ▶ if (@$rows < $batch_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows < $batch_size) 0 0 0
51 0 $self->{exhausted} = 1;
52 }
53  
54 0 ▶ return @$rows ? $rows : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$rows ? : 0 0 0
55 1 }
56  
57 # Iterate over all rows, calling callback for each
58 0 ▶async sub each {
Subroutine each: not called CC=3 SCAR=24.8
Pod documented
59 0 my ($self, $callback) = @_;
60  
61 0 my $count = 0;
62 0 ▶ while (my $batch = await $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $batch = <custom op await>) 0 0 0
63 0 for my $row (@$batch) {
64 0 $callback->($row);
65 0 $count++;
66 }
67 }
68  
69 0 return $count;
70 1 }
71  
72 # Collect all remaining rows into an array
73 0 ▶async sub all {
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
74 0 my ($self) = @_;
75  
76 0 my @all_rows;
77 0 ▶ while (my $batch = await $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $batch = <custom op await>) 0 0 0
78 0 push @all_rows, @$batch;
79 }
80  
81 0 return \@all_rows;
82 1 }
83  
84 # Close the cursor
85 0 ▶async sub close {
Subroutine close: not called CC=4 SCAR=30.0
Pod documented
86 0 my ($self) = @_;
87  
88 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
89 0 $self->{closed} = 1;
90  
91 0 ▶ if (my $conn = $self->{conn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $self->{"conn"}) 0 0 0
92 0 eval { await $conn->query("CLOSE " . $self->{name}) };
93  
94 # If we started a transaction for this cursor, commit it
95 0 ▶ if ($self->{_owns_transaction}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_owns_transaction"}) 0 0 0
96 0 eval { await $conn->query('COMMIT') };
97 0 $conn->{in_transaction} = 0;
98 }
99 }
100 1 }
101  
102 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
103 4 my ($self) = @_;
104  
105 # During global destruction, don't try to close
106 4 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 4 4
107  
108 # Note: Can't await in DESTROY, so cursor cleanup may be deferred
109 # Best practice is to explicitly call close()
110 }
111  
112 1 1;
113  
114 __END__
« Pg/Util.pm ↑ Summary Pg/Results.pm »