Showing 2 changed files with 27 additions and 17 deletions
+18 -6
lib/DBI/Custom.pm
... ...
@@ -14,9 +14,8 @@ sub user        : ClassObjectAttr { initialize => {clone => 'scalar'} }
14 14
 sub password    : ClassObjectAttr { initialize => {clone => 'scalar'} }
15 15
 sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} }
16 16
 sub database    : ClassObjectAttr { initialize => {clone => 'scalar'} }
17
-
18 17
 sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', 
19
-                                                  default => sub { {} } } }
18
+                                                   default => sub { {} } } }
20 19
 
21 20
 sub bind_filter  : ClassObjectAttr { initialize => {clone => 'scalar'} }
22 21
 sub fetch_filter : ClassObjectAttr { initialize => {clone => 'scalar'} }
... ...
@@ -213,7 +212,8 @@ sub execute {
213 212
     
214 213
     # Execute
215 214
     my $sth = $query->sth;
216
-    my $ret_val = $sth->execute(@$bind_values);
215
+    my $ret_val = eval{$sth->execute(@$bind_values)};
216
+    croak($@) if $@;
217 217
     
218 218
     # Return resultset if select statement is executed
219 219
     if ($sth->{NUM_OF_FIELDS}) {
... ...
@@ -238,7 +238,7 @@ sub _build_bind_values {
238 238
     # binding values
239 239
     my @bind_values;
240 240
     
241
-    # Filter and sdd bind values
241
+    # Create bind values
242 242
     foreach my $key_info (@$key_infos) {
243 243
         my $filtering_key = $key_info->{key};
244 244
         my $access_keys = $key_info->{access_keys};
... ...
@@ -247,6 +247,7 @@ sub _build_bind_values {
247 247
         my $table        = $key_info->{table}        || '';
248 248
         my $column       = $key_info->{column}       || '';
249 249
         
250
+        my $found;
250 251
         ACCESS_KEYS :
251 252
         foreach my $access_key (@$access_keys) {
252 253
             my $root_params = $params;
... ...
@@ -278,7 +279,7 @@ sub _build_bind_values {
278 279
                             push @bind_values, scalar $root_params->{$key};
279 280
                         }
280 281
                     }
281
-                    return @bind_values;
282
+                    $found = 1;
282 283
                 }
283 284
                 
284 285
                 if ($key eq 'ARRAY') {
... ...
@@ -290,8 +291,17 @@ sub _build_bind_values {
290 291
                 }
291 292
             }
292 293
         }
293
-        croak("Cannot find key");
294
+        
295
+        unless ($found) {
296
+            require Data::Dumper;
297
+            my $key_info_dump = Data::Dumper->Dump([$key_info], ['*key_info']);
298
+            my $params_dump    = Data::Dumper->Dump([$params], ['*params']);
299
+            croak("Key not found\n\n" . 
300
+                  "<Key information>\n$key_info_dump\n\n" .
301
+                  "<Your parameters>\n$params_dump\n");
302
+        }
294 303
     }
304
+    return \@bind_values;
295 305
 }
296 306
 
297 307
 
... ...
@@ -553,6 +563,8 @@ If tranzation is died, rollback is execute.
553 563
 
554 564
 Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
555 565
 
566
+Github L<http://github.com/yuki-kimoto>
567
+
556 568
 =head1 COPYRIGHT & LICENSE
557 569
 
558 570
 Copyright 2009 Yuki Kimoto, all rights reserved.
+9 -11
t/02-sqlite.t
... ...
@@ -105,30 +105,28 @@ $result = $dbi->execute($query);
105 105
 @rows = $result->fetch_all;
106 106
 is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
107 107
 
108
-__END__
109
-
110 108
 test 'Filter';
111 109
 $dbi->reconnect;
112
-$dbi->dbh->do($CREATE_TABLE->{0});
110
+$dbi->do($CREATE_TABLE->{0});
113 111
 
114 112
 $insert_tmpl  = "insert into table1 {insert_values key1 key2};";
115 113
 $insert_query = $dbi->create_query($insert_tmpl);
116 114
 $insert_query->bind_filter(sub {
117
-    my ($key, $value) = @_;
118
-    if ($key eq 'key1') {
115
+    my ($key, $value, $table, $column) = @_;
116
+    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
119 117
         return $value * 2;
120 118
     }
121 119
     return $value;
122 120
 });
123
-$DB::single = 1;
121
+
124 122
 $ret_val = $dbi->execute($insert_query, {key1 => 1, key2 => 2});
125 123
 ok($ret_val, "Insert success return value");
126 124
 
127
-$select_tmpl  = "select k1, k2 from table1";
128
-$select_query = $dbi->create_query($select_query);
125
+$select_tmpl  = "select key1, key2 from table1";
126
+$select_query = $dbi->create_query($select_tmpl);
129 127
 $select_query->fetch_filter(sub {
130
-    my ($key, $value);
131
-    if ($key eq 'key2') {
128
+    my ($key, $value, $type, $sth, $i) = @_;
129
+    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
132 130
         return $value * 3;
133 131
     }
134 132
     return $value;
... ...
@@ -136,7 +134,7 @@ $select_query->fetch_filter(sub {
136 134
 $result = $dbi->execute($select_query);
137 135
 
138 136
 $rows = $result->fetch_all_hash;
139
-is_deeply($rows, {k1 => 2, k2 => 6}, "$test : bind_filter fetch_filter");
137
+is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
140 138
 
141 139
 __END__
142 140