Showing 2 changed files with 39 additions and 18 deletions
+23 -17
lib/DBIx/Custom.pm
... ...
@@ -384,7 +384,7 @@ sub execute {
384 384
     }
385 385
     
386 386
     # Type rule
387
-    my $applied_filter = {};
387
+    my $type_filter = {};
388 388
     unless ($type_rule_off) {
389 389
         foreach my $name (keys %$param) {
390 390
             my $table;
... ...
@@ -399,13 +399,14 @@ sub execute {
399 399
             if (defined $table && $into->{$table} &&
400 400
                 (my $rule = $into->{$table}->{$column}))
401 401
             {
402
-                $applied_filter->{$column} = $rule;
403
-                $applied_filter->{"$table.$column"} = $rule;
402
+                $type_filter->{$column} = $rule;
403
+                $type_filter->{"$table.$column"} = $rule;
404 404
             }
405 405
         }
406 406
     }
407 407
     
408 408
     # Applied filter
409
+    my $applied_filter = {};
409 410
     foreach my $table (@$tables) {
410 411
         $applied_filter = {
411 412
             %$applied_filter,
... ...
@@ -432,6 +433,7 @@ sub execute {
432 433
         $param,
433 434
         $query->columns,
434 435
         $filter,
436
+        $type_filter,
435 437
         $type
436 438
     );
437 439
     
... ...
@@ -1171,7 +1173,7 @@ sub _apply_filter {
1171 1173
 }
1172 1174
 
1173 1175
 sub _create_bind_values {
1174
-    my ($self, $params, $columns, $filter, $type) = @_;
1176
+    my ($self, $params, $columns, $filter, $type_filter, $type) = @_;
1175 1177
     
1176 1178
     # Create bind values
1177 1179
     my $bind = [];
... ...
@@ -1201,12 +1203,14 @@ sub _create_bind_values {
1201 1203
         
1202 1204
         # Filter
1203 1205
         my $f = $filter->{$column} || $self->{default_out_filter} || '';
1206
+        $value = $f->($value) if $f;
1207
+        
1208
+        # Type rule
1209
+        my $tf = $type_filter->{$column};
1210
+        $value = $tf->($value) if $tf;
1204 1211
         
1205
-        # Type
1206
-        push @$bind, {
1207
-            value => $f ? $f->($value) : $value,
1208
-            type => $type->{$column}
1209
-        };
1212
+        # Bind values
1213
+        push @$bind, {value => $value, type => $type->{$column}};
1210 1214
         
1211 1215
         # Count up 
1212 1216
         $count->{$column}++;
... ...
@@ -2051,11 +2055,10 @@ The following opitons are available.
2051 2055
         [qw/title author/]  => sub { uc $_[0] }
2052 2056
     ]
2053 2057
 
2054
-Filter, executed before data is saved into database.
2055
-Filter value is code reference or
2056
-filter name registerd by C<register_filter()>.
2057
-
2058
-These filters are added to the C<out> filters, set by C<apply_filter()>.
2058
+Filter. You can set subroutine or filter name
2059
+registered by by C<register_filter()>.
2060
+This filter is executed before data is saved into database.
2061
+and before type rule filter is executed.
2059 2062
 
2060 2063
 =item C<query>
2061 2064
 
... ...
@@ -2389,8 +2392,8 @@ Register filters, used by C<filter> option of many methods.
2389 2392
 
2390 2393
     $dbi->type_rule(
2391 2394
         into => {
2392
-            DATE => sub { ... },
2393
-            DATETIME => sub { ... }
2395
+            date => sub { ... },
2396
+            datetime => sub { ... }
2394 2397
         },
2395 2398
         from => {
2396 2399
             # DATE
... ...
@@ -2402,12 +2405,15 @@ Register filters, used by C<filter> option of many methods.
2402 2405
     );
2403 2406
 
2404 2407
 Filtering rule when data is send into and get from database.
2405
-This has a little complex problem. 
2408
+This has a little complex problem.
2406 2409
 
2407 2410
 In C<into> you can specify type name as same as type name defined
2408 2411
 by create table, such as C<DATETIME> or C<DATE>.
2409 2412
 Type rule of C<into> is enabled on the following pattern.
2410 2413
 
2414
+Note that type name and data type don't contain upper case.
2415
+If that contain upper case charactor, you specify it lower case.
2416
+
2411 2417
 =over 4
2412 2418
 
2413 2419
 =item 1. column name
+16 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -6,7 +6,7 @@ use utf8;
6 6
 use Encode qw/encode_utf8 decode_utf8/;
7 7
 use Data::Dumper;
8 8
 
9
-#$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
9
+$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
10 10
 
11 11
 BEGIN {
12 12
     eval { require DBD::SQLite; 1 }
... ...
@@ -2690,6 +2690,21 @@ $dbi->insert({key1 => 2}, table => 'table1');
2690 2690
 $result = $dbi->select(table => 'table1');
2691 2691
 is($result->fetch->[0], 8);
2692 2692
 
2693
+test 'type_rule and filter order';
2694
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2695
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2696
+$dbi->type_rule(
2697
+    into => {
2698
+        date => sub { $_[0] . 'b' }
2699
+    },
2700
+    from => {
2701
+        date => sub { $_[0] . 'c' }
2702
+    }
2703
+);
2704
+$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2705
+$result = $dbi->select(table => 'table1');
2706
+$result->filter(key1 => sub { $_[0] . 'd' });
2707
+is($result->fetch_first->[0], '1abcd');
2693 2708
 
2694 2709
 test 'type_rule_off';
2695 2710
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');