Showing 3 changed files with 160 additions and 75 deletions
+4 -1
Changes
... ...
@@ -1,7 +1,10 @@
1 1
 0.1623
2 2
   added experimental auto_filter method
3 3
   deprecated default_bind_filter and default_fetch_filter because these are global effect.
4
-  weakend filter_check for usability
4
+  changed defautl_bind_filter and default_fetch_filter attributes to methods.
5
+  changed DBIx::Custom::Result default_filter attribute to method
6
+  changed DBIx::Custom::Result filter attribute to method.
7
+  filter_check is always done for usability
5 8
 0.1622
6 9
   deprecated DBIx::Custom::SQLite and DBIx::Custom::MySQL
7 10
   added dbi_options attribute
+98 -44
lib/DBIx/Custom.pm
... ...
@@ -15,8 +15,8 @@ use DBIx::Custom::Query;
15 15
 use DBIx::Custom::QueryBuilder;
16 16
 use Encode qw/encode_utf8 decode_utf8/;
17 17
 
18
-__PACKAGE__->attr([qw/data_source dbh default_bind_filter
19
-                      dbi_options default_fetch_filter password user/]);
18
+__PACKAGE__->attr([qw/data_source dbh
19
+                      dbi_options password user/]);
20 20
 
21 21
 __PACKAGE__->attr(cache => 1);
22 22
 __PACKAGE__->attr(cache_method => sub {
... ...
@@ -98,21 +98,38 @@ sub auto_filter {
98 98
         
99 99
         # Bind filter
100 100
         my $bind_filter  = $c->[1];
101
-        croak qq{"$bind_filter" is not registered}
102
-          unless $self->filters->{$bind_filter};
103
-        $self->{_auto_bind_filter}{$table}{$column}
104
-          = $self->filters->{$bind_filter};
105
-        $self->{_auto_bind_filter}{$table}{"$table.$column"}
106
-          = $self->filters->{$bind_filter};
101
+        if (ref $bind_filter eq 'CODE') {
102
+	        $self->{_auto_bind_filter}{$table}{$column}
103
+	          = $bind_filter;
104
+	        $self->{_auto_bind_filter}{$table}{"$table.$column"}
105
+	          = $bind_filter;
106
+        }
107
+        else {
108
+	        croak qq{"$bind_filter" is not registered}
109
+	          unless exists $self->filters->{$bind_filter};
110
+	        
111
+	        $self->{_auto_bind_filter}{$table}{$column}
112
+	          = $self->filters->{$bind_filter};
113
+	        $self->{_auto_bind_filter}{$table}{"$table.$column"}
114
+	          = $self->filters->{$bind_filter};
115
+	    }
107 116
         
108 117
         # Fetch filter
109 118
         my $fetch_filter = $c->[2];
110
-        croak qq{"$fetch_filter" is not registered}
111
-          unless $self->filters->{$fetch_filter};
112
-        $self->{_auto_fetch_filter}{$table}{$column}
113
-          = $self->filters->{$fetch_filter};
114
-        $self->{_auto_fetch_filter}{$table}{"$table.$column"}
115
-          = $self->filters->{$fetch_filter};
119
+        if (ref $fetch_filter eq 'CODE') {
120
+	        $self->{_auto_fetch_filter}{$table}{$column}
121
+	          = $fetch_filter;
122
+	        $self->{_auto_fetch_filter}{$table}{"$table.$column"}
123
+	          = $fetch_filter;
124
+        }
125
+        else {
126
+            croak qq{"$fetch_filter" is not registered}
127
+              unless exists $self->filters->{$fetch_filter};
128
+            $self->{_auto_fetch_filter}{$table}{$column}
129
+              = $self->filters->{$fetch_filter};
130
+            $self->{_auto_fetch_filter}{$table}{"$table.$column"}
131
+              = $self->filters->{$fetch_filter};
132
+        }
116 133
     }
117 134
     
118 135
     return $self;
... ...
@@ -209,6 +226,40 @@ sub create_query {
209 226
     return $query;
210 227
 }
211 228
 
229
+sub default_bind_filter {
230
+    my $self = shift;
231
+    my $fname = $_[0];
232
+    
233
+    if (@_ && !$fname) {
234
+        $self->{_default_bind_filter} = undef;
235
+    }
236
+    else {
237
+        croak qq{"$fname" is not registered}
238
+          unless exists $self->filters->{$fname};
239
+    
240
+        $self->{_default_bind_filter} = $self->filters->{$fname};
241
+    }
242
+    
243
+    return $self;
244
+}
245
+
246
+sub default_fetch_filter {
247
+    my $self = shift;
248
+    my $fname = $_[0];
249
+    
250
+    if (@_ && !$fname) {
251
+        $self->{_default_fetch_filter} = undef;
252
+    }
253
+    else {
254
+        croak qq{"$fname" is not registered}
255
+          unless exists $self->filters->{$fname};
256
+    
257
+        $self->{_default_fetch_filter} = $self->filters->{$fname};
258
+    }
259
+    
260
+    return $self;
261
+}
262
+
212 263
 our %VALID_DELETE_ARGS
213 264
   = map { $_ => 1 } qw/auto_filter_table table where append filter allow_delete_all/;
214 265
 
... ...
@@ -294,6 +345,15 @@ sub execute{
294 345
     
295 346
     # Filter
296 347
     my $filter = $args{filter} || $query->filter || {};
348
+    foreach my $column (keys %$filter) {
349
+        my $fname = $filter->{$column};
350
+        unless (ref $fname eq 'CODE') {
351
+          croak qq{"$fname" is not registered"}
352
+            unless exists $self->filters->{$fname};
353
+          
354
+          $filter->{$column} = $self->filters->{$fname};
355
+        }
356
+    }
297 357
     $filter = {%$auto_filter, %$filter};
298 358
     
299 359
     # Create bind value
... ...
@@ -320,10 +380,10 @@ sub execute{
320 380
 		# Result
321 381
 		my $result = $self->result_class->new(
322 382
             sth            => $sth,
323
-            default_filter => $self->default_fetch_filter,
324 383
             filters        => $self->filters,
325 384
             filter_check   => $self->filter_check,
326
-            _auto_filter   => $auto_fetch_filter || {}
385
+            _auto_filter   => $auto_fetch_filter || {},
386
+            _default_filter => $self->{_default_fetch_filter}
327 387
         );
328 388
 
329 389
         return $result;
... ...
@@ -594,14 +654,9 @@ sub _build_bind_values {
594 654
                   : $params->{$column};
595 655
         
596 656
         # Filtering
597
-        my $fname = $filter->{$column} || $self->default_bind_filter || '';
598
-        my $filter_func = ref $fname ? $fname
599
-                        : $fname ? $self->filters->{$fname}
600
-                        : undef;
657
+        my $f = $filter->{$column} || $self->{_default_bind_filter} || '';
601 658
         
602
-        push @bind_values, $filter_func
603
-                         ? $filter_func->($value)
604
-                         : $value;
659
+        push @bind_values, $f ? $f->($value) : $value;
605 660
         
606 661
         # Count up 
607 662
         $count->{$column}++;
... ...
@@ -715,10 +770,6 @@ Execute SQL
715 770
 
716 771
 Other features.
717 772
 
718
-    # Default filter
719
-    $dbi->default_bind_filter('encode_utf8');
720
-    $dbi->default_fetch_filter('decode_utf8');
721
-
722 773
     # Get DBI object
723 774
     my $dbh = $dbi->dbh;
724 775
 
... ...
@@ -815,18 +866,6 @@ L<DBI> object. You can call all methods of L<DBI>.
815 866
 DBI options.
816 867
 C<connect()> method use this value to connect the database.
817 868
 
818
-=head2 C<default_bind_filter>
819
-
820
-    my $default_bind_filter = $dbi->default_bind_filter
821
-    $dbi                    = $dbi->default_bind_filter('encode_utf8');
822
-
823
-Default filter when parameter binding is executed.
824
-
825
-=head2 C<default_fetch_filter>
826
-
827
-    my $default_fetch_filter = $dbi->default_fetch_filter;
828
-    $dbi                     = $dbi->default_fetch_filter('decode_utf8');
829
-
830 869
 Default filter when row is fetched.
831 870
 
832 871
 =head2 C<filters>
... ...
@@ -894,8 +933,17 @@ and implements the following new ones.
894 933
 
895 934
 C<auto_filter> is automatically filter for columns of table.
896 935
 This have effect C<insert>, C<update>, C<delete>. C<select>
897
-and L<DBIx::Custom::Result> object.
936
+and L<DBIx::Custom::Result> object. but this has'nt C<execute> method.
937
+
938
+If you want to have effect <execute< method, use C<auto_filter_table>
939
+arguments.
898 940
 
941
+    $result = $dbi->execute(
942
+        "select * from table1 where {= key1} and {= key2};",
943
+         param => {key1 => 1, key2 => 2},
944
+         auto_filter_table => ['table1']
945
+    );
946
+    
899 947
 B<Example:>
900 948
 
901 949
     $dbi->auto_filter('books', 'sale_date', 'to_date', 'date_to');
... ...
@@ -940,6 +988,16 @@ instead of suger methods.
940 988
 
941 989
     $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
942 990
 
991
+=head2 C<(deprecated) default_bind_filter>
992
+
993
+    $dbi = $dbi->default_bind_filter($fname);
994
+
995
+Default filter when parameter binding is executed.
996
+
997
+=head2 C<(deprecated) default_fetch_filter>
998
+
999
+    $dbi = $dbi->default_fetch_filter($fname);
1000
+
943 1001
 =head2 C<execute>
944 1002
 
945 1003
     my $result = $dbi->execute($query,  param => $params, filter => \%filter);
... ...
@@ -1074,10 +1132,6 @@ or arguments.
1074 1132
 
1075 1133
 =item *
1076 1134
 
1077
-C<default_bind_filter>, C<default_fetch_filter>
1078
-
1079
-=item *
1080
-
1081 1135
 C<filter> argument of C<insert()>, C<update()>,
1082 1136
 C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1083 1137
 methods
+58 -30
lib/DBIx/Custom/Result.pm
... ...
@@ -7,15 +7,50 @@ use base 'Object::Simple';
7 7
 
8 8
 use Carp 'croak';
9 9
 
10
-__PACKAGE__->attr([qw/default_filter filter
11
-                      filter_check filters sth/]);
10
+__PACKAGE__->attr([qw/filter_check filters sth/]);
11
+
12
+sub default_filter {
13
+    my $self = shift;
14
+    my $fname = $_[0];
15
+    
16
+    if (@_ && !$fname) {
17
+        $self->{_default_filter} = undef;
18
+    }
19
+    else {
20
+        croak qq{"$fname" is not registered}
21
+          unless exists $self->filters->{$fname};
22
+    
23
+        $self->{_default_filter} = $self->filters->{$fname};
24
+    }
25
+    
26
+    return $self;
27
+}
28
+
29
+sub filter {
30
+    my $self = shift;
31
+    my $filter = ref $_[0] eq 'HASH' ? $_[0] : {@_};
32
+    
33
+    foreach my $column (keys %$filter) {
34
+        my $fname = $filter->{$column};
35
+        unless (ref $fname eq 'CODE') {
36
+          croak qq{"$fname" is not registered"}
37
+            unless exists $self->filters->{$fname};
38
+          
39
+          $filter->{$column} = $self->filters->{$fname};
40
+        }
41
+    }
42
+    
43
+    $self->{_filter} = $filter;
44
+    
45
+    return $self;
46
+}
12 47
 
13 48
 sub fetch {
14 49
     my $self = shift;
15 50
     
16 51
     # Filters
17 52
     my $filters = $self->{filters} || {};
18
-    my $filter  = $self->{filter}  || {};
53
+    my $filter  = $self->{_filter}  || {};
19 54
     my $auto_filter = $self->{_auto_filter} || {};
20 55
     $filter = {%$auto_filter, %$filter};
21 56
     
... ...
@@ -31,14 +66,12 @@ sub fetch {
31 66
         
32 67
         # Filter name
33 68
         my $column = $columns->[$i];
34
-        my $fname  = exists $filter->{$column}
35
-                   ? $filter->{$column}
36
-                   : $self->{default_filter};
69
+        my $f  = exists $filter->{$column}
70
+               ? $filter->{$column}
71
+               : $self->{_default_filter};
37 72
         
38 73
         # Filtering
39
-        $row[$i] = ref $fname ? $fname->($row[$i]) 
40
-                 : $filters->{$fname}->($row[$i])
41
-          if $fname;
74
+        $row[$i] = $f->($row[$i]) if $f;
42 75
     }
43 76
 
44 77
     return \@row;
... ...
@@ -75,7 +108,7 @@ sub fetch_hash {
75 108
     
76 109
     # Filters
77 110
     my $filters = $self->{filters} || {};
78
-    my $filter  = $self->{filter}  || {};
111
+    my $filter  = $self->{_filter}  || {};
79 112
     my $auto_filter = $self->{_auto_filter} || {};
80 113
     $filter = {%$auto_filter, %$filter};
81 114
     
... ...
@@ -92,15 +125,12 @@ sub fetch_hash {
92 125
         
93 126
         # Filter name
94 127
         my $column = $columns->[$i];
95
-        my $fname  = exists $filter->{$column}
96
-                   ? $filter->{$column}
97
-                   : $self->{default_filter};
128
+        my $f  = exists $filter->{$column}
129
+               ? $filter->{$column}
130
+               : $self->{_default_filter};
98 131
         
99 132
         # Filtering
100
-        $row_hash->{$column}
101
-          = ref $fname ? $fname->($row->[$i])
102
-          : $fname     ? $filters->{$fname}->($row->[$i]) 
103
-          : $row->[$i];
133
+        $row_hash->{$column} = $f ? $f->($row->[$i]) : $row->[$i];
104 134
     }
105 135
     
106 136
     return $row_hash;
... ...
@@ -234,19 +264,6 @@ Fetch row into hash.
234 264
 
235 265
 =head1 ATTRIBUTES
236 266
 
237
-=head2 C<default_filter>
238
-
239
-    my $default_filter = $result->default_filter;
240
-    $result            = $result->default_filter('decode_utf8');
241
-
242
-Default filter when a row is fetched.
243
-
244
-=head2 C<filter>
245
-
246
-    my $filter = $result->filter;
247
-    $result    = $result->filter({title  => 'decode_utf8',
248
-                                  author => 'decode_utf8'});
249
-
250 267
 Filters when a row is fetched.
251 268
 This overwrites C<default_filter>.
252 269
 
... ...
@@ -276,6 +293,12 @@ Statement handle of L<DBI>.
276 293
 L<DBIx::Custom::Result> inherits all methods from L<Object::Simple>
277 294
 and implements the following new ones.
278 295
 
296
+=head2 C<(deprecated) default_filter>
297
+
298
+    $result = $result->default_filter($filter);
299
+
300
+Default filter when a row is fetched.
301
+
279 302
 =head2 C<fetch>
280 303
 
281 304
     my $row = $result->fetch;
... ...
@@ -326,4 +349,9 @@ Row count must be specified.
326 349
 Fetch multiple rows into array of array.
327 350
 Row count must be specified.
328 351
 
352
+=head2 C<filter>
353
+
354
+    $result    = $result->filter(title  => 'decode_utf8',
355
+                                 author => 'decode_utf8');
356
+
329 357
 =cut