blib/lib/DBIx/BLOB/Handle.pm

filter: Statementstmt 20.0% 25 / 125 Branchbran 1.7% 1 / 56 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 47.0% 8 / 17 Podpod 100% 1 / 1 totaltotal 14.1% 35 / 248 CC 41 SCAR 49.2
CC
41
Cov
12%
SEEK
306.0
READLINE
132.0
READ
110.0
CRAP 137.4
↑ Summary
1 package DBIx::BLOB::Handle;
2  
3 1 ▶use base qw( IO::Handle IO::Seekable );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 $VERSION = '0.2';
11  
12 1 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
13 1 my $class = shift;
14 0 ▶ if( grep { $_ eq ':INTO_STATEMENT' } @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":INTO_STATEMENT";} @_) 0 1 1
15 # Danger! Pretend the DBI statement class can provide blobs as handles
16 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶ *DBI::st::blob_as_handle = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 return "$class"->new(@_);
19 0 };
20 }
21 }
22  
23 # required is the DBI statement
24 # optional is the 0 based column index that contains the blob (default = 0)
25 # optional is the blocksize to be read from the database (default = 4096)
26 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
27 0 my($self, $sth, $field, $blocksize) = @_;
28 0 ▶ $self = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
29 0 my $s = Symbol::gensym;
30 0 tie $$s,$self,$sth,$field,$blocksize;
31 0 return bless $s, $self;
32 }
33  
34 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=3 SCAR=24.8
35 0 my $class = shift;
36 0 ▶ return bless {sth => shift
Condition0%0/4
shift() || 0
l!l
00
shift() || 4096
l!l
00
MC/DC0%0/1
shift() || 0
shift()
MC/DC0%0/1
shift() || 4096
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
37 ,field => shift || 0
38 ,blocksize => shift || 4096
39 ,pos => 0
40 ,eof => 0
41 ,line => undef
42 ,lines_ref => []
43 },$class;
44 }
45  
46 0 ▶sub READLINE {
Subroutine READLINE: not called CC=11 SCAR=48.8
47 0 my $self = shift;
48 0 my $buf;
49 0 ▶ ( my $sep = $/ ) ||= '';
Condition0%0/2
(my $sep = $/) ||= ""
l!l
00
MC/DC0%0/1
(my $sep = $/) ||= ""
(my $sep = $/)
Truth table0%0/2
A
A= (my $sep = $/)
Aresult
11
00
50 0 ▶ unless( $sep ){ # more efficient if we want to slurp the whole lot
Branch: 2 branches
BranchTrueFalseTotal
if ($sep) { } 0 0 0
if (do { wantarray }) { } 0 0 0
51 0 my @frags;
52 0 ▶ while( ! $self->{eof} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 0 0
53 0 READ($self, $buf);
54 0 ▶ last if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 0 0
55 0 push @frags, $buf;
56 }
57 0 $. = ++$self->{line};
58 0 ▶ wantarray ? return ( join('',@frags) ) : return join('',@frags);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
59 }
60 elsif(wantarray){
61 0 ▶ while( ! $self->{eof} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 0 0
62 0 READ($self,$buf);
63 0 my $lines = pop( @{$self->{lines_ref}} ) . $buf;
64 0 push @{$self->{lines_ref}}, $lines =~ /(.*?$sep|.+)/gs;
65 }
66 0 $. = $self->{line} = scalar @{$self->{lines_ref}};
67 0 return @{ delete $self->{lines_ref} };
68 }else{
69 0 while(1){
70 0 ▶ if( ( @{$self->{lines_ref}} > 1 ) || $self->{eof} ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"lines_ref"};} > 1 or $self->{"eof"}) 0 0 0
Condition0%0/3
@{$self->{"lines_ref"};} > 1 or $self->{"eof"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$self->{"lines_ref"};} > 1 or $self->{"eof"}
@{$self->{"lines_ref"};} > 1 $self->{"eof"}
Truth table0%0/3
A or B
A= @{$self->{"lines_ref"};} > 1B= $self->{"eof"}
ABresult
1X1
011
000
71 0 ▶ $. = ++$self->{line} if @{$self->{lines_ref}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"lines_ref"};} 0 0 0
72 0 return shift @{$self->{lines_ref}};
73 }
74 0 READ($self,$buf);
75 0 my $lines = pop( @{$self->{lines_ref}} ) . $buf;
76 0 push @{$self->{lines_ref}}, $lines =~ /(.*?$sep|.+)/gs;
77 }
78 }
79 }
80  
81 0 ▶sub TELL {
Subroutine TELL: not called CC=1 SCAR=6.9
82 0 return $_[0]->{pos};
83 }
84  
85 0 ▶sub EOF {
Subroutine EOF: not called CC=1 SCAR=6.9
86 0 return $_[0]->{eof};
87 }
88  
89 0 ▶sub GETC {
Subroutine GETC: not called CC=2 SCAR=17.9
90 0 my $self = shift;
91 0 my($len,$buf) = (0,'');
92 0 $self->{sth}->blob_read($self->{field}, $self->{pos}, 1,\$buf);
93 0 $len = length $buf;
94 0 ▶ if( $len ){
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 0 0 0
95 0 $self->{pos} += $len;
96 0 return $buf;
97 }else{
98 0 $self->{eof} = 1;
99 0 return undef;
100 }
101 }
102  
103 0 ▶sub READ {
Subroutine READ: not called CC=10 SCAR=47.0
104 0 my($self,undef,$length,$offset) = @_;
105 0 ▶ $length ||= $self->{blocksize} unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 0 0
Condition0%0/3
$length ||= $self->{"blocksize"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$length ||= $self->{"blocksize"}
$length $self->{"blocksize"}
Truth table0%0/3
A ||= B
A= $lengthB= $self->{"blocksize"}
ABresult
1X1
011
000
106 0 ▶ die "Negative length" unless $length >= 0; # like the built in read does
Branch: 1 branch
BranchTrueFalseTotal
unless $length >= 0 0 0 0
107 0 ▶ $offset ||= 0;
Condition0%0/2
$offset ||= 0
l!l
00
MC/DC0%0/1
$offset ||= 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
108 0 ▶ if( defined($_[1]) && ( ( $offset > length($_[1]) ) || ( $offset < 0 ) ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and $offset > length $_[1] || $offset < 0) 0 0 0
Condition0%0/6
defined $_[1] and $offset > length $_[1] || $offset < 0
!ll&&!rl&&r
000
$offset > length $_[1] or $offset < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $_[1] and $offset > length $_[1] || $offset < 0
defined $_[1] $offset > length $_[1] $offset < 0
Truth table0%0/4
A and B or C
A= defined $_[1]B= $offset > length $_[1]C= $offset < 0
ABCresult
0XX0
1000
11X1
1011
109 0 die "Offset outside string"; # like the built in read does
110 }
111 0 my($len,$buf) = (0,'');
112 0 $self->{sth}->blob_read($self->{field}, $self->{pos}, $length,\$buf);
113 # The 5 argument form of blob_read appears to be broken
114 # (dies when called as such) otherwise we could do
115 # $h->blob_read($field, $offset, $len [, \$buf [, $bufoffset]])
116 # and then we wouldn't have to do the substring manipulation of $_[1]
117 0 $len = length $buf;
118 0 ▶ if($len){
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 0 0 0
119 0 $self->{pos} += $len;
120 }else{
121 0 $self->{eof} = 1;
122 }
123 0 ▶ $_[1] ||= ''; # avoids substr error
Condition0%0/2
$_[1] ||= ""
l!l
00
MC/DC0%0/1
$_[1] ||= ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
124 0 substr($_[1],$offset) = $buf;
125 0 return $len;
126 }
127  
128 0 ▶sub SEEK {
Subroutine SEEK: not called CC=17 SCAR=57.2
129 0 my($self,$offset,$whence) = @_;
130 0 ▶ if( $whence == IO::Seekable::SEEK_SET() ){
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 0 0 0
elsif ($whence == 1) { } 0 0 0
elsif ($whence == 2) { } 0 0 0
131 0 ▶ if( $offset > 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > 0) { } 0 0 0
132 0 ▶ if( $offset > $self->{pos} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > $self->{'pos'}) { } 0 0 0
133 0 $offset -= $self->{pos};
134 0 ▶ while( ! $self->{eof} && ( $self->{pos} < $offset ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"eof"} and $self->{"pos"} < $offset) 0 0 0
Condition0%0/3
not $self->{"eof"} and $self->{"pos"} < $offset
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"eof"} and $self->{"pos"} < $offset
not $self->{"eof"} $self->{"pos"} < $offset
Truth table0%0/3
not(A) and B
A= not $self->{"eof"}B= $self->{"pos"} < $offset
ABresult
0X0
100
111
135 0 READ($self, undef, undef);
136 }
137 0 ▶ if( $offset < $self->{pos} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $self->{"pos"}) 0 0 0
138 0 $self->{pos} = $offset;
139 0 $self->{eof} = 0;
140 }
141 }else{
142 0 $self->{pos} = $offset;
143 0 $self->{eof} = 0;
144 }
145 }else{
146 0 $self->{pos} = 0;
147 0 $self->{eof} = 0;
148 }
149 }
150 elsif( $whence == IO::Seekable::SEEK_CUR() ){
151 0 ▶ if( $offset < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) { } 0 0 0
152 0 $self->{pos} += $offset;
153 0 ▶ $self->{pos} = 0 if $self->{pos} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pos"} < 0 0 0 0
154 0 $self->{eof} = 0;
155 }else{
156 0 my $seekto = $self->{pos} + $offset;
157 0 ▶ while( ! $self->{eof} && ( $self->{pos} < $seekto ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"eof"} and $self->{"pos"} < $seekto) 0 0 0
Condition0%0/3
not $self->{"eof"} and $self->{"pos"} < $seekto
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"eof"} and $self->{"pos"} < $seekto
not $self->{"eof"} $self->{"pos"} < $seekto
Truth table0%0/3
not(A) and B
A= not $self->{"eof"}B= $self->{"pos"} < $seekto
ABresult
0X0
100
111
158 0 READ($self, undef, undef);
159 }
160 0 ▶ if( $seekto < $self->{pos} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($seekto < $self->{"pos"}) 0 0 0
161 0 $self->{pos} = $seekto;
162 0 $self->{eof} = 0;
163 }
164 }
165 }
166 elsif( $whence == IO::Seekable::SEEK_END() ){
167 0 ▶ while( ! $self->{eof} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 0 0
168 0 READ($self, undef, undef);
169 }
170 0 ▶ if( $offset < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 0 0
171 0 $self->{eof} = 0; # reset eof
172 0 $self->{pos} += $offset;
173 0 ▶ $self->{pos} = 0 if $self->{pos} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pos"} < 0 0 0 0
174 }
175 }
176 0 return $self->{pos}; # tell
177 }
178  
179 1 1;
180  
181 __END__
↑ Summary