Skip to content

Commit 5372c94

Browse files
committed
Update comments for clarity; slight tightening of column width computation.
1 parent 134d89a commit 5372c94

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

lib/DBD/Sponge.pm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ use warnings;
9797
|| [ (0) x $numFields ];
9898
$sth->{NULLABLE} = $attribs->{NULLABLE}
9999
|| [ (2) x $numFields ];
100+
# Allow user to specify precision, otherwise
101+
# FETCH will lazily compute if needed
100102
if ($attribs->{PRECISION}) {
101103
$sth->{PRECISION} = $attribs->{PRECISION};
102-
} # else FETCH will dynamically compute
104+
}
103105
}
104106

105107
$outer;
@@ -203,11 +205,12 @@ use warnings;
203205
sub FETCH {
204206
my ($sth, $attrib) = @_;
205207
# would normally validate and only fetch known attributes
208+
# else pass up to DBI to handle
209+
206210
if ($attrib eq 'PRECISION') {
207-
# prepare() did _not_ specify PRECISION. We'll only get here once.
211+
# prepare() did _not_ specify PRECISION, so lazily compute it now
208212
return $sth->{PRECISION} = _max_col_lengths(@{$sth}{'NUM_OF_FIELDS', 'rows'});
209213
}
210-
# else pass up to DBI to handle
211214
return $sth->SUPER::FETCH($attrib);
212215
}
213216

@@ -219,18 +222,19 @@ use warnings;
219222
}
220223

221224
sub _max_col_lengths {
222-
# Compute result set PRECISION (data length) by looking for the
223-
# max lengths of each column's data.
224-
my ($numFields, $rows) = @_;
225-
my @precision = (0,) x $numFields;
225+
# compute our columns' PRECISION (data length) by looking for the
226+
# max lengths of each column's data, row by row
227+
my ($num_of_fields, $rows) = @_;
228+
my @precision = (0,) x $num_of_fields;
229+
my $n = $num_of_fields - 1;
226230
my $len;
227231
for my $row (@$rows) {
228-
for my $i (0 .. $numFields - 1) {
232+
for my $i (0 .. $n) {
229233
next unless defined($len = length($row->[$i]));
230234
$precision[$i] = $len if $len > $precision[$i];
231235
}
232236
}
233-
return wantarray ? @precision : \@precision;
237+
return \@precision;
234238
}
235239
}
236240

t/xx_sponge.t

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ use DBI qw(:sql_types);
1111

1212
# our reference table:
1313
#
14-
# A1 B1 C2
14+
# A0 B1 C2
1515
# ------- --------- -------
1616
# foo NULL bazooka
1717
# foolery bar NULL
1818
# NULL barrowman baz
1919
#
2020

21+
# Historically, DBD::Sponge defaulted an sth's PRECISION to the length
22+
# of its column names, meaning that some DBI shells could truncate row
23+
# display. For example, formatting a row ('fo', NULL, 'ba') from our
24+
# reference table above.
25+
2126
our @NAMES = ( 'A0', 'B1', 'C2' );
2227
our @ROWS = (['foo', undef, 'bazooka'],
2328
['foolery', 'bar', undef ],
@@ -46,7 +51,8 @@ is_deeply($sth->fetch(), $ROWS[1], "second row fetch as expected");
4651
is_deeply($sth->fetch(), $ROWS[2], "third row fetch as expected");
4752
ok(!defined($sth->fetch()), "fourth fetch returns undef");
4853

49-
# Test that DBD-Sponge preserves bogus user-supplied attributes
54+
# Test that DBD-Sponge preserves bogus user-supplied attributes but
55+
# ignores them when returning rows
5056
$sth = $dbh->prepare('user-supplied silly TYPE and PRECISION', {
5157
rows => dclone( \@ROWS ),
5258
NAME => [qw( first_col second_col third_col )],

0 commit comments

Comments
 (0)