Showing 2 changed files with 80 additions and 57 deletions
+68 -57
lib/DBIx/Custom.pm
... ...
@@ -361,7 +361,8 @@ sub execute {
361 361
     $param ||= $opt{param} || {};
362 362
     my $tables = $opt{table} || [];
363 363
     $tables = [$tables] unless ref $tables eq 'ARRAY';
364
-    my $filter = _array_to_hash($opt{filter});
364
+    my $filter = ref $opt{filter} eq 'ARRAY' ?
365
+      _array_to_hash($opt{filter}) : $opt{filter};
365 366
     
366 367
     # Append
367 368
     $sql .= $opt{append} if defined $opt{append} && !ref $sql;
... ...
@@ -377,7 +378,7 @@ sub execute {
377 378
     }
378 379
         
379 380
     # Save query
380
-    $self->last_sql($query->sql);
381
+    $self->{last_sql} = $query->{sql};
381 382
 
382 383
     # Return query
383 384
     return $query if $opt{query};
... ...
@@ -401,24 +402,26 @@ sub execute {
401 402
     
402 403
     # Type rule
403 404
     my $type_filters = {};
404
-    unless ($opt{type_rule_off}) {
405
-        my $type_rule_off_parts = {
406
-            1 => $opt{type_rule1_off},
407
-            2 => $opt{type_rule2_off}
408
-        };
409
-        for my $i (1, 2) {
410
-            unless ($type_rule_off_parts->{$i}) {
411
-                $type_filters->{$i} = {};
412
-                my $table_alias = $opt{table_alias} || {};
413
-                for my $alias (keys %$table_alias) {
414
-                    my $table = $table_alias->{$alias};
415
-                    
416
-                    for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
417
-                        $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
405
+    if ($self->{_type_rule_is_called}) {
406
+        unless ($opt{type_rule_off}) {
407
+            my $type_rule_off_parts = {
408
+                1 => $opt{type_rule1_off},
409
+                2 => $opt{type_rule2_off}
410
+            };
411
+            for my $i (1, 2) {
412
+                unless ($type_rule_off_parts->{$i}) {
413
+                    $type_filters->{$i} = {};
414
+                    my $table_alias = $opt{table_alias} || {};
415
+                    for my $alias (keys %$table_alias) {
416
+                        my $table = $table_alias->{$alias};
417
+                        
418
+                        for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
419
+                            $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
420
+                        }
418 421
                     }
422
+                    $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
423
+                      if $main_table;
419 424
                 }
420
-                $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
421
-                  if $main_table;
422 425
             }
423 426
         }
424 427
     }
... ...
@@ -449,17 +452,22 @@ sub execute {
449 452
     }
450 453
     
451 454
     # Create bind values
452
-    my $bind = $self->_create_bind_values($param, $query->columns,
453
-      $filter, $type_filters, _array_to_hash($opt{bind_type} || $opt{type}));
455
+    my ($bind, $bind_types) = $self->_create_bind_values($param, $query->columns,
456
+      $filter, $type_filters, $opt{bind_type} || $opt{type} || {});
454 457
 
455 458
     # Execute
456
-    my $sth = $query->sth;
459
+    my $sth = $query->{sth};
457 460
     my $affected;
458 461
     eval {
459
-        $sth->bind_param($_ + 1, $bind->[$_]->{value},
460
-            $bind->[$_]->{bind_type} ? $bind->[$_]->{bind_type} : ())
461
-          for (0 .. @$bind - 1);
462
-        $affected = $sth->execute;
462
+        if ($opt{bind_type} || $opt{type}) {
463
+            $sth->bind_param($_ + 1, $bind->[$_],
464
+                $bind_types->[$_] ? $bind_types->[$_] : ())
465
+              for (0 .. @$bind - 1);
466
+            $affected = $sth->execute;
467
+        }
468
+        else {
469
+            $affected = $sth->execute(@$bind);
470
+        }
463 471
     };
464 472
     
465 473
     $self->_croak($@, qq{. Following SQL is executed.\n}
... ...
@@ -479,39 +487,36 @@ sub execute {
479 487
         print STDERR "Bind values: " . join(', ', @output) . "\n\n";
480 488
     }
481 489
     
482
-    # Select statement
483
-    if ($sth->{NUM_OF_FIELDS}) {
484
-        
485
-        # Filter(DEPRECATED!)
486
-        my $filter = {};
487
-        if ($self->{filter}{on}) {
488
-            $filter->{in}  = {};
489
-            $filter->{end} = {};
490
-            push @$tables, $main_table if $main_table;
491
-            for my $table (@$tables) {
492
-                for my $way (qw/in end/) {
493
-                    $filter->{$way} = {%{$filter->{$way}},
494
-                      %{$self->{filter}{$way}{$table} || {}}};
495
-                }
490
+    # Not select statement
491
+    return $affected unless $sth->{NUM_OF_FIELDS};
492
+
493
+    # Filter(DEPRECATED!)
494
+    my $infilter = {};
495
+    if ($self->{filter}{on}) {
496
+        $infilter->{in}  = {};
497
+        $infilter->{end} = {};
498
+        push @$tables, $main_table if $main_table;
499
+        for my $table (@$tables) {
500
+            for my $way (qw/in end/) {
501
+                $infilter->{$way} = {%{$infilter->{$way}},
502
+                  %{$self->{filter}{$way}{$table} || {}}};
496 503
             }
497 504
         }
498
-        
499
-        # Result
500
-        my $result = $self->result_class->new(
501
-            sth => $sth,
502
-            dbi => $self,
503
-            default_filter => $self->{default_in_filter},
504
-            filter => $filter->{in} || {},
505
-            end_filter => $filter->{end} || {},
506
-            type_rule => {
507
-                from1 => $self->type_rule->{from1},
508
-                from2 => $self->type_rule->{from2}
509
-            },
510
-        );
511
-        return $result;
512 505
     }
513
-    # Not select statement
514
-    else { return $affected }
506
+    
507
+    # Result
508
+    my $result = $self->result_class->new(
509
+        sth => $sth,
510
+        dbi => $self,
511
+        default_filter => $self->{default_in_filter},
512
+        filter => $infilter->{in} || {},
513
+        end_filter => $infilter->{end} || {},
514
+        type_rule => {
515
+            from1 => $self->type_rule->{from1},
516
+            from2 => $self->type_rule->{from2}
517
+        },
518
+    );
519
+    $result;
515 520
 }
516 521
 
517 522
 sub get_table_info {
... ...
@@ -918,6 +923,8 @@ sub show_tables {
918 923
 
919 924
 sub type_rule {
920 925
     my $self = shift;
926
+
927
+    $self->{_type_rule_is_called} = 1;
921 928
     
922 929
     if (@_) {
923 930
         my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
... ...
@@ -1185,8 +1192,11 @@ sub _create_query {
1185 1192
 sub _create_bind_values {
1186 1193
     my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
1187 1194
     
1195
+    $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1196
+    
1188 1197
     # Create bind values
1189 1198
     my $bind = [];
1199
+    my $types = [];
1190 1200
     my $count = {};
1191 1201
     my $not_exists = {};
1192 1202
     for my $column (@$columns) {
... ...
@@ -1224,13 +1234,14 @@ sub _create_bind_values {
1224 1234
         $value = $tf2->($value) if $tf2;
1225 1235
        
1226 1236
         # Bind values
1227
-        push @$bind, {value => $value, bind_type => $bind_type->{$column}};
1237
+        push @$bind, $value;
1238
+        push @$types, $bind_type->{$column};
1228 1239
         
1229 1240
         # Count up 
1230 1241
         $count->{$column}++;
1231 1242
     }
1232 1243
     
1233
-    return $bind;
1244
+    return ($bind, $types);
1234 1245
 }
1235 1246
 
1236 1247
 sub _id_to_param {
+12
t/sqlite.t
... ...
@@ -162,6 +162,18 @@ $result = $dbi->execute('select length(key1) as key1_length from table1');
162 162
 $row = $result->one;
163 163
 is($row->{key1_length}, length $binary);
164 164
 
165
+test 'bind_type option'; # DEPRECATED!
166
+$binary = pack("I3", 1, 2, 3);
167
+eval { $dbi->execute('drop table table1') };
168
+$dbi->execute('create table table1(key1, key2)');
169
+$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
170
+$result = $dbi->select(table => 'table1');
171
+$row   = $result->one;
172
+is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
173
+$result = $dbi->execute('select length(key1) as key1_length from table1');
174
+$row = $result->one;
175
+is($row->{key1_length}, length $binary);
176
+
165 177
 test 'type_rule from';
166 178
 $dbi = DBIx::Custom->connect;
167 179
 $dbi->type_rule(