Showing 4 changed files with 56 additions and 6 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1687
2
+    - added EXPERIMENTAL type_rule method
1 3
 0.1686
2 4
     - select() column option can receive array reference in array.
3 5
       This is EXPERIMENTAL
+19 -5
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1685';
3
+our $VERSION = '0.1687';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -566,11 +566,12 @@ sub execute {
566 566
         
567 567
         # Result
568 568
         my $result = $self->result_class->new(
569
-            sth            => $sth,
570
-            filters        => $self->filters,
569
+            sth => $sth,
570
+            filters => $self->filters,
571 571
             default_filter => $self->{default_in_filter},
572
-            filter         => $filter->{in} || {},
573
-            end_filter     => $filter->{end} || {}
572
+            filter => $filter->{in} || {},
573
+            end_filter => $filter->{end} || {},
574
+            type_rule => $self->type_rule,
574 575
         );
575 576
 
576 577
         return $result;
... ...
@@ -986,6 +987,19 @@ sub setup_model {
986 987
     return $self;
987 988
 }
988 989
 
990
+sub type_rule {
991
+    my $self = shift;
992
+    
993
+    if (@_) {
994
+        my $type_rule = _array_to_hash([@_]);
995
+        $self->{type_rule} = $type_rule;
996
+        
997
+        return $self;
998
+    }
999
+    
1000
+    return $self->{type_rule} || {};
1001
+}
1002
+
989 1003
 our %UPDATE_ARGS
990 1004
   = map { $_ => 1 } @COMMON_ARGS, qw/param where allow_update_all where_param/;
991 1005
 
+19 -1
lib/DBIx/Custom/Result.pm
... ...
@@ -9,7 +9,7 @@ use Carp 'croak';
9 9
 use DBIx::Custom::Util qw/_array_to_hash _subname/;
10 10
 
11 11
 __PACKAGE__->attr(
12
-    [qw/filters sth/],
12
+    [qw/filters sth type_rule/],
13 13
     stash => sub { {} }
14 14
 );
15 15
 
... ...
@@ -106,8 +106,17 @@ sub fetch {
106 106
     
107 107
     # Filtering
108 108
     my $columns = $self->{sth}->{NAME};
109
+    my $types = $self->{sth}->{TYPE};
110
+    my $type_rule = $self->type_rule || {};
111
+    
109 112
     for (my $i = 0; $i < @$columns; $i++) {
110 113
         
114
+        if ($type_rule->{$types->[$i]} &&
115
+            (my $rule = $type_rule->{$types->[$i]}->{from}))
116
+        {
117
+            $row[$i] = $rule->($row[$i]);
118
+        }
119
+        
111 120
         # Filter name
112 121
         my $column = $columns->[$i];
113 122
         my $f  = exists $filter->{$column}
... ...
@@ -167,8 +176,17 @@ sub fetch_hash {
167 176
     # Filter
168 177
     my $row_hash = {};
169 178
     my $columns = $self->{sth}->{NAME};
179
+    my $types = $self->{sth}->{TYPE};
180
+    my $type_rule = $self->type_rule || {};
170 181
     for (my $i = 0; $i < @$columns; $i++) {
171 182
         
183
+        # Type rule
184
+        if ($type_rule->{$types->[$i]} &&
185
+            (my $rule = $type_rule->{$types->[$i]}->{from}))
186
+        {
187
+            $row->[$i] = $rule->($row->[$i]);
188
+        }
189
+        
172 190
         # Filter name
173 191
         my $column = $columns->[$i];
174 192
         my $f  = exists $filter->{$column}
+16
t/dbix-custom-core-sqlite.t
... ...
@@ -2561,4 +2561,20 @@ $result = $model->select(
2561 2561
 is_deeply($result->one,
2562 2562
           {'table2.key1' => 1, 'table2.key3' => 3});
2563 2563
 
2564
+
2565
+test 'type_rule from';
2566
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2567
+$dbi->type_rule(
2568
+    Date => {
2569
+        from => sub { uc $_[0] }
2570
+    }
2571
+);
2572
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2573
+$dbi->insert({key1 => 'a'}, table => 'table1');
2574
+$result = $dbi->select(table => 'table1');
2575
+is($result->fetch_first->[0], 'A');
2576
+
2577
+$result = $dbi->select(table => 'table1');
2578
+is($result->one->{key1}, 'A');
2579
+
2564 2580
 =cut