Showing 3 changed files with 71 additions and 30 deletions
+3
Changes
... ...
@@ -1,3 +1,6 @@
1
+0.22
2
+  - added EXPERIMENTAL DBIx::Custom::Result::kv method
3
+  - added EXPERIMENTAL DBIx::Custom::Result::flat method
1 4
 0.2111
2 5
   - "created_at" option is renamed to "ctime" option.
3 6
     "created_at" is DEPRECATED!
+63 -30
lib/DBIx/Custom/Result.pm
... ...
@@ -18,36 +18,6 @@ sub column {
18 18
   return $column;
19 19
 }
20 20
 
21
-sub filter {
22
-  my $self = shift;
23
-  
24
-  # Set
25
-  if (@_) {
26
-    
27
-    # Convert filter name to subroutine
28
-    my $filter = @_ == 1 ? $_[0] : [@_];
29
-    $filter = _array_to_hash($filter);
30
-    for my $column (keys %$filter) {
31
-      my $fname = $filter->{$column};
32
-      if  (exists $filter->{$column}
33
-        && defined $fname
34
-        && ref $fname ne 'CODE') 
35
-      {
36
-        croak qq{Filter "$fname" is not registered" } . _subname
37
-          unless exists $self->dbi->filters->{$fname};
38
-        $filter->{$column} = $self->dbi->filters->{$fname};
39
-      }
40
-    }
41
-    
42
-    # Merge
43
-    $self->{filter} = {%{$self->filter}, %$filter};
44
-    
45
-    return $self;
46
-  }
47
-  
48
-  return $self->{filter} ||= {};
49
-}
50
-
51 21
 sub fetch {
52 22
   my $self = shift;
53 23
   
... ...
@@ -246,6 +216,46 @@ sub fetch_one {
246 216
   return $row;
247 217
 }
248 218
 
219
+sub filter {
220
+  my $self = shift;
221
+  
222
+  # Set
223
+  if (@_) {
224
+    
225
+    # Convert filter name to subroutine
226
+    my $filter = @_ == 1 ? $_[0] : [@_];
227
+    $filter = _array_to_hash($filter);
228
+    for my $column (keys %$filter) {
229
+      my $fname = $filter->{$column};
230
+      if  (exists $filter->{$column}
231
+        && defined $fname
232
+        && ref $fname ne 'CODE') 
233
+      {
234
+        croak qq{Filter "$fname" is not registered" } . _subname
235
+          unless exists $self->dbi->filters->{$fname};
236
+        $filter->{$column} = $self->dbi->filters->{$fname};
237
+      }
238
+    }
239
+    
240
+    # Merge
241
+    $self->{filter} = {%{$self->filter}, %$filter};
242
+    
243
+    return $self;
244
+  }
245
+  
246
+  return $self->{filter} ||= {};
247
+}
248
+
249
+sub flat {
250
+  my $self = shift;
251
+  
252
+  my @flat;
253
+  while (my $row = $self->fetch) {
254
+    push @flat, @$row;
255
+  }
256
+  return @flat;
257
+}
258
+
249 259
 sub header { shift->sth->{NAME} }
250 260
 
251 261
 *one = \&fetch_hash_one;
... ...
@@ -563,6 +573,29 @@ Set filter for column.
563 573
 You can use subroutine or filter name as filter.
564 574
 This filter is executed after C<type_rule> filter.
565 575
 
576
+=head2 C<flat> EXPERIMENTAL
577
+
578
+  my $flat = $result->flat;
579
+
580
+All value is flatten and added to one array reference.
581
+  
582
+  my @flat = $dbi->select(['id', 'title'])->flat;
583
+
584
+If C<fetch_all> method return the following data
585
+
586
+  [
587
+    [1, 'Perl'],
588
+    [2, 'Ruby']
589
+  ]
590
+
591
+C<flat> method return the following data.
592
+
593
+  (1, 'Perl', 2, 'Ruby')
594
+
595
+You can create key-value pair easily.
596
+
597
+  my %titles = $dbi->select(['id', 'title'])->flat;
598
+
566 599
 =head2 C<header>
567 600
 
568 601
   my $header = $result->header;
+5
t/common.t
... ...
@@ -3579,6 +3579,11 @@ $result->filter({$key1 => 'three_times'});
3579 3579
 $rows = $result->fetch_hash_all;
3580 3580
 is_deeply($rows, [{$key1 => 3, $key2 => 2}, {$key1 => 9, $key2 => 4}], "hash");
3581 3581
 
3582
+test 'flat';
3583
+$result = $dbi->select(table => $table1);
3584
+$rows = [$result->flat];
3585
+is_deeply($rows, [1, 2, 3, 4]);
3586
+
3582 3587
 test 'DBIx::Custom::Result fetch_multi';
3583 3588
 eval { $dbi->execute("drop table $table1") };
3584 3589
 $dbi->execute($create_table1);