File Coverage

blib/lib/DBIx/Dump.pm
Criterion Covered Total %
statement 9 19 47.3
branch n/a
condition n/a
subroutine 3 6 50.0
pod 0 2 0.0
total 12 27 44.4


line stmt bran cond sub pod time code
1             package DBIx::Dump;
2            
3 1     1   6211 use 5.006;
  1         4  
  1         39  
4 1     1   5 use strict;
  1         2  
  1         37  
5 1     1   6 use warnings;
  1         6  
  1         1016  
6            
7             require Exporter;
8             #use AutoLoader qw(AUTOLOAD);
9            
10             our @ISA = qw(Exporter);
11            
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15            
16             # This allows declaration use DBIx::Dump ':all';
17             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18             # will save memory.
19             our %EXPORT_TAGS = ( 'all' => [ qw(
20            
21             ) ] );
22            
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24            
25             our @EXPORT = qw(
26            
27             );
28             our $VERSION = '0.04';
29            
30             sub new
31             {
32 0     0 0   my $self = shift;
33 0           my $attr = {@_};
34            
35 0           bless $attr, $self;
36             }
37            
38             ### Must put all anonymous subs before the %formats hash and dump sub.
39            
40             my $excel = sub {
41            
42             my $self = shift;
43            
44             $self->{excelFormat} = undef;
45            
46             require Spreadsheet::WriteExcel;
47            
48             my $workbook = $self->{Generator} || Spreadsheet::WriteExcel->new($self->{output});
49             $self->{Generator} = $workbook;
50            
51             my $worksheet = $workbook->addworksheet();
52            
53             my $col = 0; my $row = 0;
54            
55             my $cols = $self->{sth}->{NAME_uc};
56            
57             foreach my $data (@$cols)
58             {
59             $self->{eventHandler}->($self, \$data, $cols->[$col], 1) if $self->{eventHandler};
60             $worksheet->write(0, $col, $data, $self->{excelFormat});
61             $col++;
62             }
63             $row++;
64             $col = 0;
65            
66             while (my @data = $self->{sth}->fetchrow_array())
67             {
68             foreach my $data (@data)
69             {
70             $self->{eventHandler}->($self, \$data, $cols->[$col], $row+1) if $self->{eventHandler};
71             $worksheet->write($row, $col, $data, $self->{excelFormat});
72             $col++;
73             }
74             $col = 0;
75             $row++;
76             }
77             $row = 0;
78             _clean_up($self);
79             };
80            
81             my $csv = sub {
82            
83             my $self = shift;
84            
85             require Text::CSV_XS;
86             require IO::File;
87            
88             my $fh = IO::File->new("$self->{output}", "w");
89            
90             my $csvobj = $self->{Generator} || Text::CSV_XS->new({
91             'quote_char' => '"',
92             'escape_char' => '"',
93             'sep_char' => ',',
94             'binary' => 0
95             });
96            
97             $self->{Generator} = $csvobj;
98            
99             my $cols = $self->{sth}->{NAME_uc};
100             $csvobj->combine(@$cols);
101             print $fh $csvobj->string(), "\n";
102            
103             my $row = 0;
104             while (my @data = $self->{sth}->fetchrow_array())
105             {
106             my $col = 0;
107             foreach my $data (@data)
108             {
109             $self->{eventHandler}->($self, \$data, $cols->[$col], $row+1) if $self->{eventHandler};
110             $col++;
111             }
112             $csvobj->combine(@data);
113             print $fh $csvobj->string(), "\n";
114             $row++;
115             }
116             $row = 0;
117             $fh->close();
118             _clean_up($self);
119             };
120            
121            
122             #### This is experimental, don't use!!!!! ####
123             my $iQuery = sub {
124            
125             my $self = shift;
126            
127             require IO::File;
128            
129             my $fh = IO::File->new("$self->{output}", "w");
130            
131             my $stmt = $self->{sth}->{Statement};
132             $stmt =~ /from\s+(.*)\s+(where|order by|group by)*/i;
133             my @tables;
134             };
135             ###############################################
136            
137             my %formats = (
138             'excel' => $excel,
139             'csv' => $csv,
140             'iQuery' => $iQuery
141             );
142            
143             sub dump
144             {
145 0     0 0   my $self = shift;
146 0           my $attr = {@_};
147 0           $self = {%$self, %$attr};
148            
149 0           $formats{$self->{'format'}}->($self);
150             }
151            
152             sub _clean_up
153             {
154 0     0     my $self = shift;
155            
156 0           $self->{Generator} = undef;
157 0           $self->{excelFormat} = undef;
158             }
159            
160             # Preloaded methods go here.
161            
162             # Autoload methods go after =cut, and are processed by the autosplit program.
163            
164             1;
165             __END__