blib/lib/DBI/Format.pm

filter: Statementstmt 82.2% 260 / 316 Branchbran 61.9% 52 / 84 Conditioncond 48.1% 51 / 106 MC/DCmcdc 8.8% 4 / 45 Subroutinesub 90.6% 29 / 32 Podpod 0.0% 0 / 2 totaltotal 67.6% 396 / 585 CC 90 SCAR 26.6
CC
90
Cov
72%
header
90.0
trailer
20.0
_determine_width
18.5
CRAP 14.2
« blib/lib/DBI/Shell/Spool.pm ↑ Summary blib/lib/DBI/Shell/FindSqlFile.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 our $VERSION = '11.98'; # VERSION
26  
27 7 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 22 ▶sub available_formatters {
Subroutine available_formatters: called CC=7 SCAR=19.5
Pod undocumented
30 22 my ($use_abbrev) = @_;
31 22 my @fmt;
32 22 my @dir = grep { -d "$_/DBI/Format" } @INC;
33 22 foreach my $dir (@dir) {
34 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" 66 0 66
35 66 ▶ push @fmt, map { m/^(\w+)\.pm$/i ? ($1) : () } readdir DIR;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)\.pm$/i ? : 132 132 264
36 66 closedir DIR;
37 }
38 22 my %fmt = map { (lc($_) => "DBI::Format::$_") } @fmt;
39 22 $fmt{box} = "DBI::Format::Box";
40 22 $fmt{partbox} = "DBI::Format::PartBox";
41 22 $fmt{neat} = "DBI::Format::Neat";
42 22 $fmt{raw} = "DBI::Format::Raw";
43 22 $fmt{string} = "DBI::Format::String";
44 22 $fmt{html} = "DBI::Format::HTML";
45 22 my $formatters = \%fmt;
46 22 ▶ if ($use_abbrev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_abbrev) 22 0 22
47 22 $formatters = abbrev(keys %fmt);
48 22 foreach my $abbrev (sort keys %$formatters) {
49 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
50 }
51 }
52 22 return $formatters;
53 }
54  
55  
56 22 ▶sub formatter {
Subroutine formatter: called CC=7 SCAR=20.8
Pod undocumented
57 22 my ($class, $mode, $use_abbrev) = @_;
58 22 $mode = lc($mode);
59 22 my $formatters = available_formatters($use_abbrev);
60 22 my $fmt = $formatters->{$mode};
61 22 ▶ if (!$fmt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fmt) 0 22 22
62 0 $formatters = available_formatters(0);
63 0 die "Format '$mode' unavailable. Available formats: ".
64 join(", ", sort keys %$formatters)."\n";
65 }
66 {
67 # Attempt to determine if format mode is in the base class.
68 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 22 eval "$fmt->new()";
70 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
71 2 ▶ eval "use $fmt";
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
73 } elsif ($@) {
74 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
75 }
76 }
77 22 return $fmt;
78 }
79  
80  
81 package DBI::Format::Base;
82  
83 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 # DBI::Format::Foo objects are presently copies of the parent DBI::Shell
86 # session hashref at the time of instantiation, and so are not aware of
87 # `/option' updates to the parent thereafter. Check the ->{parent} member
88 # for any session-specific /option values.
89  
90 42 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
91 42 my $class = shift;
92 42 ▶ my $self = (@_ == 1) ? { %{$_[0]}, parent => $_[0] } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 22 20 42
93 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
94 42 $self;
95 }
96  
97 # Basic preparation for output, setting up 'fh', 'sth', 'rows' and possibly
98 # 'sep' members. Also caches SQL type information and sets up BOOLEAN
99 # formatting, if needed.
100 53 ▶sub header {
Subroutine header: called CC=4 SCAR=14.1
101 53 my ($self, $sth, $fh, $sep) = @_;
102 53 my $types;
103  
104 53 $self->{fh} = $self->setup_fh($fh);
105 53 $self->{sth} = $sth;
106 53 $self->{rows} = 0;
107 53 ▶ $self->{sep} = $sep if defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep 52 1 53
108  
109 53 $self->{__dbi_format_sql_types} = $types = $sth->{TYPE};
110  
111 # Parent DBI::Shell session may have changed `/option bool_format'
112 # since the last query we formatted, so update our internal bool
113 # display data if needed.
114 53 for my $t (@$types) {
115 259 ▶ next unless $t == SQL_BOOLEAN;
Branch: 1 branch
BranchTrueFalseTotal
unless $t == &SQL_BOOLEAN() 259 0 259
116 $self->{__dbi_format_bool_alterns} =
117 0 [ split(',', $self->{parent}->{bool_format}, 2) ];
118 0 last;
119 }
120  
121 53 $self;
122 }
123  
124 # $fmt->encode_value( $value_reference, $sql_type )
125 #
126 # Do not call directly. This method is called by DBI::Format::Base::row.
127 #
128 # Apply output encoding to a single, textual representation of a field
129 # value. This method is called _after_ NULLs and BOOLEANs have been
130 # stringified.
131 #
132 # Base implementation escapes \n, \t and \r and translates ASCII
133 # non-printables without regard to $sql_type (SQL_NUMERIC, SQL_VARCHAR,
134 # etc.). This is *not* ``safe'' for all terminals in all locales --
135 # the default is merely simple encoding.
136 #
137 # Subclasses may override to URI- or XML-encode certain data, for example.
138 #
139  
140 2315 ▶sub encode_value {
Subroutine encode_value: called CC=3 SCAR=11.0
141 2315 my ($self, $value_ref, $sql_type) = @_;
142  
143 2315 for ($$value_ref) {
144 2315 ▶ last unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 2315 2315
145 2315 s/\n/\\n/g;
146 2315 s/\t/\\t/g;
147 2315 s/\r/\\r/g;
148 2315 s/[\000-\037\177-\237]/./g;
149 }
150 }
151  
152 # $fmt->row( $row_ref )
153 #
154 # Basic preparation of row data, responsible for formatting NULLs and
155 # BOOLEANs according to `/option' values, and calling encode_value() on
156 # fields. As a convenience, also increments $fmt->{rows}.
157 #
158 # All subclasses should call this function from their overridden row()
159 # methods.
160 #
161 # Note that row() modifies its argument in place, so $row_ref should be
162 # a _copy_ of the (presumptively read-only) row from the active $sth.
163 #
164 509 ▶sub row {
Subroutine row: called CC=6 SCAR=18.8
165 509 my ($self, $row) = @_;
166  
167 509 my $i = 0;
168 509 for my $value (@$row) {
169 2315 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 18 2297 2315
170 18 $value = $self->{parent}->{null_format};
171 }
172  
173 2315 my $sql_type = $self->{__dbi_format_sql_types}->[$i];
174 2315 ▶ if ($sql_type == SQL_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_type == &SQL_BOOLEAN()) 0 2315 2315
175 0 ▶ $value = $self->{__dbi_format_bool_alterns}->[ $value ? 0 : 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
176 }
177  
178 2315 $self->encode_value(\$value, $sql_type);
179 } continue {
180 2315 $i++;
181 }
182  
183 509 $self->{rows}++;
184 509 ▶ return wantarray ? @$row : $row;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 509 0 509
185 }
186  
187 53 ▶sub setup_fh {
Subroutine setup_fh: called CC=6 SCAR=20.5
188 53 my ($self, $fh) = @_;
189  
190 # This method has grown confused as to what it's trying to do and why
191 # Partly because this module was written in pre-perl5.3 days
192 # the code in other methods originally did: $fh->print(...)
193 # because C<print $fh ...> didn't work reliably as a method call.
194 # Now the code uses C<print $fh ...> some of this may no longer be
195 # required. It's important that things like IO::Scalar handles work.
196  
197 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
198  
199 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
200  
201 53 ▶ return $fh if ref($fh) =~ m/GLOB/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($fh) =~ /GLOB/ 52 1 53
202  
203 1 ▶ unless (UNIVERSAL::can($fh,'print')) { # not blessed
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($fh, "print")) 0 1 1
204 0 require FileHandle;
205 0 bless $fh => "FileHandle";
206 }
207  
208 1 return $fh;
209 }
210  
211  
212 50 ▶sub trailer {
Subroutine trailer: called CC=1 SCAR=0.0
213 50 my($self) = @_;
214 50 my $fh = delete $self->{'fh'};
215 50 my $sth = delete $self->{'sth'};
216 50 my $rows = delete $self->{'rows'};
217 50 print $fh ("[$rows rows of $sth->{NUM_OF_FIELDS} fields returned]\n");
218 50 delete $self->{'sep'};
219 }
220  
221 19 ▶sub _determine_width {
Subroutine _determine_width: called CC=10 SCAR=29.2
222 19 my($self , $type, $precision) = @_;
223  
224 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
225 (!defined($type)) ? 0 : # Is type defined?
226 ($type == SQL_DATE) ? 8 : # Is type a Date?
227 ($type == SQL_INTEGER # Is type an Integer?
228 and defined $precision
229 and $precision > 15 ) ? 10 :
230 ($type == SQL_NUMERIC # Is type a Numeric?
231 and defined $precision
232 and $precision > 15 ) ? 10 :
233 defined($precision) ? $precision: 0; # Default 0
234  
235 19 return $width;
236 }
237  
238  
239 package DBI::Format::Neat;
240  
241 7 @DBI::Format::Neat::ISA = qw(DBI::Format::Base);
242  
243 26 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
244 26 my ($self, $sth, $fh, $sep) = @_;
245 26 $self->SUPER::header($sth, $fh, $sep);
246 26 print {$self->{fh}} (join($self->{sep}, @{$sth->{'NAME'}}), "\n");
247 }
248  
249 249 ▶sub row {
Subroutine row: called CC=1 SCAR=0.0
250 249 my($self, $rowref) = @_;
251 249 my @row = $self->SUPER::row([@$rowref]);
252 249 my $fh = $self->{'fh'};
253 249 print $fh (DBI::neat_list(\@row, 9999, $self->{sep}),"\n");
254 }
255  
256  
257  
258 package DBI::Format::Box;
259  
260 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 7 @DBI::Format::Box::ISA = qw(DBI::Format::Base);
263  
264 1 ▶sub header {
Subroutine header: called CC=10 SCAR=24.4
265 1 my($self, $sth, $fh, $sep) = @_;
266 1 $self->SUPER::header($sth, $fh, $sep);
267 1 my $types = $sth->{'TYPE'};
268 1 my @right_justify;
269 my @widths;
270 1 my $names = $sth->{'NAME'};
271 1 my $type;
272 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
273 14 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 14 0 14
274 14 $type = $types->[$i];
275 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
276 (defined($type) and ($type == SQL_NUMERIC ||
277 $type == SQL_DECIMAL ||
278 $type == SQL_INTEGER ||
279 $type == SQL_SMALLINT ||
280 $type == SQL_FLOAT ||
281 $type == SQL_REAL ||
282 $type == SQL_TINYINT))
283 );
284 }
285 1 $self->{'widths'} = \@widths;
286 1 $self->{'right_justify'} = \@right_justify;
287 }
288  
289  
290 10 ▶sub row {
Subroutine row: called CC=3 SCAR=11.0
291 10 my($self, $orig_row) = @_;
292 10 my $i = 0;
293 10 my $col;
294 10 my $widths = $self->{'widths'};
295 10 my @row = $self->SUPER::row([@$orig_row]); # don't mess with the original row
296 10 for (@row) {
297 140 ▶ if (length > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 12 128 140
298 12 $widths->[$i] = length;
299 }
300 140 ++$i;
301 }
302 10 push @{$self->{data}}, \@row;
303 }
304  
305  
306 1 ▶sub trailer {
Subroutine trailer: called CC=4 SCAR=13.9
307 1 my $self = shift;
308 1 my $widths = delete $self->{'widths'};
309 1 my $right_justify = delete $self->{'right_justify'};
310 1 my $sth = $self->{'sth'};
311 1 my $data = $self->{'data'};
312 1 $self->{'rows'} = @$data;
313  
314 1 my $format_sep = '+';
315 1 my $format_names = '|';
316 1 my $format_rows = '|';
317 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
318 14 $format_sep .= ('-' x $widths->[$i]) . '+';
319 14 $format_names .= sprintf("%%-%ds|", $widths->[$i]);
320 14 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 13 1 14
321 . ($right_justify->[$i] ? "" : "-") . "%ds|",
322 $widths->[$i]);
323 }
324 1 $format_sep .= "\n";
325 1 $format_names .= "\n";
326 1 $format_rows .= "\n";
327  
328 1 my $fh = $self->{'fh'};
329 1 print $fh ($format_sep);
330 1 print $fh (sprintf($format_names, @{$sth->{'NAME'}}));
331 1 foreach my $row (@$data) {
332 10 print $fh ($format_sep);
333 10 print $fh (sprintf($format_rows, @$row));
334 }
335 1 print $fh ($format_sep);
336  
337 1 $self->SUPER::trailer(@_);
338 }
339  
340 package DBI::Format::PartBox;
341  
342 7 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344 7 @DBI::Format::PartBox::ISA = qw(DBI::Format::Base);
345  
346 0 ▶sub header {
Subroutine header: not called CC=9 SCAR=45.0
347 0 my ($self, $sth, $fh, $sep) = @_;
348 0 $self->SUPER::header($sth, $fh, $sep);
349 0 my $types = $sth->{'TYPE'};
350 0 my @right_justify;
351 my @widths;
352 0 my $names = $sth->{'NAME'};
353 0 my $type;
354 0 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 0 0 0
355 0 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 0 0 0
356 0 $type = $types->[$i];
357 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
358 ($type == SQL_NUMERIC ||
359 $type == SQL_DECIMAL ||
360 $type == SQL_INTEGER ||
361 $type == SQL_SMALLINT ||
362 $type == SQL_FLOAT ||
363 $type == SQL_REAL ||
364 $type == SQL_TINYINT));
365 }
366 0 $self->{'widths'} = \@widths;
367 0 $self->{'right_justify'} = \@right_justify;
368 }
369  
370  
371 0 ▶sub row {
Subroutine row: not called CC=3 SCAR=24.8
372 0 my($self, $orig_row) = @_;
373 0 my $i = 0;
374 0 my $col;
375 0 my $widths = $self->{'widths'};
376 0 my @row = $self->SUPER::row([@$orig_row]); # don't mess with the original row
377 0 for (@row) {
378 0 ▶ if (length > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 0 0 0
379 0 $widths->[$i] = length;
380 }
381 0 ++$i;
382 }
383 0 push @{$self->{data}}, \@row;
384 }
385  
386  
387 0 ▶sub trailer {
Subroutine trailer: not called CC=4 SCAR=30.0
388 0 my $self = shift;
389 0 my $widths = delete $self->{'widths'};
390 0 my $right_justify = delete $self->{'right_justify'};
391 0 my $sth = $self->{'sth'};
392 0 my $data = $self->{'data'};
393 0 $self->{'rows'} = @$data;
394  
395 0 my $format_sep = '+';
396 0 my $format_names = '|';
397 0 my $format_rows = '|';
398 0 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 0 0 0
399 0 $format_sep .= ('-' x $widths->[$i]) . '+';
400 0 $format_names .= sprintf("%%-%ds|", $widths->[$i]);
401 0 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 0 0 0
402 . ($right_justify->[$i] ? "" : "-") . "%ds|",
403 $widths->[$i]);
404 }
405 0 $format_sep .= "\n";
406 0 $format_names .= "\n";
407 0 $format_rows .= "\n";
408  
409 0 my $fh = $self->{'fh'};
410 0 print $fh ($format_sep);
411 0 print $fh (sprintf($format_names, @{$sth->{'NAME'}}));
412 0 print $fh ($format_sep);
413 0 foreach my $row (@$data) {
414 # print $fh ($format_sep);
415 0 print $fh (sprintf($format_rows, @$row));
416 }
417 0 print $fh ($format_sep);
418  
419 0 $self->SUPER::trailer(@_);
420 }
421  
422 package DBI::Format::Raw;
423  
424 7 @DBI::Format::Raw::ISA = qw(DBI::Format::Base);
425  
426 1 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
427 1 my ($self, $sth, $fh, $sep) = @_;
428 1 $self->SUPER::header($sth, $fh, $sep);
429 1 print {$self->{fh}} (join($self->{sep}, @{$sth->{'NAME'}}), "\n");
430 }
431  
432 10 ▶sub row {
Subroutine row: called CC=1 SCAR=0.0
433 10 my($self, $rowref) = @_;
434 10 local $^W = 0;
435 10 my @row = @$rowref;
436 10 my $fh = $self->{'fh'};
437 10 print $fh (join($self->{sep}, @row), "\n");
438 }
439  
440 package DBI::Format::String;
441  
442 7 @DBI::Format::String::ISA = qw(DBI::Format::Base);
443  
444 1 ▶sub header {
Subroutine header: called CC=9 SCAR=22.6
445 1 my ($self, $sth, $fh, $sep) = @_;
446 1 $self->SUPER::header($sth, $fh, $sep);
447 1 my $types = $sth->{'TYPE'};
448 1 my @right_justify;
449 my @widths;
450 1 my $names = $sth->{'NAME'};
451 1 my $type;
452 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
453 14 $type = $types->[$i];
454 push(@widths, $self->_determine_width(
455 14 $type, $sth->{PRECISION}->[$i] ));
456  
457 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
458 (defined($type) and ($type == DBI::SQL_NUMERIC() ||
459 $type == DBI::SQL_DECIMAL() ||
460 $type == DBI::SQL_INTEGER() ||
461 $type == DBI::SQL_SMALLINT() ||
462 $type == DBI::SQL_FLOAT() ||
463 $type == DBI::SQL_REAL() ||
464 $type == DBI::SQL_TINYINT()))
465 );
466 14 my $format_names;
467 14 $format_names .= sprintf("%%-%ds ", $widths[$i]);
468 14 print {$self->{fh}} (sprintf($format_names, $names->[$i]));
469 }
470 1 $self->{'widths'} = \@widths;
471 1 $self->{'right_justify'} = \@right_justify;
472 1 print {$self->{fh}} "\n";
473  
474 }
475  
476  
477 10 ▶sub row {
Subroutine row: called CC=3 SCAR=11.0
478 10 my($self, $orig_row) = @_;
479 10 my $i = 0;
480 10 my $col;
481 10 my $widths = $self->{'widths'};
482 10 my $right_justify = $self->{'right_justify'};
483 10 my @row = $self->SUPER::row([@$orig_row]); # don't mess with the original row
484  
485 10 my $sth = $self->{'sth'};
486 10 my $format_rows = ' ';
487 10 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 140 10 150
488 140 ▶ $format_rows .= sprintf("%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 130 10 140
489 . ($right_justify->[$i] ? "" : "-") . "%ds ",
490 $widths->[$i]);
491 }
492 10 $format_rows .= "\n";
493  
494 10 my $fh = $self->{'fh'};
495 10 print $fh (sprintf($format_rows, @row));
496 }
497  
498  
499 1 ▶sub trailer {
Subroutine trailer: called CC=1 SCAR=0.0
500 1 my $self = shift;
501 1 my $widths = delete $self->{'widths'};
502 1 my $right_justify = delete $self->{'right_justify'};
503 1 $self->SUPER::trailer(@_);
504 }
505  
506 package DBI::Format::HTML;
507  
508 7 @DBI::Format::HTML::ISA = qw(DBI::Format::Base);
509  
510 1 ▶sub header {
Subroutine header: called CC=10 SCAR=24.3
511 1 my($self, $sth, $fh) = @_;
512 1 $self->SUPER::header($sth, $fh);
513 1 $self->{'data'} = [];
514 1 my $types = $sth->{'TYPE'};
515 1 my @right_justify;
516 my @widths;
517 1 my $names = $sth->{'NAME'};
518 1 my $type;
519 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
520 14 ▶ push(@widths, defined($names->[$i]) ? length($names->[$i]) : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $names->[$i] ? : 14 0 14
521 14 $type = $types->[$i];
522 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
523 (defined $type and ($type == DBI::SQL_NUMERIC() ||
524 $type == DBI::SQL_DECIMAL() ||
525 $type == DBI::SQL_INTEGER() ||
526 $type == DBI::SQL_SMALLINT() ||
527 $type == DBI::SQL_FLOAT() ||
528 $type == DBI::SQL_REAL() ||
529 $type == DBI::SQL_TINYINT()))
530 );
531 }
532 1 $self->{'widths'} = \@widths;
533 1 $self->{'right_justify'} = \@right_justify;
534 }
535  
536  
537 10 ▶sub row {
Subroutine row: called CC=3 SCAR=11.0
538 10 my($self, $orig_row) = @_;
539 10 my $i = 0;
540 10 my $col;
541 10 my $widths = $self->{'widths'};
542 10 my @row = $self->SUPER::row([@$orig_row]); # don't mess with the original row
543 10 for (@row) {
544 140 ▶ if (length($_) > $widths->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $widths->[$i]) 12 128 140
545 12 $widths->[$i] = length($_);
546 }
547 140 ++$i;
548 }
549 10 push @{$self->{data}}, \@row;
550 }
551  
552  
553 1 ▶sub trailer {
Subroutine trailer: called CC=4 SCAR=13.9
554 1 my $self = shift;
555 1 my $widths = delete $self->{'widths'};
556 1 my $right_justify = delete $self->{'right_justify'};
557 1 my $sth = $self->{'sth'};
558 1 my $data = $self->{'data'};
559 1 $self->{'rows'} = @$data;
560  
561 1 my $format_sep = '+';
562 1 my $format_names = '<TR>';
563 1 my $format_rows = '<TR>';
564 1 ▶ for (my $i = 0; $i < $sth->{'NUM_OF_FIELDS'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $sth->{"NUM_OF_FIELDS"}) 14 1 15
565 14 $format_names .= sprintf("<TH>%%-%ds</TH>", $widths->[$i]);
566 14 ▶ $format_rows .= sprintf("<TD>%%"
Branch: 1 branch
BranchTrueFalseTotal
$right_justify->[$i] ? : 13 1 14
567 . ($right_justify->[$i] ? "" : "-") . "%ds</TD>",
568 $widths->[$i]);
569 }
570 1 $format_sep .= "\n";
571 1 $format_names .= "</TR>\n";
572 1 $format_rows .= "</TR>\n";
573  
574 1 my $fh = $self->{'fh'};
575 1 print $fh("<TABLE>\n");
576 1 print $fh(sprintf($format_names, @{$sth->{'NAME'}}));
577 1 foreach my $row (@$data) {
578 10 print $fh (sprintf($format_rows, @$row));
579 }
580 1 print $fh ("</TABLE>\n");
581  
582 1 $self->SUPER::trailer(@_);
583 }
584  
585  
586 7 1;
587  
588 =head1 NAME
589  
590 DBI::Format - A package for displaying result tables
591  
592 =head1 SYNOPSIS
593  
594 # create a new result object
595 $r = DBI::Format->new('var1' => 'val1', ...);
596  
597 # Prepare it for output by creating a header
598 $r->header($sth, $fh);
599  
600 # In a loop, display rows
601 while ($ref = $sth->fetchrow_arrayref()) {
602 $r->row($ref);
603 }
604  
605 # Finally create a trailer
606 $r->trailer();
607  
608  
609 =head1 DESCRIPTION
610  
611 THIS PACKAGE IS STILL VERY EXPERIMENTAL. THINGS WILL CHANGE.
612  
613 This package is used for making the output of DBI::Shell configurable.
614 The idea is to derive a subclass for any kind of output table you might
615 create. Examples are
616  
617 =over 8
618  
619 =item *
620  
621 a very simple output format as offered by DBI::neat_list().
622 L<"AVAILABLE SUBCLASSES">.
623  
624 =item *
625  
626 a box format, as offered by the Data::ShowTable module.
627  
628 =item *
629  
630 HTML format, as used in CGI binaries
631  
632 =item *
633  
634 postscript, to be piped into lpr or something similar
635  
636 =back
637  
638 In the future the package should also support interactive methods, for
639 example tab completion.
640  
641 These are the available methods:
642  
643 =over 8
644  
645 =item new(@attr)
646  
647 =item new(\%attr)
648  
649 (Class method) This is the constructor. You'd rather call a subclass
650 constructor. The construcor is accepting either a list of key/value
651 pairs or a hash ref.
652  
653 =item header($sth, $fh)
654  
655 (Instance method) This is called when a new result table should be
656 created to display the results of the statement handle B<$sth>. The
657 (optional) argument B<$fh> is an IO handle (or any object supporting
658 a I<print> method), usually you use an IO::Wrap object for STDIN.
659  
660 The method will query the B<$sth> for its I<NAME>, I<NUM_OF_FIELDS>,
661 I<TYPE>, I<SCALE> and I<PRECISION> attributes and typically print a
662 header. In general you should not assume that B<$sth> is indeed a DBI
663 statement handle and better treat it as a hash ref with the above
664 attributes.
665  
666 =item row($ref)
667  
668 (Instance method) Prints the contents of the array ref B<$ref>. Usually
669 you obtain this array ref by calling B<$sth-E<gt>fetchrow_arrayref()>.
670  
671 =item trailer
672  
673 (Instance method) Once you have passed all result rows to the result
674 package, you should call the I<trailer> method. This method can, for
675 example print the number of result rows.
676  
677 =back
678  
679  
680 =head1 AVAILABLE SUBCLASSES
681  
682 First of all, you can use the DBI::Format package itself: It's
683 not an abstract base class, but a very simple default using
684 DBI::neat_list().
685  
686  
687 =head2 Ascii boxes
688  
689 This subclass is using the I<Box> mode of the I<Data::ShowTable> module
690 internally. L<Data::ShowTable(3)>.
691  
692 =head2 Raw
693  
694 Row is written without formating. Columns returned in comma or user defined
695 separated list.
696  
697 =head2 String
698  
699 Row is written using a string format. Future releases will include th ability
700 set the string format.
701  
702  
703 =head1 AUTHOR AND COPYRIGHT
704  
705 This module is Copyright (c) 1997, 1998
706  
707 Jochen Wiedmann
708 Am Eisteich 9
709 72555 Metzingen
710 Germany
711  
712 Email: joe@ispsoft.de
713 Phone: +49 7123 14887
714  
715 The DBD::Proxy module is free software; you can redistribute it and/or
716 modify it under the same terms as Perl itself.
717  
718  
719 =head1 SEE ALSO
720  
721 L<DBI::Shell(3)>, L<DBI(3)>, L<dbish(1)>
« blib/lib/DBI/Shell/Spool.pm ↑ Summary blib/lib/DBI/Shell/FindSqlFile.pm »