Showing 3 changed files with 45 additions and 51 deletions
+1 -1
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1692';
3
+our $VERSION = '0.1693';
4 4
 use 5.008001;
5 5
 
6 6
 use Object::Simple -base;
+31 -49
lib/DBIx/Custom/Result.pm
... ...
@@ -50,42 +50,32 @@ sub filter {
50 50
 sub fetch {
51 51
     my $self = shift;
52 52
     
53
-    # Filter
54
-    my $filter = $self->filter;
55
-    
56
-    # End filter
57
-    my $end_filter = $self->{end_filter} || {};
58
-    
59 53
     # Fetch
60 54
     my @row = $self->{sth}->fetchrow_array;
61
-    
62
-    # No row
63 55
     return unless @row;
64 56
     
65 57
     # Filtering
66 58
     my $columns = $self->{sth}->{NAME};
67 59
     my $types = $self->{sth}->{TYPE};
68 60
     my $type_rule = $self->type_rule || {};
69
-    
61
+    my $filter = $self->filter;
62
+    my $end_filter = $self->end_filter;
70 63
     for (my $i = 0; $i < @$columns; $i++) {
71 64
         
72
-        # Filter name
65
+        # Column
73 66
         my $column = $columns->[$i];
74
-        my $f  = exists $filter->{$column}
75
-               ? $filter->{$column}
76
-               : $self->{default_filter};
77
-        my $ef = $end_filter->{$column};
78 67
         
79
-        # Filtering
80
-        if ($f && !$self->filter_off) {
81
-            $row[$i] = $f->($row[$i]);
82
-        }
83
-        elsif (!$self->type_rule_off && (my $rule = $type_rule->{lc($types->[$i])}))
84
-        {
85
-            $row[$i] = $rule->($row[$i]);
86
-        }
87
-
88
-        $row[$i] = $ef->($row[$i]) if $ef && !$self->filter_off;
68
+        # Type rule
69
+        my $type_filter = $type_rule->{lc($types->[$i])};
70
+        $row[$i] = $type_filter->($row[$i])
71
+          if $type_filter && !$self->{type_rule_off};
72
+        
73
+        # Filter
74
+        my $filter  = $filter->{$column} || $self->{default_filter};
75
+        $row[$i] = $filter->($row[$i])
76
+          if $filter && !$self->{filter_off};
77
+        $row[$i] = $end_filter->{$column}->($row[$i])
78
+          if $end_filter->{$column} && !$self->{filter_off};
89 79
     }
90 80
 
91 81
     return \@row;
... ...
@@ -120,46 +110,38 @@ sub fetch_first {
120 110
 sub fetch_hash {
121 111
     my $self = shift;
122 112
     
123
-    # Filter
124
-    my $filter  = $self->filter;
125
-    
126
-    # End filter
127
-    my $end_filter = $self->{end_filter} || {};
128
-    
129 113
     # Fetch
130 114
     my $row = $self->{sth}->fetchrow_arrayref;
131
-    
132
-    # Cannot fetch
133 115
     return unless $row;
134 116
 
135 117
     # Filter
136
-    my $row_hash = {};
118
+    my $hash_row = {};
119
+    my $filter  = $self->filter;
120
+    my $end_filter = $self->end_filter || {};
137 121
     my $columns = $self->{sth}->{NAME};
138 122
     my $types = $self->{sth}->{TYPE};
139 123
     my $type_rule = $self->type_rule || {};
140 124
     for (my $i = 0; $i < @$columns; $i++) {
141 125
         
142
-        # Filter name
126
+        # Column
143 127
         my $column = $columns->[$i];
144
-        my $f  = exists $filter->{$column}
145
-               ? $filter->{$column}
146
-               : $self->{default_filter};
147
-        my $ef = $end_filter->{$column};
148 128
         
149
-        # Filtering
150
-        if ($f && !$self->filter_off) {
151
-            $row_hash->{$column} =  $f->($row->[$i]);
129
+        # Type rule
130
+        my $type_filter = $type_rule->{lc($types->[$i])};
131
+        if (!$self->{type_rule_off} && $type_filter) {
132
+            $hash_row->{$column} = $type_filter->($row->[$i]);
152 133
         }
153
-        elsif (!$self->type_rule_off && (my $rule = $type_rule->{lc($types->[$i])}))
154
-        {
155
-            $row_hash->{$column} = $rule->($row->[$i]);
156
-        }
157
-        else { $row_hash->{$column} = $row->[$i] }
158
-        $row_hash->{$column} = $ef->($row_hash->{$column})
159
-          if $ef && !$self->filter_off;
134
+        else { $hash_row->{$column} = $row->[$i] }
135
+        
136
+        # Filter
137
+        my $f = $filter->{$column} || $self->{default_filter};
138
+        $hash_row->{$column} = $f->($hash_row->{$column})
139
+          if $f && !$self->{filter_off};
140
+        $hash_row->{$column} = $end_filter->{$column}->($hash_row->{$column})
141
+          if $end_filter->{$column} && !$self->{filter_off};
160 142
     }
161 143
     
162
-    return $row_hash;
144
+    return $hash_row;
163 145
 }
164 146
 
165 147
 sub fetch_hash_all {
+13 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -2864,7 +2864,19 @@ $dbi->type_rule(
2864 2864
 $dbi->insert({key1 => 2}, table => 'table1');
2865 2865
 $result = $dbi->select(table => 'table1');
2866 2866
 $result->filter(key1 => sub { $_[0] * 3 });
2867
-is($result->one->{key1}, 6);
2867
+is($result->one->{key1}, 12);
2868
+
2869
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2870
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2871
+$dbi->type_rule(
2872
+    from => {
2873
+        date => sub { $_[0] * 2 },
2874
+    },
2875
+);
2876
+$dbi->insert({key1 => 2}, table => 'table1');
2877
+$result = $dbi->select(table => 'table1');
2878
+$result->filter(key1 => sub { $_[0] * 3 });
2879
+is($result->fetch->[0], 12);
2868 2880
 
2869 2881
 test 'separator';
2870 2882
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});