Skip to content

Commit bfe5bd2

Browse files
committed
Correctly compute Sponge PRECISION
1 parent bf88384 commit bfe5bd2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/DBD/Sponge.pm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
$sth->{TYPE} = $attribs->{TYPE}
9393
|| [ (DBI::SQL_VARCHAR()) x $numFields ];
9494
$sth->{PRECISION} = $attribs->{PRECISION}
95-
|| [ map { length($sth->{NAME}->[$_]) } 0..$numFields -1 ];
95+
|| _max_columnar_lengths($numFields, $rows);
9696
$sth->{SCALE} = $attribs->{SCALE}
9797
|| [ (0) x $numFields ];
9898
$sth->{NULLABLE} = $attribs->{NULLABLE}
@@ -152,6 +152,19 @@
152152
return $dbh->set_err(42, "not enough parameters") unless @args >= 2;
153153
return \@args;
154154
}
155+
156+
sub _max_columnar_lengths {
157+
my ($numFields, $rows) = @_;
158+
my @precision = (0,) x $numFields;
159+
my $len;
160+
for my $row (@$rows) {
161+
for my $i (0 .. $numFields - 1) {
162+
next unless defined $len = length($row->[$i]);
163+
$precision[$i] = $len if $len > $precision[$i];
164+
}
165+
}
166+
return wantarray ? @precision : \@precision;
167+
}
155168
}
156169

157170

@@ -279,7 +292,7 @@ The number and order should match the number and ordering of the C<$data> column
279292
280293
C<%attr> is a hash of other standard DBI attributes that you might pass to a prepare statement.
281294
282-
Currently only NAME, TYPE, and PRECISION are supported.
295+
Currently only NAME, TYPE, and PRECISION are supported. PRECISION will be automatically computed if not supplied.
283296
284297
=back
285298

0 commit comments

Comments
 (0)