Showing 5 changed files with 91 additions and 55 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1689
2
+    - added EXPERIMENTAL available_data_type
3
+    - simplified type_rule
4
+    - changed type_rule arguments format.
1 5
 0.1688
2 6
     - fixed bug that model insert, update, delete select can't
3 7
       odd number arguments
+45 -12
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1688';
3
+our $VERSION = '0.1689';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -1005,7 +1005,7 @@ sub setup_model {
1005 1005
 sub available_data_type {
1006 1006
     my $self = shift;
1007 1007
     
1008
-    my $data_types = "Data Type (Type name)\n";
1008
+    my $data_types = '';
1009 1009
     foreach my $i (-1000 .. 1000) {
1010 1010
          my $type_info = $self->dbh->type_info($i);
1011 1011
          my $data_type = $type_info->{DATA_TYPE};
... ...
@@ -1013,6 +1013,8 @@ sub available_data_type {
1013 1013
          $data_types .= "$data_type ($type_name)\n"
1014 1014
            if defined $data_type;
1015 1015
     }
1016
+    return "Data Type maybe equal to Type Name" unless $data_types;
1017
+    $data_types = "Data Type (Type name)\n" . $data_types;
1016 1018
     return $data_types;
1017 1019
 }
1018 1020
 
... ...
@@ -1020,15 +1022,17 @@ sub type_rule {
1020 1022
     my $self = shift;
1021 1023
     
1022 1024
     if (@_) {
1023
-        my $type_rule = _array_to_hash([@_]);
1025
+        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1026
+        $type_rule->{from} = _array_to_hash($type_rule->{from});
1027
+        $type_rule->{into} = _array_to_hash($type_rule->{into});
1024 1028
         $self->{type_rule} = $type_rule;
1025 1029
         $self->{_into} ||= {};
1026 1030
         $self->each_column(sub {
1027 1031
             my ($dbi, $table, $column, $column_info) = @_;
1028 1032
             
1029 1033
             my $type = $column_info->{TYPE_NAME};
1030
-            if ($type_rule->{$type} &&
1031
-                (my $rule = $type_rule->{$type}->{into}))
1034
+            if ($type_rule->{into} &&
1035
+                (my $rule = $type_rule->{into}->{$type}))
1032 1036
             {
1033 1037
                 $self->{_into}{$table}{$column} = $rule;
1034 1038
             }
... ...
@@ -1845,6 +1849,12 @@ L<DBIx::Custom> inherits all methods from L<Object::Simple>
1845 1849
 and use all methods of L<DBI>
1846 1850
 and implements the following new ones.
1847 1851
 
1852
+=head2 C<available_data_type> EXPERIMENTAL
1853
+
1854
+    print $dbi->available_data_type;
1855
+
1856
+Get available data type.
1857
+
1848 1858
 =head2 C<apply_filter>
1849 1859
 
1850 1860
     $dbi->apply_filter(
... ...
@@ -2366,17 +2376,40 @@ Register filters, used by C<filter> option of many methods.
2366 2376
 =head2 C<type_rule> EXPERIMENTAL
2367 2377
 
2368 2378
     $dbi->type_rule(
2369
-        DATE => {
2370
-            from => sub { ... },
2371
-            into => sub { ... }
2379
+        into => {
2380
+            DATE => sub { ... },
2381
+            DATETIME => sub { ... }
2372 2382
         },
2373
-        DATETIME => {
2374
-            from => sub { ... }
2375
-            into => sub { ... }
2383
+        from => {
2384
+            # DATE
2385
+            9 => sub { ... },
2386
+            
2387
+            # DATETIME or TIMESTAMP
2388
+            11 => sub { ... },
2376 2389
         }
2377 2390
     );
2378 2391
 
2379
-Filter based on type.
2392
+Filtering rule when data is send into and get from database.
2393
+This has a little complex problem. 
2394
+In C<into> you can specify type name as same as type name defined
2395
+by create table, such as C<DATETIME> or C<DATE>.
2396
+but in C<from> you can't specify type name defined by create table.
2397
+You must specify data type, this is internal one.
2398
+You get all data type by C<available_data_type>.
2399
+
2400
+    print $dbi->available_data_type;
2401
+
2402
+You can also specify multiple types
2403
+
2404
+    $dbi->type_rule(
2405
+        into => [
2406
+            [qw/DATE DATETIME/] => sub { ... },
2407
+        ],
2408
+        from => {
2409
+            # DATE
2410
+            [qw/9 11/] => sub { ... },
2411
+        }
2412
+    );
2380 2413
 
2381 2414
 =head2 C<select>
2382 2415
 
+4 -4
lib/DBIx/Custom/Result.pm
... ...
@@ -111,8 +111,8 @@ sub fetch {
111 111
     
112 112
     for (my $i = 0; $i < @$columns; $i++) {
113 113
         
114
-        if (!$self->type_rule_off && $type_rule->{$types->[$i]} &&
115
-            (my $rule = $type_rule->{$types->[$i]}->{from}))
114
+        if (!$self->type_rule_off && $type_rule->{from} &&
115
+            (my $rule = $type_rule->{from}->{$types->[$i]}))
116 116
         {
117 117
             $row[$i] = $rule->($row[$i]);
118 118
         }
... ...
@@ -181,8 +181,8 @@ sub fetch_hash {
181 181
     for (my $i = 0; $i < @$columns; $i++) {
182 182
         
183 183
         # Type rule
184
-        if (!$self->type_rule_off && $type_rule->{$types->[$i]} &&
185
-            (my $rule = $type_rule->{$types->[$i]}->{from}))
184
+        if (!$self->type_rule_off && $type_rule->{from} &&
185
+            (my $rule = $type_rule->{from}->{$types->[$i]}))
186 186
         {
187 187
             $row->[$i] = $rule->($row->[$i]);
188 188
         }
+23 -19
t/dbix-custom-core-sqlite.t
... ...
@@ -2583,8 +2583,8 @@ is_deeply($result->one,
2583 2583
 test 'type_rule from';
2584 2584
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2585 2585
 $dbi->type_rule(
2586
-    Date => {
2587
-        from => sub { uc $_[0] }
2586
+    from => {
2587
+        Date => sub { uc $_[0] }
2588 2588
     }
2589 2589
 );
2590 2590
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
... ...
@@ -2600,8 +2600,8 @@ test 'type_rule into';
2600 2600
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2601 2601
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2602 2602
 $dbi->type_rule(
2603
-    Date => {
2604
-        into => sub { uc $_[0] }
2603
+    into => {
2604
+        Date => sub { uc $_[0] }
2605 2605
     }
2606 2606
 );
2607 2607
 $dbi->insert({key1 => 'a'}, table => 'table1');
... ...
@@ -2611,9 +2611,9 @@ is($result->one->{key1}, 'A');
2611 2611
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2612 2612
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2613 2613
 $dbi->type_rule(
2614
-    [qw/Date datetime/] => {
2615
-        into => sub { uc $_[0] }
2616
-    }
2614
+    into => [
2615
+         [qw/Date datetime/] => sub { uc $_[0] }
2616
+    ]
2617 2617
 );
2618 2618
 $dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2619 2619
 $result = $dbi->select(table => 'table1');
... ...
@@ -2625,9 +2625,9 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2625 2625
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2626 2626
 $dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2627 2627
 $dbi->type_rule(
2628
-    [qw/Date datetime/] => {
2629
-        into => sub { uc $_[0] }
2630
-    }
2628
+    into => [
2629
+        [qw/Date datetime/] => sub { uc $_[0] }
2630
+    ]
2631 2631
 );
2632 2632
 $result = $dbi->execute(
2633 2633
     "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
... ...
@@ -2641,9 +2641,9 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2641 2641
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2642 2642
 $dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2643 2643
 $dbi->type_rule(
2644
-    [qw/Date datetime/] => {
2645
-        into => sub { uc $_[0] }
2646
-    }
2644
+    into => [
2645
+        [qw/Date datetime/] => sub { uc $_[0] }
2646
+    ]
2647 2647
 );
2648 2648
 $result = $dbi->execute(
2649 2649
     "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
... ...
@@ -2659,9 +2659,11 @@ test 'type_rule_off';
2659 2659
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2660 2660
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2661 2661
 $dbi->type_rule(
2662
-    Date => {
2663
-        from => sub { $_[0] * 2 },
2664
-        into => sub { $_[0] * 3 },
2662
+    from => {
2663
+        Date => sub { $_[0] * 2 },
2664
+    },
2665
+    into => {
2666
+        Date => sub { $_[0] * 2 },
2665 2667
     }
2666 2668
 );
2667 2669
 $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
... ...
@@ -2671,9 +2673,11 @@ is($result->fetch->[0], 2);
2671 2673
 $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
2672 2674
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
2673 2675
 $dbi->type_rule(
2674
-    Date => {
2675
-        from => sub { $_[0] * 2 },
2676
-        into => sub { $_[0] * 3 },
2676
+    from => {
2677
+        DATE => sub { $_[0] * 2 },
2678
+    },
2679
+    into => {
2680
+        DATE => sub { $_[0] * 3 },
2677 2681
     }
2678 2682
 );
2679 2683
 $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
+15 -20
t/dbix-custom-mysql-private.t
... ...
@@ -135,35 +135,30 @@ $dbi = DBIx::Custom->connect(
135 135
 );
136 136
 eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
137 137
 $dbi->type_rule(
138
-    DATE => {
139
-        into=> sub {
138
+    into => {
139
+        DATE=> sub {
140 140
             my $date = shift;
141 141
             $date =~ s/aaaaa//g;
142 142
             return $date;
143 143
         },
144
-    },
145
-    DATETIME => {
146
-        into => sub {
144
+        DATETIME => sub {
147 145
             my $date = shift;
148 146
             $date =~ s/ccccc//g;
149 147
             return $date;
150
-            
151 148
         },
152 149
     },
153
-    # DATE
154
-    9 => {
155
-        from => sub {
156
-            my $date = shift;
157
-            $date .= 'bbbbb';
158
-            return $date;
159
-        }
160
-    },
161
-    # DATETIME or TIMPESTANM
162
-    11 => {
163
-        from => sub {
164
-            my $date = shift;
165
-            $date .= 'ddddd';
166
-            return $date;
150
+    from => {
151
+        # DATE
152
+        9 => sub {
153
+                my $date = shift;
154
+                $date .= 'bbbbb';
155
+                return $date;
156
+        },
157
+        # DATETIME or TIMPESTANM
158
+        11 => sub {
159
+                my $date = shift;
160
+                $date .= 'ddddd';
161
+                return $date;
167 162
         }
168 163
     }
169 164
 );