File Coverage

blib/lib/DBIx/Simple/UTF8Columns.pm
Criterion Covered Total %
statement 30 95 31.5
branch 0 30 0.0
condition 0 15 0.0
subroutine 10 21 47.6
pod 2 3 66.6
total 42 164 25.6


line stmt bran cond sub pod time code
1 4     4   4179 use strict;
  4         7  
  4         172  
2 4     4   22 use warnings;
  4         377  
  4         143  
3 4     4   23 use Carp ();
  4         7  
  4         71  
4 4     4   60 use 5.8.1;
  4         14  
  4         853  
5 4     4   1117 use utf8;
  4         15  
  4         27  
6 4     4   6726 use Encode ();
  4         131642  
  4         140  
7 4     4   10233 use DBIx::Simple 1.28;
  4         255019  
  4         379  
8              
9             $Carp::Internal{$_} = 1
10             for qw( DBIx::Simple::UTF8Columns
11             DBIx::Simple::UTF8Columns::Result );
12              
13             package DBIx::Simple::UTF8Columns;
14              
15             our $VERSION = '0.03';
16              
17 4     4   53 use base qw( DBIx::Simple );
  4         8  
  4         5761  
18              
19             our $DEFAULT_ENCODING = 'utf8';
20              
21             sub connect {
22 0     0 1   my $class = shift;
23 0           my $db = $class->SUPER::connect(@_);
24              
25 0 0         if (defined $db) {
26             # 'result_class' is lvalue
27 0           $db->result_class = 'DBIx::Simple::UTF8Columns::Result';
28             }
29              
30 0           return $db;
31             }
32              
33             sub encoding {
34 0     0 0   my $self = shift;
35              
36 0 0         if (! ref $self) { # class method
37 0 0         if (@_) {
38 0           $DEFAULT_ENCODING = shift;
39             }
40 0           return $DEFAULT_ENCODING;
41             }
42             else { # instance method
43 0 0         if (@_) {
    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             sub _encoder {
56 0     0     my $self = shift;
57              
58 0 0         if (! defined $self->{_encoder}) {
59 0           $self->{_encoder} = Encode::find_encoding($self->encoding);
60             }
61              
62 0           return $self->{_encoder};
63             }
64              
65             sub query {
66 0     0 1   my ($self, $query, @binds) = @_;
67              
68 0           foreach my $data ($query, @binds) {
69 0 0 0       if (defined $data && ! ref $data && utf8::is_utf8($data)) {
      0        
70 0           $data = $self->_encoder->encode($data);
71             }
72             }
73              
74 0           my $result = $self->SUPER::query($query, @binds);
75            
76 0 0 0       if ($self->{success} && defined $result) {
77 0           $result->{_encoder} = $self->_encoder;
78             }
79              
80 0           return $result;
81             }
82              
83             package DBIx::Simple::UTF8Columns::Result;
84              
85 4     4   36 use base qw( DBIx::Simple::Result );
  4         8  
  4         3052  
86 4     4   30 use Carp;
  4         8  
  4         2927  
87              
88             sub _encoder {
89 0     0     my ($self) = @_;
90              
91 0 0         if (! defined $self->{_encoder}) {
92 0           $self->{_encoder}
93             = Encode::find_encoding($DBIx::Simple::UTF8Columns::DEFAULT_ENCODING);
94             }
95              
96 0           return $self->{_encoder};
97             }
98              
99             sub _decode {
100 0     0     my ($self, $data) = @_;
101            
102 0 0 0       if (defined $data && ! utf8::is_utf8($data)) {
103 0           $data = $self->_encoder->decode($data);
104             }
105 0           return $data;
106             }
107              
108             sub _encode {
109 0     0     my ($self, $data) = @_;
110            
111 0 0 0       if (defined $data && utf8::is_utf8($data)) {
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             sub list {
122 0     0     my $self = shift;
123              
124 0           my @results = $self->SUPER::list(@_);
125              
126 0 0         if (wantarray) {
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             sub array {
138 0     0     my $self = shift;
139              
140 0           my $ref_result = $self->SUPER::array(@_);
141              
142 0 0         if (defined $ref_result) {
143 0           foreach my $data (@$ref_result) {
144 0           $data = $self->_decode($data);
145             }
146             }
147              
148 0           return $ref_result;
149             }
150              
151             sub hash {
152 0     0     my $self = shift;
153              
154 0           my $ref_results = $self->SUPER::hash(@_);
155              
156 0 0         if (defined $ref_results) {
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             sub arrays {
168 0     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 0         return wantarray ? @results : \@results;
177             }
178              
179             # UNTOUCH: hashes, map_hashes, map_arrays, map, rows, xto, html, text
180              
181             1;
182             __END__