DBI/st_deep.pm

filter: Statementstmt 67.8% 59 / 87 Branchbran 65.6% 21 / 32 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 52.9% 9 / 17 Podpod 9.0% 1 / 11 totaltotal 60.0% 90 / 150 SCAR 34.2
CC
18
Cov
66%
tr_hash
13.1
fetchrow_hashref
12.0
execute
12.0
CRAP 30.6
« DBI/db.pm ↑ Summary DBI.pm »
1  
2 package Tao::DBI::st_deep;
3  
4 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Exporter;
9  
10 2 our @ISA = qw(Tao::DBI::st);
11 2 our @EXPORT = qw();
12  
13 2 our $VERSION = '0.011';
14  
15 2 ▶use Tao::DBI::st;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # instance variables:
19 # META
20  
21 # initiates a Tao::DBI::st_deep object
22 # { dbh => , sql => , meta => , }
23 sub initialize {
24 0 ▶ my ( $self, $args ) = @_;
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
25 0 $self->SUPER::initialize($args);
26 0 $self->{META} = $args->{meta};
27 0 return $self;
28 }
29  
30 ###############
31  
32 sub to_perl {
33 1 ▶ require Data::Dumper;
Subroutine to_perl: called CC=1 SCAR=0.0
Pod undocumented
34 1 local $Data::Dumper::SortKeys = 1;
35 1 return Data::Dumper::Dumper(shift);
36 }
37  
38 sub to_yaml {
39 0 ▶ require YAML;
Subroutine to_yaml: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return YAML::Dump(shift);
41 }
42  
43 sub to_json {
44 0 ▶ require JSON;
Subroutine to_json: not called CC=1 SCAR=6.9
Pod undocumented
45 0 return JSON::encode_json(shift)
46 }
47  
48 sub from_perl {
49 2 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶ my $data = eval shift; # oops! that's DANGEROUS!
Subroutine from_perl: called CC=2 SCAR=7.1
Pod undocumented
51 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
52 1 return $data;
53 }
54  
55 sub from_yaml {
56 0 ▶ require YAML;
Subroutine from_yaml: not called CC=1 SCAR=6.9
Pod undocumented
57 0 return YAML::Load(shift);
58 }
59  
60 sub from_json {
61 0 ▶ require JSON;
Subroutine from_json: not called CC=1 SCAR=6.9
Pod undocumented
62 0 return JSON::decode_json(shift);
63 }
64  
65 2 my %tr_functions = (
66 ddumper => \&to_perl,
67 yaml => \&to_yaml,
68 json => \&to_json,
69 );
70  
71 2 my %i_tr_functions = (
72 ddumper => \&from_perl,
73 yaml => \&from_yaml,
74 json => \&from_json,
75 );
76  
77 # $g = tr_hash($h, $ctl) converts hashrefs to hashrefs
78 # $g = tr_hash($h, $ctl, 1) does the reverse convertion
79 #
80 # requires:
81 # $ctl is an array ref with an even number of elements
82 sub tr_hash {
83 4 ▶ my $h = shift;
Subroutine tr_hash: called CC=13 SCAR=25.7
Pod undocumented
84 4 ▶ return undef unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 4 4
85  
86 4 my $ctl = shift;
87 4 my $inv = shift;
88  
89 4 my %h = %$h;
90 4 my %g; # the result
91 my %m; # the visited keys
92 4 my @ctl = @$ctl;
93  
94 4 ▶ while (@ctl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ctl) 12 4 16
95 12 my ( $k, $fk ) = split ':', shift @ctl;
96 12 my ( $v, $fv ) = split ':', shift @ctl;
97  
98 12 ▶ if ($inv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inv) 6 6 12
99 6 ( $k, $v ) = ( $v, $k );
100 6 ( $fk, $fv ) = ( $fv, $fk );
101 }
102  
103 12 ▶ if ( $k eq '*' ) { # h{*} -> g{$k}
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq '*') { } 2 10 12
elsif ($v eq '*') { } 2 8 10
104 2 ▶ while ( my ( $a, $b ) = each %h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %h) 10 2 12
105 10 ▶ $g{$v}{$a} = $b, $m{$a}++ unless $m{$a};
Branch: 1 branch
BranchTrueFalseTotal
unless $m{$a} 6 4 10
106 }
107 2 ▶ if ($fv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fv) 1 1 2
108 1 $g{$v} = &{ $tr_functions{$fv} }( $g{$v} );
109 }
110 }
111 elsif ( $v eq '*' ) { # h{$k} -> g{*}
112 2 ▶ if ($fk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fk) 1 1 2
113 1 $h{$k} = &{ $i_tr_functions{$fk} }( $h{$k} );
114 }
115 croak "val at '$k' (", ( ref $h{$k} || 'non-ref scalar' ),
116 ") should be hashref"
117 2 ▶ unless ref $h{$k} eq 'HASH'; # FIXME:
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h{$k} eq "HASH" 0 2 2
Condition0%0/2
ref $h{$k} || 'non-ref scalar'
l!l
00
MC/DC0%0/1
ref $h{$k} || 'non-ref scalar'
ref $h{$k}
Truth table0%0/2
A
A= ref $h{$k}
Aresult
11
00
118 2 ▶ while ( my ( $a, $b ) = each %{ $h{$k} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$h{$k};}) 6 2 8
119 6 $g{$a} = $b;
120 }
121 2 $m{$k}++;
122 }
123 else {
124 8 $g{$v} = $h{$k};
125 8 $m{$k}++;
126 }
127  
128 }
129  
130 4 return \%g;
131 }
132  
133 # sub comp_map_h {
134 # }
135 # returns a sub which does the same map_h
136  
137 ###############
138  
139 sub trace {
140 0 ▶ my $self = shift;
Subroutine trace: not called CC=1 SCAR=6.9
Pod undocumented
141 0 return 0; # FIXME: $self->{TRACE} || $self->{DBH}->{TRACE}
142 }
143  
144 sub fetchrow_hashref {
145 0 ▶ my $self = shift;
Subroutine fetchrow_hashref: not called CC=3 SCAR=24.8
Pod undocumented
146 0 my $raw = $self->SUPER::fetchrow_hashref(@_);
147 0 ▶ return undef unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 0 0
148 0 ▶ if ( $self->trace ) { require YAML; warn YAML::Dump( { RAW => $raw } ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trace) 0 0 0
149 0 my $row = tr_hash( $raw, $self->{META}, 1 );
150 }
151  
152 sub execute {
153 0 ▶ my $self = shift;
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
154 0 my $bind_values = shift;
155 0 ▶ if ( ref $bind_values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bind_values) { } 0 0 0
156 0 my $raw = {};
157 0 ▶ $raw = tr_hash( $bind_values, $self->{META} ) if $bind_values;
Branch: 1 branch
BranchTrueFalseTotal
if $bind_values 0 0 0
158 0 return $self->SUPER::execute( $raw, @_ );
159 }
160 else { # single non-ref arg - we don't try transformations
161 0 return $self->SUPER::execute( $bind_values, @_ );
162 }
163 }
164  
165 __END__
« DBI/db.pm ↑ Summary DBI.pm »