blib/lib/DBIx/Formatter.pm untested

filter: Statementstmt 0.0% 0 / 309 Branchbran 0.0% 0 / 156 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 586 CC 79 SCAR 63.0
CC
79
Cov
0%
new
1482.0
breaks
110.0
ofmt
110.0
CRAP 542.2
1 ###########################################################################
2 # PACKAGE: Formatter
3 ###########################################################################
4 # AUTH Vecchio Fabrizio (pay0vec)
5 # DATE 5/1/00 - 11.23
6 # FILE Formatter.pm
7 # COPY Payroll S.r.l.
8 ###########################################################################
9 # NOTE Perform report generation
10 ###########################################################################
11  
12 package Formatter;
13  
14 use vars qw($VERSION);
15 use DBI;
16  
17 require Exporter;
18 @ISA=qw(Exporter);
19  
20 $VERSION='0.01';
21  
22  
23  
24 #######:> new
25 # DATE :> 5/1/00 - 11.30
26 # NOTE :> Costruttore oggetto Formatter
27 ############################################################################
28 sub new
29 {
30 my $type=shift;
31 my $class=ref($type) || $type;
32 my %params=@_;
33 my $shelf={};
34 my ($key)="";
35  
36 $self->{TYPE}=$type;
37 bless $self;
38 $self->_DEFINE_ERRORS;
39  
40 foreach $key (keys %params) {
41 $self->{PRG_CALLER}=$params{$key} if ( $key eq "PRG_CALLER" );
42 $self->{DBI_DRIVER}=$params{$key} if ( $key eq "DBI_DRIVER" );
43 $self->{DBI_DATABASE}=$params{$key} if ( $key eq "DBI_DATABASE" );
44 $self->{DBI_USERNAME}=$params{$key} if ( $key eq "DBI_USERNAME" );
45 $self->{DBI_PASSWORD}=$params{$key} if ( $key eq "DBI_PASSWORD" );
46 $self->{DBD_QUERY}=$params{$key} if ( $key eq "DBD_QUERY");
47 $self->{FORMAT_PAGESIZE}=$params{$key} if ( $key eq "FORMAT_PAGESIZE" );
48 $self->{FORMAT_LINESIZE}=$params{$key} if ( $key eq "FORMAT_LINESIZE" );
49 $self->{FORMAT_FORMFEED}=$params{$key} if ( $key eq "FORMAT_FORMFEED" );
50 $self->{FORMAT_HEADER}=$params{$key} if ( $key eq "FORMAT_HEADER" );
51 $self->{FORMAT_TTITLE}=$params{$key} if ( $key eq "FORMAT_TTITLE" );
52 $self->{FORMAT_BTITLE}=$params{$key} if ( $key eq "FORMAT_BTITLE" );
53 $self->{FORMAT_BTITLE_HEIGHT}=$params{$key} if ( $key eq "FORMAT_BTITLE_HEIGHT" );
54 $self->{FORMAT_BODY}=$params{$key} if ( $key eq "FORMAT_BODY" );
55 $self->{EVENT_PREHEADER}=$params{$key} if ( $key eq "EVENT_PREHEADER" );
56 $self->{EVENT_POSTHEADER}=$params{$key} if ( $key eq "EVENT_POSTHEADER" );
57 $self->{EVENT_PREBODY}=$params{$key} if ( $key eq "EVENT_PREBODY" );
58 $self->{EVENT_POSTBODY}=$params{$key} if ( $key eq "EVENT_POSTBODY" );
59 $self->{EVENT_PRETTITLE}=$params{$key} if ( $key eq "EVENT_PRETTITLE" );
60 $self->{EVENT_POSTTTITLE}=$params{$key} if ( $key eq "EVENT_POSTTTITLE" );
61 $self->{EVENT_PREBTITLE}=$params{$key} if ( $key eq "EVENT_PREBTITLE" );
62 $self->{EVENT_POSTBTITLE}=$params{$key} if ( $key eq "EVENT_POSTBTITLE" );
63 $self->{EVENT_ALLBREAKS}=$params{$key} if ( $key eq "EVENT_ALLBREAKS" );
64 $self->_FMT_GENBREAKS($params{$key}) if ( $key eq "EVENT_BREAKS" );
65 $self->{BREAKS}=$params{$key} if ( $key eq "BREAKS" );
66 $self->{FORMAT_BREAKS}=$params{$key} if ( $key eq "FORMAT_BREAKS" );
67 $self->{BREAKS_SKIP_PAGE}=$params{$key} if ( $key eq "BREAKS_SKIP_PAGE" );
68 $self->{COMPUTE}=$params{$key} if ( $key eq "COMPUTE" );
69 $self->{COMPUTE_BREAKS}=$params{$key} if ( $key eq "COMPUTE_BREAKS" );
70 }
71 $self->{PRG_CALLER}="main" if ! defined ($self->{PRG_CALLER});
72 $self->{FORMAT_LINESIZE}=130 if ! defined ($self->{FORMAT_LINESIZE});
73  
74 $self->_HANDLE_ERRORS(4) if ! defined ($self->{DBI_DRIVER});
75 $self->_HANDLE_ERRORS(5) if ! defined ($self->{DBI_DATABASE});
76 $self->_HANDLE_ERRORS(6) if ! defined ($self->{DBI_USERNAME});
77 $self->_HANDLE_ERRORS(7) if ! defined ($self->{DBI_PASSWORD});
78 $self->_HANDLE_ERRORS(8) if ! defined ($self->{DBD_QUERY});
79 $self->_HANDLE_ERRORS(11) if ((defined $self->{FORMAT_BTITLE}) && (!defined $self->{FORMAT_BTITLE_HEIGHT}));
80  
81 bless $self;
82 } ##new
83  
84 #######:> _DBI_CONNECT
85 # DATE :> 5/1/00 - 12.13
86 # NOTE :> Perform connection to database
87 ############################################################################
88 sub _DBI_CONNECT
89 {
90 my $self=shift;
91 my ($error)=1;
92 my ($nodriver)=1;
93  
94 foreach (DBI->available_drivers) {
95 $error=0 if ( $_ eq $self->{DBI_DRIVER} );
96 $nodriver=0;
97 };
98  
99 $error=2 if $nodriver;
100  
101 if ( ! $error ) {
102 $self->{DBH}=DBI->connect(
103 "dbi:".$self->{DBI_DRIVER}.":".$self->{DBI_DATABASE},
104 $self->{DBI_USERNAME},
105 $self->{DBI_PASSWORD}, {
106 PrintError => 0
107 }
108 ) || $self->_HANDLE_ERRORS(3);
109 } else {
110 $self->_HANDLE_ERRORS($error);
111 };
112 } ##_DBI_CONNECT
113  
114 #######:> _DBI_DISCONNECT
115 # DATE :> 5/1/00 - 13.29
116 # NOTE :> Perform database disconnection
117 ############################################################################
118 sub _DBI_DISCONNECT
119 {
120 my $self=shift;
121 $self->{DBH}->disconnect;
122 } ##_DBI_DISCONNECT
123  
124  
125 #######:> _DBI_FETCHROW_HASHREF
126 # DATE :> 5/1/00 - 13.50
127 # NOTE :> Perform DBI fetchrow_hashref
128 ############################################################################
129 sub _DBI_FETCHROW_HASHREF
130 {
131 my $self=shift;
132 my $h_rows={};
133 my $firstloop=1;
134 my $v_name="";
135 my $cmd="";
136  
137  
138 $self->_DBI_CONNECT;
139 $self->{STH}=$self->{DBH}->prepare($self->{DBD_QUERY}) || $self->_HANDLE_ERRORS(9,$self->{DBH}->errstr);
140 $self->{STH}->execute || $self->_HANDLE_ERRORS(9,$self->{DBH}->errstr);
141  
142  
143 format TMPTOP=
144 .
145 $^=TMPTOP;
146 $==$self->{FORMAT_PAGESIZE};
147 $^L=$self->{FORMAT_FORMFEED};
148 $-=$=;
149 $%=1;
150  
151 while ( $h_rows=$self->{STH}->fetchrow_hashref ) {
152  
153 # Genearation of caller program variable
154 $self->{NEWPAGE}=0;
155  
156 foreach $key ( keys %$h_rows ) {
157 $v_name=$self->{PRG_CALLER}."::".uc($key);
158 $$v_name=$h_rows->{$key};
159 $self->{ROWS}->{$key}=$h_rows->{$key};
160 $self->{LAST_BREAKS}->{$key}=$h_rows->{$key} if $firstloop;
161 };
162 bless $self;
163  
164  
165 if ( $firstloop ) {
166 $self->header;
167 $firstloop=0;
168 }
169  
170 $self->breaks(1) if ! $firstloop;
171 $self->compute;
172 $self->compute_breaks;
173 $self->breaks_events;
174  
175  
176 $self->newpage if ( $-== $self->{FORMAT_BTITLE_HEIGHT} );
177 $self->body;
178 };
179  
180 $self->{LAST_BREAKS}={}; bless $self;
181 $self->breaks(0);
182 $self->bottomtitle;
183  
184 $self->{STH}->finish || $self->_HANDLE_ERRORS(9,$self->{DBH}->errstr);
185 $self->_DBI_DISCONNECT;
186  
187 } ##_DBI_FETCHROW_HASHREF
188  
189 #######:> compute_breaks
190 # DATE :> 7/1/00 - 18.30
191 # NOTE :> Check the for compute in breaks
192 ############################################################################
193 sub compute_breaks {
194 my $self=shift;
195 my $break=$self->{COMPUTE_BREAKS};
196  
197 foreach $key (keys %$break) {
198 $compute=$break->{$key};
199 foreach $operation (keys %$compute) {
200 $fields=$compute->{$operation};
201 foreach $field (keys %$fields) {
202 $self->sum ($field,$fields->{$field}) if ( uc($operation) eq "SUM" );
203 $self->count ($field,$fields->{$field}) if ( uc($operation) eq "COUNT" );
204 $self->avg ($field,$fields->{$field}) if ( uc($operation) eq "AVG" );
205 };
206 };
207 };
208  
209 };
210  
211  
212  
213 #######:> compute
214 # DATE :> 7/1/00 - 18.30
215 # NOTE :> Check the for compute in global
216 ############################################################################
217 sub compute
218 {
219 my $self=shift;
220 my $compute=$self->{COMPUTE};
221  
222 foreach $operation (keys %$compute) {
223 $fields=$compute->{$operation};
224 foreach $field (keys %$fields) {
225 $self->sum ($field,$fields->{$field}) if ( uc($operation) eq "SUM" );
226 $self->count ($field,$fields->{$field}) if ( uc($operation) eq "COUNT" );
227 $self->avg ($field,$fields->{$field}) if ( uc($operation) eq "AVG" );
228 };
229 };
230  
231 } ##compute
232  
233 #######:> sum
234 # DATE :> 7/1/00 - 18.38
235 # NOTE :> Perform sum of field
236 ############################################################################
237 sub sum
238 {
239 my $self=shift;
240 my $field=@_[0];
241 my $total=@_[1];
242  
243 $self->{CSUM}->{$total}+=$self->{ROWS}->{$field};
244 $v_name=$self->{PRG_CALLER}."::".$total;
245 $$v_name=$self->{CSUM}->{$total};
246  
247 bless $self;
248 return $self->{CSUM}->{$total};
249 } ##sum
250  
251 #######:> count
252 # DATE :> 7/1/00 - 18.38
253 # NOTE :> Perform count of field
254 ############################################################################
255 sub count
256 {
257 my $self=shift;
258 my $field=@_[0];
259 my $total=@_[1];
260  
261 $self->{CCOUNT}->{$total}++;
262 $v_name=$self->{PRG_CALLER}."::".$total;
263 $$v_name=$self->{CCOUNT}->{$total};
264  
265 bless $self;
266 return $self->{CCOUNT}->{$total};
267 } ##count
268  
269 #######:> avg
270 # DATE :> 7/1/00 - 18.38
271 # NOTE :> Perform averange of field
272 ############################################################################
273 sub avg
274 {
275 my $self=shift;
276 my $field=@_[0];
277 my $total=@_[1];
278  
279 $self->{CAVG}->{$total}=$self->sum($field,"CAVG".$total) / $self->count($field,"CAVG".$total);
280 $v_name=$self->{PRG_CALLER}."::".$total;
281 $$v_name=$self->{CAVG}->{$total};
282  
283 bless $self;
284 return $self->{CAVG}->{$total};
285 } ##avg
286  
287 #######:> breaks_events
288 # DATE :> 7/1/00 - 15.31
289 # NOTE :> Perform check for breaks events
290 ############################################################################
291 sub breaks_events
292 {
293 my $self=shift;
294 my $breaks_event=$self->{EVENT_BREAKS};
295 my $breaks=$self->{BREAKS};
296 my $brk_routine="";
297  
298 foreach $key (@$breaks) {
299 $brk_routine=$breaks_event->{$key};
300 &$brk_routine if (($self->{LAST_BREAKS_EVENTS}->{$key} ne $self->{ROWS}->{$key}) && (defined $self->{EVENT_BREAKS}->{$key}));
301 $self->{LAST_BREAKS_EVENTS}->{$key}=$self->{ROWS}->{$key};
302 };
303  
304 bless $self;
305 } ##breaks
306  
307 #######:> breaks
308 # DATE :> 7/1/00 - 15.31
309 # NOTE :> Perform check for breaks
310 ############################################################################
311 sub breaks
312 {
313 my $self=shift;
314 my $skippage=@_[0];
315 my $breaks_event=$self->{EVENT_BREAKS};
316 my $breaks=$self->{BREAKS};
317 my $brk_routine="";
318 my $newpage=0;
319  
320 foreach $key (@$breaks) {
321 $brk_routine=$breaks_event->{$key};
322 if (($self->{LAST_BREAKS}->{$key} ne $self->{ROWS}->{$key})) {
323 $self->fmt_breaks($key) if defined $self->{FORMAT_BREAKS}->{$key};
324 $newpage=1 if $self->{BREAKS_SKIP_PAGE}->{$key};
325  
326 $coumpute=$self->{COMPUTE_BREAKS}->{$key};
327 foreach $operation (keys %$compute) {
328 $fields=$compute->{$operation};
329 foreach $field (keys %$fields) {
330 $self->{CSUM}->{$fields->{$field}}=0 if (uc($operation) eq "SUM");
331 $self->{CCOUNT}->{$fields->{$field}}=0 if (uc($operation) eq "COUNT");
332 $self->{CAVG}->{$fields->{$field}}=0 if (uc($operation) eq "AVG");
333 $self->{CSUM}->{"CAVG".$fields->{$field}}=0 if (uc($operation) eq "AVG");
334 $self->{CCOUNT}->{"CAVG".$fields->{$field}}=0 if (uc($operation) eq "AVG");
335 };
336 };
337  
338 }
339 $self->{LAST_BREAKS}->{$key}=$self->{ROWS}->{$key};
340 };
341  
342 $self->newpage if ($newpage && $skippage);
343 bless $self;
344 } ##breaks
345  
346  
347 #######:> header
348 # DATE :> 7/1/00 - 09.37
349 # NOTE :> Print out the header for the report
350 ############################################################################
351 sub header
352 {
353 $self=shift;
354  
355 if ( defined $self->{FORMAT_HEADER} ) {
356 $~="$self->{FORMAT_HEADER}";
357 $self->_HANDLE_EVENTS("EVENT_PREHEADER");
358 write;
359 $self->_HANDLE_EVENTS("EVENT_POSTHEADER");
360 }
361  
362 $self->formfeed;
363 $-=0;
364 $self->toptitle;
365 } ##header
366  
367  
368 #######:> body
369 # DATE :> 7/1/00 - 09.23
370 # NOTE :> Print out the body of the page
371 ############################################################################
372 sub body {
373 my $self=shift;
374 my $savefmt="";
375  
376 $savefmt=$~;
377  
378 if ( defined $self->{FORMAT_BODY} ) {
379 $~="$self->{FORMAT_BODY}";
380 $self->_HANDLE_EVENTS("EVENT_PREBODY");
381 write;
382 $self->_HANDLE_EVENTS("EVENT_POSTBODY");
383 };
384  
385 $~=$savefmt;
386 } ##body
387  
388 #######:> fmt_breaks
389 # DATE :> 7/1/00 - 16.43
390 # NOTE :> Perform write format for breaks
391 ############################################################################
392 sub fmt_breaks
393 {
394 my $self=shift;
395 my $break_key=@_[0];
396 my $savefmt="";
397  
398 $savefmt=$~;
399  
400 if ( defined $self->{FORMAT_BREAKS}->{$key} ) {
401 $~="$self->{FORMAT_BREAKS}->{$break_key}";
402 write;
403 };
404  
405 $~=$savefmt;
406 } ##fmt_breaks
407  
408  
409 #######:> bottomtitle
410 # DATE :> 7/1/00 - 09.21
411 # NOTE :> Print out the bottom title for the page
412 ############################################################################
413 sub bottomtitle
414 {
415 my $self=shift;
416 my $savefmt="";
417  
418 $savefmt=$~;
419 if ( defined $self->{FORMAT_BTITLE} ) {
420 $~="$self->{FORMAT_BTITLE}";
421 $self->_HANDLE_EVENTS("EVENT_PREBTITLE");
422 write;
423 $self->_HANDLE_EVENTS("EVENT_POSTBTITLE");
424 };
425  
426 $~=$savefmt;
427  
428 } ##bottomtitile
429  
430  
431 #######:> toptitle
432 # DATE :> 7/1/00 - 09.16
433 # NOTE :> Print out the top title for the page
434 ############################################################################
435 sub toptitle
436 {
437 my $self=shift;
438 my $savefmt="";
439  
440 $savefmt=$~;
441  
442 if ( defined $self->{FORMAT_TTITLE} ) {
443 $-=$=;
444 $~="$self->{FORMAT_TTITLE}";
445 $self->_HANDLE_EVENTS("EVENT_PRETTITLE");
446 write;
447 $self->_HANDLE_EVENTS("EVENT_POSTTTITLE");
448 };
449  
450 $~=$savefmt;
451 } ##toptitle
452  
453  
454 #######:> generate
455 # DATE :> 5/1/00 - 17.26
456 # NOTE :> Perform generation of reprot
457 ############################################################################
458 sub generate
459 {
460 my $self=shift;
461  
462 $self->_DBI_FETCHROW_HASHREF;
463 } ##generate
464  
465 #######:> putformat
466 # DATE :> 7/1/00 - 17.31
467 # NOTE :> Put a format in the report
468 ############################################################################
469 sub putformat
470 {
471 $self=shift;
472 $tmp_format=@_[0];
473 my $savefmt="";
474  
475 $savefmt=$~;
476 $~="$tmp_format";
477 write;
478 $~=$savefmt;
479 } ##putformat
480  
481 #######:> newpage
482 # DATE :> 5/1/00 - 17.06
483 # NOTE :> Send a new page event
484 ############################################################################
485 sub newpage
486 {
487 my $self=shift;
488  
489 $self->{NEWPAGE}=1;
490 $self->bottomtitle;
491 $self->formfeed;
492 $self->toptitle;
493 bless $self;
494  
495 } ##newpage
496  
497 #######:> isnewpage
498 # DATE :> 7/1/00 - 16.25
499 # NOTE :> Query if a new page is been performed
500 ############################################################################
501 sub isnewpage
502 {
503 $self=shift;
504 return $self->{NEWPAGE};
505 } ##isnewpage
506  
507 #######:> formfeed
508 # DATE :> 7/1/00 - 09.15
509 # NOTE :> Send a formfeed to the report
510 ############################################################################
511 sub formfeed
512 {
513 my $self=shift;
514 printf $^L;
515 $%++;
516 $-=$=;
517  
518 } ##formfeed
519  
520 #######:> page
521 # DATE :> 7/1/00 - 10.31
522 # NOTE :> Get the page number
523 ############################################################################
524 sub page
525 {
526 $self=shift;
527 return $%;
528 } ##page
529  
530 #######:> line
531 # DATE :> 7/1/00 - 10.38
532 # NOTE :> Get the line number for current page
533 ############################################################################
534 sub line
535 {
536 $self=shift;
537 return ($=-$-+1);
538 } ##line
539  
540  
541 #######:> ofmt
542 # DATE :> 5/1/00 - 17.17
543 # NOTE :> Perform a printf statement and decrease $- value
544 ############################################################################
545 sub ofmt
546 {
547 my $self=shift;
548 my ($s_value,$params,$position)=@_;
549 my ($t_value)="";
550  
551 ($t_value=$s_value)=~ s/\n//g;
552 $params="<" if ! defined $params;
553 $position-- if defined $position;
554 $position=0 if ((! defined $position) || ($position > $self->{FORMAT_LINESIZE}) || ($position < 0));
555  
556 $self->_HANDLE_ERRORS(10) if (($params ne "<") && ($params ne ">") && ($params ne "|") && ($params ne "@"));
557  
558 printf "%".$self->{FORMAT_LINESIZE}."s\n",substr($s_value,0,$self->{FORMAT_LINESIZE}) if $params eq ">";
559 printf "%-".$self->{FORMAT_LINESIZE}."s\n",substr($s_value,0,$self->{FORMAT_LINESIZE}) if $params eq "<";
560 printf "%-".$self->{FORMAT_LINESIZE}."s\n",(" " x (int(($self->{FORMAT_LINESIZE} /2) - (length(substr($s_value,0,$self->{FORMAT_LINESIZE}))/2)))).substr($s_value,0,$self->{FORMAT_LINESIZE}) if $params eq "|";
561 printf "%-".$self->{FORMAT_LINESIZE}."s\n",(" " x ($position)).substr($s_value,0,$self->{FORMAT_LINESIZE}-$position) if $params eq "@";
562 $---;
563 $self->formfeed if ( $-==0);
564 } ##ofmt
565  
566 #######:> _HANDLE_EVENTS
567 # DATE :> 7/1/00 - 09.43
568 # NOTE :> Perform event generation
569 ############################################################################
570 sub _HANDLE_EVENTS
571 {
572 my $self=shift;
573 my $EVENT=@_[0];
574 my $EVENT_ROUTINE="";
575  
576 if ( defined $self->{$EVENT} ) {
577 $EVENT_ROUTINE=$self->{$EVENT};
578 &$EVENT_ROUTINE;
579 }
580 } ##_HANDLE_EVENTS
581  
582  
583 #######:> _HANDLE_ERRORS
584 # DATE :> 5/1/00 - 12.16
585 # NOTE :> Perform error handling in all package
586 ############################################################################
587 sub _HANDLE_ERRORS
588 {
589 my $self=shift;
590 my ($error_code,$error_msg)=@_;
591 my (@desc)=();
592  
593 format ERRORFMT =
594 *************************************************************************
595 * !!! WARINING ERROR !!! *
596 *************************************************************************
597 * TYPE : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
598 $desc[1]
599 * NUMBER : @<<< *
600 $error_code
601 *************************************************************************
602 * EVENT : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
603 $desc[2]
604 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
605 $desc[3]
606 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
607 $desc[4]
608 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
609 $desc[5]
610 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
611 $desc[6]
612 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
613 $desc[7]
614 * @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
615 $desc[8]
616 *************************************************************************
617 .
618 if ( defined $error_msg ) {
619 my $m_start=0;
620 my $m_many=length($error_msg);
621 my $m_mesg="";
622 while ( $m_many > 0 ) {$m_mesg=$m_mesg."|".substr($error_msg,$m_start,55);$m_many=$m_many-55;$m_start=$m_start+55}
623 $self->{ERRORS}[$error_code]=$self->{ERRORS}[$error_code].$m_mesg;
624 };
625  
626 @desc=split(/\|/,$self->{ERRORS}[$error_code]);
627 $~=ERRORFMT; write;
628 die if ($desc[0] eq "D");
629 warn if ($desc[0] eq "W");
630 } ##_HANDLE_ERRORS
631  
632 #######:> _FMT_GENBREAKS
633 # DATE :> 5/1/00 - 15.03
634 # NOTE :> Perform generation of hash reference for breaks
635 ############################################################################
636 sub _FMT_GENBREAKS ($%)
637 {
638 my $self=shift;
639 my $breaks=shift;
640  
641 foreach $key ( keys %$breaks ) {$self->{EVENT_BREAKS}->{$key}=$breaks->{$key}};
642 } ##_FMT_GENBREAKS
643  
644 #######:> _DEFINE_ERRORS
645 # DATE :> 5/1/00 - 12.24
646 # NOTE :> Load error string
647 ############################################################################
648 sub _DEFINE_ERRORS
649 {
650 my $self=shift;
651  
652 $self->{ERRORS}[1]="D|CRITICAL|Specified DBI Driver don't exist or not installed|properly\.";
653 $self->{ERRORS}[2]="D|CRITICAL|Is not possible to retrieve driver information.|Check your DBI,DBD installation\.";
654 $self->{ERRORS}[3]="D|CRITICAL|Specified connection parameter possible invalid\.|Impossible connect to database\.";
655 $self->{ERRORS}[4]="D|CRITICAL|Specify a DBI driver\.";
656 $self->{ERRORS}[5]="D|CRITICAL|Specify a DBD database name\.";
657 $self->{ERRORS}[6]="D|CRITICAL|Missing DBI username\.";
658 $self->{ERRORS}[7]="D|CRITICAL|Missing DBI password\.";
659 $self->{ERRORS}[8]="D|CRITICAL|Specify a query to retrieve data from database\.";
660 $self->{ERRORS}[9]="D|CRITICAL";
661 $self->{ERRORS}[10]="D|CRITICAL|Invalide parameter in ofmt routine\.";
662 $self->{ERRORS}[11]="D|CRITICAL|FORMAT_BTITLE_HEIGHT not specified when FORMAT_BTITLE|declared\.";
663 } ##_DEFINE_ERRORS
664  
665 1;
666  
667 =head1 NAME
668  
669 Formatter - Module to perform report generation via query DBI
670  
671 =head1 SYNOPSIS
672  
673 use Formatter;
674  
675 format FMT_HEADER=
676 **************************************************
677 * FIRST PAGE OF REPORT *
678 **************************************************
679 .
680  
681 format FMT_TTITLE=
682 **************************************************
683 * SOC : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< *
684 $DEAZIEND
685 **************************************************
686 PROG CDAZIEND CDDIPEND
687 ---- -------- --------
688 .
689  
690 format FMT_BODY=
691 @<<< @<<< @<<<<<
692 $FMT->line, $CDAZIEND,$CDDIPEND
693 .
694  
695 format FMT_CD1LVSTR=
696 TOT]--> @<<<<<<< @<<<<<<< @<<<<<<<
697 111, 111, 111
698 .
699 format FMT_BTITLE=
700 --------------------------------------------------
701 @</@</@< P.@<<<
702 $DAY,$MONTH,$YEAR, $FMT->page
703 .
704  
705 $BREAKS[0]="CDAZIEND";
706 $BREAKS[1]="CDDIPEND";
707  
708 $FMT=new Formatter(
709 'DBI_DRIVER' => 'Oracle',
710 'DBI_DATABASE' => 'database',
711 'DBI_USERNAME' => 'dbusername',
712 'DBI_PASSWORD' => 'dbpassword',
713 'DBD_QUERY' => 'SELECT * FROM ANAGRA WHERE CLPERRIF=199901 ORDER BY CDAZIEND',
714 'BREAKS' => \@BREAKS,
715 'BREAKS_SKIP_PAGE' => {
716 CD1LVSTR => 1,
717 CDCCOSTO => 0
718 },
719 'FORMAT_PAGESIZE' => 40,
720 'FORMAT_LINESIZE' => 50,
721 'FORMAT_FORMFEED' => "\f",
722 'FORMAT_HEADER' => *FMT_HEADER,
723 'FORMAT_TTITLE' => *FMT_TTITLE,
724 'FORMAT_BTITLE' => *FMT_BTITLE,
725 'FORMAT_BTITLE_HEIGHT' => 2,
726 'FORMAT_BODY' => *FMT_BODY,
727 'FORMAT_BREAKS' => {
728 CD1LVSTR => *FMT_CD1LVSTR,
729 },
730 'EVENT_PREHEADER' => \&PREHEADER,
731 'EVENT_POSTHEADER' => \&POSTHEADER,
732 'EVENT_PRETTITLE' => \&PRETTITLE,
733 'EVENT_POSTTTITLE' => \&POSTTTITLE,
734 'EVENT_PREBODY' => \&PREBODY,
735 'EVENT_POSTBODY' => \&POSTBODY,
736 'EVENT_PREBTITLE' => \&PREBTITLE,
737 'EVENT_POSTBTITLE' => \&POSTBTITLE,
738 'EVENT_ALLBREAKS' => \&BREAKALL,
739 'EVENT_BREAKS' => {
740 CDAZIEND => \&CDAZIEND,
741 CDDIPEND => \&CDDIPEND
742 }
743 );
744  
745 $FMT->generate();
746  
747 sub PREHEADER {do something before header print out}
748 sub POSTHEADER {do something after header print out}
749 sub PRETTITLE {do something before top title print out}
750 sub POSTTTITLE {do something after top title print out}
751 sub PREBTITLE {do something before bottom title print out}
752 sub POSTBTITLE {do something after bottom title print out}
753 sub PREBODY {do something before body print out}
754 sub POSTBODY {do something after body print out}
755  
756 $FMT->ofmt("Print out a line during report generation",">");
757  
758 =head1 DESCRIPTION
759  
760 Formatter module perform report generation based on DBI query.
761  
762  
763 =head1 DEFINITION
764  
765 =head2 Function new
766  
767 The <new> function create the Formatter object and configure it
768 for all parameters required for report generation.
769 One by one parameter definition:
770  
771 * DBI_DRIVER => Specify wath driver you would use
772 for connect to your database.
773 (See DBD drivers for specific)
774 * DBI_DATABASE => Specify database name (or instance).
775 In union of last parameter it create
776 connection string : dbi:DBI_DRIVER:DBI_DATABASE
777 * DBI_USERNAME => Specify database username
778 * DBI_PASSWORD => Specify database DBI_USERNAME password
779 * DBD_QUERY => Point to a string where Sql Query are located
780 * BREAKS => Point to an array containing fields that cause a
781 break in the report
782 * BREAKS_SKIP_PAGE => Point to an hash that specify if a new page are
783 performed when the break FORMAT ar printed
784 * FORMAT_PAGESIZE => Specify page height in character
785 * FORMAT_LINESIZE => Specify line width in character (only for outf use)
786 * FORMAT_FORMFEED => Specify formfeed sequence when a formfeed or newpage
787 are requested
788 * FORMAT_HEADER => Point to filehandle of the header definition for the
789 report
790 * FORMAT_TTITLE => Point to filehandle of the top title definition for
791 the report
792 * FORMAT_BODY => Point to filehandle of the body definition for the
793 report
794 * FORMAT_BTITLE => Point to filehandle of the bottom title definition for
795 the report
796 * FORMAT_BTITLE_HEIGHT => Height in lines of BTITLE
797 * FORMAT_BREAKS => Point to an hash containig break fields related to its
798 format filehandle definition
799 * EVENT_PREHEDER => Point to the subroutine that is called before header
800 generation
801 * EVENT_POSTHEDER => Point to the subroutine that is called after header
802 generation
803 * EVENT_PRETTITLE => Point to the subroutine that is called before top title
804 generation
805 * EVENT_POSTTTITLE => Point to the subroutine that is called after top title
806 generation
807 * EVENT_PREBODY => Point to the subroutine that is called before body
808 generation
809 * EVENT_POSTBODY => Point to the subroutine that is called after body
810 generation
811 * EVENT_PREBTITLE => Point to the subroutine that is called before bottom title
812 generation
813 * EVENT_POSTBTITLE => Point to the subroutine that is called after bottom title
814 generation
815 * EVENT_BREAKS => Point to structure that contains break fields related to
816 subroutine to execute when the break is berformed
817  
818 =head2 DBI_DRIVER
819  
820 DBI_DRIVER specify driver to use for connection within Database via DBI::DBD module.
821  
822 Ex.
823  
824 * Oracle (Oracle database)
825 * CSV (Comma separated database)
826 * Pg (PostgreSQL database)
827 ... ecc.
828  
829 For specific look at DBD::<Driver>
830  
831 =head2 DBI_DATABASE
832  
833 The clause DBI_DATABASE can change from db to db , look at DBD::Driver for your specific.
834 Example parameter are:
835  
836 * ORACLE_SID for Oracle database
837 * f_dir=/csv/data for CSV file specify directory location of text-file-table
838 * dbname=your db name for PostgreSQL database
839 * DSN for ADO db connection
840  
841  
842 =head2 DBI_USERNAME DBI_PASSWORD
843  
844 Specify in order username and password of the user granted to use database
845  
846 =head2 DBD_QUERY
847  
848 Here you can pass your query (SQL) , that is fetched for report generation.
849 Example to pass query are:
850  
851 * Example 1 (Direct via parameters)
852 $FMT=new Formatter(
853 'DBI_DRIVER' => 'Oracle',
854 'DBI_DATABASE' => 'database',
855 'DBI_USERNAME' => 'dbusername',
856 'DBI_PASSWORD' => 'dbpassword',
857 'DBD_QUERY' => 'SELECT * FROM ANAGRA WHERE CLPERRIF=199901 ORDER BY CDAZIEND',
858 ...
859  
860 * Example 2 (Via variable)
861 $query = qq {
862 SELECT
863 *
864 FROM
865 ANAGRA
866 WHERE
867 CLPERRIF=199901
868 AND
869 CDAZIEND=345
870 ORDER BY
871 CDAZIEND
872 };
873  
874 $FMT=new Formatter(
875 'DBI_DRIVER' => 'Oracle',
876 'DBI_DATABASE' => 'database',
877 'DBI_USERNAME' => 'dbusername',
878 'DBI_PASSWORD' => 'dbpassword',
879 'DBD_QUERY' => $query,
880 ...
881  
882 =head2 BREAKS
883  
884 Specify an array containing fields that cause a break in the report.
885 When a breaks is performed 2 step are executed, first an EVENT_BREAKS is called if
886 defined , second a FORMAT_BREAKS is printed out if defined.
887 For convenience if you would like to generate a new format when a field change you can
888 use first statement because is called after FORMAT_BREAKS (and if they are subtotal are
889 printed before), using the putformat function in the event routine, for example:
890  
891 format FMT_DEPARTEMENT=
892 TOTALS FOR DEPARTEMENT ARE ]--> @<<<<<<< @<<<<<<< @<<<<<<<
893 111, 111, 111
894 .
895  
896 format FMT_SUBDEPARTEMENT
897 *********************************
898 * @|||||||||||||||||||||||||||| *
899 $SUBDEPARTEMENT
900 *********************************
901 .
902  
903 $BREAKS[0]="DEPARTEMENT";
904 $BREAKS[1]="SUBDEPARTEMENT";
905  
906 $FMT=Formatter->new (
907 ...
908 'BREAKS' => \@BREAKS,
909 'BREAKS_SKIP_PAGE' => {
910 DEPARTEMENT => 1,
911 SUBDEPARTEMENT => 0
912 },
913 'FORMAT_BREAKS' => {
914 DEPARTEMENT => *FMT_DEPARTEMENT
915 },
916 'EVENT_BREAKS' => {
917 SUBDEPARTEMENT => \&MySubDepartement
918 },
919 ...
920 );
921  
922 sub MySubDepartement {$FMT->putformat(*FMT_SUBDEPARTEMENT)};
923  
924 In this way a possible output is :
925 *******************************************************
926 * REPORT BY SUBDEPARTMENT *
927 *******************************************************
928 *********************************
929 * CHEMICALS *
930 *********************************
931 10 20 40
932 10 20 40
933 10 20 40
934 *********************************
935 * PHARMACIA *
936 *********************************
937 10 20 40
938 10 20 40
939 10 20 40
940  
941 TOTALS FOR DEPARTEMENT ARE ]--> 111 111 111
942  
943 =head2 BREAKS_SKIP_PAGE
944  
945 This parameter specify only if a new page is called after a break FORMAT is writed.
946  
947 =head2 FORMAT_PAGESIZE
948  
949 (Optional-Deafult=60)
950  
951 Specify how many lines are printed for each piece of paper.
952  
953 =head2 FORMAT_LINESIZE
954  
955 (Optional-Default=130)
956  
957 Specify how many character are counted by an outf function to perform
958 alignement of text.
959  
960 =head2 FORMAT_FORMFEED
961  
962 (Optional-Deafult=\f)
963  
964 Specify the form feed sequence that are called whem a newpage or a formfeed
965 function are called ( and alse a new page of report is required)
966 Default value are CTRL-L (\f)
967  
968 =head2 FORMAT_HEADER
969  
970 (Optional)
971  
972 This parameter point to the format fileheader for HEADER page.
973 Definition of format specific can be found in Perl documentation.
974 Header page is printed only one time at beginning of the report and normally
975 include general specification or purpose of report.
976  
977 Example:
978  
979 format MY_HEADER=
980 ******************************************
981 * DATE : @</@</@<<< *
982 $dd,$mm,$yyyy
983 * PURPOSE : Statistique about user login *
984 ******************************************
985 .
986  
987 =head2 FORMAT_TTITLE
988  
989 (Optional)
990  
991 Identical to HEADER definition.
992 TTITLE is printed on every change of page in the top of the report page
993  
994 =head2 FORMAT_BODY
995  
996 (Optional)
997  
998 Identical to HEADER definition.
999 BODY is printed on every change of value in fetch of query statament,
1000 values of query are passed and traslated to real variable in the main caller
1001 program.
1002  
1003 Example :
1004 if query is SELECT NAME,SURNAME,ADDRESS FROM ADDRESSBOOK
1005 in yours format values $NAME,$SURNAME,$ADDRESS are created and updated
1006 on every fetch.
1007  
1008 format MYBODY=
1009 @<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1010 $NAME, $SURNAME $ADDRESS
1011 .
1012  
1013 =head2 FORMAT_BTITLE
1014  
1015 (Optional)
1016  
1017 Identical to HEADER definition.
1018 BTITLE is printed on every change of page in the bottom of the report page
1019  
1020 =head2 FORMAT_BTITLE_HEIGHT
1021  
1022 (Option if FORMAT_BTITLE not specified)
1023  
1024 This parameter specify how many lines are printed by the FORMAT_BTITLE definition.
1025 I have put this parameter beacuse i don't know how to inform automatically my package in
1026 what lines is used by a format definition.
1027 If anyone know how to make it possible please contact me.
1028  
1029 =head2 FORMAT_BREAKS
1030  
1031 This parameter point to a structure where are located by fields format filehandle definitions.
1032 For use see the BREAKS specifics.
1033  
1034 =head2 EVENT_PRE...
1035  
1036 All EVENT_PRE... (EVENT_PREHEADER,EVENT_PRETTITLE ...) parameters points to a routine defined
1037 by the user that is called before the event handle is performed.
1038 For example if you would like to change a value in the top title before is print out you need to
1039 create a sub for example named MyBeforeTopTitle in which you change a value , and then you pass
1040 this reference to the EVENT_PRETTITLE parameter :
1041  
1042 'EVENT_PRETTITLE' => \&MyBeforeTopTitle
1043  
1044 Remember that all values created by the fetch statement are already updated when all events are
1045 generated
1046  
1047 =head2 EVENT_POST...
1048  
1049 Is identical to the last but is applied after the event handle occurs.
1050  
1051 =head2 EVENT_BREAKS
1052  
1053 This parameter point to a structure that specify by fields events that can be
1054 generated when a break is performed after a FORMAT_BREAKS is printed out.
1055  
1056 =head1 FUNCTIONS
1057  
1058 =head2 Function generate
1059  
1060 This function call all parameters and build the report.
1061  
1062 =head2 Function ofmt
1063  
1064 This function place a text in the report over formatting definition , is only for special
1065 case in which is impossible to place text on format definition (if you found when please
1066 contact me).
1067 Formatter->ofmt ("Text to print out",position flag,position character);
1068 * Position flag
1069 The position flag specify where text are printed , possible values are:
1070 < Text are aligned to left of the line
1071 > Text are aligned to right of the line
1072 | Text are aligned in the middle of the line
1073 C Enable Position
1074  
1075 * Position character
1076 The position character specify the position in the line where the text are printed
1077  
1078 =head2 Function page
1079  
1080 The page function return number of page in this moment
1081  
1082 =head2 Function line
1083  
1084 The line function return the line position in the moment
1085  
1086 =head2 Function newpage
1087  
1088 The new page function send a complete new page to the report this is the next
1089 sequence :
1090  
1091 * Print the bottom title in the current page
1092 * Send the form feed sequence
1093 * Print the top title in the new page
1094  
1095 =head2 Function formfeed
1096  
1097 The formfeed function send only the form feed sequence to the report and not
1098 perform the title generation.
1099  
1100 =head1 AUTHOR
1101  
1102 Vecchio Fabrizio <jacote@tiscalinet.it>
1103  
1104 =head1 SEE ALSO
1105  
1106 L<DBI>
1107  
1108 =cut
1109  
1110  
1111  
1112  
1113  
1114  
1115  
1116  
1117  
1118  
1119  
1120  
1121