@@ -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
0 commit comments