Array/Export.pm

filter: Statementstmt 16.6% 12 / 72 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.0% 3 / 10 Podpod 100% 5 / 5 totaltotal 17.0% 20 / 117 CC 17 SCAR 41.0
CC
17
Cov
12%
xml_arrayhashname
132.0
_csv
20.0
_join_csv
6.0
CRAP 60.6
↑ Summary Array/Session/Action.pm »
1 package DBIx::Array::Export;
2 1 ▶use base qw{DBIx::Array};
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION='0.64';
7 1 our $PACKAGE=__PACKAGE__;
8  
9 =head1 NAME
10  
11 DBIx::Array::Export - Extends DBIx::Array with convenient export functions
12  
13 =head1 SYNOPSIS
14  
15 use DBIx::Array::Export;
16 my $dbx=DBIx::Array::Export->new;
17 $dbx->connect($connection, $user, $pass, \%opt); #passed to DBI
18  
19 =head1 DESCRIPTION
20  
21 =head1 USAGE
22  
23 =head1 METHODS (Export)
24  
25 =head2 xml_arrayhashname
26  
27 Returns XML given an arrayhashname data structure
28
29 $dbx->execute(q{ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS"Z"'});
30 my @arrayhashname=$dbx->sqlarrayhashname($sql);
31 my $xml=$dbx->xml_arrayhashname(data => \@arrayhashname,
32 comment => "Text String Comment",
33 uom => {col1=>"min", col2=>"ft"});
34  
35 =cut
36  
37 sub xml_arrayhashname {
38 0 ▶ my $self=shift;
Subroutine xml_arrayhashname: not called CC=11 SCAR=48.8
Pod documented
39 0 my $opt={@_};
40 0 ▶ my $data=$opt->{'data'} || [];
Condition0%0/2
$opt->{'data'} || []
l!l
00
MC/DC0%0/1
$opt->{'data'} || []
$opt->{'data'}
Truth table0%0/2
A
A= $opt->{'data'}
Aresult
11
00
41 0 ▶ $data=[] unless ref($data) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
42 0 ▶ my $uom=$opt->{'uom'} || {};
Condition0%0/2
$opt->{'uom'} || {}
l!l
00
MC/DC0%0/1
$opt->{'uom'} || {}
$opt->{'uom'}
Truth table0%0/2
A
A= $opt->{'uom'}
Aresult
11
00
43 0 ▶ $uom={} unless ref($uom) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uom eq "HASH" 0 0 0
44  
45 0 my $header=shift(@$data);
46 0 foreach (@$data) {
47 0 foreach my $key (keys %$_) {
48 0 ▶ if (defined($_->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{$key}) { } 0 0 0
49 0 $_->{$key}=[$_->{$key}]; #This is needed for XML::Simple to make pretty XML.
50 } else {
51 0 CORE::delete($_->{$key}); #This is a choice that I made but I'm not sure if it's smart
52 }
53 }
54 }
55 0 ▶ @$header=map {exists($uom->{$_})? {content=>$_, uom=>$uom->{$_}} : $_} @$header;
Branch: 1 branch
BranchTrueFalseTotal
exists $uom->{$_} ? : 0 0 0
56  
57 0 my $module="XML::Simple";
58 0 eval("use $module;");
59 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
60 0 die("Error: $PACKAGE->xml_arrayhashname method requres $module");
61 } else {
62 0 my $xs=XML::Simple->new(XMLDecl=>1, RootName=>q{document}, ForceArray=>1);
63 0 my $head={};
64 0 ▶ $head->{'comment'}=[$opt->{'comment'}] if $opt->{'comment'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"comment"} 0 0 0
65 0 $head->{'columns'}=[{column=>$header}];
66 0 $head->{'counts'}=[{rows=>[scalar(@$data)], columns=>[scalar(@$header)]}];
67 0 return $xs->XMLout({
68 head=>$head,
69 body=>{rows=>[{row=>$data}]},
70 });
71 }
72 }
73  
74 =head2 csv_arrayarrayname
75  
76 Returns CSV given an arrayarrayname data structure
77  
78 my $csv=$dbx->csv_arrayarrayname($data);
79  
80 =cut
81  
82 sub csv_arrayarrayname {
83 0 ▶ my $self=shift;
Subroutine csv_arrayarrayname: not called CC=1 SCAR=6.9
Pod documented
84 0 my $data=shift;
85 0 return join "", map {&_join_csv($self->_csv, @$_)} @$data;
86  
87 sub _join_csv {
88 0 ▶ my $csv=shift;
Subroutine _join_csv: not called CC=2 SCAR=17.9
89 0 my $status=$csv->combine(@_);
90 0 ▶ return $status ? $csv->string."\r\n" : (); #\r\n per RFC 4180
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
91 }
92 }
93  
94 =head2 csv_cursor
95  
96 Writes CSV to file handle given an executed cursor (with header row from $sth)
97  
98 binmode($fh);
99 $dbx->csv_cursor($fh, $sth);
100  
101 Due to portability issues, I choose not to force the passed file handle into binmode. However, it IS required! For most file handle objects you can run binmode($fh) or $fh->binmode;
102  
103 =cut
104  
105 sub csv_cursor {
106 0 ▶ my $self=shift;
Subroutine csv_cursor: not called CC=1 SCAR=6.9
Pod documented
107 0 my $fh=shift;
108 0 my $sth=shift;
109 0 $self->_csv->print($fh, scalar($sth->{'NAME'}));
110 0 print $fh "\r\n";
111 0 $self->csvappend_cursor($fh, $sth);
112 }
113  
114 =head2 csvappend_cursor
115  
116 Appends CSV to file handle given an executed cursor (no header row)
117  
118 binmode($fh);
119 $dbx->csvappend_cursor($fh, $sth);
120  
121 =cut
122  
123 sub csvappend_cursor {
124 0 ▶ my $self=shift;
Subroutine csvappend_cursor: not called CC=2 SCAR=17.9
Pod documented
125 0 my $fh=shift;
126 0 my $sth=shift;
127 0 my $row=[];
128 0 local $|=0;
129 0 ▶ while ($row=$sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_arrayref) 0 0 0
130 0 $self->_csv->print($fh, $row);
131 0 print $fh "\r\n";
132 }
133 0 $sth->finish;
134 }
135  
136 sub _csv {
137 0 ▶ my $self=shift;
Subroutine _csv: not called CC=4 SCAR=30.0
138 0 ▶ $self->{"_csv"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
139 0 eval("use Text::CSV_XS;");
140 0 ▶ die("Error: CSV Export Methods requre Text::CSV_XS") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
141 0 ▶ $self->{"_csv"}=Text::CSV_XS->new unless defined $self->{"_csv"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_csv"} 0 0 0
142 0 return $self->{"_csv"};
143 }
144  
145 =head2 xls_arrayarrayname
146  
147 Returns XLS data blob given an arrayarrayname data structure
148  
149 my $xls=$dbx->xls_arrayarrayname("Tab One"=>$data, "Tab Two"=>$data2, ...);
150  
151 =cut
152  
153 sub xls_arrayarrayname {
154 0 ▶ my $self=shift;
Subroutine xls_arrayarrayname: not called CC=2 SCAR=17.9
Pod documented
155 0 my $module="Spreadsheet::WriteExcel::Simple::Tabs";
156 0 eval("use $module;");
157 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
158 0 die("Error: $PACKAGE->xls_arrayarrayname method requres $module");
159 } else {
160 0 my $ss=Spreadsheet::WriteExcel::Simple::Tabs->new();
161 0 $ss->add(@_);
162 0 return $ss->content;
163 }
164 }
165  
166 =head1 TODO
167  
168 Switch out L<XML::Simple> for L<XML::LibXML::LazyBuilder>
169  
170 =head1 BUGS
171  
172 Send email to author and log on RT.
173  
174 =head1 SUPPORT
175  
176 DavisNetworks.com supports all Perl applications big or small.
177  
178 =head1 AUTHOR
179  
180 Michael R. Davis
181 CPAN ID: MRDVT
182 STOP, LLC
183 domain=>stopllc,tld=>com,account=>mdavis
184 http://www.stopllc.com/
185  
186 =head1 COPYRIGHT
187  
188 This program is free software licensed under the...
189  
190 The BSD License
191  
192 The full text of the license can be found in the LICENSE file included with this module.
193  
194 =head1 SEE ALSO
195  
196 =head2 Building Blocks
197  
198 L<XML::Simple>, L<Text::CSV_XS>, L<Spreadsheet::WriteExcel::Simple::Tabs>
199  
200 =head2 Similar Capabilities
201  
202 L<Data::Table> see csv and tsv methods, L<Data::Table::Excel>
203  
204 =cut
205  
206 1 1;
↑ Summary Array/Session/Action.pm »