File Coverage

test/test_extended.pl
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2 1     1   1242 use strict;
  1         2  
  1         45  
3 1     1   6 use DBI;
  1         7  
  1         65  
4 1     1   8 use DBIx::SQLCrosstab 1.17;
  1         43  
  1         47  
5 1     1   7 use DBIx::SQLCrosstab::Format 0.07;
  1         24  
  1         42  
6 1     1   7 use Data::Dumper;
  1         3  
  1         2032  
7              
8             my $dbh;
9              
10             my $driver = shift || 'SQLite';
11              
12             if ($driver eq 'SQLite') {
13             $dbh = DBI->connect("dbi:SQLite:test/crosstab.sqlite",
14             "","",{RaiseError=>1, PrintError=> 0 });
15             }
16             elsif($driver eq 'mysql') {
17             # Adjust host, username, and password according to your needs
18             $dbh = DBI->connect("dbi:mysql:crosstab; host=localhost"
19             . ";mysql_read_default_file=$ENV{HOME}/.my.cnf" # only Unix. Remove this line for Windows
20             , undef, # username
21             undef, # password
22             {RaiseError=>1, PrintError=> 0 })
23             }
24             else {
25             die "You need a connection statement for driver <$driver>\n";
26             }
27             $dbh or die "Error in connection [ driver $driver ] ($DBI::errstr)\n";
28              
29             my $params = {
30             dbh => $dbh,
31             op => [['COUNT','person_id'], [ 'SUM', 'salary']],
32             title => 'TBD',
33             title_in_header=> 1,
34             remove_if_null => 1, # remove columns with all nulls
35             remove_if_zero => 1, # remove columns with all zeroes
36             add_colors => 1, # distinct colors for string and numbers
37             add_real_names => 1, # real column name as comment in query
38             col_total => 1,
39             col_sub_total => 1,
40             row_total => 1,
41             row_sub_total => 1,
42             commify => 1, # add thousand separating commas in numbers
43             rows =>
44             [
45             { col => 'CASE WHEN country="Italy" THEN "S" ELSE "N" END', alias => 'Area' },
46             { col => 'country'},
47             { col => 'loc', alias => 'location' }
48             ],
49             cols =>
50             [
51             {
52             id => 'dept_id',
53             value => 'department',
54             from => 'xtab_departments'
55             },
56             {
57             id => 'cat_id',
58             value => 'category',
59             from => 'xtab_categories'
60             },
61             {
62             id => 'gender',
63             col_list => [ {id=>'f'}, {id =>'m'}],
64             from => 'xtab_person'
65             },
66             ],
67              
68             from =>
69             qq{xtab_person
70             INNER JOIN xtab_locations
71             ON (xtab_person.loc_id=xtab_locations.loc_id)
72             INNER JOIN xtab_countries
73             ON (xtab_countries.country_id=xtab_locations.country_id)
74             },
75             };
76            
77             $params->{title} = "personnel by "
78             . (join "/", map {exists $_->{alias} ?
79             $_->{alias} : $_->{col}} @{$params->{rows}} )
80             . " and "
81             . (join "/", map {exists $_->{value} ?
82             $_->{value} : $_->{id}} @{$params->{cols}} );
83              
84             my $xtab1 = DBIx::SQLCrosstab::Format->new($params)
85             or die "Error in \$xtab1 creation $DBIx::SQLCrosstab::errstr\n";
86              
87             my $query = $xtab1->get_query ('#')
88             or die "$DBIx::SQLCrosstab::errstr\n";
89              
90             my $recs = $xtab1->get_recs
91             or die "$DBIx::SQLCrosstab::errstr\n";
92              
93             my @rows = (
94             #{ col => 'loc', alias => 'location'},
95             { col => 'customer' },
96             #{ col => 'class_name', alias =>'class'}
97             );
98            
99             #
100             # Add a database-dependent expression
101             #
102             if ($driver eq 'mysql') {
103             unshift @rows,
104             { col => qq{date_format(sale_date,"%Y-%m")},
105             alias => "'yyyy-mm'" };
106             }
107             elsif ($driver eq 'SQLite') {
108             unshift @rows,
109             { col => qq{substr(sale_date,1,7)},
110             alias => "yyyy_mm" };
111             }
112              
113             #
114             # Using the alternative params setting
115             #
116              
117             #
118             # First, create a dummy object
119             #
120             my $xtab2 = DBIx::SQLCrosstab::Format->new('STUB')
121             or die "error in \$xtab2 creation ($DBIx::SQLCrosstab::errstr)\n";
122            
123             #
124             # Then, pass parameters to it. You can do it one-by-one ...
125             #
126             $xtab2->set_param( dbh => $dbh )
127             or die "error adding mandatory parameters ($DBIx::SQLCrosstab::errstr)";
128              
129             #
130             # ... or several ones at once
131             #
132             $xtab2->set_param(
133             op => [ ['SUM', 'sale_amount'] ],
134             # op_col => 'sale_amount',
135             rows => \@rows,
136             cols => [
137             {
138             id => 'country_id',
139             value => 'country',
140             from => 'xtab_countries'
141             },
142             {
143             id => 'xtab_person.person_id',
144             value => 'name',
145             from => 'xtab_person'
146             },
147             {
148             id => 'xtab_class.class_id',
149             value => 'class_name',
150             from => 'xtab_class'
151             },
152             ],
153             from =>
154             qq{xtab_sales
155             INNER JOIN xtab_customers
156             ON (xtab_sales.customer_id=xtab_customers.customer_id)
157             INNER JOIN xtab_person
158             ON (xtab_sales.person_id=xtab_person.person_id)
159             INNER JOIN xtab_class
160             ON (xtab_sales.class_id=xtab_class.class_id)
161             INNER JOIN xtab_locations
162             ON (xtab_locations.loc_id=xtab_person.loc_id)
163             },
164             )
165             or die "error adding mandatory parameters ($DBIx::SQLCrosstab::errstr)";
166              
167             $xtab2->set_param(
168             title => 'Sales',
169             remove_if_null => 1,
170             remove_if_zero => 1,
171             use_real_names => 1,
172             add_colors => 1,
173             col_total => 1,
174             col_sub_total => 1,
175             row_total => 1,
176             row_sub_total => 1,
177             commify => 1,
178             table_border => 3,
179             header_color => "#009999",
180             text_color => "#3399cc",
181             number_color => "#ff00ff",
182             footer_color => "#33cc33",
183             )
184             or die "error adding optional parameters ($DBIx::SQLCrosstab::errstr)";
185              
186             #
187             # Check that everything is OK.
188             # Alternatively, you can save the query and the recordset
189             # to a variable for further use
190             #
191              
192             unless ($xtab2->get_query and $xtab2->get_recs) {
193             die "$DBIx::SQLCrosstab::errstr";
194             }
195              
196             #
197             # Save the current parameters to a file
198             #
199             $xtab2->save_params("test/xtab2.pl") or die "$DBIx::SQLCrosstab::errstr";
200              
201             #
202             # Create a third object
203             #
204             my $xtab3 = DBIx::SQLCrosstab::Format->new('STUB')
205             or die "error in \$xtab3 creation ($DBIx::SQLCrosstab::errstr)\n";
206              
207             #
208             # Use the saved parameters to set it up
209             #
210             $xtab3->load_params("test/xtab2.pl")
211             or die "\$xtab3 -> $DBIx::SQLCrosstab::errstr";
212             $xtab3->set_param( dbh => $dbh)
213             or die "\$xtab3 -> $DBIx::SQLCrosstab::errstr";
214              
215             unless ($xtab3->get_query and $xtab3->get_recs) {
216             die "$DBIx::SQLCrosstab::errstr";
217             }
218              
219             my $fname = 'table00';
220              
221             for my $xt (($xtab1, $xtab2, $xtab3)) {
222             $fname++;
223             #
224             # create a html example
225             #
226             open HTML, ">test/$fname.html"
227             or die "can't create $fname.html\n";
228             print HTML $xt->html_header;
229             print HTML "

",$xt->op_list, " FROM ", $xt->{title}, "

";
230              
231             my $table = $xt->as_html;
232             $table =~ s/\bzzzz\b/total/g;
233             print HTML $table;
234             my $bare_table = $xt->as_bare_html;
235             $bare_table =~ s/\bzzzz\b/total/g;
236             print HTML "

\n",$bare_table;
237             print HTML $xt->html_footer;
238             close HTML;
239             print "$fname.html created\n";
240              
241             #
242             # create a xml example
243             #
244             my $xml = $xt->as_xml
245             or die "$DBIx::SQLCrosstab::errstr";
246             open XML, ">test/$fname.xml"
247             or die "can't create $fname.xml";
248             print XML $xml;
249             close XML;
250             print "$fname.xml created\n";
251              
252             #
253             # create a xls example (requires Spreadsheet::WriteExcel)
254             #
255             eval { require Spreadsheet::WriteExcel; } ;
256             # only if Spreadsheet::WriteExcel is installed
257             if ($@) {
258             print "Spreadsheet::WriteExcel not installed - test skipped\n"
259             }
260             else {
261             if ( $xt->as_xls("test/$fname.xls", "both") ) {
262             print "$fname.xls created\n";
263             }
264             else {
265             print "$DBIx::SQLCrosstab::errstr\n";
266             }
267             }
268             #
269             # create a csv example
270             #
271             open CSV, ">test/$fname.csv"
272             or die "can't create $fname.csv\n";
273             my $csv = $xt->as_csv('header')
274             or die "$DBIx::SQLCrosstab::errstr\n";
275             print CSV $csv;
276             close CSV;
277             print "$fname.csv created\n";
278              
279             #
280             # create a yaml example
281             #
282             eval { require YAML; };
283             # only if YAML is installed
284             if ($@) {
285             print "YAML not installed - test skipped\n";
286             }
287             else {
288             open YAML, ">test/$fname.yaml"
289             or die "can't create $fname.yaml\n";
290             my $yaml = $xt->as_yaml;
291             if ($yaml) {
292             print YAML $yaml;
293             }
294             else {
295             print "$DBIx::SQLCrosstab::errstr\n";
296             }
297             close YAML;
298             print "$fname.yaml created\n" if $yaml;
299             }
300             #
301             # create a sample of generated Perl structures
302             #
303             open STRUCT, ">test/$fname.pl"
304             or die "can't create $fname.pl\n";
305             local $Data::Dumper::Indent=1;
306             print STRUCT Data::Dumper->Dump(
307             [
308             $xt->as_perl_struct('loh'),
309             $xt->as_perl_struct('losh'),
310             $xt->as_perl_struct('hoh')
311             ],
312             ['loh','losh','hoh']);
313             close STRUCT;
314             print "$fname.pl created\n";
315             #print map {"$_\n"} @{ $xt->{header_tree}->draw_ascii_tree};
316             #print map {"$_\n"} @{ $xt->{recs_tree}->draw_ascii_tree};
317             #print YAML::Dump( $xt->{header_tree});
318             #print YAML::Dump($xt->{recs_tree});
319             #print Dumper($xt->{recs_formats});
320             #print Dumper($xt->{header_formats});
321             }
322