... | ... |
@@ -249,9 +249,10 @@ sub fetch { |
249 | 249 |
|
250 | 250 |
# Filter |
251 | 251 |
if ($fetch_filter) { |
252 |
- my $keys = $sth->{NAME_lc}; |
|
252 |
+ my $keys = $sth->{NAME_lc}; |
|
253 |
+ my $types = $sth->{TYPE}; |
|
253 | 254 |
for (my $i = 0; $i < @$keys; $i++) { |
254 |
- $row->[$i] = $fetch_filter->($keys->[$i], $row->[$i]); |
|
255 |
+ $row->[$i] = $fetch_filter->($keys->[$i], $row->[$i], $types->[$i], $sth, $i); |
|
255 | 256 |
} |
256 | 257 |
} |
257 | 258 |
return wantarray ? @$row : $row; |
... | ... |
@@ -264,20 +265,52 @@ sub fetch_hash { |
264 | 265 |
my $fetch_filter = $self->fetch_filter; |
265 | 266 |
|
266 | 267 |
# Fetch |
267 |
- my $row = $sth->fetchrow_hashref; |
|
268 |
+ my $row = $sth->fetchrow_arrayref; |
|
268 | 269 |
|
269 | 270 |
# Cannot fetch |
270 | 271 |
return unless $row; |
271 | 272 |
|
273 |
+ # Keys |
|
274 |
+ my $keys = $sth->{NAME_lc}; |
|
275 |
+ |
|
272 | 276 |
# Filter |
277 |
+ my $row_hash = {}; |
|
273 | 278 |
if ($fetch_filter) { |
274 |
- foreach my $key (keys %$row) { |
|
275 |
- $row->{$key} = $fetch_filter->($key, $row->{$key}); |
|
279 |
+ my $types = $sth->{TYPE}; |
|
280 |
+ for (my $i = 0; $i < @$keys; $i++) { |
|
281 |
+ $row_hash->{$keys->[$i]} = $fetch_filter->($keys->[$i], $row->[$i], $types->[$i], $sth, $i); |
|
276 | 282 |
} |
277 | 283 |
} |
278 |
- return wantarray ? %$row : $row; |
|
284 |
+ else { |
|
285 |
+ for (my $i = 0; $i < @$keys; $i++) { |
|
286 |
+ $row_hash->{$keys->[$i]} = $row->[$i]; |
|
287 |
+ } |
|
288 |
+ } |
|
289 |
+ return wantarray ? %$row_hash : $row_hash; |
|
279 | 290 |
} |
280 | 291 |
|
292 |
+# Fetch (hash) |
|
293 |
+#sub fetch_hash { |
|
294 |
+# my $self = shift; |
|
295 |
+# my $sth = $self->sth; |
|
296 |
+# my $fetch_filter = $self->fetch_filter; |
|
297 |
+# |
|
298 |
+# # Fetch |
|
299 |
+# my $row = $sth->fetchrow_hashref; |
|
300 |
+# |
|
301 |
+# # Cannot fetch |
|
302 |
+# return unless $row; |
|
303 |
+# |
|
304 |
+# # Filter |
|
305 |
+# if ($fetch_filter) { |
|
306 |
+# foreach my $key (keys %$row) { |
|
307 |
+# $row->{$key} = $fetch_filter->($key, $row->{$key}); |
|
308 |
+# } |
|
309 |
+# } |
|
310 |
+# return wantarray ? %$row : $row; |
|
311 |
+#} |
|
312 |
+ |
|
313 |
+ |
|
281 | 314 |
# Fetch all (array) |
282 | 315 |
sub fetch_all { |
283 | 316 |
my $self = shift; |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
use Test::More; |
2 | 2 |
use strict; |
3 | 3 |
use warnings; |
4 |
-use DBI; |
|
4 |
+use DBI qw/:sql_types/; |
|
5 | 5 |
|
6 | 6 |
BEGIN { |
7 | 7 |
eval { require DBD::SQLite; 1 } |
... | ... |
@@ -139,8 +139,8 @@ $t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub |
139 | 139 |
|
140 | 140 |
#--------------------------------------------------------------------- |
141 | 141 |
$dbi->fetch_filter(sub { |
142 |
- my ($key, $value) = @_; |
|
143 |
- if ($key eq 'k1' && $value == 1 ) { |
|
142 |
+ my ($key, $value, $type, $sth, $i) = @_; |
|
143 |
+ if ($key eq 'k1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
|
144 | 144 |
return $value * 3; |
145 | 145 |
} |
146 | 146 |
return $value; |