Showing 5 changed files with 116 additions and 49 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1641
2
+  fix  bug : filter can't overwirite undef value.
3
+  add feature to apply_filter(). you can apply end filter.
4
+  add feature to apply_filter(). TABLE__COLUMN is filterded now.
1 5
 0.1640
2 6
   removed experimental DBIx::Custom::Table base() method
3 7
   table created by tabled method can call base_$method correponding to base_table's one
+49 -46
lib/DBIx/Custom.pm
... ...
@@ -81,54 +81,45 @@ sub apply_filter {
81 81
     
82 82
     # Create filters
83 83
     my $usage = "Usage: \$dbi->apply_filter(" .
84
-                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1}, " .
85
-                "COLUMN2, {in => INFILTER2, out => OUTFILTER2}, ...)";
84
+                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
85
+                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
86 86
 
87 87
     for (my $i = 0; $i < @cinfos; $i += 2) {
88 88
         
89 89
         # Column
90 90
         my $column = $cinfos[$i];
91 91
         
92
-        # Filter
93
-        my $filter = $cinfos[$i + 1] || {};
94
-        croak $usage unless  ref $filter eq 'HASH';
95
-        foreach my $ftype (keys %$filter) {
96
-            croak $usage unless $ftype eq 'in' || $ftype eq 'out'; 
92
+        # Filter info
93
+        my $finfo = $cinfos[$i + 1] || {};
94
+        croak $usage unless  ref $finfo eq 'HASH';
95
+        foreach my $ftype (keys %$finfo) {
96
+            croak $usage unless $ftype eq 'in' || $ftype eq 'out'
97
+                             || $ftype eq 'end'; 
97 98
         }
98
-        my $in_filter = $filter->{in};
99
-        my $out_filter = $filter->{out};
100 99
         
101
-        # Out filter
102
-        if (ref $out_filter eq 'CODE') {
103
-            $self->{filter}{out}{$table}{$column}
104
-              = $out_filter;
105
-            $self->{filter}{out}{$table}{"$table.$column"}
106
-              = $out_filter;
107
-        }
108
-        elsif (defined $out_filter) {
109
-            croak qq{Filter "$out_filter" is not registered}
110
-              unless exists $self->filters->{$out_filter};
100
+        foreach my $way (qw/in out end/) {
101
+            my $filter = $finfo->{$way};
111 102
             
112
-            $self->{filter}{out}{$table}{$column}
113
-              = $self->filters->{$out_filter};
114
-            $self->{filter}{out}{$table}{"$table.$column"}
115
-              = $self->filters->{$out_filter};
116
-        }
117
-        
118
-        # In filter
119
-        if (ref $in_filter eq 'CODE') {
120
-            $self->{filter}{in}{$table}{$column}
121
-              = $in_filter;
122
-            $self->{filter}{in}{$table}{"$table.$column"}
123
-              = $in_filter;
124
-        }
125
-        elsif (defined $in_filter) {
126
-            croak qq{Filter "$in_filter" is not registered}
127
-              unless exists $self->filters->{$in_filter};
128
-            $self->{filter}{in}{$table}{$column}
129
-              = $self->filters->{$in_filter};
130
-            $self->{filter}{in}{$table}{"$table.$column"}
131
-              = $self->filters->{$in_filter};
103
+            # State
104
+            my $state = !exists $finfo->{$way} ? 'not_exists'
105
+                      : !defined $filter        ? 'not_defined'
106
+                      : ref $filter eq 'CODE'   ? 'code'
107
+                      : 'name';
108
+            
109
+            next if $state eq 'not_exists';
110
+            
111
+            # Check filter
112
+            croak qq{Filter "$filter" is not registered}
113
+              if  $state eq 'name'
114
+               && ! exists $self->filters->{$filter};
115
+            
116
+            # Filter
117
+            my $f = $state eq 'not_defined' ? undef
118
+                  : $state eq 'code'        ? $filter
119
+                  : $self->filters->{$filter};
120
+            $self->{filter}{$way}{$table}{$column} = $f;
121
+            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
122
+            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
132 123
         }
133 124
     }
134 125
     
... ...
@@ -345,13 +336,18 @@ sub execute{
345 336
     # Return resultset if select statement is executed
346 337
     if ($sth->{NUM_OF_FIELDS}) {
347 338
         
348
-        # Auto in filter
349
-        my $in_filter = {};
339
+        # Result in and end filter
340
+        my $in_filter  = {};
341
+        my $end_filter = {};
350 342
         foreach my $table (@$tables) {
351 343
             $in_filter = {
352 344
                 %$in_filter,
353 345
                 %{$self->{filter}{in}{$table} || {}}
354
-            }
346
+            };
347
+            $end_filter = {
348
+                %$end_filter,
349
+                %{$self->{filter}{end}{$table} || {}}
350
+            };
355 351
         }
356 352
         
357 353
         # Result
... ...
@@ -360,7 +356,8 @@ sub execute{
360 356
             filters        => $self->filters,
361 357
             filter_check   => $self->filter_check,
362 358
             default_filter => $self->{default_in_filter},
363
-            filter         => $in_filter || {}
359
+            filter         => $in_filter || {},
360
+            end_filter     => $end_filter || {}
364 361
         );
365 362
 
366 363
         return $result;
... ...
@@ -1028,8 +1025,8 @@ and implements the following new ones.
1028 1025
 
1029 1026
     $dbi->apply_filter(
1030 1027
         $table,
1031
-        $column1 => {in => $infilter1, out => $outfilter1}
1032
-        $column2 => {in => $infilter2, out => $outfilter2}
1028
+        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1029
+        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
1033 1030
         ...,
1034 1031
     );
1035 1032
 
... ...
@@ -1045,7 +1042,13 @@ arguments.
1045 1042
          param => {key1 => 1, key2 => 2},
1046 1043
          table => ['table1']
1047 1044
     );
1048
-    
1045
+
1046
+You can use three name as column name.
1047
+
1048
+    1. column        : author
1049
+    2. table.column  : book.author
1050
+    3. table__column : book__author
1051
+
1049 1052
 =head2 C<connect>
1050 1053
 
1051 1054
     my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
-1
lib/DBIx/Custom/Query.pm
... ...
@@ -36,7 +36,6 @@ Column names.
36 36
                                  title  => 'to_something'});
37 37
 
38 38
 Filters when parameter binding is executed.
39
-This overwrites C<default_filter>.
40 39
 
41 40
 =head2 C<sql>
42 41
 
+10 -2
lib/DBIx/Custom/Result.pm
... ...
@@ -20,7 +20,11 @@ sub filter {
20 20
         
21 21
         foreach my $column (keys %$filter) {
22 22
             my $fname = $filter->{$column};
23
-            unless (ref $fname eq 'CODE') {
23
+
24
+            if  (exists $filter->{$column}
25
+              && defined $fname
26
+              && ref $fname ne 'CODE') 
27
+            {
24 28
               croak qq{Filter "$fname" is not registered"}
25 29
                 unless exists $self->filters->{$fname};
26 30
               
... ...
@@ -44,7 +48,11 @@ sub end_filter {
44 48
         
45 49
         foreach my $column (keys %$end_filter) {
46 50
             my $fname = $end_filter->{$column};
47
-            unless (ref $fname eq 'CODE') {
51
+            
52
+            if  (exists $end_filter->{$column}
53
+              && defined $fname
54
+              && ref $fname ne 'CODE') 
55
+            {
48 56
               croak qq{Filter "$fname" is not registered"}
49 57
                 unless exists $self->filters->{$fname};
50 58
               
+53
t/dbix-custom-core-sqlite.t
... ...
@@ -600,6 +600,21 @@ $result = $dbi->select(table => 'table1');
600 600
 $row   = $result->fetch_hash_first;
601 601
 is_deeply($row, {key1 => 6, key2 => 12}, "insert");
602 602
 
603
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
604
+$dbi->execute($CREATE_TABLE->{0});
605
+$dbi->register_filter(twice => sub { $_[0] * 2 });
606
+$dbi->register_filter(three_times => sub { $_[0] * 3});
607
+$dbi->apply_filter(
608
+    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
609
+              'key2' => {out => 'three_times', in => 'twice'});
610
+$dbi->apply_filter(
611
+    'table1', 'key1' => {out => undef}
612
+); 
613
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
614
+$result = $dbi->execute($SELECT_SOURCES->{0});
615
+$row   = $result->fetch_hash_first;
616
+is_deeply($row, {key1 => 1, key2 => 6}, "insert");
617
+
603 618
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
604 619
 $dbi->execute($CREATE_TABLE->{0});
605 620
 $dbi->register_filter(twice => sub { $_[0] * 2 });
... ...
@@ -852,6 +867,27 @@ $result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
852 867
 $row = $result->fetch_hash_first;
853 868
 is_deeply($row, {key1 => 6, key2 => 40});
854 869
 
870
+$dbi->register_filter(five_times => sub { $_[0] * 5 });
871
+$dbi->apply_filter('table1',
872
+    key1 => {end => sub { $_[0] * 3 } },
873
+    key2 => {end => 'five_times'}
874
+);
875
+$result = $dbi->select(table => 'table1');
876
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
877
+$row = $result->fetch_hash_first;
878
+is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
879
+
880
+$dbi->register_filter(five_times => sub { $_[0] * 5 });
881
+$dbi->apply_filter('table1',
882
+    key1 => {end => sub { $_[0] * 3 } },
883
+    key2 => {end => 'five_times'}
884
+);
885
+$result = $dbi->select(table => 'table1');
886
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
887
+$result->filter(key1 => undef);
888
+$result->end_filter(key1 => undef);
889
+$row = $result->fetch_hash_first;
890
+is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
855 891
 
856 892
 test 'empty where select';
857 893
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -1264,3 +1300,20 @@ is($table->three_times(1), 3, 'child table method');
1264 1300
 eval {$dbi->base_two};
1265 1301
 ok($@);
1266 1302
 
1303
+test 'filter __ expression';
1304
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1305
+$dbi->execute('create table company (id, name, location_id)');
1306
+$dbi->execute('create table location (id, name)');
1307
+$dbi->apply_filter('location',
1308
+  name => {in => sub { uc $_[0] } }
1309
+);
1310
+
1311
+$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2});
1312
+$dbi->insert(table => 'location', param => {id => 2, name => 'b'});
1313
+
1314
+$result = $dbi->select(
1315
+    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1316
+    column => ['location.name as location__name']
1317
+);
1318
+is($result->fetch_first->[0], 'B');
1319
+