Showing 3 changed files with 60 additions and 34 deletions
+3
Changes
... ...
@@ -1,4 +1,7 @@
1 1
 0.1738
2
+    - removed example that query pass execute method in documentation
3
+      this is many bug reason much more than I have expected
4
+      and passing query to execute method is DEPRECATED!
2 5
     - insert method id value is not copied to parameter
3 6
       if the key exists in parameter
4 7
 0.1737
+31 -34
lib/DBIx/Custom.pm
... ...
@@ -361,12 +361,35 @@ sub execute {
361 361
     my $filter = ref $opt{filter} eq 'ARRAY' ?
362 362
       _array_to_hash($opt{filter}) : $opt{filter};
363 363
     
364
+    # Merge second parameter
365
+    my @cleanup;
366
+    if (ref $param eq 'ARRAY') {
367
+        my $param2 = $param->[1];
368
+        $param = $param->[0];
369
+        for my $column (keys %$param2) {
370
+            if (!exists $param->{$column}) {
371
+                $param->{$column} = $param2->{$column};
372
+                push @cleanup, $column;
373
+            }
374
+            else {
375
+                delete $param->{$_} for @cleanup;
376
+                @cleanup = ();
377
+                $param = $self->merge_param($param, $param2);
378
+                last;
379
+            }
380
+        }
381
+    }
382
+    
364 383
     # Append
365 384
     $sql .= $opt{append} if defined $opt{append} && !ref $sql;
366 385
     
367 386
     # Query
368 387
     my $query;
369
-    if (ref $sql) { $query = $sql }
388
+    if (ref $sql) {
389
+        $query = $sql;
390
+        warn "execute method receiving query object as first parameter is DEPRECATED!" .
391
+             "because this is very buggy.";
392
+    }
370 393
     else {
371 394
         $query = $opt{reuse}->{$sql} if $opt{reuse};
372 395
         $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
... ...
@@ -389,7 +412,6 @@ sub execute {
389 412
     my $main_table = @{$tables}[-1];
390 413
 
391 414
     # Merge id to parameter
392
-    my @cleanup;
393 415
     if (defined $opt{id}) {
394 416
         croak "execute id option must be specified with primary_key option"
395 417
           unless $opt{primary_key};
... ...
@@ -402,7 +424,7 @@ sub execute {
402 424
              $statement eq 'delete' || $statement eq 'select';
403 425
            next if exists $param->{$key};
404 426
            $param->{$key} = $opt{id}->[$i];
405
-           push @cleanup, $key;
427
+           push @cleanup, $key;1
406 428
         }
407 429
     }
408 430
     
... ...
@@ -485,6 +507,7 @@ sub execute {
485 507
 
486 508
     # Remove id from parameter
487 509
     delete $param->{$_} for @cleanup;
510
+
488 511
     
489 512
     # DEBUG message
490 513
     if ($ENV{DBIX_CUSTOM_DEBUG}) {
... ...
@@ -593,6 +616,7 @@ sub insert {
593 616
     
594 617
     # Merge id to parameter
595 618
     my @cleanup;
619
+    my $id_param = {};
596 620
     if (defined $opt{id}) {
597 621
         croak "insert id option must be specified with primary_key option"
598 622
           unless $opt{primary_key};
... ...
@@ -1051,9 +1075,6 @@ sub update {
1051 1075
     my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1052 1076
       delete $opt{id}, $opt{primary_key}, $opt{table});
1053 1077
     
1054
-    # Merge where parameter to parameter
1055
-    $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
1056
-    
1057 1078
     # Update statement
1058 1079
     my $sql = "update ";
1059 1080
     $sql .= "$opt{prefix} " if defined $opt{prefix};
... ...
@@ -1061,7 +1082,7 @@ sub update {
1061 1082
     
1062 1083
     # Execute query
1063 1084
     $opt{statement} = 'update';
1064
-    $self->execute($sql, $param, %opt);
1085
+    $self->execute($sql, [$param, $w->{param}], %opt);
1065 1086
 }
1066 1087
 
1067 1088
 sub update_all { shift->update(allow_update_all => 1, @_) };
... ...
@@ -2580,38 +2601,12 @@ The above is same as the followin one.
2580 2601
     query => 1
2581 2602
 
2582 2603
 C<execute> method return L<DBIx::Custom::Query> object, not executing SQL.
2583
-You can check SQL or get statment handle.
2604
+You can check SQL, column, or get statment handle.
2584 2605
 
2585 2606
     my $sql = $query->sql;
2586 2607
     my $sth = $query->sth;
2587 2608
     my $columns = $query->columns;
2588 2609
     
2589
-If you want to execute SQL fast, you can do the following way.
2590
-
2591
-    my $query;
2592
-    for my $row (@$rows) {
2593
-      $query ||= $dbi->insert($row, table => 'table1', query => 1);
2594
-      $dbi->execute($query, $row);
2595
-    }
2596
-
2597
-Statement handle is reused and SQL parsing is finished,
2598
-so you can get more performance than normal way.
2599
-
2600
-If you want to execute SQL as possible as fast and don't need filtering.
2601
-You can do the following way.
2602
-    
2603
-    my $query;
2604
-    my $sth;
2605
-    for my $row (@$rows) {
2606
-      $query ||= $dbi->insert($row, table => 'book', query => 1);
2607
-      $sth ||= $query->sth;
2608
-      $sth->execute(map { $row->{$_} } sort keys %$row);
2609
-    }
2610
-
2611
-Note that $row must be simple hash reference, such as
2612
-{title => 'Perl', author => 'Ken'}.
2613
-and don't forget to sort $row values by $row key asc order.
2614
-
2615 2610
 =item C<primary_key>
2616 2611
 
2617 2612
     primary_key => 'id'
... ...
@@ -3456,6 +3451,8 @@ L<DBIx::Custom>
3456 3451
     execute method's sqlfilter option # will be removed at 2017/1/1
3457 3452
     
3458 3453
     # Others
3454
+    execute($query, ...) # execute method receiving query object.
3455
+                         # this is removed at 2017/1/1
3459 3456
     execute("select * from {= title}"); # execute method's
3460 3457
                                         # tag parsing functionality
3461 3458
                                         # will be removed at 2017/1/1
+26
t/common.t
... ...
@@ -767,6 +767,32 @@ $result = $dbi->execute("select * from $table1");
767 767
 $rows   = $result->all;
768 768
 is($rows->[0]->{$key1}, $rows->[0]->{$key2});
769 769
 
770
+eval { $dbi->execute("drop table $table1") };
771
+$dbi->execute($create_table1_2);
772
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
773
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
774
+$param = {$key2 => 11};
775
+$dbi->update($param, table => $table1, where => {$key1 => 1});
776
+is_deeply($param, {$key2 => 11});
777
+$result = $dbi->execute("select * from $table1 order by $key1");
778
+$rows   = $result->all;
779
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
780
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
781
+                  "basic");
782
+
783
+eval { $dbi->execute("drop table $table1") };
784
+$dbi->execute($create_table1_2);
785
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5});
786
+$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10});
787
+$param = {$key2 => 11};
788
+$dbi->update($param, table => $table1, where => {$key2 => 2});
789
+is_deeply($param, {$key2 => 11});
790
+$result = $dbi->execute("select * from $table1 order by $key1");
791
+$rows   = $result->all;
792
+is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5},
793
+                  {$key1 => 6, $key2 => 7,  $key3 => 8, $key4 => 9, $key5 => 10}],
794
+                  "basic");
795
+
770 796
 test 'update_all';
771 797
 eval { $dbi->execute("drop table $table1") };
772 798
 $dbi->execute($create_table1_2);