Showing 2 changed files with 32 additions and 25 deletions
+23 -16
lib/DBIx/Custom.pm
... ...
@@ -436,14 +436,23 @@ sub insert {
436 436
     return $ret_val;
437 437
 }
438 438
 
439
+our %VALID_UPDATE_ARGS
440
+  = map { $_ => 1 } qw/where append query_edit_cb allow_update_all/;
441
+
439 442
 sub update {
440
-    my $self             = shift;
441
-    my $table            = shift || '';
442
-    my $update_params    = shift || {};
443
-    my $where_params     = shift || {};
444
-    my $append_statement = shift unless ref $_[0];
445
-    my $query_edit_cb    = shift;
446
-    my $options          = shift;
443
+    my ($self, $table, $update_params, $args) = @_;
444
+    
445
+    # Check arguments
446
+    foreach my $name (keys %$args) {
447
+        croak "\"$name\" is invalid name"
448
+          unless $VALID_UPDATE_ARGS{$name};
449
+    }
450
+    
451
+    # Arguments
452
+    my $where_params     = $args->{where} || {};
453
+    my $append_statement = $args->{append} || '';
454
+    my $query_edit_cb    = $args->{query_edit_cb};
455
+    my $allow_update_all = $args->{allow_update_all};
447 456
     
448 457
     # Update keys
449 458
     my @update_keys = keys %$update_params;
... ...
@@ -457,7 +466,7 @@ sub update {
457 466
     
458 467
     # Not exists where keys
459 468
     croak("Key-value pairs for where clause must be specified to 'update' third argument")
460
-      if !@where_keys && !$options->{allow_update_all};
469
+      if !@where_keys && !$allow_update_all;
461 470
     
462 471
     # Update clause
463 472
     my $update_clause = '{update ' . join(' ', @update_keys) . '}';
... ...
@@ -502,16 +511,14 @@ sub update {
502 511
 }
503 512
 
504 513
 sub update_all {
505
-    my $self             = shift;
506
-    my $table            = shift || '';
507
-    my $update_params    = shift || {};
508
-    my $append_statement = shift unless ref $_[0];
509
-    my $query_edit_cb    = shift;
510
-    my $options          = {allow_update_all => 1};
514
+    my ($self, $table, $update_params, $args) = @_;
515
+    
516
+    $args ||= {};
517
+    
518
+    $args->{allow_update_all} = 1;
511 519
     
512 520
     # Update all rows
513
-    return $self->update($table, $update_params, {}, $append_statement,
514
-                         $query_edit_cb, $options);
521
+    return $self->update($table, $update_params, $args);
515 522
 }
516 523
 
517 524
 sub delete {
+9 -9
t/dbix-custom-core-sqlite.t
... ...
@@ -475,7 +475,7 @@ $dbi = DBIx::Custom->new($NEW_ARGS->{0});
475 475
 $dbi->do($CREATE_TABLE->{1});
476 476
 $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
477 477
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
478
-$dbi->update('table1', {key2 => 11}, {key1 => 1});
478
+$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}});
479 479
 $result = $dbi->query($SELECT_TMPLS->{0});
480 480
 $rows   = $result->fetch_hash_all;
481 481
 is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
... ...
@@ -485,7 +485,7 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
485 485
 $dbi->do("delete from table1");
486 486
 $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
487 487
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
488
-$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
488
+$dbi->update('table1', {key2 => 12}, {where => {key2 => 2, key3 => 3}});
489 489
 $result = $dbi->query($SELECT_TMPLS->{0});
490 490
 $rows   = $result->fetch_hash_all;
491 491
 is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
... ...
@@ -495,7 +495,7 @@ is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
495 495
 $dbi->do("delete from table1");
496 496
 $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
497 497
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
498
-$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
498
+$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, query_edit_cb => sub {
499 499
     my $query = shift;
500 500
     $query->bind_filter(sub {
501 501
         my ($value, $table, $column, $dbi) = @_;
... ...
@@ -504,18 +504,18 @@ $dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
504 504
         }
505 505
         return $value;
506 506
     });
507
-});
507
+}});
508 508
 $result = $dbi->query($SELECT_TMPLS->{0});
509 509
 $rows   = $result->fetch_hash_all;
510 510
 is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
511 511
                   {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
512 512
                   "$test : query edit callback");
513 513
 
514
-$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
514
+$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, append => '   ', query_edit_cb => sub {
515 515
     my $query = shift;
516 516
     is($query->sql, 'update table1 set key2 = ? where table1.key1 = ?    ;',
517 517
        "$test: append statement");
518
-});
518
+}});
519 519
 
520 520
 
521 521
 test 'update error';
... ...
@@ -529,7 +529,7 @@ eval{$dbi->update('table1', {key2 => 1})};
529 529
 like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/,
530 530
          "$test : where key-value pairs not specified");
531 531
 
532
-eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, '', 'aaa')};
532
+eval{$dbi->update('table1', {key2 => 1}, {where => {key2 => 3}, append => '', query_edit_cb => 'aaa'})};
533 533
 like($@, qr/Query edit callback must be code reference/, 
534 534
          "$test : query edit callback not code reference");
535 535
 
... ...
@@ -539,13 +539,13 @@ $dbi = DBIx::Custom->new($NEW_ARGS->{0});
539 539
 $dbi->do($CREATE_TABLE->{1});
540 540
 $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
541 541
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
542
-$dbi->update_all('table1', {key2 => 10}, sub {
542
+$dbi->update_all('table1', {key2 => 10}, {query_edit_cb => sub {
543 543
     my $query = shift;
544 544
     $query->bind_filter(sub {
545 545
         my ($value, $table, $column, $dbi) = @_;
546 546
         return $value * 2;
547 547
     })
548
-});
548
+}});
549 549
 $result = $dbi->query($SELECT_TMPLS->{0});
550 550
 $rows   = $result->fetch_hash_all;
551 551
 is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},