Showing 4 changed files with 127 additions and 75 deletions
+5
Changes
... ...
@@ -1,3 +1,8 @@
1
+0.1747
2
+    - fixed bug DBIx::Custom::Result fetch_hash_multi throw warnings
3
+      which cannnot fetch any more
4
+    - fixed bug DBIx::Custom::Result fetch_hash_multi throw warnings
5
+      which cannnot fetch any more
1 6
 0.1746
2 7
     - micro optimization
3 8
 0.1745
+10 -1
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package DBIx::Custom;
2 2
 use Object::Simple -base;
3 3
 
4
-our $VERSION = '0.1745';
4
+our $VERSION = '0.1747';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -3401,6 +3401,15 @@ executed SQL and bind values are printed to STDERR.
3401 3401
 
3402 3402
 DEBUG output encoding. Default to UTF-8.
3403 3403
 
3404
+=head2 C<DBIX_CUSTOM_TAG_PARSE>
3405
+
3406
+If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off.
3407
+
3408
+=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE>
3409
+
3410
+If you set DBIX_CUSTOM_DISABLE_MODEL_EXECUTE to 1,
3411
+L<DBIx::Custom::Model> execute method call L<DBIx::Custom> execute.
3412
+
3404 3413
 =head1 DEPRECATED FUNCTIONALITY
3405 3414
 
3406 3415
 L<DBIx::Custom>
+48 -73
lib/DBIx/Custom/Result.pm
... ...
@@ -39,24 +39,6 @@ sub filter {
39 39
     return $self->{filter} ||= {};
40 40
 }
41 41
 
42
-sub _cache {
43
-    my $self = shift;
44
-    $self->{_type_map} = {};
45
-    $self->{_pos} = {};
46
-    $self->{_columns} = {};
47
-    for (my $i = 0; $i < @{$self->{sth}->{NAME}}; $i++) {
48
-        my $type = lc $self->{sth}{TYPE}[$i];
49
-        my $name = $self->{sth}{NAME}[$i];
50
-        $self->{_type_map}{$type} ||= [];
51
-        push @{$self->{_type_map}{$type}}, $name;
52
-        $self->{_pos}{$name} ||= [];
53
-        push @{$self->{_pos}{$name}}, $i;
54
-        $self->{_columns}{$name} = 1;
55
-    }
56
-    $self->{_cache} = 1;
57
-}
58
-
59
-=pod
60 42
 sub fetch {
61 43
     my $self = shift;
62 44
     
... ...
@@ -68,41 +50,48 @@ sub fetch {
68 50
     return unless @row;
69 51
     
70 52
     # Type rule
71
-    if ((my $from = $self->type_rule->{from1}) && !$self->{type_rule_off} && !$self->{type_rule1_off}) {
53
+    if ($self->{type_rule}->{from1} && !$self->{type_rule_off} && !$self->{type_rule1_off}) {
54
+        my $from = $self->{type_rule}->{from1};
72 55
         for my $type (keys %$from) {
73 56
             for my $column (@{$self->{_type_map}->{$type}}) {
74 57
                 $row[$_] = $from->{$type}->($row[$_])
75
-                  for @{$self->{_pos}{$column}};
58
+                  for @{$self->{_pos}{$column} || []};
76 59
             }
77 60
         }
78 61
     }
79
-    if ((my $from = $self->type_rule->{from2}) && !$self->{type_rule_off} && !$self->{type_rule2_off}) {
62
+    if ($self->{type_rule}->{from2} && !$self->{type_rule_off} && !$self->{type_rule2_off}) {
63
+        my $from = $self->{type_rule}->{from2};
80 64
         for my $type (keys %$from) {
81 65
             for my $column (@{$self->{_type_map}->{$type}}) {
82 66
                 $row[$_] = $from->{$type}->($row[$_])
83
-                  for @{$self->{_pos}{$column}};
67
+                  for @{$self->{_pos}{$column} || []};
84 68
             }
85 69
         }
86 70
     }
87 71
     
88 72
     # Filter
89 73
     if (($self->{filter} || $self->{default_filter}) && !$self->{filter_off}) {
90
-        for my $column (keys %{$self->{filter}}) {
91
-            $row[$_] = ($self->{filter}->{$column} || $self->{default_filter} || sub { shift })
92
-                ->($row[$_])
93
-              for @{$self->{_pos}{$column}};
94
-        }
74
+         my @columns = $self->{default_filter} ? keys %{$self->{_columns}}
75
+           : keys %{$self->{filter}};
76
+         
77
+         for my $column (@columns) {
78
+             my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column}
79
+               : $self->{default_filter};
80
+             next unless $filter;
81
+             $row[$_] = $filter->($row[$_])
82
+               for @{$self->{_pos}{$column} || []};
83
+         }
95 84
     }
96 85
     if ($self->{end_filter} && !$self->{filter_off}) {
97 86
          for my $column (keys %{$self->{end_filter}}) {
87
+             next unless $self->{end_filter}->{$column};
98 88
              $row[$_] = $self->{end_filter}->{$column}->($row[$_])
99
-               for @{$self->{_pos}{$column}};
89
+               for @{$self->{_pos}{$column} || []};
100 90
          }
101 91
     }
102 92
 
103 93
     return \@row;
104 94
 }
105
-=cut
106 95
 
107 96
 sub fetch_hash {
108 97
     my $self = shift;
... ...
@@ -154,48 +143,6 @@ sub fetch_hash {
154 143
     $row;
155 144
 }
156 145
 
157
-sub fetch {
158
-    my $self = shift;
159
-    
160
-    # Info
161
-    my $columns = $self->{sth}->{NAME};
162
-    my $types = $self->{sth}->{TYPE};
163
-    
164
-    # Fetch
165
-    my @row = $self->{sth}->fetchrow_array;
166
-    return unless @row;
167
-    
168
-    # Filtering
169
-    my $type_rule1 = $self->type_rule->{from1} || {};
170
-    my $type_rule2 = $self->type_rule->{from2} || {};
171
-    my $filter = $self->filter;
172
-    my $end_filter = $self->{end_filter} || {};
173
-    for (my $i = 0; $i < @$columns; $i++) {
174
-        
175
-        # Column
176
-        my $column = $columns->[$i];
177
-        
178
-        # Type rule
179
-        my $type_filter1 = $type_rule1->{lc($types->[$i])};
180
-        $row[$i] = $type_filter1->($row[$i])
181
-          if  $type_filter1 && !$self->{type_rule_off}
182
-           && !$self->{type_rule1_off};
183
-        my $type_filter2 = $type_rule2->{lc($types->[$i])};
184
-        $row[$i] = $type_filter2->($row[$i])
185
-          if  $type_filter2 && !$self->{type_rule_off}
186
-           && !$self->{type_rule2_off};
187
-        
188
-        # Filter
189
-        my $filter  = $filter->{$column} || $self->{default_filter};
190
-        $row[$i] = $filter->($row[$i])
191
-          if $filter && !$self->{filter_off};
192
-        $row[$i] = $end_filter->{$column}->($row[$i])
193
-          if $end_filter->{$column} && !$self->{filter_off};
194
-    }
195
-
196
-    return \@row;
197
-}
198
-
199 146
 sub fetch_all {
200 147
     my $self = shift;
201 148
     
... ...
@@ -248,10 +195,16 @@ sub fetch_hash_multi {
248 195
     # Fetch multiple rows
249 196
     croak 'Row count must be specified ' . _subname
250 197
       unless $count;
198
+    
199
+    return if $self->{_finished};
200
+
251 201
     my $rows = [];
252 202
     for (my $i = 0; $i < $count; $i++) {
253 203
         my $row = $self->fetch_hash;
254
-        last unless $row;
204
+        unless ($row) {
205
+            $self->{_finished} = 1;
206
+            last;
207
+        }
255 208
         push @$rows, $row;
256 209
     }
257 210
     
... ...
@@ -266,11 +219,16 @@ sub fetch_multi {
266 219
     croak 'Row count must be specified ' . _subname
267 220
       unless $count;
268 221
     
222
+    return if $self->{_finished};
223
+    
269 224
     # Fetch multi rows
270 225
     my $rows = [];
271 226
     for (my $i = 0; $i < $count; $i++) {
272 227
         my $row = $self->fetch;
273
-        last unless $row;
228
+        unless ($row) {
229
+            $self->{_finished} = 1;
230
+            last;
231
+        }
274 232
         push @$rows, $row;
275 233
     }
276 234
     
... ...
@@ -347,6 +305,23 @@ sub type_rule2_on {
347 305
     return $self;
348 306
 }
349 307
 
308
+sub _cache {
309
+    my $self = shift;
310
+    $self->{_type_map} = {};
311
+    $self->{_pos} = {};
312
+    $self->{_columns} = {};
313
+    for (my $i = 0; $i < @{$self->{sth}->{NAME}}; $i++) {
314
+        my $type = lc $self->{sth}{TYPE}[$i];
315
+        my $name = $self->{sth}{NAME}[$i];
316
+        $self->{_type_map}{$type} ||= [];
317
+        push @{$self->{_type_map}{$type}}, $name;
318
+        $self->{_pos}{$name} ||= [];
319
+        push @{$self->{_pos}{$name}}, $i;
320
+        $self->{_columns}{$name} = 1;
321
+    }
322
+    $self->{_cache} = 1;
323
+}
324
+
350 325
 # DEPRECATED!
351 326
 sub filter_off {
352 327
     warn "filter_off method is DEPRECATED!";
+64 -1
t/common.t
... ...
@@ -1113,6 +1113,15 @@ $result->filter({$key1 => 'three_times'});
1113 1113
 $row = $result->one;
1114 1114
 is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter");
1115 1115
 
1116
+$dbi->default_fetch_filter('twice');
1117
+eval { $dbi->execute("drop table $table1") };
1118
+$dbi->execute($create_table1);
1119
+$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
1120
+$result = $dbi->select(column => [$key1, $key1, $key2], table => $table1);
1121
+$result->filter({$key1 => 'three_times'});
1122
+$row = $result->fetch_first;
1123
+is_deeply($row, [3, 3, 4], "default_fetch_filter and filter");
1124
+
1116 1125
 test 'filters';
1117 1126
 $dbi = DBIx::Custom->new;
1118 1127
 
... ...
@@ -1449,6 +1458,16 @@ $result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
1449 1458
 $row = $result->fetch_first;
1450 1459
 is_deeply($row, [6, 40]);
1451 1460
 
1461
+$dbi = DBIx::Custom->connect;
1462
+eval { $dbi->execute("drop table $table1") };
1463
+$dbi->execute($create_table1);
1464
+$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
1465
+$result = $dbi->select(column => [$key1, $key1, $key2], table => $table1);
1466
+$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1467
+$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 });
1468
+$row = $result->fetch_first;
1469
+is_deeply($row, [6, 6, 40]);
1470
+
1452 1471
 $dbi = DBIx::Custom->connect;
1453 1472
 eval { $dbi->execute("drop table $table1") };
1454 1473
 $dbi->execute($create_table1);
... ...
@@ -1498,6 +1517,13 @@ $result->end_filter($key1 => undef);
1498 1517
 $row = $result->one;
1499 1518
 is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite');
1500 1519
 
1520
+$result = $dbi->select(column => [$key1, $key1, $key2], table => $table1);
1521
+$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 });
1522
+$result->filter($key1 => undef);
1523
+$result->end_filter($key1 => undef);
1524
+$row = $result->fetch;
1525
+is_deeply($row, [1, 1, 40], 'apply_filter overwrite');
1526
+
1501 1527
 test 'remove_end_filter and remove_filter';
1502 1528
 $dbi = DBIx::Custom->connect;
1503 1529
 eval { $dbi->execute("drop table $table1") };
... ...
@@ -3279,16 +3305,49 @@ is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
3279 3305
 $result = $dbi->select(table => $table1);
3280 3306
 $result->dbi->filters({three_times => sub { $_[0] * 3}});
3281 3307
 $result->filter({$key1 => 'three_times'});
3282
-
3283 3308
 $rows = $result->fetch_all;
3284 3309
 is_deeply($rows, [[3, 2], [9, 4]], "array");
3285 3310
 
3311
+$result = $dbi->select(column => [$key1, $key1, $key2], table => $table1);
3312
+$result->dbi->filters({three_times => sub { $_[0] * 3}});
3313
+$result->filter({$key1 => 'three_times'});
3314
+$rows = $result->fetch_all;
3315
+is_deeply($rows, [[3, 3, 2], [9, 9, 4]], "array");
3316
+
3286 3317
 $result = $dbi->select(table => $table1);
3287 3318
 $result->dbi->filters({three_times => sub { $_[0] * 3}});
3288 3319
 $result->filter({$key1 => 'three_times'});
3289 3320
 $rows = $result->fetch_hash_all;
3290 3321
 is_deeply($rows, [{$key1 => 3, $key2 => 2}, {$key1 => 9, $key2 => 4}], "hash");
3291 3322
 
3323
+test 'DBIx::Custom::Result fetch_multi';
3324
+eval { $dbi->execute("drop table $table1") };
3325
+$dbi->execute($create_table1);
3326
+$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3327
+$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
3328
+$dbi->insert({$key1 => 5, $key2 => 6}, table => $table1);
3329
+$result = $dbi->select(table => $table1);
3330
+$rows = $result->fetch_multi(2);
3331
+is_deeply($rows, [[1, 2], [3, 4]]);
3332
+$rows = $result->fetch_multi(2);
3333
+is_deeply($rows, [[5, 6]]);
3334
+$rows = $result->fetch_multi(2);
3335
+ok(!$rows);
3336
+
3337
+test 'DBIx::Custom::Result fetch_hash_multi';
3338
+eval { $dbi->execute("drop table $table1") };
3339
+$dbi->execute($create_table1);
3340
+$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1);
3341
+$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1);
3342
+$dbi->insert({$key1 => 5, $key2 => 6}, table => $table1);
3343
+$result = $dbi->select(table => $table1);
3344
+$rows = $result->fetch_hash_multi(2);
3345
+is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
3346
+$rows = $result->fetch_hash_multi(2);
3347
+is_deeply($rows, [{$key1 => 5, $key2 => 6}]);
3348
+$rows = $result->fetch_hash_multi(2);
3349
+ok(!$rows);
3350
+
3292 3351
 test "query_builder";
3293 3352
 $datas = [
3294 3353
     # Basic tests
... ...
@@ -3905,6 +3964,10 @@ $dbi->type_rule(
3905 3964
 $dbi->insert({$key1 => '2010-02-02'}, table => $table1);
3906 3965
 $result = $dbi->select(table => $table1);
3907 3966
 like($result->fetch->[0], qr/^2010-03-03/);
3967
+$result = $dbi->select(column => [$key1, $key1], table => $table1);
3968
+$row = $result->fetch;
3969
+like($row->[0], qr/^2010-03-03/);
3970
+like($row->[1], qr/^2010-03-03/);
3908 3971
 
3909 3972
 test 'type_rule and filter order';
3910 3973
 $dbi = DBIx::Custom->connect;