fixed bug that type_rule from option can't receive...
...filter name
... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1691 |
|
2 |
+ - fixed bug that type_rule from option can't receive filter name |
|
1 | 3 |
0.1690 |
2 | 4 |
- use latest Object::Simple features |
3 | 5 |
0.1689 |
... | ... |
@@ -1028,18 +1028,19 @@ sub type_rule { |
1028 | 1028 |
|
1029 | 1029 |
if (@_) { |
1030 | 1030 |
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
1031 |
- $type_rule->{from} = _array_to_hash($type_rule->{from}); |
|
1031 |
+ |
|
1032 |
+ # Into |
|
1032 | 1033 |
$type_rule->{into} = _array_to_hash($type_rule->{into}); |
1033 | 1034 |
$self->{type_rule} = $type_rule; |
1034 | 1035 |
$self->{_into} ||= {}; |
1035 | 1036 |
$self->each_column(sub { |
1036 | 1037 |
my ($dbi, $table, $column, $column_info) = @_; |
1037 | 1038 |
|
1038 |
- my $type = $column_info->{TYPE_NAME}; |
|
1039 |
+ my $type_name = $column_info->{TYPE_NAME}; |
|
1039 | 1040 |
if ($type_rule->{into} && |
1040 |
- (my $filter = $type_rule->{into}->{$type})) |
|
1041 |
+ (my $filter = $type_rule->{into}->{$type_name})) |
|
1041 | 1042 |
{ |
1042 |
- return unless exists $type_rule->{into}->{$type}; |
|
1043 |
+ return unless exists $type_rule->{into}->{$type_name}; |
|
1043 | 1044 |
if (defined $filter && ref $filter ne 'CODE') |
1044 | 1045 |
{ |
1045 | 1046 |
my $fname = $filter; |
... | ... |
@@ -1053,6 +1054,19 @@ sub type_rule { |
1053 | 1054 |
} |
1054 | 1055 |
}); |
1055 | 1056 |
|
1057 |
+ |
|
1058 |
+ # From |
|
1059 |
+ $type_rule->{from} = _array_to_hash($type_rule->{from}); |
|
1060 |
+ foreach my $data_type (keys %{$type_rule->{from} || {}}) { |
|
1061 |
+ my $fname = $type_rule->{from}{$data_type}; |
|
1062 |
+ if (defined $fname && ref $fname ne 'CODE') { |
|
1063 |
+ croak qq{Filter "$fname" is not registered" } . _subname |
|
1064 |
+ unless exists $self->filters->{$fname}; |
|
1065 |
+ |
|
1066 |
+ $type_rule->{from}{$data_type} = $self->filters->{$fname}; |
|
1067 |
+ } |
|
1068 |
+ } |
|
1069 |
+ |
|
1056 | 1070 |
return $self; |
1057 | 1071 |
} |
1058 | 1072 |
|
... | ... |
@@ -2654,6 +2654,21 @@ $row = $result->one; |
2654 | 2654 |
is($row->{key1}, 'A'); |
2655 | 2655 |
is($row->{key2}, 'B'); |
2656 | 2656 |
|
2657 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
2658 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
2659 |
+$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
2660 |
+$dbi->type_rule( |
|
2661 |
+ from => { |
|
2662 |
+ Date => 'twice', |
|
2663 |
+ }, |
|
2664 |
+ into => { |
|
2665 |
+ Date => 'twice', |
|
2666 |
+ } |
|
2667 |
+); |
|
2668 |
+$dbi->insert({key1 => 2}, table => 'table1'); |
|
2669 |
+$result = $dbi->select(table => 'table1'); |
|
2670 |
+is($result->fetch->[0], 8); |
|
2671 |
+ |
|
2657 | 2672 |
|
2658 | 2673 |
test 'type_rule_off'; |
2659 | 2674 |
$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
... | ... |
@@ -2789,4 +2804,9 @@ $result->end_filter(key1 => sub { $_[0] * 5}); |
2789 | 2804 |
is_deeply($result->fetch_first, |
2790 | 2805 |
[1, 2, 1, 3]); |
2791 | 2806 |
|
2807 |
+ |
|
2808 |
+test 'available_date_type'; |
|
2809 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
2810 |
+ok($dbi->can('available_data_type')); |
|
2811 |
+ |
|
2792 | 2812 |
=cut |
... | ... |
@@ -134,6 +134,12 @@ $dbi = DBIx::Custom->connect( |
134 | 134 |
password => $PASSWORD |
135 | 135 |
); |
136 | 136 |
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")}; |
137 |
+$dbi->each_column( |
|
138 |
+ sub { |
|
139 |
+ my ($self, $table, $column, $column_info) = @_; |
|
140 |
+ } |
|
141 |
+); |
|
142 |
+ |
|
137 | 143 |
$dbi->type_rule( |
138 | 144 |
into => { |
139 | 145 |
DATE=> sub { |