| 1 | package DBIx::Repgen; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | ############################################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ############################################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 11 | ############################################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | DBIx::Repgen - simple report generator from DB-selected data | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | use Repgen; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 22 | dbh => DBI->connect(...), | ||||||||||||||||||||||||||||||||||||||||||
| 23 | query => 'select ... from ...', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | repdata => { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | today => `date` | ||||||||||||||||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | group => ['id'], | ||||||||||||||||||||||||||||||||||||||||||
| 30 | header => "========\n", | ||||||||||||||||||||||||||||||||||||||||||
| 31 | footer => sub {my ($r, $data) = @_; return "$data->{NAME} : $data->{VALUE}"}, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | item => ["%20s %s", qw/NAME VALUE/], | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | output => \$out; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | $r->run(cust => 'tolik'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | print $out; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | This package implements class C<DBIx::Repgen>, which is simple report generator from data | ||||||||||||||||||||||||||||||||||||||||||
| 43 | received from relational database by some select-statement. Such a report can contain | ||||||||||||||||||||||||||||||||||||||||||
| 44 | hyerarchical grouping by field values, record counters and cumulative totals (sums) of numeric | ||||||||||||||||||||||||||||||||||||||||||
| 45 | fields for each group as well as for whole report. Each rerort part formatting may be set | ||||||||||||||||||||||||||||||||||||||||||
| 46 | as literal string, arguments of C<sprint> function or be code reference. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =head2 new, class constructor | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | Constructor has one argument, hashe. Elements of this hashe define the report and are | ||||||||||||||||||||||||||||||||||||||||||
| 51 | descriebed below. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =item sth, dbh, query - data source setting | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | The report data are got by executing some select statement against relational database | ||||||||||||||||||||||||||||||||||||||||||
| 58 | environment. There are following wais for defining this statement. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =item 1. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | Constructor receives in C<sth> element prepared (C<$dbh->prepare>) but not executed | ||||||||||||||||||||||||||||||||||||||||||
| 65 | (C<$sth->execute>) statement handle. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =item 2. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Constructor receives database connection handle (from C<DBI->connect(...)>) and full text | ||||||||||||||||||||||||||||||||||||||||||
| 70 | of select statement to be executed. Needed C<prepare> and C<execute> calls will perform | ||||||||||||||||||||||||||||||||||||||||||
| 71 | by the report run. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =item 3. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | Constructor receives already prepared and executed statement handle. In this case C<noexec> | ||||||||||||||||||||||||||||||||||||||||||
| 76 | constructor parameter must be set to true. This feature may be useful by dynamic-made select | ||||||||||||||||||||||||||||||||||||||||||
| 77 | queryes in calling programm. No prepare nor execute action will be performed by report run. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | I<Important note>: you have to reset (by C<Set> method) this statemeny handle before each next | ||||||||||||||||||||||||||||||||||||||||||
| 80 | report run. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | Samples: | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | $dbh = DBI->connect('dbi:Oracle:SID', 'user', 'password'); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | $sth1 = $dbh->prepare('select name, value from tab where value between ? and ?'); | ||||||||||||||||||||||||||||||||||||||||||
| 88 | $rep1 = DBIx::Repgen->new(sth => $sth); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | $rep2 = DBIx::Repgen->new(dbh => $dbh, query => "select ... "); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | $sth3 = $dbh->prepare('select ...'); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | $sth3->execute(@param); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | $rep3 = DBIx::Repgen->new(sth => $sth3, noexec => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | Using first two methods you may parametrize the report. This means sql-query can contain | ||||||||||||||||||||||||||||||||||||||||||
| 97 | placeholders, for substituting values in report run time. See below about report parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =item param - report parameters | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | The report may have set of named parameters. Single parameter definition contain its name, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | number (or some numbers) of placeholders in source select query and optional default value. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | Parametrs definition is a hash reference, value of C<param> element of constructor. Keys in this | ||||||||||||||||||||||||||||||||||||||||||
| 104 | hash are parameter names and values contain placeholder numbers and default values. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | In the simpliest case parameter definition can be just zero-based number of the only placeholder | ||||||||||||||||||||||||||||||||||||||||||
| 107 | corresponding to this parameter. In more complex cases is is hash reference. This hash I<must> | ||||||||||||||||||||||||||||||||||||||||||
| 108 | have C<n> key with value of integer or list of integers and I<may> have C<dflt> key, which | ||||||||||||||||||||||||||||||||||||||||||
| 109 | value must be scalar, code reference or array reference (where first element is code reference). | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | The C<n> key defines zero based number (or numbers) of placeholdes in source select query | ||||||||||||||||||||||||||||||||||||||||||
| 112 | corresponding to this parameter. The C<dflt> key defines default value for optional | ||||||||||||||||||||||||||||||||||||||||||
| 113 | parameters. If value of C<dflt> is code reference then default value is result of this code call (without | ||||||||||||||||||||||||||||||||||||||||||
| 114 | arguments). If value of C<dflt> is array reference then first element of this array must | ||||||||||||||||||||||||||||||||||||||||||
| 115 | be code reference. Default value of parameter in this case is result of call this code with arguments - | ||||||||||||||||||||||||||||||||||||||||||
| 116 | the rest of array. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | Sample of parameter definition. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | $rep = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 122 | param => { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | name => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | dep => {n => 1}, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | startdate => {n => [2, 4], dflt => '2000/01/01'}, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | enddate => {n => 3, dflt => \&DefEndDate}, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | salary => {n => 5, dflt => [sub {...}, 1000, 2000]} | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | In the example C<name> and C<dep> are required parameters corresponding to zero and first placeholders. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | C<startdate> has explicit default value and substituted to second and fouth placeholders. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | C<enddate> and C<salary> have defaults defining by code call in report run time, without and | ||||||||||||||||||||||||||||||||||||||||||
| 134 | with arguments in correspondence. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =item output - the way of report output | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | The C<output> constructor's parameter sets how and where the report puts its output data. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item undef or not present | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | The whole output data are the result of C<run> method call. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =item string reference | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | The output data are put into this string. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =item code reference | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | This code will be called with two arguments: the report object and string to be out. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Output samples. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | $r = DBIx::Repgen(...); | ||||||||||||||||||||||||||||||||||||||||||
| 160 | print $r->run(); | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | $s = ''; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | $r = DBIx::Repgen(..., output => \$s,); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | $r->run(); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | print $s; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | sub myprint { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | my ($r, $s) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | print "*** $s ***"; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | $r = DBIx::Repgen(..., output => \&myprint,); | ||||||||||||||||||||||||||||||||||||||||||
| 172 | $r->run(); | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =item group - repport groupping | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | The report may be I<groupped>. The group is sequence of records having the constant value of some | ||||||||||||||||||||||||||||||||||||||||||
| 177 | field. This field called I<group field>. The report may have several includded groups. For | ||||||||||||||||||||||||||||||||||||||||||
| 178 | group setting you have to define C<group> parameter of report constructor as a reference to | ||||||||||||||||||||||||||||||||||||||||||
| 179 | an array of group fields. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | Note that the right record's sequence must be provided by C<order> part in used select query, not | ||||||||||||||||||||||||||||||||||||||||||
| 182 | by report itself. Sample of grouping by countries and cities. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 185 | ..., | ||||||||||||||||||||||||||||||||||||||||||
| 186 | query => "select country, city, population from cities | ||||||||||||||||||||||||||||||||||||||||||
| 187 | order by country, city", | ||||||||||||||||||||||||||||||||||||||||||
| 188 | group => [qw/COUNTRY CITY/], | ||||||||||||||||||||||||||||||||||||||||||
| 189 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Note I<all> field names are in uppercase, regardless used database server. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =item total - cumulative totals | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | Value of this argument of constructor is reference to array with report fields to compute | ||||||||||||||||||||||||||||||||||||||||||
| 197 | totals. Each field summation executed for all the report as well as for each group. See | ||||||||||||||||||||||||||||||||||||||||||
| 198 | below about access to totals values. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =item header, footer, item etc. - definition of report parts | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | There are following I<parts> generated during report output. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =item item | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Outputs for each record of the report. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | =item header | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | Begin of whole report. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =item footer | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | Outputs after all, in the very end of report. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =item header_GROUPFIELD | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | Outputs in the begin of record group by GROUPFIELD field. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =item footer_GROUPFIELD | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | Outputs after record group by GROUPFIELD field. | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | Each of these report pats may be defined by several ways. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | =item string | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | The string will be printed "as is", without any processing. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 237 | header => "\t\tReport about countries and cities\n", | ||||||||||||||||||||||||||||||||||||||||||
| 238 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =item reference to array of strings | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | First element of this array have to be in form of C<sprintf> function format. The rest | ||||||||||||||||||||||||||||||||||||||||||
| 245 | of values in the array are I<names> (not values!) of current report data. See below | ||||||||||||||||||||||||||||||||||||||||||
| 246 | about current report data. | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 249 | footer => ["Total %d countries, %d cities, population %d people\n", | ||||||||||||||||||||||||||||||||||||||||||
| 250 | qw/num_COUNTRY num_CITY sum_POPULATION/], | ||||||||||||||||||||||||||||||||||||||||||
| 251 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =item code reference | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | The code is called with two arguments: report object and hash reference storing | ||||||||||||||||||||||||||||||||||||||||||
| 257 | current report data. Subroutine may use C<Output> method for output any needed | ||||||||||||||||||||||||||||||||||||||||||
| 258 | information or just return output string as its result. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 261 | item => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | my ($r, $d) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | $r->Output("%d %s", | ||||||||||||||||||||||||||||||||||||||||||
| 264 | $d->{POPULATION}, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | $d->{POPULATION} > 1_000_000 ? '*' : ' '); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | footer => sub {return "Report ended at " . `date`} | ||||||||||||||||||||||||||||||||||||||||||
| 269 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 270 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =item reference to array where first element is code reference | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | The code is called with following arguments: report object, current report data, the rest of | ||||||||||||||||||||||||||||||||||||||||||
| 275 | array elements. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | $r = DBIx::Repgen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 278 | header_COUNTRY => [\&hfcountry, 'header'], | ||||||||||||||||||||||||||||||||||||||||||
| 279 | header_COUNTRY => [\&hfcountry, 'footer'], | ||||||||||||||||||||||||||||||||||||||||||
| 280 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | sub hfcountry { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | my ($r, $d, $header_or_footer) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 285 | if ($header_or_footer eq 'header') {...} else {...}; | ||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =item max_items - max record number limit | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | If this parameter (integer number) is present then no more than C<max_items> records will be | ||||||||||||||||||||||||||||||||||||||||||
| 291 | output. It is possible to know via C<Aborted> method call if not all records were output. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | =head3 Current report data | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | All report state data are stored in internal report variables. Access to these data from | ||||||||||||||||||||||||||||||||||||||||||
| 298 | report parts is possible by data names. There are following fields in current | ||||||||||||||||||||||||||||||||||||||||||
| 299 | report data. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | =item FIELDNAME | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | Fields of current report's record. Name is in I<uppercase>. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | =item prev_FIELDNAME | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | Value of FIELDNAME in previous record. When group boundary is detected group field has new value, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | but its previous value is still stored. This value can be used in group footers. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | =item num_report | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | Number (one-based) of current output record for the whole report. This counter never resets. | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | =item num_item | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | Number of record in the innermost group. | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =item num_GROUPNAME | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | Number of group GROUPNAME in including group. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =item total_FIELDNAME | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | Cumulative total of FIELDNAME field for the whole report. Remember FIELDNAME must be listed | ||||||||||||||||||||||||||||||||||||||||||
| 327 | in C<total> constructor's parameter. | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =item total_GROUPNAME_FIELDNAME | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | Cumulative total by FIELDNAME field into GROUPNAME. These summators are reset each time | ||||||||||||||||||||||||||||||||||||||||||
| 332 | the group boundary is reached. | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | my ($class, %par) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 1 | ▶ | return bless \%par, ($class || ref $class); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$class || ref $class
MC/DC0%0/2
$class || ref $class
$class
ref $class
Truth table33%1/3
A || B
A= $classB= ref $class
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | =head2 run, report execution | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | $r->run(%param); | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | The report is run and output. Input parameters are substituted as values for select query | ||||||||||||||||||||||||||||||||||||||||||
| 356 | placeholders (see above about report's parameters). If there was no C<output> constructor's parameter | ||||||||||||||||||||||||||||||||||||||||||
| 357 | then the text of report returned as a result of this method. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=31 SCAR=55.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | my ($rep, %param) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | my $warn = $^W; | |||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | $^W = 0; | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | ▶ | unless ($rep->{sth}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | croak "Missing 'dbh' arg" unless exists $rep->{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | croak "Missing or non-select query" unless $rep->{query} && $rep->{query} =~ /^\s*select\b/si; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rep->{"query"} and $rep->{"query"} =~ /^\s*select\b/is
MC/DC0%0/2
$rep->{"query"} and $rep->{"query"} =~ /^\s*select\b/is
$rep->{"query"}
$rep->{"query"} =~ /^\s*select\b/is
Truth table0%0/3
A and B
A= $rep->{"query"}B= $rep->{"query"} =~ /^\s*select\b/is
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $rep->{sth} = $rep->{dbh}->prepare($rep->{query}); | |||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 1 | ▶ | unless ($rep->{output}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 1 | $rep->{outputstr} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | $rep->{output} = \$rep->{outputstr}; | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 1 | delete $rep->{not_first}; | |||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 1 | $rep->{data} = {num_report => 0, num_item => 0}; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 1 | ▶ | $rep->{param} = {} unless exists $rep->{param}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | my @param = (); | |||||||||||||||||||||||||||||||||||||||||
| 384 | 1 | ▶ | goto AFTEREXEC if $rep->{noexec}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | for my $p (keys %{$rep->{param}}) { | |||||||||||||||||||||||||||||||||||||||||
| 386 | $rep->{param}{$p} = {n => $rep->{param}{$p}} | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | unless ref($rep->{param}{$p}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | croak "No positions are given for '$p' parameter" unless exists $rep->{param}{$p}{n}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | my @n; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | if (ref ($rep->{param}{$p}{n}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | @n = @{$rep->{param}{$p}{n}}; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } elsif (!ref($rep->{param}{$p}{n})) { | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | @n = ($rep->{param}{$p}{n}); | |||||||||||||||||||||||||||||||||||||||||
| 395 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | croak "Non scalar nor array reference positions for '$p' parameter"; | |||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my $val; | |||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | if (defined($param{$p}) && $param{$p} ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $param{$p} and $param{$p} ne ""
MC/DC0%0/2
defined $param{$p} and $param{$p} ne ""
defined $param{$p}
$param{$p} ne ""
Truth table0%0/3
A and B
A= defined $param{$p}B= $param{$p} ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | $val = $param{$p}; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } elsif (defined $rep->{param}{$p}{dflt}) { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | $val = $rep->{param}{$p}{dflt}; | |||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | unless (ref $val) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | # nothing | ||||||||||||||||||||||||||||||||||||||||||
| 406 | } elsif (ref($val) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | $val = $val->(); | |||||||||||||||||||||||||||||||||||||||||
| 408 | ▶ | } elsif (ref($val) eq 'ARRAY' && $val->[0] && (ref($val->[0]) eq 'CODE')) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
ref $val eq "ARRAY" and $val->[0] and ref $val->[0] eq "CODE"
ref $val eq "ARRAY" and $val->[0]
MC/DC0%0/3
ref $val eq "ARRAY" and $val->[0] and ref $val->[0] eq "CODE"
ref $val eq "ARRAY"
$val->[0]
ref $val->[0] eq "CODE"
Truth table0%0/4
A and B and C
A= ref $val eq "ARRAY"B= $val->[0]C= ref $val->[0] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my ($sub, @pars) = @$val; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | $val = $sub->(@pars); | |||||||||||||||||||||||||||||||||||||||||
| 411 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | croak "Wrong dflt for '$p' parameter"; | |||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||
| 414 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | croak "Cannot determine value for parameter '$p'"; | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | $param[$_] = $val for grep {$_ >= 0} @n; | |||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | $rep->{data}{"param_$p"} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | $rep->{sth}->execute(@param); | |||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | AFTEREXEC: | |||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | # úÁÇÏÌÏ×ÏË ÏÔÞÅÔÁ | ||||||||||||||||||||||||||||||||||||||||||
| 425 | $rep->PrintPart('header'); | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | # ÓÔÒÏËÉ ÏÔÞÅÔÁ | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | while ($rep->{row} = $rep->{sth}->fetchrow_hashref('NAME_uc')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 9 | $rep->PrintItem(); | |||||||||||||||||||||||||||||||||||||||||
| 430 | 9 | ▶ | $rep->Abort() if $rep->{max_items} && $rep->{max_items} <= $rep->{data}{num_report}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rep->{"max_items"} and $rep->{"max_items"} <= $rep->{"data"}{"num_report"}
MC/DC0%0/2
$rep->{"max_items"} and $rep->{"max_items"} <= $rep->{"data"}{"num_report"}
$rep->{"max_items"}
$rep->{"max_items"} <= $rep->{"data"}{"num_report"}
Truth table33%1/3
A and B
A= $rep->{"max_items"}B= $rep->{"max_items"} <= $rep->{"data"}{"num_report"}
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 9 | ▶ | last if $rep->Aborted(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | 1 | $rep->{sth}->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | # åÓÌÉ ÎÁÄÏ - ÚÁ×ÅÒÛÉÔÅÌÉ ÇÒÕÐÐ ÐÏÓÌÅ ÏÔÞÅÔÁ | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 1 | ▶ | if (exists $rep->{group}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | # æÏÒÍÉÒÕÅÍ "ÐÕÓÔÕÀ" ÓÔÒÏËÕ | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 1 | for (keys %{$rep->{data}}) { | |||||||||||||||||||||||||||||||||||||||||
| 439 | 11 | ▶ | $rep->{row}{$1} = undef if /prev_(.+)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 1 | $rep->PrintHeaderFooter(0, 'footer'); | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | # úÁ×ÅÒÛÉÔÅÌØ ÏÔÞÅÔÁ | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 1 | $rep->PrintPart('footer'); | |||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 1 | $^W = $warn; | |||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | # úÁËÒÙÔØ ËÏÎÎÅËÃÉÀ ÅÓÌÉ ÎÁÄÏ | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 1 | ▶ | $rep->{dbh}->disconnect() if $rep->{dbh} && $rep->{autoclose}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rep->{"dbh"} and $rep->{"autoclose"}
MC/DC0%0/2
$rep->{"dbh"} and $rep->{"autoclose"}
$rep->{"dbh"}
$rep->{"autoclose"}
Truth table33%1/3
A and B
A= $rep->{"dbh"}B= $rep->{"autoclose"}
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | return $rep->{outputstr}; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 9 | ▶ | sub PrintItem { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PrintItem: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 9 | my ($r) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | # óËÏÐÉÒÏ×ÁÔØ ÐÏÌÑ ÓÔÒÏËÉ × data | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 9 | $r->{data}{$_} = $r->{row}{$_} for keys %{$r->{row}}; | |||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | # ðÒÏÄ×ÉÎÕÔØ "ÓË×ÏÚÎÙÅ" ÓÕÍÍÁÔÏÒÙ ÐÏ ÐÏÌÑÍ | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 9 | ▶ | if (exists $r->{total}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | 9 | $r->{data}{'total_' . $_} += $r->{row}{$_} for @{$r->{total}}; | |||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | # åÓÔØ ÌÉ ÇÒÁÎÉÃÁ ÇÒÕÐÐÙ? | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 9 | my $group = $r->GroupGranze(); | |||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | # åÓÌÉ ÜÔÏ ÎÅ ÓÁÍÙÊ ÐÅÒ×ÙÊ ÒÁÚ - ×Ù×ÅÓÔÉ ÚÁ×ÅÒÛÉÔÅÌÉ ÇÒÕÐÐ | ||||||||||||||||||||||||||||||||||||||||||
| 470 | $r->PrintHeaderFooter($group, 'footer') | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 9 | ▶ | if defined $group && $r->{not_first}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $group and $r->{"not_first"}
MC/DC100%2/2
defined $group and $r->{"not_first"}
defined $group
$r->{"not_first"}
Truth table100%3/3
A and B
A= defined $groupB= $r->{"not_first"}
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | # õÓÔÁÎÏ×ÉÔØ, ÞÔÏ ÕÖÅ - ÎÅ ÐÅÒ×ÙÊ ÒÁÚ | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 9 | $r->{not_first} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | # ðÒÏÄ×ÉÎÕÔØ ÓË×ÏÚÎÏÊ ÎÏÍÅÒ É ÎÏÍÅÒ × ÐÒÅÄÅÌÁÈ ÍÌÁÄÛÅÊ ÇÒÕÐÐÙ | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 9 | $r->{data}{num_report} ++; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 9 | $r->{data}{num_item} ++; | |||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | # ÷Ù×ÅÓÔÉ ÚÁÇÏÌÏ×ÏË ÇÒÕÐÐÙ (ÐÒÉ ÜÔÏÍ ÓÂÒÁÓÙ×ÁÀÔÓÑ ÓÕÍÍÁÔÏÒÙ É ÎÕÍÅÒÁÔÏÒÙ) | ||||||||||||||||||||||||||||||||||||||||||
| 481 | 9 | ▶ | $r->PrintHeaderFooter($group, 'header') if defined $group; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | # ðÒÏÓÕÍÍÉÒÏ×ÔØ ÇÒÕÐÐÏ×ÙÅ ÓÕÍÍÁÔÏÒÙ | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 9 | ▶ | if ($r->{group} && $r->{total}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$r->{"group"} and $r->{"total"}
MC/DC0%0/2
$r->{"group"} and $r->{"total"}
$r->{"group"}
$r->{"total"}
Truth table33%1/3
A and B
A= $r->{"group"}B= $r->{"total"}
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 9 | for my $grname (@{$r->{group}}) { | |||||||||||||||||||||||||||||||||||||||||
| 486 | 9 | $r->{data}{'total_' . $grname . '_' . $_} += $r->{row}{$_} for @{$r->{total}}; | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | # ÷Ù×ÅÓÔÉ ÉÔÅÍ | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 9 | $r->PrintPart('item'); | |||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | # úÁÐÉÓÁÔØ × $data ÐÒÅÄÙÄÕÝÉÅ ÚÎÁÞÅÎÉÑ ÓÔÒÏËÉ | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 9 | $r->{data}{'prev_' . $_} = $r->{row}{$_} for keys %{$r->{row}}; | |||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | 6 | ▶ | sub PrintHeaderFooter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PrintHeaderFooter: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 6 | my ($r, $group, $hf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 501 | 6 | my @group = @{$r->{group}}; | |||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | # åÓÌÉ ÚÁÇÏÌÏ×ÏË | ||||||||||||||||||||||||||||||||||||||||||
| 504 | 6 | ▶ | if ($hf eq 'header') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | # óÂÒÏÓÉÔØ ÓÕÍÍÁÔÏÒÙ ÄÌÑ ËÁÖÄÏÊ ÇÒÕÐÐÙ ÓÔÁÒÛÅ ÕËÁÚÁÎÎÏÊ | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 3 | ▶ | if ($r->{total}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | 3 | for my $grname ((@group)[$group .. $#group]) { | |||||||||||||||||||||||||||||||||||||||||
| 508 | 3 | $r->{data}{'total_' . $grname . '_' . $_} = 0 for @{$r->{total}}; | |||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | # é ÎÕÍÅÒÁÔÏÒÙ | ||||||||||||||||||||||||||||||||||||||||||
| 513 | 3 | $r->{data}{'num_' . $r->{group}[$group]}++; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 3 | $r->{data}{'num_' . $_} = 1 for (@group)[$group+1 .. $#group]; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 3 | $r->{data}{'num_item'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | # ôÁËÉ ÎÁÐÅÞÁÔÁÔØ ÚÁÇÏÌÏ×ËÉ ÉÌÉ ÚÁ×ÅÒÛÉÔÅÌÉ | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 6 | $r->PrintPart($hf . '_' . $_) for (@group)[$group .. $#group]; | |||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | 9 | ▶ | sub GroupGranze { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
GroupGranze: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 9 | my ($r) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | 9 | ▶ | return undef unless $r->{group}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 9 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 528 | 9 | for my $fname (@{$r->{group}}) { | |||||||||||||||||||||||||||||||||||||||||
| 529 | 9 | ▶ | croak "No '$fname' group field in data" unless exists $r->{row}{$fname}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | return $i if | ||||||||||||||||||||||||||||||||||||||||||
| 532 | !exists($r->{data}{'prev_' . $fname}) || | ||||||||||||||||||||||||||||||||||||||||||
| 533 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 534 | (($r->{data}{'prev_' . $fname} ne $r->{row}{$fname})) || | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 9 | ▶ | (($r->{data}{'prev_' . $fname} != $r->{row}{$fname})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not exists $r->{"data"}{'prev_' . $fname} or ($r->{"data"}{'prev_' . $fname} ne $r->{"row"}{$fname} or $r->{"data"}{'prev_' . $fname} != $r->{"row"}{$fname})
$r->{"data"}{'prev_' . $fname} ne $r->{"row"}{$fname} or $r->{"data"}{'prev_' . $fname} != $r->{"row"}{$fname}
MC/DC66%2/3
not exists $r->{"data"}{'prev_' . $fname} or ($r->{"data"}{'prev_' . $fname} ne $r->{"row"}{$fname} or $r->{"data"}{'prev_' . $fname} != $r->{"row"}{$fname})
not exists $r->{"data"}{'prev_' . $fname}
$r->{"data"}{'prev_' . $fname} ne $r->{"row"}{$fname}
$r->{"data"}{'prev_' . $fname} != $r->{"row"}{$fname}
Truth table75%3/4
not(A) or B or C
A= not exists $r->{"data"}{'prev_' . $fname}B= $r->{"data"}{'prev_' . $fname} ne $r->{"row"}{$fname}C= $r->{"data"}{'prev_' . $fname} != $r->{"row"}{$fname}
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 6 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | 6 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | 17 | ▶ | sub PrintPart { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PrintPart: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 17 | my ($r, $part) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | 17 | ▶ | return unless $r->{$part}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | 17 | my ($fmt, @par); | |||||||||||||||||||||||||||||||||||||||||
| 550 | 17 | ▶ | if (ref($r->{$part}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | 14 | ($fmt, @par) = @{$r->{$part}}; | |||||||||||||||||||||||||||||||||||||||||
| 552 | ▶ | } elsif (ref($r->{$part}) eq 'CODE' || !ref($r->{$part})) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $r->{$part} eq "CODE" or not ref $r->{$part}
MC/DC0%0/2
ref $r->{$part} eq "CODE" or not ref $r->{$part}
ref $r->{$part} eq "CODE"
not ref $r->{$part}
Truth table33%1/3
A or not(B)
A= ref $r->{$part} eq "CODE"B= not ref $r->{$part}
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | 3 | ($fmt, @par) = ($r->{$part}); | |||||||||||||||||||||||||||||||||||||||||
| 554 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | croak sprintf("Non supported type of format: '%s'", ref($r->{$part})); | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 17 | my $s; | |||||||||||||||||||||||||||||||||||||||||
| 559 | 17 | ▶ | if (ref $fmt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 560 | 4 | $s = $fmt->($r, $r->{data}, @par); | |||||||||||||||||||||||||||||||||||||||||
| 561 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 562 | 13 | $s = sprintf($fmt, map {$r->{data}{$_}} @par); | |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 17 | $r->Output($s); | |||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | =head2 Output | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | $r->Output("Any values: %s and %d", 'qazwsx', 654); | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | This method has the same arguments as C<sprintf> function. It adds formatted string to the output | ||||||||||||||||||||||||||||||||||||||||||
| 573 | stream (set by C<output> param). This method is useful in the code called during the output of | ||||||||||||||||||||||||||||||||||||||||||
| 574 | report parts. | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 17 | ▶ | sub Output { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Output: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 17 | my ($r, $s, @par) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 17 | ▶ | $s = sprintf($s, @par) if @par; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | 17 | ▶ | if (ref($r->{output}) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | $r->{output}->($r, $s); | |||||||||||||||||||||||||||||||||||||||||
| 585 | } elsif (ref($r->{output}) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 17 | ${$r->{output}} .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 587 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | croak "Non supported output method"; | |||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | =head2 Get, querying of report parameters | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | @group = @{$r->Get('group')}; | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | Method returns value of named parameter which is set in constructor or via C<Set> method. | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | ▶ | sub Get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | my ($r, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | return $r->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | =head2 Set, setting report parameters | ||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||
| 607 | $r->Set( | ||||||||||||||||||||||||||||||||||||||||||
| 608 | header => "Very new header", | ||||||||||||||||||||||||||||||||||||||||||
| 609 | item => ["%s %s", qw/NAME VALUE/] | ||||||||||||||||||||||||||||||||||||||||||
| 610 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | Method redefines report parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | ▶ | sub Set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Set: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | my ($r, %set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | ▶ | while (my ($k, $v) = each %set) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | $r->{$k} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | =head2 Abort | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | $r->Abort(); | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | Being called in the code it breaks report running. | ||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | ▶ | sub Abort {$_[0]->{aborted} = 1} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Abort: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | =head2 Aborted | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | if ($r->Aborted()) {...} | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | Method returns true if report execution was aborted by C<Abort> method. | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 9 | ▶ | sub Aborted {$_[0]->{aborted}} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Aborted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | __END__ | ||||||||||||||||||||||||||||||||||||||||||