Showing 2 changed files with 77 additions and 36 deletions
+49 -33
lib/DBI/Custom.pm
... ...
@@ -235,53 +235,69 @@ use Object::Simple;
235 235
 sub sth          : Attr {}
236 236
 sub fetch_filter : Attr {}
237 237
 
238
+# Fetch (array)
238 239
 sub fetch {
239 240
     my ($self, $type) = @_;
240 241
     my $sth = $self->sth;
241
-    $type ||= 'array';
242
-    
243 242
     my $fetch_filter = $self->fetch_filter;
244
-    if ($type eq 'hash') {
245
-        my $hash = $sth->fetchrow_hashref;
246
-        return unless $hash;
247
-        if ($fetch_filter) {
248
-            foreach my $key (keys %$hash) {
249
-                $hash->{$key} = $fetch_filter->($key, $hash->{$key});
250
-            }
243
+    
244
+    # Fetch
245
+    my $row = $sth->fetchrow_arrayref;
246
+    
247
+    # Cannot fetch
248
+    return unless $row;
249
+    
250
+    # Filter
251
+    if ($fetch_filter) {
252
+        my $keys = $sth->{NAME_lc};
253
+        for (my $i = 0; $i < @$keys; $i++) {
254
+            $row->[$i] = $fetch_filter->($keys->[$i], $row->[$i]);
251 255
         }
252
-        return wantarray ? %$hash : $hash;
253 256
     }
254
-    else {
255
-        my $array = $sth->fetchrow_arrayref;
256
-        return unless $array;
257
-        if ($fetch_filter) {
258
-            my $keys = $sth->{NAME_lc};
259
-            for (my $i = 0; $i < @$keys; $i++) {
260
-                $array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]);
261
-            }
257
+    return wantarray ? @$row : $row;
258
+}
259
+
260
+# Fetch (hash)
261
+sub fetch_hash {
262
+    my $self = shift;
263
+    my $sth = $self->sth;
264
+    my $fetch_filter = $self->fetch_filter;
265
+    
266
+    # Fetch
267
+    my $row = $sth->fetchrow_hashref;
268
+    
269
+    # Cannot fetch
270
+    return unless $row;
271
+    
272
+    # Filter
273
+    if ($fetch_filter) {
274
+        foreach my $key (keys %$row) {
275
+            $row->{$key} = $fetch_filter->($key, $row->{$key});
262 276
         }
263
-        return wantarray ? @$array : $array;
264 277
     }
278
+    return wantarray ? %$row : $row;
265 279
 }
266 280
 
281
+# Fetch all (array)
267 282
 sub fetch_all {
268
-    my ($self, $type) = @_;
269
-    $type ||= 'array';
283
+    my $self = shift;
270 284
     
271
-    if ($type eq 'hash') {
272
-        my $array_of_hash = [];
273
-        while(my %hash = $self->fetch('hash')) {
274
-            push @$array_of_hash, {%hash};
275
-        }
276
-        return wantarray ? @$array_of_hash : $array_of_hash;
285
+    my $rows = [];
286
+    while(my %row = $self->fetch) {
287
+        push @$rows, {%row};
277 288
     }
278
-    else {
279
-        my $array_of_array = [];
280
-        while(my %array = $self->fetch('hash')) {
281
-            push @$array_of_array, {%array};
282
-        }
283
-        return wantarray ? @$array_of_array : $array_of_array;
289
+    return wantarray ? @$rows : $rows;
290
+}
291
+
292
+# Fetch all (hash)
293
+sub fetch_all_hash {
294
+    my $self = shift;
295
+    
296
+    my $rows = [];
297
+    while(my %row = $self->fetch_hash) {
298
+        push @$rows, {%row};
284 299
     }
300
+    return wantarray ? @$rows : $rows;
285 301
 }
286 302
 
287 303
 sub err    { shift->sth->err }
+28 -3
t/02-sqlite.t
... ...
@@ -65,6 +65,31 @@ my $t = Test::DBI::Custom->new;
65 65
 $t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub {
66 66
     my $dbi = shift;
67 67
     
68
+    my $r;     # resultset
69
+    my @rows;
70
+    
71
+    # Simple query array
72
+    $r = $dbi->query("select k1, k2 from t1");
73
+    
74
+    @rows = ();
75
+    while (my $row = $r->fetch) {
76
+        push @rows, [@$row];
77
+    }
78
+    is_deeply(\@rows, [[1, 2], [3, 4]], 'Simple query array');
79
+    
80
+    # Simple query hash
81
+    $r = $dbi->query("select k1, k2 from t1;");
82
+    
83
+    @rows = ();
84
+    while (my $row = $r->fetch_hash) {
85
+        push @rows, {%$row};
86
+    }
87
+    is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'Simple query array');
88
+    
89
+    
90
+    
91
+    
92
+    
68 93
     $dbi->fetch_filter(sub {
69 94
         my ($key, $value) = @_;
70 95
         if ($key eq 'k1' && $value == 1 ) {
... ...
@@ -73,11 +98,11 @@ $t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub
73 98
         return $value;
74 99
     });
75 100
     
76
-    my $result = $dbi->query("select k1, k2 from t1");
101
+    $r = $dbi->query("select k1, k2 from t1");
77 102
     
78
-    my $row = $result->fetch;
103
+    my $row = $r->fetch;
79 104
     my @values = @$row;
80
-    $result->finish;
105
+    $r->finish;
81 106
     
82 107
     is_deeply(\@values, [3, 2]);
83 108
 });