scripts/sqlreport

filter: Statementstmt 83.7% 62 / 74 Branchbran 64.2% 18 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 79.1% 91 / 115 CC 14 SCAR 24.1
CC
14
Cov
78%
process_args
16.2
BEGIN
1.0
BEGIN
1.0
CRAP 11.1
« blib/lib/SQLite/Work/Mail.pm ↑ Summary
1 #!/usr/bin/env perl
2 package main;
3 3 $main::VERSION = '0.1601';
4 # vim:ts=8 sw=4 sts=4 ai
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 sqlreport - make reports on a table in an SQLite database
11  
12 =head1 VERSION
13  
14 version 0.1601
15  
16 =head1 SYNOPSIS
17  
18 sqlreport --help | --manpage | --version
19  
20 sqlreport [ --all_pages ] --database I<database_file>
21 [ --distinct ] { --force_show I<colname>=1 }
22 { --groups I<template> } { --headers I<template> } { --header_start I<num> }
23 [ --index_template I<template> ] [ --filename_format I<format> ]
24 [ --layout I<string> ] [ --limit I<number> ] [ --link_suffix I<string> ]
25 { --not_where I<colname>=1 } [ --outfile I<filename> ]
26 [ --page I<number> ] [ --report_style I<string> ]
27 { --row_ids I<table>=I<colname> } [ --report_template I<template> ]
28 [ --row_template I<template> ] [ --prev_next_template I<template> ]
29 [ --multi_page_template I<template> ] [ --split_ind_template I<template> ]
30 { --show I<colname> }
31 { --sort_by I<colname> } { --sort_reversed I<colname>=1 }
32 [ --split_col I<colname> [ --split_alpha I<number> ] ]
33 --table I<table> [ --table_border I<number> ]
34 [ -- table_header I<string> ]
35 [ --title I<string> ] [ --total ] [ --truncate_colnames I<number> ]
36 { --use_package I<pkgname> } { --where I<colname>=I<string> }
37  
38 =head1 DESCRIPTION
39  
40 This makes a report in HTML format, of a single table from an
41 SQLite database. One can also create a non-HTML report if one
42 gives a certain combination of options, but this is more oriented
43 towards HTML reports.
44  
45 =head1 OPTIONS
46  
47 =over
48  
49 =item --all_pages
50  
51 Make a multi-page report, generating all pages, by page-number.
52 The --limit and --outfile options are required for this.
53  
54 =item --database
55  
56 The name of the database file to use. (required)
57  
58 =item --distinct
59  
60 If columns are given to show (see L<show>), then this will
61 ensure that rows with exactly the same values will not be
62 repeated.
63  
64 =item --force_show
65  
66 An set of columns to always show in a row, even if they've
67 already been shown in a header (see L<show>).
68  
69 =item --groups
70  
71 Group template(s) (or filenames of files containing
72 group templates). A group template is a template for values
73 which are "grouped" under a corresponding header. The first
74 group in the array is placed just after the first header in
75 the report, and so on.
76  
77 This argument can be repeated.
78  
79 See L<headers> for more information.
80  
81 =item --headers
82  
83 An array of header templates (or filenames of files containing header
84 templates). A header template lays out what values should be put
85 into headers rather than the body of the report. The first header
86 template is given a H1 header, the second a H2 header, and so on.
87 Headers are shown only when the value(s) they depend on change,
88 but they get their values from each row in the report. Therefore
89 the columns used in the headers should match the columns used in the
90 L<sort_by> array.
91  
92 The column names are the variable names in this template. This has
93 a different format to the L<report_template>; it is more sophisticated.
94  
95 The format is as follows:
96  
97 =over
98  
99 =item {$colname}
100  
101 A variable; will display the value of the column, or nothing if
102 that value is empty.
103  
104 =item {?colname stuff [$colname] more stuff}
105  
106 A conditional. If the value of 'colname' is not empty, this will
107 display "stuff value-of-column more stuff"; otherwise it displays
108 nothing.
109  
110 {?col1 stuff [$col1] thing [$col2]}
111  
112 This would use both the values of col1 and col2 if col1 is not
113 empty.
114  
115 =item {?colname stuff [$colname] more stuff!!other stuff}
116  
117 A conditional with "else". If the value of 'colname' is not empty, this
118 will display "stuff value-of-column more stuff"; otherwise it displays
119 "other stuff".
120  
121 This version can likewise use multiple columns in its display parts.
122  
123 {?col1 stuff [$col1] thing [$col2]!![$col3]}
124  
125 =back
126  
127 The same format is used for L<groups> and L<row_template> and L<prev_next_template>.
128  
129 =item --help
130  
131 Print help message and exit.
132  
133 =item --index_template
134  
135 Similar to the L<report_template>, but this is used for the index-pages
136 in multi-page and split reports. It has the same format, but it
137 can be useful to have them as two separate templates as one may wish
138 to change the way the title is treated for indexes versus actual
139 reports.
140  
141 =item --layout
142  
143 The layout of the report. This determines both how rows are grouped,
144 and what is in the generated L<row_template> if no row_template is
145 given.
146  
147 =over
148  
149 =item table
150  
151 The report is a (group of) tables, each row of the report is a row in
152 the table; a new table occurs after the heading(s).
153  
154 =item para
155  
156 The report is in paragraphs, each row of the report is one paragraph.
157  
158 =item list
159  
160 The report is a (group of) lists, each row of the report is an item in
161 the list; a new list occurs after the heading(s).
162  
163 =item fieldval
164  
165 The rows are not HTML-formatted. The generated row_template is made up
166 of Field:Value pairs, one on each line.
167  
168 =item none
169  
170 The rows are not HTML-formatted. The generated row_template is made up
171 of values, one on each line.
172  
173 =back
174  
175 =item --limit
176  
177 The maximum number of rows to display per page. If this is zero,
178 then all rows are displayed in one page.
179  
180 =item --link_suffix I<string>
181  
182 The 'link_suffix' argument, if given, overrides the suffix given
183 in links to the other pages in a multi-page report; this is useful
184 if you're post-processing the files (and thus changing their extensions)
185 or are using something like Apache MultiViews to eliminate the need for
186 extensions in links.
187  
188 --link_suffix '.shtml'
189  
190 --link_suffix ''
191  
192 =item --manpage
193  
194 Print the full help documentation (manual page) and exit.
195  
196 =item --not_where
197  
198 A hash containing the column names where the selection criteria
199 in L<where> should be negated.
200  
201 =item --outfile
202  
203 The name of the output file. If this is not given, or the name is '-'
204 then the output goes to STDOUT.
205  
206 =item --page
207  
208 Select which page to generate, if limit is not zero.
209  
210 =item --prev_next_template
211  
212 Template for previous and next links on multi-page reports.
213  
214 =item --report_style
215  
216 The style of the report, especially as regards table layout.
217  
218 =over
219  
220 =item full
221  
222 =item medium
223  
224 =item compact
225  
226 =item bare
227  
228 =back
229  
230 =item --report_template
231  
232 Either a string containing a template, or string containing the name of
233 a template file. The template variables are in the following format:
234  
235 <!--sqlr_title-->
236  
237 The following variables are set for the report:
238  
239 =over
240  
241 =item sqlr_title
242  
243 Title (generally the table name).
244  
245 =item sqlr_contents
246  
247 The report itself.
248  
249 =back
250  
251 =item --row_ids
252  
253 The default column-name which identifies rows in SQLite is 'rowid', but
254 for tables which have a primary integer key, this doesn't work (even
255 though the documentation says it ought to). Therefore it is necessary
256 to identify, for the given database, which tables need to use a
257 different column-name for this. (This can be repeated)
258  
259 =item --row_template
260  
261 The template for each row. This uses the same format as for L<headers>.
262 If none is given, then a default row_template will be generated,
263 depending on what L<layout> and which columns are going to be shown
264 (see L<show>).
265  
266 Therefore it is important that if one provides a row_template, that
267 it matches the current layout.
268  
269 Also note that if a column is given in a header, it will not be
270 displayed in a row, even if it is put into the row_template.
271  
272 =item --show
273  
274 An array of columns to select; also the order in which they should
275 be shown when a L<row_template> has not been given.
276 If this option is not used, all columns in the table will be shown.
277  
278 =item --sort_by
279  
280 An array of column names by which the result should be sorted.
281 (Repeat the argument for each new value)
282  
283 =item --sort_reversed
284  
285 A hash of column names where the sorting given in L<sort_by> should
286 be reversed.
287  
288 =item --split_col
289  
290 Generate a multi-page report where pages are split by the value
291 of the given column (as well as by page-number if a limit is given)
292  
293 =item --split_alpha
294  
295 If one is generating a split_col report, giving the 'split_alpha' option
296 splits the report not by the distinct values of that column, but
297 by truncated values of the column; giving a split_alpha value
298 of 1 takes only the first letter, and so on.
299  
300 =item --table
301  
302 The table to report on. (required)
303  
304 =item --table_border
305  
306 For fine-tuning the L<report_style>; if the L<layout> is 'table',
307 then this overrides the default border-size of the table.
308  
309 =item --table_header
310  
311 When the report layout is 'table' and the report_style is not 'bare',
312 then this argument can be used to customize the table-header
313 of the report table. This must either contain the contents
314 of the table-header, or the name of a file which contains
315 the contents of the table-header.
316  
317 If this argument is not given, the table-header will be constructed
318 from the column names of the columns to be shown.
319  
320 =item --title
321  
322 The title of the report; if this is empty, a title will be generated.
323  
324 =item --total
325  
326 Just print the total matching rows, then exit.
327  
328 =item --truncate_colnames
329  
330 For fine-tuning the L<report_style>; this affects the length of
331 column names given in layouts which use them, that is, 'table'
332 (for all styles except 'bare') and 'para'. If the value is zero,
333 the column names are not truncated at all; otherwise they are
334 truncated to that number of characters.
335  
336 =item --verbose
337  
338 Print informational messages.
339  
340 =item --version
341  
342 Print version information and exit.
343  
344 =item --where
345  
346 A hash containing selection criteria. The keys are the column names
347 and the values are strings suitable for using in a GLOB condition;
348 that is, '*' is a multi-character wildcard, and '?' is a
349 single-character wildcard. All the conditions will be ANDed together.
350  
351 Yes, this is limited and doesn't use the full power of SQL, but it's
352 useful enough for most purposes.
353  
354 =item --use_package
355  
356 An array of package names of packages to "use".
357 This is mainly so that the {&funcname())} construct of
358 the templates (see L<SQLite::Work::Template>) can call
359 functions within these packages (using their fully-qualified
360 names).
361  
362 =back
363  
364 =head1 REQUIRES
365  
366 Getopt::Long
367 Pod::Usage
368 Getopt::ArgvFile
369 SQLite::Work;
370  
371 =head1 SEE ALSO
372  
373 perl(1)
374 Getopt::Long
375 Getopt::ArgvFile
376 Pod::Usage
377  
378 =cut
379  
380 3 ▶use Getopt::Long 2.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
381 3 ▶use Getopt::ArgvFile qw(argvFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
382 3 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
383 3 ▶use SQLite::Work;
Subroutine BEGIN: called CC=1 SCAR=0.0
384  
385 #========================================================
386 # Subroutines
387  
388 3 ▶sub init_data ($) {
Subroutine init_data: called CC=1 SCAR=0.0
389 3 my $data_ref = shift;
390  
391 3 $data_ref->{args} = {};
392 3 $data_ref->{args}->{manpage} = 0;
393 3 $data_ref->{args}->{verbose} = 0;
394 3 $data_ref->{args}->{debug} = 0;
395 3 $data_ref->{args}->{link_suffix} = undef;
396 } # init_data
397  
398 3 ▶sub process_args ($) {
Subroutine process_args: called CC=14 SCAR=27.8
399 3 my $data_ref = shift;
400  
401 3 my $ok = 1;
402  
403 3 argvFile(home=>1,
404 current=>1,
405 startupFilename=>'.sqlreportrc',
406 fileOption=>'options');
407  
408 3 ▶ pod2usage(2) unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 3 3
409  
410 3 my $op = new Getopt::Long::Parser;
411 3 $op->configure(qw(auto_version auto_help));
412 $op->getoptions($data_ref->{args},
413 3 ▶ 'verbose!',
Branch: 1 branch
BranchTrueFalseTotal
$op->getoptions($data_ref->{"args"}, "verbose!", "debug!", "manpage", "database=s", "distinct!", "row_ids=s%", "report_template=s", "index_template=s", "filename_format=s", "table=s", "where=s%", "not_where=s%", "sort_by=s\@", "sort_reversed=s%", "show=s\@", "force_show_cols=s%", "headers=s\@", "header_start=i", "groups=s\@", "limit=i", "page=i", "report_style=s", "table_border=i", "table_header=s", "truncate_colnames=i", "layout=s", "row_template=s", "prev_next_template=s", "multi_page_template=s", "split_ind_template=s", "default_format=s%", "title=s", "outfile=s", "all_pages!", "total!", "split_col=s", "split_titlefmt=s", "split_alpha=n", "use_package=s\@", "link_suffix=s") or pod2usage(2) 3 0 3
414 'debug!',
415 'manpage',
416 'database=s',
417 'distinct!',
418 'row_ids=s%',
419 'report_template=s',
420 'index_template=s',
421 'filename_format=s',
422 'table=s',
423 'where=s%',
424 'not_where=s%',
425 'sort_by=s@',
426 'sort_reversed=s%',
427 'show=s@',
428 'force_show_cols=s%',
429 'headers=s@',
430 'header_start=i',
431 'groups=s@',
432 'limit=i',
433 'page=i',
434 'report_style=s',
435 'table_border=i',
436 'table_header=s',
437 'truncate_colnames=i',
438 'layout=s',
439 'row_template=s',
440 'prev_next_template=s',
441 'multi_page_template=s',
442 'split_ind_template=s',
443 'default_format=s%',
444 'title=s',
445 'outfile=s',
446 'all_pages!',
447 'total!',
448 'split_col=s',
449 'split_titlefmt=s',
450 'split_alpha=n',
451 'use_package=s@',
452 'link_suffix=s',
453 ) or pod2usage(2);
454  
455 3 ▶ if ($data_ref->{'manpage'})
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"manpage"}) 0 3 3
456 {
457 0 pod2usage({ -message => "$0 version $::VERSION",
458 -exitval => 0,
459 -verbose => 2,
460 });
461 }
462 # set the parameters for 'new'
463 3 $data_ref->{new_params} = {};
464 3 foreach my $key (qw(database row_ids report_template index_template use_package))
465 {
466 15 ▶ if (exists $data_ref->{args}->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data_ref->{"args"}{$key}) 6 9 15
467 {
468 $data_ref->{new_params}->{$key}
469 6 = $data_ref->{args}->{$key};
470 }
471 }
472 3 ▶ if (!$data_ref->{new_params}->{database})
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_ref->{"new_params"}{"database"}) 0 3 3
473 {
474 0 warn "$0: no database given!\n";
475 0 pod2usage({ -message => "$0 version $::VERSION",
476 -exitval => 1,
477 -verbose => 0,
478 });
479 }
480  
481 # parse the default_format argument
482 3 ▶ if (exists $data_ref->{args}->{default_format}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data_ref->{"args"}{"default_format"} and defined $data_ref->{"args"}{"default_format"}) 1 2 3
Condition66%2/3
exists $data_ref->{"args"}{"default_format"} and defined $data_ref->{"args"}{"default_format"}
!ll&&!rl&&r
201
MC/DC50%1/2
exists $data_ref->{"args"}{"default_format"} and defined $data_ref->{"args"}{"default_format"}
exists $data_ref->{"args"}{"default_format"} defined $data_ref->{"args"}{"default_format"}
Truth table66%2/3
A and B
A= exists $data_ref->{"args"}{"default_format"}B= defined $data_ref->{"args"}{"default_format"}
ABresult
0X0
100
111
483 and defined $data_ref->{args}->{default_format})
484 {
485 1 $data_ref->{new_params}->{default_format} = {};
486 1 ▶ while (my ($key, $val) = each %{$data_ref->{args}->{default_format}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$data_ref{"args"}{"default_format"};}) 2 1 3
487 {
488 2 ▶ if ($key =~ m/(\w+)\+(\w+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(\w+)\+(\w+)/) 2 0 2
489 {
490 2 my $table = $1;
491 2 my $col = $2;
492 2 $data_ref->{new_params}->{default_format}->{$table}->{$col}
493 = $val;
494 }
495 }
496 }
497  
498 # set the parameters for 'report'
499 3 $data_ref->{report_params} = {};
500 3 foreach my $key (qw(verbose title table distinct where not_where sort_by sort_reversed force_show_cols show headers header_start groups limit page report_style layout link_suffix table_border table_header truncate_colnames prev_next_template multi_page_template split_ind_template row_template filename_format outfile split_col split_alpha split_titlefmt debug))
501 {
502 93 ▶ if (exists $data_ref->{args}->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data_ref->{"args"}{$key}) 28 65 93
503 {
504 $data_ref->{report_params}->{$key}
505 28 = $data_ref->{args}->{$key};
506 }
507 }
508 3 ▶ if (!$data_ref->{report_params}->{table})
Branch: 1 branch
BranchTrueFalseTotal
if (not $data_ref->{"report_params"}{"table"}) 0 3 3
509 {
510 0 warn "$0: no table given!\n";
511 0 pod2usage({ -message => "$0 version $::VERSION",
512 -exitval => 1,
513 -verbose => 0,
514 });
515 }
516  
517 } # process_args
518  
519 #========================================================
520 # Main
521  
522 MAIN: {
523 3 my %data = ();
524  
525 3 init_data(\%data);
526 3 process_args(\%data);
527 3 my $rep = SQLite::Work->new(%{$data{new_params}});
528 3 ▶ if ($rep->do_connect())
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->do_connect) 3 0 3
529 {
530 3 ▶ if ($data{args}->{total})
Branch: 3 branches
BranchTrueFalseTotal
if ($data{'args'}{'total'}) { } 0 3 3
elsif ($data{'args'}{'split_col'}) { } 0 3 3
elsif ($data{'args'}{'all_pages'}) { } 0 3 3
531 {
532 0 my $total = $rep->get_total_matching(%{$data{report_params}});
533 0 print $total, "\n";
534 }
535 elsif ($data{args}->{split_col})
536 {
537 0 $rep->do_split_report(%{$data{report_params}});
538 }
539 elsif ($data{args}->{all_pages})
540 {
541 0 $rep->do_multi_page_report(%{$data{report_params}});
542 }
543 else
544 {
545 3 $rep->do_report(%{$data{report_params}});
546 }
547 3 $rep->do_disconnect();
548 }
549 }
550  
551 =head1 BUGS
552  
553 Please report any bugs or feature requests to the author.
554  
555 =head1 AUTHOR
556  
557 Kathryn Andersen (RUBYKAT)
558 perlkat AT katspace dot com
559 http://www.katspace.com
560  
561 =head1 COPYRIGHT AND LICENCE
562  
563 Copyright (c) 2005 by Kathryn Andersen
564  
565 This program is free software; you can redistribute it and/or modify it
566 under the same terms as Perl itself.
567  
568  
569 =cut
570  
571 __END__
« blib/lib/SQLite/Work/Mail.pm ↑ Summary