blib/lib/DBI/Format.pm

filter: Statementstmt 80.3% 262 / 326 Branchbran 58.8% 53 / 90 Conditioncond 48.1% 51 / 106 MC/DCmcdc 8.8% 4 / 45 Subroutinesub 89.6% 26 / 29 Podpod 0.0% 0 / 2 totaltotal 66.2% 396 / 598 CC 88 SCAR 28.5
CC
88
Cov
70%
header
110.0
trailer
20.0
_determine_width
18.5
CRAP 17.4
« blib/lib/DBI/Format/SQLMinus.pm ↑ Summary blib/lib/DBI/Shell/Spool.pm »
1 # -*- perl -*-
2 # vim:ts=2:sw=2:aw:ai:sta:nows
3 #
4 # DBI::Format - a package for displaying result tables
5 #
6 # Copyright (c) 1998 Jochen Wiedmann
7 # Copyright (c) 1998 Tim Bunce
8 #
9 # The DBI::Shell:Result module is free software; you can redistribute
10 # it and/or modify it under the same terms as Perl itself.
11 #
12 # Author: Jochen Wiedmann
13 # Am Eisteich 9
14 # 72555 Metzingen
15 # Germany
16 #
17 # Email: joe@ispsoft.de
18 # Phone: +49 7123 14881
19 #
20  
21 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 package DBI::Format;
24  
25 7 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub available_formatters {
28 22 ▶ my ($use_abbrev) = @_;
Subroutine available_formatters: called CC=7 SCAR=19.5
Pod undocumented
29 22 my @fmt;
30 22 my @dir = grep { -d "$_/DBI/Format" } @INC;
31 22 foreach my $dir (@dir) {
32 66 ▶ opendir DIR, "$dir/DBI/Format" or warn "Unable to read $dir/DBI: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, "$dir/DBI/Format" or warn "Unable to read $dir/DBI: $!\n" 0 66 66
33 66 ▶ push @fmt, map { m/^(\w+)\.pm$/i ? ($1) : () } readdir DIR;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)\.pm$/i ? : 132 132 264
34 66 closedir DIR;
35 }
36 22 my %fmt = map { (lc($_) => "DBI::Format::$_") } @fmt;
37 22 $fmt{box} = "DBI::Format::Box";
38 22 $fmt{partbox} = "DBI::Format::PartBox";
39 22 $fmt{neat} = "DBI::Format::Neat";
40 22 $fmt{raw} = "DBI::Format::Raw";
41 22 $fmt{string} = "DBI::Format::String";
42 22 $fmt{html} = "DBI::Format::HTML";
43 22 my $formatters = \%fmt;
44 22 ▶ if ($use_abbrev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_abbrev) 22 0 22
45 22 $formatters = abbrev(keys %fmt);
46 22 foreach my $abbrev (sort keys %$formatters) {
47 792 ▶ $formatters->{$abbrev} = $fmt{ $formatters->{$abbrev} } || die;
Condition50%1/2
$fmt{$formatters->{$abbrev}} || die()
l!l
7920
MC/DC0%0/1
$fmt{$formatters->{$abbrev}} || die()
$fmt{$formatters->{$abbrev}}
Truth table50%1/2
A
A= $fmt{$formatters->{$abbrev}}
Aresult
11
00
48 }
49 }
50 22 return $formatters;
51 }
52  
53  
54 sub formatter {
55 22 ▶ my ($class, $mode, $use_abbrev) = @_;
Subroutine formatter: called CC=7 SCAR=20.8
Pod undocumented
56 22 $mode = lc($mode);
57 22 my $formatters = available_formatters($use_abbrev);
58 22 my $fmt = $formatters->{$mode};
59 22 ▶ if (!$fmt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fmt) 0 22 22
60 0 $formatters = available_formatters(0);
61 0 die "Format '$mode' unavailable. Available formats: ".
62 join(", ", sort keys %$formatters)."\n";
63 }
64 {
65 # Attempt to determine if format mode is in the base class.
66 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 22 eval "$fmt->new()";
68 22 ▶ if ( $@ and $@ =~ m/locate/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ and $@ =~ /locate/) { } 2 20 22
elsif ($@) { } 0 20 20
Condition66%2/3
$@ and $@ =~ /locate/
!ll&&!rl&&r
2002
MC/DC50%1/2
$@ and $@ =~ /locate/
$@ $@ =~ /locate/
Truth table66%2/3
A and B
A= $@B= $@ =~ /locate/
ABresult
0X0
100
111
69 2 ▶ eval "use $fmt";
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
71 } elsif ($@) {
72 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
73 }
74 }
75 22 return $fmt;
76 }
77  
78  
79 package DBI::Format::Base;
80  
81 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 sub new {
84 42 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
85 42 ▶ my $self = (@_ == 1) ? { %{shift()} } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 22 20 42
86 42 ▶ bless ($self, (ref($class) || $class));
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0420
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
87 42 $self;
88 }
89  
90 sub setup_fh {
91 53 ▶ my ($self, $fh) = @_;
Subroutine setup_fh: called CC=6 SCAR=20.5
92  
93 # This method has grown confused as to what it's trying to do and why
94 # Partly because this module was written in pre-perl5.3 days
95 # the code in other methods originally did: $fh->print(...)
96 # because C<print $fh ...> didn't work reliably as a method call.
97 # Now the code uses C<print $fh ...> some of this may no longer be
98 # required. It's important that things like IO::Scalar handles work.
99  
100 53 ▶ return $self->{fh} if !$fh && $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
if not $fh and $self->{"fh"} 0 53 53
Condition33%1/3
not $fh and $self->{"fh"}
!ll&&!rl&&r
5300
MC/DC0%0/2
not $fh and $self->{"fh"}
not $fh $self->{"fh"}
Truth table33%1/3
not(A) and B
A= not $fhB= $self->{"fh"}
ABresult
0X0
100
111
101  
102 53 ▶ $fh ||= \*STDOUT;
Condition50%1/2
$fh ||= \*STDOUT
l!l
530
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table50%1/2
A
A= $fh
Aresult
11
00
103  
104 53 ▶ return $fh if ref($fh) =~ m/GLOB/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($fh) =~ /GLOB/ 52 1 53
105  
106 1 ▶ unless (UNIVERSAL::can($fh,'print')) { # not blessed
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($fh, "print")) 0 1 1
107 0 require FileHandle;
108 0 bless $fh => "FileHandle";
109 }
110  
111 1 return $fh;
112 }
113  
114  
115 sub trailer {
116 50 ▶ my($self) = @_;
Subroutine trailer: called CC=1 SCAR=0.0
117 50 my $fh = delete $self->{'fh'};
118 50 my $sth = delete $self->{'sth'};
119 50 my $rows = delete $self->{'rows'};
120 50 print $fh ("[$rows rows of $sth->{NUM_OF_FIELDS} fields returned]\n");
121 50 delete $self->{'sep'};
122 }
123  
124 sub _determine_width {
125 19 ▶ my($self , $type, $precision) = @_;
Subroutine _determine_width: called CC=10 SCAR=29.2
126  
127 19 ▶ my $width =
Branch: 5 branches
BranchTrueFalseTotal
!defined($type) ? : 0 19 19
$type == &SQL_DATE() ? : 0 19 19
$type == &SQL_INTEGER() && defined $precision && $precision > 15 ? : 0 19 19
$type == &SQL_NUMERIC() && defined $precision && $precision > 15 ? : 0 19 19
defined $precision ? : 19 0 19
Condition50%6/12
$type == &SQL_INTEGER() and defined $precision and $precision > 15
!ll&&!rl&&r
1180
$type == &SQL_INTEGER() and defined $precision
!ll&&!rl&&r
1018
$type == &SQL_NUMERIC() and defined $precision and $precision > 15
!ll&&!rl&&r
1900
$type == &SQL_NUMERIC() and defined $precision
!ll&&!rl&&r
1900
MC/DC0%0/3
$type == &SQL_INTEGER() and defined $precision and $precision > 15
$type == &SQL_INTEGER() defined $precision $precision > 15
MC/DC0%0/3
$type == &SQL_NUMERIC() and defined $precision and $precision > 15
$type == &SQL_NUMERIC() defined $precision $precision > 15
Truth table50%2/4
A and B and C
A= $type == &SQL_INTEGER()B= defined $precisionC= $precision > 15
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and B and C
A= $type == &SQL_NUMERIC()B= defined $precisionC= $precision > 15
ABCresult
0XX0
10X0
1100
1111
128 (!defined($type)) ? 0 : # Is type defined?
129 ($type == SQL_DATE) ? 8 : # Is type a Date?
130 ($type == SQL_INTEGER # Is type an Integer?
131 and defined $precision
132 and $precision > 15 ) ? 10 :
133 ($type == SQL_NUMERIC # Is type a Numeric?
134 and defined $precision
135 and $precision > 15 ) ? 10 :
136 defined($precision) ? $precision: 0; # Default 0
137  
138 19 return $width;
139 }
140  
141  
142 package DBI::Format::Neat;
143  
144 7 @DBI::Format::Neat::ISA = qw(DBI::Format::Base);
145  
146 sub header {
147 26 ▶ my($self, $sth, $fh, $sep) = @_;
Subroutine header: called CC=2 SCAR=7.0
148 26 $self->{'fh'} = $fh = $self->setup_fh($fh);
149 26 $self->{'sth'} = $sth;
150 26 $self->{'rows'} = 0;
151 26 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 26 0 26
152 26 print $fh (join($self->{sep}, @{$sth->{'NAME'}}), "\n");
153 }
154  
155 sub row {
156 249 ▶ my($self, $rowref) = @_;
Subroutine row: called CC=3 SCAR=11.0
157 249 my @row = @$rowref;
158 # XXX note that neat/neat_list output is *not* ``safe''
159 # in the sense the it does not escape any chars and
160 # may truncate the string and may translate non-printable chars.
161 # We only deal with simple escaping here.
162 249 foreach(@row) {
163 1425 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 18 1407 1425
164 1407 s/'/\\'/g;
165 1407 s/\n/ /g;
166 }
167 249 my $fh = $self->{'fh'};
168 249 print $fh (DBI::neat_list(\@row, 9999, $self->{sep}),"\n");
169 249 ++$self->{'rows'};
170 }
171  
172  
173  
174 package DBI::Format::Box;
175  
176 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 7 @DBI::Format::Box::ISA = qw(DBI::Format::Base);
179  
180 sub header {
181 1 ▶ my($self, $sth, $fh, $sep) = @_;
Subroutine header: called CC=11 SCAR=25.4
182 1 $self->{'fh'} = $fh = $self->setup_fh($fh);
183 1 $self->{'sth'} = $sth;
184 1 $self->{'data'} = [];
185 1 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 1 0 1
186 1 my $types = $sth->{'TYPE'};
187 1 my @right_justify;
188 my @widths;
189 1 my $names = $sth->{'NAME'};
190 1 my $type;
191 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
192 14 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 14 0 14
193 14 $type = $types->[$i];
194 14 ▶ push(@right_justify,
Condition61%13/21
defined $type && ($type == &SQL_NUMERIC() || $type == &SQL_DECIMAL() || $type == &SQL_INTEGER() || $type == &SQL_SMALLINT() || $type == &SQL_FLOAT() || $type == &SQL_REAL() || $type == &SQL_TINYINT())
!ll&&!rl&&r
0113
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_FLOAT() or $type == &SQL_REAL() or $type == &SQL_TINYINT()
l!l&&r!l&&!r
1301
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_FLOAT() or $type == &SQL_REAL()
l!l&&r!l&&!r
1301
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_FLOAT()
l!l&&r!l&&!r
1301
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT()
l!l&&r!l&&!r
1301
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER()
l!l&&r!l&&!r
0131
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL()
l!l&&r!l&&!r
0014
MC/DC12%1/8
defined $type && ($type == &SQL_NUMERIC() || $type == &SQL_DECIMAL() || $type == &SQL_INTEGER() || $type == &SQL_SMALLINT() || $type == &SQL_FLOAT() || $type == &SQL_REAL() || $type == &SQL_TINYINT())
defined $type $type == &SQL_NUMERIC() $type == &SQL_DECIMAL() $type == &SQL_INTEGER() $type == &SQL_SMALLINT() $type == &SQL_FLOAT() $type == &SQL_REAL() $type == &SQL_TINYINT()
Truth table22%2/9
A && B or C or D or E or F or G or H
A= defined $typeB= $type == &SQL_NUMERIC()C= $type == &SQL_DECIMAL()D= $type == &SQL_INTEGER()E= $type == &SQL_SMALLINT()F= $type == &SQL_FLOAT()G= $type == &SQL_REAL()H= $type == &SQL_TINYINT()
ABCDEFGHresult
0XXXXXXX0
100000000
11XXXXXX1
101XXXXX1
1001XXXX1
10001XXX1
100001XX1
1000001X1
100000011
195 (defined($type) and ($type == SQL_NUMERIC ||
196 $type == SQL_DECIMAL ||
197 $type == SQL_INTEGER ||
198 $type == SQL_SMALLINT ||
199 $type == SQL_FLOAT ||
200 $type == SQL_REAL ||
201 $type == SQL_TINYINT))
202 );
203 }
204 1 $self->{'widths'} = \@widths;
205 1 $self->{'right_justify'} = \@right_justify;
206 }
207  
208  
209 sub row {
210 10 ▶ my($self, $orig_row) = @_;
Subroutine row: called CC=3 SCAR=11.0
211 10 my $i = 0;
212 10 my $col;
213 10 my $widths = $self->{'widths'};
214 10 my @row = @$orig_row; # don't mess with the original row
215 map {
216 10 ▶ if (!defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 140 140
217 0 $_ = ' (NULL) ';
218 } else {
219 140 $_ =~ s/\n/\\n/g;
220 140 $_ =~ s/\t/\\t/g;
221 140 $_ =~ s/[\000-\037\177-\237]/./g;
222 }
223 140 ▶ if (length($_) > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 10 130 140
224 10 $widths->[$i] = length($_);
225 }
226 140 ++$i;
227 } @row;
228 10 push @{$self->{data}}, \@row;
229 }
230  
231  
232 sub trailer {
233 1 ▶ my $self = shift;
Subroutine trailer: called CC=4 SCAR=13.9
234 1 my $widths = delete $self->{'widths'};
235 1 my $right_justify = delete $self->{'right_justify'};
236 1 my $sth = $self->{'sth'};
237 1 my $data = $self->{'data'};
238 1 $self->{'rows'} = @$data;
239  
240 1 my $format_sep = '+';
241 1 my $format_names = '|';
242 1 my $format_rows = '|';
243 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
244 14 $format_sep .= ('-' x $widths->[$i]) . '+';
245 14 $format_names .= sprintf("%%-%ds|", $widths->[$i]);
246 14 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 13 1 14
247 . ($right_justify->[$i] ? "" : "-") . "%ds|",
248 $widths->[$i]);
249 }
250 1 $format_sep .= "\n";
251 1 $format_names .= "\n";
252 1 $format_rows .= "\n";
253  
254 1 my $fh = $self->{'fh'};
255 1 print $fh ($format_sep);
256 1 print $fh (sprintf($format_names, @{$sth->{'NAME'}}));
257 1 foreach my $row (@$data) {
258 10 print $fh ($format_sep);
259 10 print $fh (sprintf($format_rows, @$row));
260 }
261 1 print $fh ($format_sep);
262  
263 1 $self->SUPER::trailer(@_);
264 }
265  
266 package DBI::Format::PartBox;
267  
268 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
269  
270 7 @DBI::Format::PartBox::ISA = qw(DBI::Format::Base);
271  
272 sub header {
273 0 ▶ my($self, $sth, $fh, $sep) = @_;
Subroutine header: not called CC=10 SCAR=47.0
274 0 $self->{'fh'} = $fh = $self->setup_fh($fh);
275 0 $self->{'sth'} = $sth;
276 0 $self->{'data'} = [];
277 0 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 0 0 0
278 0 my $types = $sth->{'TYPE'};
279 0 my @right_justify;
280 my @widths;
281 0 my $names = $sth->{'NAME'};
282 0 my $type;
283 0 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 0 0 0
284 0 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 0 0 0
285 0 $type = $types->[$i];
286 0 ▶ push(@right_justify,
Condition0%0/18
$type == &SQL_NUMERIC() || $type == &SQL_DECIMAL() || $type == &SQL_INTEGER() || $type == &SQL_SMALLINT() || $type == &SQL_FLOAT() || $type == &SQL_REAL() || $type == &SQL_TINYINT()
l!l&&r!l&&!r
000
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_FLOAT() or $type == &SQL_REAL()
l!l&&r!l&&!r
000
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_FLOAT()
l!l&&r!l&&!r
000
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT()
l!l&&r!l&&!r
000
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL() or $type == &SQL_INTEGER()
l!l&&r!l&&!r
000
$type == &SQL_NUMERIC() or $type == &SQL_DECIMAL()
l!l&&r!l&&!r
000
MC/DC0%0/7
$type == &SQL_NUMERIC() || $type == &SQL_DECIMAL() || $type == &SQL_INTEGER() || $type == &SQL_SMALLINT() || $type == &SQL_FLOAT() || $type == &SQL_REAL() || $type == &SQL_TINYINT()
$type == &SQL_NUMERIC() $type == &SQL_DECIMAL() $type == &SQL_INTEGER() $type == &SQL_SMALLINT() $type == &SQL_FLOAT() $type == &SQL_REAL() $type == &SQL_TINYINT()
Truth table0%0/8
A or B or C or D or E or F || G
A= $type == &SQL_NUMERIC()B= $type == &SQL_DECIMAL()C= $type == &SQL_INTEGER()D= $type == &SQL_SMALLINT()E= $type == &SQL_FLOAT()F= $type == &SQL_REAL()G= $type == &SQL_TINYINT()
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
287 ($type == SQL_NUMERIC ||
288 $type == SQL_DECIMAL ||
289 $type == SQL_INTEGER ||
290 $type == SQL_SMALLINT ||
291 $type == SQL_FLOAT ||
292 $type == SQL_REAL ||
293 $type == SQL_TINYINT));
294 }
295 0 $self->{'widths'} = \@widths;
296 0 $self->{'right_justify'} = \@right_justify;
297 }
298  
299  
300 sub row {
301 0 ▶ my($self, $orig_row) = @_;
Subroutine row: not called CC=3 SCAR=24.8
302 0 my $i = 0;
303 0 my $col;
304 0 my $widths = $self->{'widths'};
305 0 my @row = @$orig_row; # don't mess with the original row
306 map {
307 0 ▶ if (!defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
308 0 $_ = ' (NULL) ';
309 } else {
310 0 $_ =~ s/\n/\\n/g;
311 0 $_ =~ s/\t/\\t/g;
312 0 $_ =~ s/[\000-\037\177-\237]/./g;
313 }
314 0 ▶ if (length($_) > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 0 0 0
315 0 $widths->[$i] = length($_);
316 }
317 0 ++$i;
318 } @row;
319 0 push @{$self->{data}}, \@row;
320 }
321  
322  
323 sub trailer {
324 0 ▶ my $self = shift;
Subroutine trailer: not called CC=4 SCAR=30.0
325 0 my $widths = delete $self->{'widths'};
326 0 my $right_justify = delete $self->{'right_justify'};
327 0 my $sth = $self->{'sth'};
328 0 my $data = $self->{'data'};
329 0 $self->{'rows'} = @$data;
330  
331 0 my $format_sep = '+';
332 0 my $format_names = '|';
333 0 my $format_rows = '|';
334 0 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 0 0 0
335 0 $format_sep .= ('-' x $widths->[$i]) . '+';
336 0 $format_names .= sprintf("%%-%ds|", $widths->[$i]);
337 0 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 0 0 0
338 . ($right_justify->[$i] ? "" : "-") . "%ds|",
339 $widths->[$i]);
340 }
341 0 $format_sep .= "\n";
342 0 $format_names .= "\n";
343 0 $format_rows .= "\n";
344  
345 0 my $fh = $self->{'fh'};
346 0 print $fh ($format_sep);
347 0 print $fh (sprintf($format_names, @{$sth->{'NAME'}}));
348 0 print $fh ($format_sep);
349 0 foreach my $row (@$data) {
350 # print $fh ($format_sep);
351 0 print $fh (sprintf($format_rows, @$row));
352 }
353 0 print $fh ($format_sep);
354  
355 0 $self->SUPER::trailer(@_);
356 }
357  
358 package DBI::Format::Raw;
359  
360 7 @DBI::Format::Raw::ISA = qw(DBI::Format::Base);
361  
362 sub header {
363 1 ▶ my($self, $sth, $fh, $sep) = @_;
Subroutine header: called CC=2 SCAR=7.0
364 1 $self->{'fh'} = $fh = $self->setup_fh($fh);
365 1 $self->{'sth'} = $sth;
366 1 $self->{'rows'} = 0;
367 1 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 1 0 1
368 1 print $fh (join($self->{sep}, @{$sth->{'NAME'}}), "\n");
369 }
370  
371 sub row {
372 10 ▶ my($self, $rowref) = @_;
Subroutine row: called CC=1 SCAR=0.0
373 10 local $^W = 0;
374 10 my @row = @$rowref;
375 10 my $fh = $self->{'fh'};
376 10 print $fh (join($self->{sep}, @row), "\n");
377 10 ++$self->{'rows'};
378 }
379  
380 package DBI::Format::String;
381  
382 7 @DBI::Format::String::ISA = qw(DBI::Format::Base);
383  
384 sub header {
385 1 ▶ my($self, $sth, $fh, $sep) = @_;
Subroutine header: called CC=10 SCAR=23.8
386 1 $self->{'fh'} = $fh = $self->setup_fh($fh);
387 1 $self->{'sth'} = $sth;
388 1 $self->{'data'} = [];
389 1 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 1 0 1
390 1 my $types = $sth->{'TYPE'};
391 1 my @right_justify;
392 my @widths;
393 1 my $names = $sth->{'NAME'};
394 1 my $type;
395 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
396 14 $type = $types->[$i];
397 push(@widths, $self->_determine_width(
398 14 $type, $sth->{PRECISION}->[$i] ));
399  
400 14 ▶ push(@right_justify,
Condition61%13/21
defined $type && ($type == &DBI::SQL_NUMERIC() || $type == &DBI::SQL_DECIMAL() || $type == &DBI::SQL_INTEGER() || $type == &DBI::SQL_SMALLINT() || $type == &DBI::SQL_FLOAT() || $type == &DBI::SQL_REAL() || $type == &DBI::SQL_TINYINT())
!ll&&!rl&&r
0113
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT() or $type == &DBI::SQL_REAL() or $type == &DBI::SQL_TINYINT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT() or $type == &DBI::SQL_REAL()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER()
l!l&&r!l&&!r
0131
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL()
l!l&&r!l&&!r
0014
MC/DC12%1/8
defined $type && ($type == &DBI::SQL_NUMERIC() || $type == &DBI::SQL_DECIMAL() || $type == &DBI::SQL_INTEGER() || $type == &DBI::SQL_SMALLINT() || $type == &DBI::SQL_FLOAT() || $type == &DBI::SQL_REAL() || $type == &DBI::SQL_TINYINT())
defined $type $type == &DBI::SQL_NUMERIC() $type == &DBI::SQL_DECIMAL() $type == &DBI::SQL_INTEGER() $type == &DBI::SQL_SMALLINT() $type == &DBI::SQL_FLOAT() $type == &DBI::SQL_REAL() $type == &DBI::SQL_TINYINT()
Truth table22%2/9
A && B or C or D or E or F or G or H
A= defined $typeB= $type == &DBI::SQL_NUMERIC()C= $type == &DBI::SQL_DECIMAL()D= $type == &DBI::SQL_INTEGER()E= $type == &DBI::SQL_SMALLINT()F= $type == &DBI::SQL_FLOAT()G= $type == &DBI::SQL_REAL()H= $type == &DBI::SQL_TINYINT()
ABCDEFGHresult
0XXXXXXX0
100000000
11XXXXXX1
101XXXXX1
1001XXXX1
10001XXX1
100001XX1
1000001X1
100000011
401 (defined($type) and ($type == DBI::SQL_NUMERIC() ||
402 $type == DBI::SQL_DECIMAL() ||
403 $type == DBI::SQL_INTEGER() ||
404 $type == DBI::SQL_SMALLINT() ||
405 $type == DBI::SQL_FLOAT() ||
406 $type == DBI::SQL_REAL() ||
407 $type == DBI::SQL_TINYINT()))
408 );
409 14 my $format_names;
410 14 $format_names .= sprintf("%%-%ds ", $widths[$i]);
411 14 print $fh (sprintf($format_names, $names->[$i]));
412 }
413 1 $self->{'widths'} = \@widths;
414 1 $self->{'right_justify'} = \@right_justify;
415 1 print $fh "\n";
416  
417 }
418  
419  
420 sub row {
421 10 ▶ my($self, $orig_row) = @_;
Subroutine row: called CC=4 SCAR=13.9
422 10 my $i = 0;
423 10 my $col;
424 10 my $widths = $self->{'widths'};
425 10 my $right_justify = $self->{'right_justify'};
426 10 my @row = @$orig_row; # don't mess with the original row
427 map {
428 10 ▶ if (!defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 140 140
429 0 $_ = ' (NULL) ';
430 } else {
431 140 $_ =~ s/\n/\\n/g;
432 140 $_ =~ s/\t/\\t/g;
433 140 $_ =~ s/[\000-\037\177-\237]/./g;
434 }
435 140 ++$i;
436 } @row;
437  
438 10 my $sth = $self->{'sth'};
439 10 my $data = $self->{'data'};
440 10 my $format_rows = ' ';
441 10 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 140 10 150
442 140 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 130 10 140
443 . ($right_justify->[$i] ? "" : "-") . "%ds ",
444 $widths->[$i]);
445 }
446 10 $format_rows .= "\n";
447  
448 10 my $fh = $self->{'fh'};
449 10 print $fh (sprintf($format_rows, @row));
450 10 ++$self->{'rows'};
451 }
452  
453  
454 sub trailer {
455 1 ▶ my $self = shift;
Subroutine trailer: called CC=1 SCAR=0.0
456 1 my $widths = delete $self->{'widths'};
457 1 my $right_justify = delete $self->{'right_justify'};
458 1 $self->SUPER::trailer(@_);
459 }
460  
461 package DBI::Format::HTML;
462  
463 7 @DBI::Format::HTML::ISA = qw(DBI::Format::Base);
464  
465 sub header {
466 1 ▶ my($self, $sth, $fh) = @_;
Subroutine header: called CC=10 SCAR=24.2
467 1 $self->{'fh'} = $fh = $self->setup_fh($fh);
468 1 $self->{'sth'} = $sth;
469 1 $self->{'data'} = [];
470 1 my $types = $sth->{'TYPE'};
471 1 my @right_justify;
472 my @widths;
473 1 my $names = $sth->{'NAME'};
474 1 my $type;
475 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
476 14 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 14 0 14
477 14 $type = $types->[$i];
478 14 ▶ push(@right_justify,
Condition61%13/21
defined $type && ($type == &DBI::SQL_NUMERIC() || $type == &DBI::SQL_DECIMAL() || $type == &DBI::SQL_INTEGER() || $type == &DBI::SQL_SMALLINT() || $type == &DBI::SQL_FLOAT() || $type == &DBI::SQL_REAL() || $type == &DBI::SQL_TINYINT())
!ll&&!rl&&r
0113
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT() or $type == &DBI::SQL_REAL() or $type == &DBI::SQL_TINYINT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT() or $type == &DBI::SQL_REAL()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT() or $type == &DBI::SQL_FLOAT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER() or $type == &DBI::SQL_SMALLINT()
l!l&&r!l&&!r
1301
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL() or $type == &DBI::SQL_INTEGER()
l!l&&r!l&&!r
0131
$type == &DBI::SQL_NUMERIC() or $type == &DBI::SQL_DECIMAL()
l!l&&r!l&&!r
0014
MC/DC12%1/8
defined $type && ($type == &DBI::SQL_NUMERIC() || $type == &DBI::SQL_DECIMAL() || $type == &DBI::SQL_INTEGER() || $type == &DBI::SQL_SMALLINT() || $type == &DBI::SQL_FLOAT() || $type == &DBI::SQL_REAL() || $type == &DBI::SQL_TINYINT())
defined $type $type == &DBI::SQL_NUMERIC() $type == &DBI::SQL_DECIMAL() $type == &DBI::SQL_INTEGER() $type == &DBI::SQL_SMALLINT() $type == &DBI::SQL_FLOAT() $type == &DBI::SQL_REAL() $type == &DBI::SQL_TINYINT()
Truth table22%2/9
A && B or C or D or E or F or G or H
A= defined $typeB= $type == &DBI::SQL_NUMERIC()C= $type == &DBI::SQL_DECIMAL()D= $type == &DBI::SQL_INTEGER()E= $type == &DBI::SQL_SMALLINT()F= $type == &DBI::SQL_FLOAT()G= $type == &DBI::SQL_REAL()H= $type == &DBI::SQL_TINYINT()
ABCDEFGHresult
0XXXXXXX0
100000000
11XXXXXX1
101XXXXX1
1001XXXX1
10001XXX1
100001XX1
1000001X1
100000011
479 (defined $type and ($type == DBI::SQL_NUMERIC() ||
480 $type == DBI::SQL_DECIMAL() ||
481 $type == DBI::SQL_INTEGER() ||
482 $type == DBI::SQL_SMALLINT() ||
483 $type == DBI::SQL_FLOAT() ||
484 $type == DBI::SQL_REAL() ||
485 $type == DBI::SQL_TINYINT()))
486 );
487 }
488 1 $self->{'widths'} = \@widths;
489 1 $self->{'right_justify'} = \@right_justify;
490 }
491  
492  
493 sub row {
494 10 ▶ my($self, $orig_row) = @_;
Subroutine row: called CC=3 SCAR=11.0
495 10 my $i = 0;
496 10 my $col;
497 10 my $widths = $self->{'widths'};
498 10 my @row = @$orig_row; # don't mess with the original row
499 map {
500 10 ▶ if (!defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 140 140
501 0 $_ = ' (NULL) ';
502 } else {
503 140 $_ =~ s/\n/\\n/g;
504 140 $_ =~ s/\t/\\t/g;
505 140 $_ =~ s/[\000-\037\177-\237]/./g;
506 }
507 140 ▶ if (length($_) > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 10 130 140
508 10 $widths->[$i] = length($_);
509 }
510 140 ++$i;
511 } @row;
512 10 push @{$self->{data}}, \@row;
513 }
514  
515  
516 sub trailer {
517 1 ▶ my $self = shift;
Subroutine trailer: called CC=4 SCAR=13.9
518 1 my $widths = delete $self->{'widths'};
519 1 my $right_justify = delete $self->{'right_justify'};
520 1 my $sth = $self->{'sth'};
521 1 my $data = $self->{'data'};
522 1 $self->{'rows'} = @$data;
523  
524 1 my $format_sep = '+';
525 1 my $format_names = '<TR>';
526 1 my $format_rows = '<TR>';
527 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
528 14 $format_names .= sprintf("<TH>%%-%ds</TH>", $widths->[$i]);
529 14 ▶ $format_rows .= sprintf("<TD>%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 13 1 14
530 . ($right_justify->[$i] ? "" : "-") . "%ds</TD>",
531 $widths->[$i]);
532 }
533 1 $format_sep .= "\n";
534 1 $format_names .= "</TR>\n";
535 1 $format_rows .= "</TR>\n";
536  
537 1 my $fh = $self->{'fh'};
538 1 print $fh("<TABLE>\n");
539 1 print $fh(sprintf($format_names, @{$sth->{'NAME'}}));
540 1 foreach my $row (@$data) {
541 10 print $fh (sprintf($format_rows, @$row));
542 }
543 1 print $fh ("</TABLE>\n");
544  
545 1 $self->SUPER::trailer(@_);
546 }
547  
548  
549 7 1;
550  
551 =head1 NAME
552  
553 DBI::Format - A package for displaying result tables
554  
555 =head1 SYNOPSIS
556  
557 # create a new result object
558 $r = DBI::Format->new('var1' => 'val1', ...);
559  
560 # Prepare it for output by creating a header
561 $r->header($sth, $fh);
562  
563 # In a loop, display rows
564 while ($ref = $sth->fetchrow_arrayref()) {
565 $r->row($ref);
566 }
567  
568 # Finally create a trailer
569 $r->trailer();
570  
571  
572 =head1 DESCRIPTION
573  
574 THIS PACKAGE IS STILL VERY EXPERIMENTAL. THINGS WILL CHANGE.
575  
576 This package is used for making the output of DBI::Shell configurable.
577 The idea is to derive a subclass for any kind of output table you might
578 create. Examples are
579  
580 =over 8
581  
582 =item *
583  
584 a very simple output format as offered by DBI::neat_list().
585 L<"AVAILABLE SUBCLASSES">.
586  
587 =item *
588  
589 a box format, as offered by the Data::ShowTable module.
590  
591 =item *
592  
593 HTML format, as used in CGI binaries
594  
595 =item *
596  
597 postscript, to be piped into lpr or something similar
598  
599 =back
600  
601 In the future the package should also support interactive methods, for
602 example tab completion.
603  
604 These are the available methods:
605  
606 =over 8
607  
608 =item new(@attr)
609  
610 =item new(\%attr)
611  
612 (Class method) This is the constructor. You'd rather call a subclass
613 constructor. The construcor is accepting either a list of key/value
614 pairs or a hash ref.
615  
616 =item header($sth, $fh)
617  
618 (Instance method) This is called when a new result table should be
619 created to display the results of the statement handle B<$sth>. The
620 (optional) argument B<$fh> is an IO handle (or any object supporting
621 a I<print> method), usually you use an IO::Wrap object for STDIN.
622  
623 The method will query the B<$sth> for its I<NAME>, I<NUM_OF_FIELDS>,
624 I<TYPE>, I<SCALE> and I<PRECISION> attributes and typically print a
625 header. In general you should not assume that B<$sth> is indeed a DBI
626 statement handle and better treat it as a hash ref with the above
627 attributes.
628  
629 =item row($ref)
630  
631 (Instance method) Prints the contents of the array ref B<$ref>. Usually
632 you obtain this array ref by calling B<$sth-E<gt>fetchrow_arrayref()>.
633  
634 =item trailer
635  
636 (Instance method) Once you have passed all result rows to the result
637 package, you should call the I<trailer> method. This method can, for
638 example print the number of result rows.
639  
640 =back
641  
642  
643 =head1 AVAILABLE SUBCLASSES
644  
645 First of all, you can use the DBI::Format package itself: It's
646 not an abstract base class, but a very simple default using
647 DBI::neat_list().
648  
649  
650 =head2 Ascii boxes
651  
652 This subclass is using the I<Box> mode of the I<Data::ShowTable> module
653 internally. L<Data::ShowTable(3)>.
654  
655 =head2 Raw
656  
657 Row is written without formating. Columns returned in comma or user defined
658 separated list.
659  
660 =head2 String
661  
662 Row is written using a string format. Future releases will include th ability
663 set the string format.
664  
665  
666 =head1 AUTHOR AND COPYRIGHT
667  
668 This module is Copyright (c) 1997, 1998
669  
670 Jochen Wiedmann
671 Am Eisteich 9
672 72555 Metzingen
673 Germany
674  
675 Email: joe@ispsoft.de
676 Phone: +49 7123 14887
677  
678 The DBD::Proxy module is free software; you can redistribute it and/or
679 modify it under the same terms as Perl itself.
680  
681  
682 =head1 SEE ALSO
683  
684 L<DBI::Shell(3)>, L<DBI(3)>, L<dbish(1)>
« blib/lib/DBI/Format/SQLMinus.pm ↑ Summary blib/lib/DBI/Shell/Spool.pm »