Showing 3 changed files with 34 additions and 29 deletions
+2 -2
Changes
... ...
@@ -1,6 +1,6 @@
1 1
 0.1629
2
-  renamed auto_filter to filter
3
-  changed filter method arguments
2
+  renamed auto_filter to apply_filter
3
+  changed apply_filter method arguments
4 4
   deprecated cache_method
5 5
 0.1628
6 6
   remove DBIx::Custom::Model
+19 -18
lib/DBIx/Custom.pm
... ...
@@ -66,35 +66,37 @@ sub apply_filter {
66 66
     
67 67
     $self->{filter} ||= {};
68 68
     
69
-    # Table
70
-    my $table = shift;
69
+    # Table and column informations
70
+    my ($table, @cs) = @_;
71 71
     
72
-    if (@_) {
73
-        # Column infomations
74
-        my @cs = @_;
72
+    if (@cs) {
75 73
         
76 74
         # Initialize filters
77 75
         $self->{filter}{out} ||= {};
78 76
         $self->{filter}{in} ||= {};
79 77
         
80
-        # Create auto filters
81
-        foreach my $c (@cs) {
82
-            croak "Usage \$dbi->apply_filter(" .
83
-                  "TABLE, COLUMN, {in => INFILTER, out => OUTFILTER}, ...)"
84
-              unless ref $c eq 'ARRAY' && @$c == 3;
78
+        # Create filters
79
+        for (my $i = 0; $i < @cs; $i += 2) {
85 80
             
86 81
             # Column
87
-            my $column = $c->[0];
82
+            my $column = $cs[$i];
83
+            
84
+            # Filter
85
+            my $filter = $cs[$i + 1] || {};
86
+            my $in_filter = delete $filter->{in};
87
+            my $out_filter = delete $filter->{out};
88
+            croak "Usage \$dbi->apply_filter(" .
89
+                  "TABLE, COLUMN, {in => INFILTER, out => OUTFILTER}, ...)"
90
+              if ref $filter ne 'HASH' || keys %$filter;
88 91
             
89
-            # Bind filter
90
-            my $out_filter  = $c->[1];
92
+            # Out filter
91 93
             if (ref $out_filter eq 'CODE') {
92 94
                 $self->{filter}{out}{$table}{$column}
93 95
                   = $out_filter;
94 96
                 $self->{filter}{out}{$table}{"$table.$column"}
95 97
                   = $out_filter;
96 98
             }
97
-            else {
99
+            elsif (defined $out_filter) {
98 100
                 croak qq{"$out_filter" is not registered}
99 101
                   unless exists $self->filters->{$out_filter};
100 102
                 
... ...
@@ -102,17 +104,16 @@ sub apply_filter {
102 104
                   = $self->filters->{$out_filter};
103 105
                 $self->{filter}{out}{$table}{"$table.$column"}
104 106
                   = $self->filters->{$out_filter};
105
-              }
107
+            }
106 108
             
107
-            # Fetch filter
108
-            my $in_filter = $c->[2];
109
+            # In filter
109 110
             if (ref $in_filter eq 'CODE') {
110 111
                 $self->{filter}{in}{$table}{$column}
111 112
                   = $in_filter;
112 113
                 $self->{filter}{in}{$table}{"$table.$column"}
113 114
                   = $in_filter;
114 115
             }
115
-            else {
116
+            elsif (defined $in_filter) {
116 117
                 croak qq{"$in_filter" is not registered}
117 118
                   unless exists $self->filters->{$in_filter};
118 119
                 $self->{filter}{in}{$table}{$column}
+13 -9
t/dbix-custom-core-sqlite.t
... ...
@@ -549,23 +549,27 @@ is($dbi->twice(5), 10 , "$test : second");
549 549
 eval {$dbi->XXXXXX};
550 550
 like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "$test : not exists");
551 551
 
552
-test 'auto bind filter';
552
+test 'out filter';
553 553
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
554 554
 $dbi->execute($CREATE_TABLE->{0});
555 555
 $dbi->register_filter(twice => sub { $_[0] * 2 });
556 556
 $dbi->register_filter(three_times => sub { $_[0] * 3});
557 557
 $dbi->apply_filter(
558
-    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
558
+    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
559
+              'key2' => {out => 'three_times', in => 'twice'});
559 560
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
560 561
 $result = $dbi->execute($SELECT_SOURCES->{0});
561 562
 $row   = $result->fetch_hash_first;
562 563
 is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert");
564
+$result = $dbi->select(table => 'table1');
565
+$row   = $result->fetch_hash_first;
566
+is_deeply($row, {key1 => 6, key2 => 12}, "$test : insert");
563 567
 
564 568
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
565 569
 $dbi->execute($CREATE_TABLE->{0});
566 570
 $dbi->register_filter(twice => sub { $_[0] * 2 });
567 571
 $dbi->apply_filter(
568
-    'table1', ['key1', 'twice', 'twice']
572
+    'table1', 'key1' => {out => 'twice', in => 'twice'}
569 573
 );
570 574
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
571 575
 $dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
... ...
@@ -577,7 +581,7 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
577 581
 $dbi->execute($CREATE_TABLE->{0});
578 582
 $dbi->register_filter(twice => sub { $_[0] * 2 });
579 583
 $dbi->apply_filter(
580
-    'table1', ['key1', 'twice', 'twice']
584
+    'table1', 'key1' => {out => 'twice', in => 'twice'}
581 585
 );
582 586
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
583 587
 $dbi->delete(table => 'table1', where => {key1 => 1});
... ...
@@ -589,7 +593,7 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
589 593
 $dbi->execute($CREATE_TABLE->{0});
590 594
 $dbi->register_filter(twice => sub { $_[0] * 2 });
591 595
 $dbi->apply_filter(
592
-    'table1', ['key1', 'twice', 'twice']
596
+    'table1', 'key1' => {out => 'twice', in => 'twice'}
593 597
 );
594 598
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
595 599
 $result = $dbi->select(table => 'table1', where => {key1 => 1});
... ...
@@ -601,7 +605,7 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
601 605
 $dbi->execute($CREATE_TABLE->{0});
602 606
 $dbi->register_filter(twice => sub { $_[0] * 2 });
603 607
 $dbi->apply_filter(
604
-    'table1', ['key1', 'twice', 'twice']
608
+    'table1', 'key1' => {out => 'twice', in => 'twice'}
605 609
 );
606 610
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
607 611
 $result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
... ...
@@ -616,10 +620,10 @@ $dbi->execute($CREATE_TABLE->{2});
616 620
 $dbi->register_filter(twice => sub { $_[0] * 2 });
617 621
 $dbi->register_filter(three_times => sub { $_[0] * 3 });
618 622
 $dbi->apply_filter(
619
-    'table1', ['key2', 'twice', 'twice']
623
+    'table1', 'key2' => {out => 'twice', in => 'twice'}
620 624
 );
621 625
 $dbi->apply_filter(
622
-    'table2', ['key3', 'three_times', 'three_times']
626
+    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
623 627
 );
624 628
 $dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
625 629
 $dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
... ...
@@ -641,7 +645,7 @@ $result->filter({'key2' => 'twice'});
641 645
 $rows   = $result->fetch_hash_all;
642 646
 is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
643 647
 
644
-test 'apply_filter_easy_build';
648
+test 'iterate_all_columns';
645 649
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
646 650
 $dbi->execute($CREATE_TABLE->{2});
647 651
 $dbi->execute($CREATE_TABLE->{3});