Skip to content

Commit dea97d1

Browse files
committed
lazy PRECISION
1 parent bfe5bd2 commit dea97d1

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/DBD/Sponge.pm

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@
9191
|| [ map { "col$_" } 1..$numFields ];
9292
$sth->{TYPE} = $attribs->{TYPE}
9393
|| [ (DBI::SQL_VARCHAR()) x $numFields ];
94-
$sth->{PRECISION} = $attribs->{PRECISION}
95-
|| _max_columnar_lengths($numFields, $rows);
9694
$sth->{SCALE} = $attribs->{SCALE}
9795
|| [ (0) x $numFields ];
9896
$sth->{NULLABLE} = $attribs->{NULLABLE}
9997
|| [ (2) x $numFields ];
98+
if ($attribs->{PRECISION}) {
99+
$sth->{PRECISION} = $attribs->{PRECISION};
100+
} # else FETCH will dynamically compute
100101
}
101102

102103
$outer;
@@ -153,18 +154,6 @@
153154
return \@args;
154155
}
155156

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-
}
168157
}
169158

170159

@@ -212,6 +201,10 @@
212201
sub FETCH {
213202
my ($sth, $attrib) = @_;
214203
# would normally validate and only fetch known attributes
204+
if ($attrib eq 'PRECISION') {
205+
# prepare() did _not_ specify PRECISION. We'll only get here once.
206+
return $sth->{PRECISION} = _max_col_lengths(@{$sth}{'NUM_OF_FIELDS', 'rows'});
207+
}
215208
# else pass up to DBI to handle
216209
return $sth->SUPER::FETCH($attrib);
217210
}
@@ -222,6 +215,19 @@
222215
# else pass up to DBI to handle
223216
return $sth->SUPER::STORE($attrib, $value);
224217
}
218+
219+
sub _max_col_lengths {
220+
my ($numFields, $rows) = @_;
221+
my @precision = (0,) x $numFields;
222+
my $len;
223+
for my $row (@$rows) {
224+
for my $i (0 .. $numFields - 1) {
225+
next unless defined($len = length($row->[$i]));
226+
$precision[$i] = $len if $len > $precision[$i];
227+
}
228+
}
229+
return wantarray ? @precision : \@precision;
230+
}
225231
}
226232

227233
1;

0 commit comments

Comments
 (0)