... | ... |
@@ -208,21 +208,24 @@ Object::Simple->build_class; |
208 | 208 |
package DBI::Custom::Result; |
209 | 209 |
use Object::Simple; |
210 | 210 |
|
211 |
-sub sth : Attr {} |
|
212 |
-sub fetch_filter {} |
|
211 |
+sub sth : Attr {} |
|
212 |
+sub fetch_filter : Attr {} |
|
213 | 213 |
|
214 | 214 |
sub fetchrow_arrayref { |
215 | 215 |
my $self = shift; |
216 | 216 |
my $sth = $self->{sth}; |
217 | 217 |
|
218 |
+ $DB::single = 1; |
|
218 | 219 |
my $array = $sth->fetchrow_arrayref; |
219 | 220 |
|
220 | 221 |
return $array unless $array; |
221 | 222 |
|
222 | 223 |
my $keys = $sth->{NAME_lc}; |
223 |
- |
|
224 |
- for (my $i = 0; $i < @$keys; $i++) { |
|
225 |
- $array->[$i] = $self->fetch_filter($keys->[$i], $array->[$i]); |
|
224 |
+ my $fetch_filter = $self->fetch_filter; |
|
225 |
+ if ($fetch_filter) { |
|
226 |
+ for (my $i = 0; $i < @$keys; $i++) { |
|
227 |
+ $array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]); |
|
228 |
+ } |
|
226 | 229 |
} |
227 | 230 |
return $array; |
228 | 231 |
} |
... | ... |
@@ -237,8 +240,11 @@ sub fetchrow_array { |
237 | 240 |
|
238 | 241 |
my $keys = $sth->{NAME_lc}; |
239 | 242 |
|
240 |
- for (my $i = 0; $i < @$keys; $i++) { |
|
241 |
- $array[$i] = $self->fetch_filter($keys->[$i], $array[$i]); |
|
243 |
+ my $fetch_filter = $self->fetch_filter; |
|
244 |
+ if ($fetch_filter) { |
|
245 |
+ for (my $i = 0; $i < @$keys; $i++) { |
|
246 |
+ $array[$i] = $fetch_filter->($keys->[$i], $array[$i]); |
|
247 |
+ } |
|
242 | 248 |
} |
243 | 249 |
return @array; |
244 | 250 |
} |
... | ... |
@@ -250,9 +256,12 @@ sub fetchrow_hashref { |
250 | 256 |
my $hash = $sth->fetchrow_hashref; |
251 | 257 |
|
252 | 258 |
return unless $hash; |
259 |
+ my $fetch_filter = $self->fetch_filter; |
|
253 | 260 |
|
254 |
- foreach my $key (keys %$hash) { |
|
255 |
- $hash->{$key} = $self->fetch_filter($key, $hash->{$key}); |
|
261 |
+ if ($fetch_filter) { |
|
262 |
+ foreach my $key (keys %$hash) { |
|
263 |
+ $hash->{$key} = $fetch_filter->($key, $hash->{$key}); |
|
264 |
+ } |
|
256 | 265 |
} |
257 | 266 |
return $hash; |
258 | 267 |
} |
... | ... |
@@ -264,6 +264,32 @@ our ($U, $P, $D) = connect_info(); |
264 | 264 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
265 | 265 |
} |
266 | 266 |
|
267 |
+{ |
|
268 |
+ my $dbi = DBI::Custom->new( |
|
269 |
+ connect_info => { |
|
270 |
+ user => $U, |
|
271 |
+ password => $P, |
|
272 |
+ data_source => "dbi:mysql:$D" |
|
273 |
+ } |
|
274 |
+ ); |
|
275 |
+ |
|
276 |
+ $dbi->fetch_filter(sub { |
|
277 |
+ my ($key, $value) = @_; |
|
278 |
+ if ($key eq 'key1' && $value == 1 ) { |
|
279 |
+ return $value * 3; |
|
280 |
+ } |
|
281 |
+ return $value; |
|
282 |
+ }); |
|
283 |
+ |
|
284 |
+ my $result = $dbi->query("select key1, key2 from test1"); |
|
285 |
+ |
|
286 |
+ my $row = $result->fetchrow_arrayref; |
|
287 |
+ my @values = @$row; |
|
288 |
+ $result->finish; |
|
289 |
+ |
|
290 |
+ is_deeply(\@values, [3, 2]); |
|
291 |
+} |
|
292 |
+ |
|
267 | 293 |
sub connect_info { |
268 | 294 |
my $file = 'password.tmp'; |
269 | 295 |
open my $fh, '<', $file |