blib/lib/DBI/Gofer/Transport/Base.pm

filter: Statementstmt 65.0% 52 / 80 Branchbran 46.1% 12 / 26 Conditioncond 25.7% 9 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 2 totaltotal 47.4% 83 / 175 CC 54 SCAR 33.4
CC
54
Cov
52%
_dump
90.0
_dump
90.0
_thaw_data
10.8
CRAP 28.2
« blib/lib/DBI/DBD.pm ↑ Summary blib/lib/DBD/DBM.pm »
1 package DBI::Gofer::Transport::Base;
2  
3 # $Id: Base.pm 12536 2009-02-24 22:37:09Z Tim $
4 #
5 # Copyright (c) 2007, Tim Bunce, Ireland
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9  
10 126 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 126 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 126 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 63 ▶use base qw(DBI::Util::_accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 63 ▶use DBI::Gofer::Serializer::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 63 ▶use DBI::Gofer::Serializer::DataDumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 our $VERSION = "0.012537";
21  
22 __PACKAGE__->mk_accessors(qw(
23 trace
24 keep_meta_frozen
25 serializer_obj
26 ));
27  
28  
29 # see also $ENV{DBI_GOFER_TRACE} in DBI::Gofer::Execute
30 0 ▶sub _init_trace { (split(/=/,$ENV{DBI_GOFER_TRACE}||0))[0] }
Subroutine _init_trace: not called CC=2 SCAR=17.9
Condition0%0/2
$ENV{'DBI_GOFER_TRACE'} || 0
l!l
00
MC/DC0%0/1
$ENV{'DBI_GOFER_TRACE'} || 0
$ENV{'DBI_GOFER_TRACE'}
Truth table0%0/2
A
A= $ENV{'DBI_GOFER_TRACE'}
Aresult
11
00
31  
32  
33 sub new {
34 746 ▶ my ($class, $args) = @_;
Subroutine new: called CC=5 SCAR=18.4
Pod undocumented
35 746 ▶ $args->{trace} ||= $class->_init_trace;
Condition33%1/3
$args->{"trace"} ||= $class->_init_trace
l!l&&r!l&&!r
00746
MC/DC0%0/2
$args->{"trace"} ||= $class->_init_trace
$args->{"trace"} $class->_init_trace
Truth table33%1/3
A ||= B
A= $args->{"trace"}B= $class->_init_trace
ABresult
1X1
011
000
36 746 ▶ $args->{serializer_obj} ||= DBI::Gofer::Serializer::Storable->new();
Condition33%1/3
$args->{"serializer_obj"} ||= "DBI::Gofer::Serializer::Storable"->new
l!l&&r!l&&!r
07460
MC/DC0%0/2
$args->{"serializer_obj"} ||= "DBI::Gofer::Serializer::Storable"->new
$args->{"serializer_obj"} "DBI::Gofer::Serializer::Storable"->new
Truth table33%1/3
A ||= B
A= $args->{"serializer_obj"}B= "DBI::Gofer::Serializer::Storable"->new
ABresult
1X1
011
000
37 746 my $self = bless {}, $class;
38 746 $self->$_( $args->{$_} ) for keys %$args;
39 0 ▶ $self->trace_msg("$class->new({ @{[ %$args ]} })\n") if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 746 746
40 746 return $self;
41 }
42  
43 my $packet_header_text = "GoFER1:";
44 my $packet_header_regex = qr/^GoFER(\d+):/;
45  
46  
47 sub _freeze_data {
48 13897 ▶ my ($self, $data, $serializer, $skip_trace) = @_;
Subroutine _freeze_data: called CC=6 SCAR=19.3
49 13897 my $frozen = eval {
50 13897 ▶ $self->_dump("freezing $self->{trace} ".ref($data), $data)
Branch: 1 branch
BranchTrueFalseTotal
if not $skip_trace and $self->trace 0 13897 13897
Condition66%2/3
not $skip_trace and $self->trace
!ll&&!rl&&r
657973180
MC/DC0%0/2
not $skip_trace and $self->trace
not $skip_trace $self->trace
Truth table66%2/3
not(A) and B
A= not $skip_traceB= $self->trace
ABresult
0X0
100
111
51 if !$skip_trace and $self->trace;
52  
53 13897 local $data->{meta}; # don't include meta in serialization
54 13897 ▶ $serializer ||= $self->{serializer_obj};
Condition33%1/3
$serializer ||= $self->{"serializer_obj"}
l!l&&r!l&&!r
0138970
MC/DC0%0/2
$serializer ||= $self->{"serializer_obj"}
$serializer $self->{"serializer_obj"}
Truth table33%1/3
A ||= B
A= $serializerB= $self->{"serializer_obj"}
ABresult
1X1
011
000
55 13897 my ($data, $deserializer_class) = $serializer->serialize($data);
56  
57 13897 $packet_header_text . $data;
58 };
59 13897 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13897 13897
60 0 chomp $@;
61 0 die "Error freezing ".ref($data)." object: $@";
62 }
63  
64 # stash the frozen data into the data structure itself
65 # to make life easy for the client caching code in DBD::Gofer::Transport::Base
66 13897 ▶ $data->{meta}{frozen} = $frozen if $self->keep_meta_frozen;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keep_meta_frozen 46 13851 13897
67  
68 13897 return $frozen;
69 }
70 # public aliases used by subclasses
71 *freeze_request = \&_freeze_data;
72 *freeze_response = \&_freeze_data;
73  
74  
75 sub _thaw_data {
76 13883 ▶ my ($self, $frozen_data, $serializer, $skip_trace) = @_;
Subroutine _thaw_data: called CC=8 SCAR=23.8
77 13883 my $data;
78 13883 eval {
79 # check for and extract our gofer header and the info it contains
80 13883 ▶ (my $frozen = $frozen_data) =~ s/$packet_header_regex//o
Branch: 1 branch
BranchTrueFalseTotal
(my $frozen = $frozen_data) =~ s/$packet_header_regex//o or die "does not have gofer header\n" 13883 0 13883
81 or die "does not have gofer header\n";
82 13883 my ($t_version) = $1;
83 13883 ▶ $serializer ||= $self->{serializer_obj};
Condition33%1/3
$serializer ||= $self->{"serializer_obj"}
l!l&&r!l&&!r
0138830
MC/DC0%0/2
$serializer ||= $self->{"serializer_obj"}
$serializer $self->{"serializer_obj"}
Truth table33%1/3
A ||= B
A= $serializerB= $self->{"serializer_obj"}
ABresult
1X1
011
000
84 13883 $data = $serializer->deserialize($frozen);
85 13883 ▶ die ref($serializer)."->deserialize didn't return a reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 13883 13883
86 unless ref $data;
87 13883 $data->{_transport}{version} = $t_version;
88  
89 13883 ▶ $data->{meta}{frozen} = $frozen_data if $self->keep_meta_frozen;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keep_meta_frozen 32 13851 13883
90 };
91 13883 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13883 13883
92 0 chomp(my $err = $@);
93 # remove extra noise from Storable
94 0 $err =~ s{ at \S+?/Storable.pm \(autosplit into \S+?/Storable/thaw.al\) line \d+(, \S+ line \d+)?}{};
95 0 my $msg = sprintf "Error thawing: %s (data=%s)", $err, DBI::neat($frozen_data,50);
96 0 Carp::cluck("$msg, pid $$ stack trace follows:"); # XXX if $self->trace;
97 0 die $msg;
98 }
99 13883 ▶ $self->_dump("thawing $self->{trace} ".ref($data), $data)
Branch: 1 branch
BranchTrueFalseTotal
if not $skip_trace and $self->trace 0 13883 13883
Condition66%2/3
not $skip_trace and $self->trace
!ll&&!rl&&r
655873250
MC/DC0%0/2
not $skip_trace and $self->trace
not $skip_trace $self->trace
Truth table66%2/3
not(A) and B
A= not $skip_traceB= $self->trace
ABresult
0X0
100
111
100 if !$skip_trace and $self->trace;
101  
102 13883 return $data;
103 }
104 # public aliases used by subclasses
105 *thaw_request = \&_thaw_data;
106 *thaw_response = \&_thaw_data;
107  
108  
109 # this should probably live in the request and response classes
110 # and the tace level passed in
111 sub _dump {
112 0 ▶ my ($self, $label, $data) = @_;
Subroutine _dump: not called CC=9 SCAR=45.0
113  
114 # don't dump the binary
115 0 ▶ local $data->{meta}{frozen} if $data->{meta} && $data->{meta}{frozen};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"meta"} and $data->{"meta"}{"frozen"} 0 0 0
Condition0%0/3
$data->{"meta"} and $data->{"meta"}{"frozen"}
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"meta"} and $data->{"meta"}{"frozen"}
$data->{"meta"} $data->{"meta"}{"frozen"}
Truth table0%0/3
A and B
A= $data->{"meta"}B= $data->{"meta"}{"frozen"}
ABresult
0X0
100
111
116  
117 0 my $trace_level = $self->trace;
118 0 my $summary;
119 0 ▶ if ($trace_level >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($trace_level >= 4) { } 0 0 0
elsif ($trace_level >= 2) { } 0 0 0
120 0 require Data::Dumper;
121 0 local $Data::Dumper::Indent = 1;
122 0 local $Data::Dumper::Terse = 1;
123 0 local $Data::Dumper::Useqq = 0;
124 0 local $Data::Dumper::Sortkeys = 1;
125 0 local $Data::Dumper::Quotekeys = 0;
126 0 local $Data::Dumper::Deparse = 0;
127 0 local $Data::Dumper::Purity = 0;
128 0 $summary = Data::Dumper::Dumper($data);
129 }
130 elsif ($trace_level >= 2) {
131 0 ▶ $summary = eval { $data->summary_as_text } || $@ || "no summary available\n";
Condition0%0/5
eval { do { $data->summary_as_text } } || $@ || "no summary available\n"
l!l
00
eval { do { $data->summary_as_text } } or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $data->summary_as_text } } || $@ || "no summary available\n"
eval { do { $data->summary_as_text } } $@
Truth table0%0/3
A or B
A= eval { do { $data->summary_as_text } }B= $@
ABresult
1X1
011
000
132 }
133 else {
134 0 ▶ $summary = eval { $data->outline_as_text."\n" } || $@ || "no summary available\n";
Condition0%0/5
eval { do { $data->outline_as_text . "\n" } } || $@ || "no summary available\n"
l!l
00
eval { do { $data->outline_as_text . "\n" } } or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $data->outline_as_text . "\n" } } || $@ || "no summary available\n"
eval { do { $data->outline_as_text . "\n" } } $@
Truth table0%0/3
A or B
A= eval { do { $data->outline_as_text . "\n" } }B= $@
ABresult
1X1
011
000
135 }
136 0 $self->trace_msg("$label: $summary");
137 }
138  
139  
140 sub trace_msg {
141 10708 ▶ my ($self, $msg, $min_level) = @_;
Subroutine trace_msg: called CC=3 SCAR=11.4
Pod undocumented
142 10708 ▶ $min_level //= 1;
Condition50%1/2
$min_level //= 1
l!l
010708
MC/DC0%0/1
$min_level //= 1
$min_level
Truth table50%1/2
A
A= $min_level
Aresult
11
00
143 # transport trace level can override DBI's trace level
144 10708 ▶ $min_level = 0 if $self->trace >= $min_level;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace >= $min_level 0 10708 10708
145 10708 return DBI->trace_msg("gofer ".$msg, $min_level);
146 }
147  
148 1;
149  
150 =head1 NAME
151  
152 DBI::Gofer::Transport::Base - Base class for Gofer transports
153  
154 =head1 DESCRIPTION
155  
156 This is the base class for server-side Gofer transports.
157  
158 It's also the base class for the client-side base class L<DBD::Gofer::Transport::Base>.
159  
160 This is an internal class.
161  
162 =head1 AUTHOR
163  
164 Tim Bunce, L<http://www.tim.bunce.name>
165  
166 =head1 LICENCE AND COPYRIGHT
167  
168 Copyright (c) 2007, Tim Bunce, Ireland. All rights reserved.
169  
170 This module is free software; you can redistribute it and/or
171 modify it under the same terms as Perl itself. See L<perlartistic>.
172  
173 =cut
174  
« blib/lib/DBI/DBD.pm ↑ Summary blib/lib/DBD/DBM.pm »