blib/lib/DBIx/Simple/UTF8Columns.pm

filter: Statementstmt 33.6% 33 / 98 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 47.6% 10 / 21 Podpod 66.6% 2 / 3 totaltotal 25.5% 45 / 176 CC 27 SCAR 28.9
CC
27
Cov
23%
query
56.0
encoding
30.0
arrays
20.0
CRAP 18.0
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use 5.8.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use DBIx::Simple 1.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 $Carp::Internal{$_} = 1
10 4 for qw( DBIx::Simple::UTF8Columns
11 DBIx::Simple::UTF8Columns::Result );
12  
13 package DBIx::Simple::UTF8Columns;
14  
15 4 our $VERSION = '0.03';
16  
17 4 ▶use base qw( DBIx::Simple );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 our $DEFAULT_ENCODING = 'utf8';
20  
21 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
22 0 my $class = shift;
23 0 my $db = $class->SUPER::connect(@_);
24  
25 0 ▶ if (defined $db) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db) 0 0 0
26 # 'result_class' is lvalue
27 0 $db->result_class = 'DBIx::Simple::UTF8Columns::Result';
28 }
29  
30 0 return $db;
31 }
32  
33 0 ▶sub encoding {
Subroutine encoding: not called CC=5 SCAR=34.0
Pod undocumented
34 0 my $self = shift;
35  
36 0 ▶ if (! ref $self) { # class method
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self) { } 0 0 0
37 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
38 0 $DEFAULT_ENCODING = shift;
39 }
40 0 return $DEFAULT_ENCODING;
41 }
42 else { # instance method
43 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif (not defined $self->{'_encoding'}) { } 0 0 0
44 0 $self->{_encoding} = shift;
45 0 $self->{_encoder} = undef;
46 }
47 elsif (! defined $self->{_encoding}) {
48 0 $self->{_encoding} = $DEFAULT_ENCODING;
49 0 $self->{_encoder} = undef;
50 }
51 0 return $self->{_encoding};
52 }
53 }
54  
55 0 ▶sub _encoder {
Subroutine _encoder: not called CC=2 SCAR=17.9
56 0 my $self = shift;
57  
58 0 ▶ if (! defined $self->{_encoder}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_encoder"}) 0 0 0
59 0 $self->{_encoder} = Encode::find_encoding($self->encoding);
60 }
61  
62 0 return $self->{_encoder};
63 }
64  
65 0 ▶sub query {
Subroutine query: not called CC=7 SCAR=40.3
Pod documented
66 0 my ($self, $query, @binds) = @_;
67  
68 0 foreach my $data ($query, @binds) {
69 0 ▶ if (defined $data && ! ref $data && utf8::is_utf8($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and not ref $data and utf8::is_utf8($data)) 0 0 0
Condition0%0/6
defined $data and not ref $data and utf8::is_utf8($data)
!ll&&!rl&&r
000
defined $data and not ref $data
!ll&&!rl&&r
000
MC/DC0%0/3
defined $data and not ref $data and utf8::is_utf8($data)
defined $data not ref $data utf8::is_utf8($data)
Truth table0%0/4
A and not(B) and C
A= defined $dataB= not ref $dataC= utf8::is_utf8($data)
ABCresult
0XX0
10X0
1100
1111
70 0 $data = $self->_encoder->encode($data);
71 }
72 }
73  
74 0 my $result = $self->SUPER::query($query, @binds);
75
76 0 ▶ if ($self->{success} && defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"success"} and defined $result) 0 0 0
Condition0%0/3
$self->{"success"} and defined $result
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"success"} and defined $result
$self->{"success"} defined $result
Truth table0%0/3
A and B
A= $self->{"success"}B= defined $result
ABresult
0X0
100
111
77 0 $result->{_encoder} = $self->_encoder;
78 }
79  
80 0 return $result;
81 }
82  
83 package DBIx::Simple::UTF8Columns::Result;
84  
85 4 ▶use base qw( DBIx::Simple::Result );
Subroutine BEGIN: called CC=1 SCAR=0.0
86 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 0 ▶sub _encoder {
Subroutine _encoder: not called CC=2 SCAR=17.9
89 0 my ($self) = @_;
90  
91 0 ▶ if (! defined $self->{_encoder}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_encoder"}) 0 0 0
92 $self->{_encoder}
93 0 = Encode::find_encoding($DBIx::Simple::UTF8Columns::DEFAULT_ENCODING);
94 }
95  
96 0 return $self->{_encoder};
97 }
98  
99 0 ▶sub _decode {
Subroutine _decode: not called CC=3 SCAR=24.8
100 0 my ($self, $data) = @_;
101
102 0 ▶ if (defined $data && ! utf8::is_utf8($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and not utf8::is_utf8($data)) 0 0 0
Condition0%0/3
defined $data and not utf8::is_utf8($data)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and not utf8::is_utf8($data)
defined $data not utf8::is_utf8($data)
Truth table0%0/3
A and not(B)
A= defined $dataB= not utf8::is_utf8($data)
ABresult
0X0
100
111
103 0 $data = $self->_encoder->decode($data);
104 }
105 0 return $data;
106 }
107  
108 0 ▶sub _encode {
Subroutine _encode: not called CC=3 SCAR=24.8
109 0 my ($self, $data) = @_;
110
111 0 ▶ if (defined $data && utf8::is_utf8($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and utf8::is_utf8($data)) 0 0 0
Condition0%0/3
defined $data and utf8::is_utf8($data)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and utf8::is_utf8($data)
defined $data utf8::is_utf8($data)
Truth table0%0/3
A and B
A= defined $dataB= utf8::is_utf8($data)
ABresult
0X0
100
111
112 0 $data = $self->_encoder->encode($data);
113 }
114 0 return $data;
115 }
116  
117 # UNSUPPORTED: func, attr
118 # UNTOUCH: columns
119 # UNSUPPORTED: bind, fetch, into
120  
121 0 ▶sub list {
Subroutine list: not called CC=3 SCAR=24.8
122 0 my $self = shift;
123  
124 0 my @results = $self->SUPER::list(@_);
125  
126 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
127 0 foreach my $result (@results) {
128 0 $result = $self->_decode($result);
129 }
130 0 return @results;
131 }
132 else {
133 0 return $self->_decode($results[-1]);
134 }
135 }
136  
137 0 ▶sub array {
Subroutine array: not called CC=3 SCAR=24.8
138 0 my $self = shift;
139  
140 0 my $ref_result = $self->SUPER::array(@_);
141  
142 0 ▶ if (defined $ref_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_result) 0 0 0
143 0 foreach my $data (@$ref_result) {
144 0 $data = $self->_decode($data);
145 }
146 }
147  
148 0 return $ref_result;
149 }
150  
151 0 ▶sub hash {
Subroutine hash: not called CC=3 SCAR=24.8
152 0 my $self = shift;
153  
154 0 my $ref_results = $self->SUPER::hash(@_);
155  
156 0 ▶ if (defined $ref_results) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_results) 0 0 0
157 0 foreach my $result (values %$ref_results) {
158 0 $result = $self->_decode($result);
159 }
160 }
161  
162 0 return $ref_results;
163 }
164  
165 # UNTOUCH: flat
166  
167 0 ▶sub arrays {
Subroutine arrays: not called CC=4 SCAR=30.0
168 0 my $self = shift;
169  
170 0 my @results = $self->SUPER::arrays(@_);
171 0 foreach my $result (@results) {
172 0 foreach my $column (@$result) {
173 0 $column = $self->_decode($column);
174 }
175 }
176 0 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
177 }
178  
179 # UNTOUCH: hashes, map_hashes, map_arrays, map, rows, xto, html, text
180  
181 4 1;
182 __END__
↑ Summary