Showing 5 changed files with 186 additions and 26 deletions
+1
Changes
... ...
@@ -1,4 +1,5 @@
1 1
 0.1692
2
+    - added EXPERIMENTAL DBIx::Custom::Result type_rule
2 3
     - added EXPERIMENTAL available_type_name method 
3 4
     - EXPERIMENTAL type_rule_off is not passed form execute method 
4 5
       to DBIx::Custom::Result object
+8 -2
lib/DBIx/Custom.pm
... ...
@@ -521,7 +521,7 @@ sub execute {
521 521
             default_filter => $self->{default_in_filter},
522 522
             filter => $filter->{in} || {},
523 523
             end_filter => $filter->{end} || {},
524
-            type_rule => $self->type_rule,
524
+            type_rule => \%{$self->type_rule->{from}},
525 525
         );
526 526
 
527 527
         return $result;
... ...
@@ -989,10 +989,14 @@ sub type_rule {
989 989
         $type_rule->{into} = _array_to_hash($type_rule->{into});
990 990
         $self->{type_rule} = $type_rule;
991 991
         $self->{_into} ||= {};
992
+        foreach my $type_name (keys %{$type_rule->{into} || {}}) {
993
+            croak qq{type name of into section must be lower case}
994
+              if $type_name =~ /[A-Z]/;
995
+        }
992 996
         $self->each_column(sub {
993 997
             my ($dbi, $table, $column, $column_info) = @_;
994 998
             
995
-            my $type_name = $column_info->{TYPE_NAME};
999
+            my $type_name = lc $column_info->{TYPE_NAME};
996 1000
             if ($type_rule->{into} &&
997 1001
                 (my $filter = $type_rule->{into}->{$type_name}))
998 1002
             {
... ...
@@ -1014,6 +1018,8 @@ sub type_rule {
1014 1018
         # From
1015 1019
         $type_rule->{from} = _array_to_hash($type_rule->{from});
1016 1020
         foreach my $data_type (keys %{$type_rule->{from} || {}}) {
1021
+            croak qq{data type of into section must be lower case or number}
1022
+              if $data_type =~ /[A-Z]/;
1017 1023
             my $fname = $type_rule->{from}{$data_type};
1018 1024
             if (defined $fname && ref $fname ne 'CODE') {
1019 1025
                 croak qq{Filter "$fname" is not registered" } . _subname
+44 -6
lib/DBIx/Custom/Result.pm
... ...
@@ -5,7 +5,7 @@ use Object::Simple -base;
5 5
 use Carp 'croak';
6 6
 use DBIx::Custom::Util qw/_array_to_hash _subname/;
7 7
 
8
-has [qw/filters filter_off sth type_rule type_rule_off/],
8
+has [qw/filters filter_off sth type_rule_off/],
9 9
     stash => sub { {} };
10 10
 
11 11
 *all = \&fetch_hash_all;
... ...
@@ -68,9 +68,7 @@ sub fetch {
68 68
     my $type_rule = $self->type_rule || {};
69 69
     
70 70
     for (my $i = 0; $i < @$columns; $i++) {
71
-        
72
-        if (!$self->type_rule_off && $type_rule->{from} &&
73
-            (my $rule = $type_rule->{from}->{$types->[$i]}))
71
+        if (!$self->type_rule_off && (my $rule = $type_rule->{lc($types->[$i])}))
74 72
         {
75 73
             $row[$i] = $rule->($row[$i]);
76 74
         }
... ...
@@ -139,8 +137,7 @@ sub fetch_hash {
139 137
     for (my $i = 0; $i < @$columns; $i++) {
140 138
         
141 139
         # Type rule
142
-        if (!$self->type_rule_off && $type_rule->{from} &&
143
-            (my $rule = $type_rule->{from}->{$types->[$i]}))
140
+        if (!$self->type_rule_off && (my $rule = $type_rule->{lc($types->[$i])}))
144 141
         {
145 142
             $row->[$i] = $rule->($row->[$i]);
146 143
         }
... ...
@@ -229,6 +226,36 @@ sub fetch_multi {
229 226
 
230 227
 *one = \&fetch_hash_first;
231 228
 
229
+sub type_rule {
230
+    my $self = shift;
231
+    
232
+    # Merge type rule
233
+    if (@_) {
234
+        my $type_rule = @_ == 1 ? $_[0] : [@_];
235
+        $type_rule = _array_to_hash($type_rule) || {};
236
+        foreach my $data_type (keys %{$type_rule || {}}) {
237
+            croak qq{data type of into section must be lower case or number}
238
+              if $data_type =~ /[A-Z]/;
239
+            my $fname = $type_rule->{$data_type};
240
+            if (defined $fname && ref $fname ne 'CODE') {
241
+                croak qq{Filter "$fname" is not registered" } . _subname
242
+                  unless exists $self->filters->{$fname};
243
+                
244
+                $type_rule->{$data_type} = $self->filters->{$fname};
245
+            }
246
+        }
247
+        $self->{type_rule} = {%{$self->type_rule}, %$type_rule};
248
+    }
249
+    
250
+    return $self->{type_rule} ||= {};
251
+}
252
+
253
+sub clear_type_rule {
254
+    my $self = shift;
255
+    $self->{type_rule} = {};
256
+    return $self;
257
+}
258
+
232 259
 # DEPRECATED!
233 260
 sub end_filter {
234 261
     my $self = shift;
... ...
@@ -501,6 +528,17 @@ Remove filter. End filter is not removed.
501 528
 
502 529
 Stash is hash reference to save your data.
503 530
 
531
+=head2 C<type_rule> EXPERIMENTAL
532
+
533
+    $result->type_rule(
534
+        # DATE
535
+        9 => sub { ... },
536
+        # DATETIME or TIMESTAMP
537
+        11 => sub { ... }
538
+    );
539
+
540
+This override L<DBIx::Custom>'s C<type_rule> C<from> section.
541
+
504 542
 =head2 C<remove_end_filter> DEPRECATED!
505 543
 
506 544
     $result->remove_end_filter;
+131 -16
t/dbix-custom-core-sqlite.t
... ...
@@ -2605,7 +2605,7 @@ test 'type_rule from';
2605 2605
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2606 2606
 $dbi->type_rule(
2607 2607
     from => {
2608
-        Date => sub { uc $_[0] }
2608
+        date => sub { uc $_[0] }
2609 2609
     }
2610 2610
 );
2611 2611
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
... ...
@@ -2622,7 +2622,7 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2622 2622
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2623 2623
 $dbi->type_rule(
2624 2624
     into => {
2625
-        Date => sub { uc $_[0] }
2625
+        date => sub { uc $_[0] }
2626 2626
     }
2627 2627
 );
2628 2628
 $dbi->insert({key1 => 'a'}, table => 'table1');
... ...
@@ -2630,10 +2630,10 @@ $result = $dbi->select(table => 'table1');
2630 2630
 is($result->one->{key1}, 'A');
2631 2631
 
2632 2632
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2633
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2633
+$dbi->execute("create table table1 (key1 date, key2 datetime)");
2634 2634
 $dbi->type_rule(
2635 2635
     into => [
2636
-         [qw/Date datetime/] => sub { uc $_[0] }
2636
+         [qw/date datetime/] => sub { uc $_[0] }
2637 2637
     ]
2638 2638
 );
2639 2639
 $dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
... ...
@@ -2647,7 +2647,7 @@ $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2647 2647
 $dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2648 2648
 $dbi->type_rule(
2649 2649
     into => [
2650
-        [qw/Date datetime/] => sub { uc $_[0] }
2650
+        [qw/date datetime/] => sub { uc $_[0] }
2651 2651
     ]
2652 2652
 );
2653 2653
 $result = $dbi->execute(
... ...
@@ -2663,7 +2663,7 @@ $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2663 2663
 $dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2664 2664
 $dbi->type_rule(
2665 2665
     into => [
2666
-        [qw/Date datetime/] => sub { uc $_[0] }
2666
+        [qw/date datetime/] => sub { uc $_[0] }
2667 2667
     ]
2668 2668
 );
2669 2669
 $result = $dbi->execute(
... ...
@@ -2676,14 +2676,14 @@ is($row->{key1}, 'A');
2676 2676
 is($row->{key2}, 'B');
2677 2677
 
2678 2678
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2679
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2679
+$dbi->execute("create table table1 (key1 date, key2 datetime)");
2680 2680
 $dbi->register_filter(twice => sub { $_[0] * 2 });
2681 2681
 $dbi->type_rule(
2682 2682
     from => {
2683
-        Date => 'twice',
2683
+        date => 'twice',
2684 2684
     },
2685 2685
     into => {
2686
-        Date => 'twice',
2686
+        date => 'twice',
2687 2687
     }
2688 2688
 );
2689 2689
 $dbi->insert({key1 => 2}, table => 'table1');
... ...
@@ -2696,10 +2696,10 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2696 2696
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2697 2697
 $dbi->type_rule(
2698 2698
     from => {
2699
-        Date => sub { $_[0] * 2 },
2699
+        date => sub { $_[0] * 2 },
2700 2700
     },
2701 2701
     into => {
2702
-        Date => sub { $_[0] * 2 },
2702
+        date => sub { $_[0] * 2 },
2703 2703
     }
2704 2704
 );
2705 2705
 $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
... ...
@@ -2711,22 +2711,50 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2711 2711
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2712 2712
 $dbi->type_rule(
2713 2713
     from => {
2714
-        DATE => sub { $_[0] * 2 },
2714
+        date => sub { $_[0] * 2 },
2715 2715
     },
2716 2716
     into => {
2717
-        DATE => sub { $_[0] * 3 },
2717
+        date => sub { $_[0] * 3 },
2718 2718
     }
2719 2719
 );
2720 2720
 $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2721 2721
 $result = $dbi->select(table => 'table1', type_rule_off => 1);
2722
-is($result->one->{key1}, 2);
2722
+is($result->one->{key1}, 4);
2723
+
2724
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2725
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2726
+$dbi->type_rule(
2727
+    from => {
2728
+        date => sub { $_[0] * 2 },
2729
+    },
2730
+    into => {
2731
+        date => sub { $_[0] * 3 },
2732
+    }
2733
+);
2734
+$dbi->insert({key1 => 2}, table => 'table1');
2735
+$result = $dbi->select(table => 'table1');
2736
+is($result->one->{key1}, 12);
2737
+
2738
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2739
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2740
+$dbi->type_rule(
2741
+    from => {
2742
+        date => sub { $_[0] * 2 },
2743
+    },
2744
+    into => {
2745
+        date => sub { $_[0] * 3 },
2746
+    }
2747
+);
2748
+$dbi->insert({key1 => 2}, table => 'table1');
2749
+$result = $dbi->select(table => 'table1');
2750
+is($result->fetch->[0], 12);
2723 2751
 
2724 2752
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2725 2753
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2726 2754
 $dbi->register_filter(ppp => sub { uc $_[0] });
2727 2755
 $dbi->type_rule(
2728 2756
     into => {
2729
-        Date => 'ppp'
2757
+        date => 'ppp'
2730 2758
     }
2731 2759
 );
2732 2760
 $dbi->insert({key1 => 'a'}, table => 'table1');
... ...
@@ -2735,11 +2763,98 @@ is($result->one->{key1}, 'A');
2735 2763
 
2736 2764
 eval{$dbi->type_rule(
2737 2765
     into => {
2738
-        Date => 'pp'
2766
+        date => 'pp'
2739 2767
     }
2740 2768
 )};
2741 2769
 like($@, qr/not registered/);
2742 2770
 
2771
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2772
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2773
+$dbi->type_rule(
2774
+    from => {
2775
+        date => sub { $_[0] * 2 },
2776
+    },
2777
+    into => {
2778
+        date => sub { $_[0] * 3 },
2779
+    }
2780
+);
2781
+$dbi->insert({key1 => 2}, table => 'table1');
2782
+$result = $dbi->select(table => 'table1');
2783
+delete $result->type_rule->{date};
2784
+is($result->one->{key1}, 6);
2785
+
2786
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2787
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2788
+eval {
2789
+    $dbi->type_rule(
2790
+        from => {
2791
+            Date => sub { $_[0] * 2 },
2792
+        }
2793
+    );
2794
+};
2795
+like($@, qr/lower/);
2796
+
2797
+eval {
2798
+    $dbi->type_rule(
2799
+        into => {
2800
+            Date => sub { $_[0] * 2 },
2801
+        }
2802
+    );
2803
+};
2804
+like($@, qr/lower/);
2805
+
2806
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2807
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2808
+$dbi->type_rule(
2809
+    from => {
2810
+        date => sub { $_[0] * 2 },
2811
+    },
2812
+    into => {
2813
+        date => sub { $_[0] * 3 },
2814
+    }
2815
+);
2816
+$dbi->insert({key1 => 2}, table => 'table1');
2817
+$result = $dbi->select(table => 'table1');
2818
+$result->type_rule_off(1);
2819
+is($result->one->{key1}, 6);
2820
+
2821
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2822
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2823
+$dbi->type_rule(
2824
+    from => {
2825
+        date => sub { $_[0] * 2 },
2826
+        datetime => sub { $_[0] * 4 },
2827
+    },
2828
+);
2829
+$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
2830
+$result = $dbi->select(table => 'table1');
2831
+$result->type_rule(date => sub { $_[0] * 3 });
2832
+$row = $result->one;
2833
+is($row->{key1}, 6);
2834
+is($row->{key2}, 8);
2835
+$result = $dbi->select(table => 'table1');
2836
+$result->type_rule({date => sub { $_[0] * 3 }});
2837
+$row = $result->one;
2838
+is($row->{key1}, 6);
2839
+is($row->{key2}, 8);
2840
+$result = $dbi->select(table => 'table1');
2841
+$result->type_rule([date => sub { $_[0] * 3 }]);
2842
+$row = $result->one;
2843
+is($row->{key1}, 6);
2844
+is($row->{key2}, 8);
2845
+$dbi->register_filter(fivetimes => sub { $_[0] * 5});
2846
+$result = $dbi->select(table => 'table1');
2847
+$result->type_rule(date => 'fivetimes');
2848
+$row = $result->one;
2849
+is($row->{key1}, 10);
2850
+is($row->{key2}, 8);
2851
+$result = $dbi->select(table => 'table1');
2852
+$result->type_rule(date => undef);
2853
+$row = $result->one;
2854
+is($row->{key1}, 2);
2855
+is($row->{key2}, 8);
2856
+
2857
+
2743 2858
 test 'result_filter';
2744 2859
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2745 2860
 $dbi->execute($CREATE_TABLE->{0});
+2 -2
t/dbix-custom-mysql-private.t
... ...
@@ -142,12 +142,12 @@ $dbi->each_column(
142 142
 
143 143
 $dbi->type_rule(
144 144
     into => {
145
-        DATE=> sub {
145
+        date=> sub {
146 146
             my $date = shift;
147 147
             $date =~ s/aaaaa//g;
148 148
             return $date;
149 149
         },
150
-        DATETIME => sub {
150
+        datetime => sub {
151 151
             my $date = shift;
152 152
             $date =~ s/ccccc//g;
153 153
             return $date;