Showing 4 changed files with 89 additions and 8 deletions
+2 -1
Changes
... ...
@@ -2,7 +2,8 @@
2 2
     - added EXPERIMENTAL available_data_type
3 3
     - simplified type_rule
4 4
     - changed type_rule arguments format
5
-    - added DBIx::Custom result_filter attribute
5
+    - added DBIx::Custom::Model result_filter attribute
6
+    - added DBIx::Custom::Result filter_off method
6 7
 0.1688
7 8
     - fixed bug that model insert, update, delete select can't
8 9
       odd number arguments
+12 -2
lib/DBIx/Custom.pm
... ...
@@ -1042,9 +1042,19 @@ sub type_rule {
1042 1042
             
1043 1043
             my $type = $column_info->{TYPE_NAME};
1044 1044
             if ($type_rule->{into} &&
1045
-                (my $rule = $type_rule->{into}->{$type}))
1045
+                (my $filter = $type_rule->{into}->{$type}))
1046 1046
             {
1047
-                $self->{_into}{$table}{$column} = $rule;
1047
+                return unless exists $type_rule->{into}->{$type};
1048
+                if  (defined $filter && ref $filter ne 'CODE') 
1049
+                {
1050
+                    my $fname = $filter;
1051
+                    croak qq{Filter "$fname" is not registered" } . _subname
1052
+                      unless exists $self->filters->{$fname};
1053
+                    
1054
+                    $filter = $self->filters->{$fname};
1055
+                }
1056
+
1057
+                $self->{_into}{$table}{$column} = $filter;
1048 1058
             }
1049 1059
         });
1050 1060
         
+7 -5
lib/DBIx/Custom/Result.pm
... ...
@@ -9,7 +9,7 @@ use Carp 'croak';
9 9
 use DBIx::Custom::Util qw/_array_to_hash _subname/;
10 10
 
11 11
 __PACKAGE__->attr(
12
-    [qw/filters sth type_rule type_rule_off/],
12
+    [qw/filters filter_off sth type_rule type_rule_off/],
13 13
     stash => sub { {} }
14 14
 );
15 15
 
... ...
@@ -125,8 +125,8 @@ sub fetch {
125 125
         my $ef = $end_filter->{$column};
126 126
         
127 127
         # Filtering
128
-        $row[$i] = $f->($row[$i]) if $f;
129
-        $row[$i] = $ef->($row[$i]) if $ef;
128
+        $row[$i] = $f->($row[$i]) if $f && !$self->filter_off;
129
+        $row[$i] = $ef->($row[$i]) if $ef && !$self->filter_off;
130 130
     }
131 131
 
132 132
     return \@row;
... ...
@@ -195,8 +195,10 @@ sub fetch_hash {
195 195
         my $ef = $end_filter->{$column};
196 196
         
197 197
         # Filtering
198
-        $row_hash->{$column} = $f ? $f->($row->[$i]) : $row->[$i];
199
-        $row_hash->{$column} = $ef->($row_hash->{$column}) if $ef;
198
+        $row_hash->{$column} = $f && !$self->filter_off ? $f->($row->[$i])
199
+                                                        : $row->[$i];
200
+        $row_hash->{$column} = $ef->($row_hash->{$column})
201
+          if $ef && !$self->filter_off;
200 202
     }
201 203
     
202 204
     return $row_hash;
+68
t/dbix-custom-core-sqlite.t
... ...
@@ -2684,6 +2684,24 @@ $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2684 2684
 $result = $dbi->select(table => 'table1', type_rule_off => 1);
2685 2685
 is($result->one->{key1}, 2);
2686 2686
 
2687
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2688
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2689
+$dbi->register_filter(ppp => sub { uc $_[0] });
2690
+$dbi->type_rule(
2691
+    into => {
2692
+        Date => 'ppp'
2693
+    }
2694
+);
2695
+$dbi->insert({key1 => 'a'}, table => 'table1');
2696
+$result = $dbi->select(table => 'table1');
2697
+is($result->one->{key1}, 'A');
2698
+
2699
+eval{$dbi->type_rule(
2700
+    into => {
2701
+        Date => 'pp'
2702
+    }
2703
+)};
2704
+like($@, qr/not registered/);
2687 2705
 
2688 2706
 test 'result_filter';
2689 2707
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -2721,4 +2739,54 @@ is_deeply($result->one,
2721 2739
           {key1 => 2, key2 => 2, 'table2.key1' => 3, 'table2.key3' => 9});
2722 2740
 is_deeply($model2->select->one, {key1 => 3, key3 => 9});
2723 2741
 
2742
+test 'filter_off';
2743
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2744
+$dbi->execute($CREATE_TABLE->{0});
2745
+$dbi->execute($CREATE_TABLE->{2});
2746
+
2747
+$dbi->create_model(
2748
+    table => 'table1',
2749
+    join => [
2750
+       'left outer join table2 on table1.key1 = table2.key1'
2751
+    ],
2752
+    primary_key => ['key1'],
2753
+    result_filter => {
2754
+        key1 => sub { $_[0] * 2 }
2755
+    },
2756
+);
2757
+$model2 = $dbi->create_model(
2758
+    table => 'table2',
2759
+    result_filter => [
2760
+        [qw/key1 key3/] => sub { $_[0] * 3 }
2761
+    ]
2762
+);
2763
+$dbi->setup_model;
2764
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2765
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2766
+$model = $dbi->model('table1');
2767
+$result = $model->select(
2768
+    column => [
2769
+        $model->mycolumn,
2770
+        {table2 => [qw/key1 key3/]}
2771
+    ],
2772
+    where => {'table1.key1' => 1}
2773
+);
2774
+$result->filter_off(1);
2775
+$result->end_filter(key1 => sub { $_[0] * 5});
2776
+is_deeply($result->one,
2777
+          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2778
+
2779
+$result = $model->select(
2780
+    column => [
2781
+        $model->mycolumn,
2782
+        {table2 => [qw/key1 key3/]}
2783
+    ],
2784
+    where => {'table1.key1' => 1}
2785
+);
2786
+$result->filter_off(1);
2787
+$result->end_filter(key1 => sub { $_[0] * 5});
2788
+
2789
+is_deeply($result->fetch_first,
2790
+          [1, 2, 1, 3]);
2791
+
2724 2792
 =cut