... | ... |
@@ -521,20 +521,33 @@ sub update_all { |
521 | 521 |
return $self->update($table, $update_params, $args); |
522 | 522 |
} |
523 | 523 |
|
524 |
+our %VALID_DELETE_ARGS |
|
525 |
+ = map { $_ => 1 } qw/where append query_edit_cb allow_delete_all/; |
|
526 |
+ |
|
524 | 527 |
sub delete { |
525 |
- my $self = shift; |
|
526 |
- my $table = shift || ''; |
|
527 |
- my $where_params = shift || {}; |
|
528 |
- my $append_statement = shift unless ref $_[0]; |
|
529 |
- my $query_edit_cb = shift; |
|
530 |
- my $options = shift; |
|
528 |
+ my ($self, $table, $args) = @_; |
|
529 |
+ |
|
530 |
+ # Table |
|
531 |
+ $table ||= ''; |
|
532 |
+ |
|
533 |
+ # Check arguments |
|
534 |
+ foreach my $name (keys %$args) { |
|
535 |
+ croak "\"$name\" is invalid name" |
|
536 |
+ unless $VALID_DELETE_ARGS{$name}; |
|
537 |
+ } |
|
538 |
+ |
|
539 |
+ # Arguments |
|
540 |
+ my $where_params = $args->{where} || {}; |
|
541 |
+ my $append_statement = $args->{append}; |
|
542 |
+ my $query_edit_cb = $args->{query_edit_cb}; |
|
543 |
+ my $allow_delete_all = $args->{allow_delete_all}; |
|
531 | 544 |
|
532 | 545 |
# Where keys |
533 | 546 |
my @where_keys = keys %$where_params; |
534 | 547 |
|
535 | 548 |
# Not exists where keys |
536 | 549 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
537 |
- if !@where_keys && !$options->{allow_delete_all}; |
|
550 |
+ if !@where_keys && !$allow_delete_all; |
|
538 | 551 |
|
539 | 552 |
# Where clause |
540 | 553 |
my $where_clause = ''; |
... | ... |
@@ -567,15 +580,14 @@ sub delete { |
567 | 580 |
} |
568 | 581 |
|
569 | 582 |
sub delete_all { |
570 |
- my $self = shift; |
|
571 |
- my $table = shift || ''; |
|
572 |
- my $append_statement = shift unless ref $_[0]; |
|
573 |
- my $query_edit_cb = shift; |
|
574 |
- my $options = {allow_delete_all => 1}; |
|
583 |
+ my ($self, $table, $args) = @_; |
|
584 |
+ |
|
585 |
+ $args ||= {}; |
|
586 |
+ |
|
587 |
+ $args->{allow_delete_all} = 1; |
|
575 | 588 |
|
576 | 589 |
# Delete all rows |
577 |
- return $self->delete($table, {}, $append_statement, $query_edit_cb, |
|
578 |
- $options); |
|
590 |
+ return $self->delete($table, $args); |
|
579 | 591 |
} |
580 | 592 |
|
581 | 593 |
sub _select_usage { return << 'EOS' } |
... | ... |
@@ -558,7 +558,7 @@ $dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
558 | 558 |
$dbi->do($CREATE_TABLE->{0}); |
559 | 559 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
560 | 560 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
561 |
-$dbi->delete('table1', {key1 => 1}); |
|
561 |
+$dbi->delete('table1', {where => {key1 => 1}}); |
|
562 | 562 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
563 | 563 |
$rows = $result->fetch_hash_all; |
564 | 564 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
... | ... |
@@ -566,28 +566,28 @@ is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
566 | 566 |
$dbi->do("delete from table1;"); |
567 | 567 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
568 | 568 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
569 |
-$dbi->delete('table1', {key2 => 1}, sub { |
|
569 |
+$dbi->delete('table1', {where => {key2 => 1}, query_edit_cb => sub { |
|
570 | 570 |
my $query = shift; |
571 | 571 |
$query->bind_filter(sub { |
572 | 572 |
my ($value, $table, $column, $dbi) = @_; |
573 | 573 |
return $value * 2; |
574 | 574 |
}); |
575 |
-}); |
|
575 |
+}}); |
|
576 | 576 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
577 | 577 |
$rows = $result->fetch_hash_all; |
578 | 578 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback"); |
579 | 579 |
|
580 |
-$dbi->delete('table1', {key1 => 1}, ' ', sub { |
|
580 |
+$dbi->delete('table1', {where => {key1 => 1}, append => ' ', query_edit_cb => sub { |
|
581 | 581 |
my $query = shift; |
582 | 582 |
is($query->sql, 'delete from table1 where table1.key1 = ? ;', |
583 | 583 |
"$test: append statement"); |
584 |
-}); |
|
584 |
+}}); |
|
585 | 585 |
|
586 | 586 |
|
587 | 587 |
$dbi->delete_all('table1'); |
588 | 588 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
589 | 589 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
590 |
-$dbi->delete('table1', {key1 => 1, key2 => 2}); |
|
590 |
+$dbi->delete('table1', {where => {key1 => 1, key2 => 2}}); |
|
591 | 591 |
$rows = $dbi->select('table1')->fetch_hash_all; |
592 | 592 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
593 | 593 |
|
... | ... |
@@ -599,7 +599,7 @@ eval{$dbi->delete('table1')}; |
599 | 599 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
600 | 600 |
"$test : where key-value pairs not specified"); |
601 | 601 |
|
602 |
-eval{$dbi->delete('table1', {key1 => 1}, '', 'aaa')}; |
|
602 |
+eval{$dbi->delete('table1', {where => {key1 => 1}, append => '', query_edit_cb => 'aaa'})}; |
|
603 | 603 |
like($@, qr/Query edit callback must be code reference/, |
604 | 604 |
"$test : query edit callback not code ref"); |
605 | 605 |
|