Showing 3 changed files with 66 additions and 0 deletions
+1
Changes
... ...
@@ -1,6 +1,7 @@
1 1
 0.1688
2 2
     - fixed bug that model insert, update, delete select can't
3 3
       odd number arguments
4
+    - improved type_rule method
4 5
 0.1687
5 6
     - added EXPERIMENTAL type_rule method
6 7
     - added EXPERIMENTAL execute() type_rule_off option
+13
lib/DBIx/Custom.pm
... ...
@@ -1009,6 +1009,19 @@ sub type_rule {
1009 1009
         my $type_rule = _array_to_hash([@_]);
1010 1010
         $self->{type_rule} = $type_rule;
1011 1011
         $self->{_into} ||= {};
1012
+
1013
+        foreach my $i (-1000 .. 1000) {
1014
+             my $type_info = $self->dbh->type_info($i);
1015
+             my $data_type = $type_info->{DATA_TYPE};
1016
+             my $type_name = $type_info->{TYPE_NAME};
1017
+             foreach my $type (keys %$type_rule) {
1018
+                 use Data::Dumper;
1019
+                 if ($type_name && lc $type eq lc $type_name) {
1020
+                     $type_rule->{$data_type} = $type_rule->{$type};
1021
+                 }
1022
+             }
1023
+        }
1024
+
1012 1025
         $self->each_column(sub {
1013 1026
             my ($dbi, $table, $column, $column_info) = @_;
1014 1027
             
+52
t/dbix-custom-mysql-private.t
... ...
@@ -126,6 +126,58 @@ $rows = $dbi->select(
126 126
 is_deeply($rows, [{key1 => 1, key2 => 2}]);
127 127
 $dbi->delete_all(table => 'table1');
128 128
 
129
+
130
+test 'type_rule';
131
+$dbi = DBIx::Custom->connect(
132
+    dsn => "dbi:mysql:database=$DATABASE",
133
+    user => $USER,
134
+    password => $PASSWORD
135
+);
136
+eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
137
+$dbi->type_rule(
138
+    DATE => {
139
+        into=> sub {
140
+            my $date = shift;
141
+            $date =~ s/aaaaa//g;
142
+            return $date;
143
+        },
144
+        from => sub {
145
+            my $date = shift;
146
+            $date .= 'bbbbb';
147
+            return $date;
148
+        }
149
+    },
150
+    'DATETIME' => {
151
+        into => sub {
152
+            my $date = shift;
153
+            $date =~ s/ccccc//g;
154
+            return $date;
155
+            
156
+        },
157
+    },
158
+    'TIMESTAMP' => {
159
+        from => sub {
160
+            my $date = shift;
161
+            $date .= 'ddddd';
162
+            return $date;
163
+        }
164
+    }
165
+);
166
+
167
+$dbi->insert(
168
+    {
169
+        date => 'aaaaa2010-aaaaa11-12aaaaa',
170
+        datetime => '2010-11ccccc-12 10:ccccc55:56'
171
+    },
172
+    table => 'date_test'
173
+);
174
+is_deeply(
175
+    $dbi->select(table => 'date_test')->fetch,
176
+    ['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd']
177
+);
178
+
179
+$dbi->execute("drop table date_test");
180
+
129 181
 test 'dbh';
130 182
 {
131 183
     my $connector = DBIx::Connector->new(