Showing 4 changed files with 63 additions and 4 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1624
2
+  added experimental iterate_all_columns method.
1 3
 0.1623
2 4
   added experimental auto_filter method
3 5
   deprecated default_bind_filter and default_fetch_filter because these are global effect.
+32
lib/DBIx/Custom.pm
... ...
@@ -447,6 +447,25 @@ sub insert {
447 447
     return $ret_val;
448 448
 }
449 449
 
450
+sub iterate_all_columns {
451
+    my ($self, $cb) = @_;
452
+    
453
+    # Iterate all tables
454
+    my $sth_tables = $self->dbh->table_info;
455
+    while (my $table_info = $sth_tables->fetchrow_hashref) {
456
+        
457
+        # Table
458
+        my $table = $table_info->{TABLE_NAME};
459
+        
460
+        # Iterate all columns
461
+        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
462
+        while (my $column_info = $sth_columns->fetchrow_hashref) {
463
+            my $column = $column_info->{COLUMN_NAME};
464
+            $cb->($table, $column, $column_info);
465
+        }
466
+    }
467
+}
468
+
450 469
 sub new {
451 470
     my $self = shift->SUPER::new(@_);
452 471
     
... ...
@@ -1120,6 +1139,19 @@ B<Example:>
1120 1139
 
1121 1140
 Create a new L<DBIx::Custom> object.
1122 1141
 
1142
+=head2 C<(experimental) iterate_all_columns>
1143
+
1144
+    $dbi->iterate_all_columns(
1145
+        sub {
1146
+            my ($table, $column, $column_info) = @_;
1147
+            
1148
+            # do something;
1149
+        }
1150
+    );
1151
+
1152
+Iterate all columns of all tables. Argument is callback.
1153
+You can do anything by callback.
1154
+
1123 1155
 =head2 C<register_filter>
1124 1156
 
1125 1157
     $dbi->register_filter(%filters);
+2 -2
lib/DBIx/Custom/Result.pm
... ...
@@ -61,7 +61,7 @@ sub fetch {
61 61
     return unless @row;
62 62
     
63 63
     # Filtering
64
-    my $columns = $self->{sth}->{NAME_lc};
64
+    my $columns = $self->{sth}->{NAME};
65 65
     for (my $i = 0; $i < @$columns; $i++) {
66 66
         
67 67
         # Filter name
... ...
@@ -120,7 +120,7 @@ sub fetch_hash {
120 120
 
121 121
     # Filter
122 122
     my $row_hash = {};
123
-    my $columns = $self->{sth}->{NAME_lc};
123
+    my $columns = $self->{sth}->{NAME};
124 124
     for (my $i = 0; $i < @$columns; $i++) {
125 125
         
126 126
         # Filter name
+27 -2
t/dbix-custom-core-sqlite.t
... ...
@@ -25,7 +25,8 @@ sub test {
25 25
 my $CREATE_TABLE = {
26 26
     0 => 'create table table1 (key1 char(255), key2 char(255));',
27 27
     1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
28
-    2 => 'create table table2 (key1 char(255), key3 char(255));'
28
+    2 => 'create table table2 (key1 char(255), key3 char(255));',
29
+    3 => 'create table table1 (key1 Date, key2 datetime);'
29 30
 };
30 31
 
31 32
 my $SELECT_SOURCES = {
... ...
@@ -60,7 +61,7 @@ my $select_query;
60 61
 my $insert_query;
61 62
 my $update_query;
62 63
 my $ret_val;
63
-
64
+my $infos;
64 65
 
65 66
 # Prepare table
66 67
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -721,4 +722,28 @@ $result->filter({'key2' => 'twice'});
721 722
 $rows   = $result->fetch_hash_all;
722 723
 is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
723 724
 
725
+test 'auto_filter_easy_build';
726
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
727
+$dbi->execute($CREATE_TABLE->{2});
728
+$dbi->execute($CREATE_TABLE->{3});
729
+
730
+$infos = [];
731
+$dbi->iterate_all_columns(sub {
732
+    my ($table, $column, $cinfo) = @_;
733
+    
734
+    if ($table =~ /^table/) {
735
+         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
736
+         push @$infos, $info;
737
+    }
738
+});
739
+$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
740
+is_deeply($infos, 
741
+    [
742
+        ['table1', 'key1', 'key1'],
743
+        ['table1', 'key2', 'key2'],
744
+        ['table2', 'key1', 'key1'],
745
+        ['table2', 'key3', 'key3']
746
+    ]
747
+    , $test
748
+);
724 749