| ... | ... |
@@ -235,10 +235,13 @@ sub execute {
|
| 235 | 235 |
# Execute error |
| 236 | 236 |
if (my $execute_error = $@) {
|
| 237 | 237 |
require Data::Dumper; |
| 238 |
- my $sql = $query->{sql} || '';
|
|
| 239 |
- my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
| 238 |
+ my $sql = $query->{sql} || '';
|
|
| 239 |
+ my $key_infos_dump = Data::Dumper->Dump([$query->key_infos], ['*key_infos']); |
|
| 240 |
+ my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
| 240 | 241 |
|
| 241 |
- croak("$execute_error<Your SQL>\n$sql\n<Your parameters>\n$params_dump");
|
|
| 242 |
+ croak("$execute_error" .
|
|
| 243 |
+ "<Your SQL>\n$sql\n" . |
|
| 244 |
+ "<Your parameters>\n$params_dump"); |
|
| 242 | 245 |
} |
| 243 | 246 |
|
| 244 | 247 |
# Return resultset if select statement is executed |
| ... | ... |
@@ -269,6 +272,7 @@ sub _build_bind_values {
|
| 269 | 272 |
my @bind_values; |
| 270 | 273 |
|
| 271 | 274 |
# Create bind values |
| 275 |
+ KEY_INFOS : |
|
| 272 | 276 |
foreach my $key_info (@$key_infos) {
|
| 273 | 277 |
# Set variable |
| 274 | 278 |
my $access_keys = $key_info->{access_keys};
|
| ... | ... |
@@ -333,6 +337,7 @@ sub _build_bind_values {
|
| 333 | 337 |
|
| 334 | 338 |
# Key is found |
| 335 | 339 |
$found = 1; |
| 340 |
+ next KEY_INFOS; |
|
| 336 | 341 |
} |
| 337 | 342 |
# First or middle key |
| 338 | 343 |
else {
|
| ... | ... |
@@ -432,13 +437,14 @@ sub last_insert_id {
|
| 432 | 437 |
# Insert |
| 433 | 438 |
sub insert {
|
| 434 | 439 |
my ($self, $table, $insert_params, $query_edit_cb) = @_; |
| 440 |
+ $table ||= ''; |
|
| 435 | 441 |
$insert_params ||= {};
|
| 436 | 442 |
|
| 437 | 443 |
# Insert keys |
| 438 | 444 |
my @insert_keys = keys %$insert_params; |
| 439 | 445 |
|
| 440 | 446 |
# Not exists insert keys |
| 441 |
- croak("key-value pairs must be specified to 'insert' second argument")
|
|
| 447 |
+ croak("Key-value pairs for insert must be specified to 'insert' second argument")
|
|
| 442 | 448 |
unless @insert_keys; |
| 443 | 449 |
|
| 444 | 450 |
# Templte for insert |
| ... | ... |
@@ -460,36 +466,41 @@ sub insert {
|
| 460 | 466 |
return $ret_val; |
| 461 | 467 |
} |
| 462 | 468 |
|
| 469 |
+# Update |
|
| 463 | 470 |
sub update {
|
| 464 | 471 |
my ($self, $table, $update_params, |
| 465 | 472 |
$where_params, $query_edit_cb, $options) = @_; |
| 466 | 473 |
|
| 474 |
+ $table ||= ''; |
|
| 467 | 475 |
$update_params ||= {};
|
| 468 | 476 |
$where_params ||= {};
|
| 469 | 477 |
|
| 470 | 478 |
# Update keys |
| 471 |
- my @update_keys = keys %$where_params; |
|
| 479 |
+ my @update_keys = keys %$update_params; |
|
| 472 | 480 |
|
| 473 | 481 |
# Not exists update kyes |
| 474 |
- croak("Update key must be specified")
|
|
| 482 |
+ croak("Key-value pairs for update must be specified to 'update' second argument")
|
|
| 475 | 483 |
unless @update_keys; |
| 476 | 484 |
|
| 477 | 485 |
# Where keys |
| 478 | 486 |
my @where_keys = keys %$where_params; |
| 479 | 487 |
|
| 480 | 488 |
# Not exists where keys |
| 481 |
- croak("Where key must be specified")
|
|
| 489 |
+ croak("Key-value pairs for where clause must be specified to 'update' third argument")
|
|
| 482 | 490 |
if !@where_keys && !$options->{allow_update_all};
|
| 483 | 491 |
|
| 484 | 492 |
# Update clause |
| 485 | 493 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}';
|
| 486 | 494 |
|
| 487 | 495 |
# Where clause |
| 488 |
- my $where_clause = 'where '; |
|
| 489 |
- foreach my $where_key (@where_keys) {
|
|
| 490 |
- $where_clause .= "{= $where_key} && ";
|
|
| 496 |
+ my $where_clause = ''; |
|
| 497 |
+ if (@where_keys) {
|
|
| 498 |
+ $where_clause = 'where '; |
|
| 499 |
+ foreach my $where_key (@where_keys) {
|
|
| 500 |
+ $where_clause .= "{= $where_key} && ";
|
|
| 501 |
+ } |
|
| 502 |
+ $where_clause =~ s/ && $//; |
|
| 491 | 503 |
} |
| 492 |
- $where_clause =~ s/ && $//; |
|
| 493 | 504 |
|
| 494 | 505 |
# Template for update |
| 495 | 506 |
my $template = "update $table $update_clause $where_clause"; |
| ... | ... |
@@ -524,21 +535,25 @@ sub update_all {
|
| 524 | 535 |
# Delete |
| 525 | 536 |
sub delete {
|
| 526 | 537 |
my ($self, $table, $where_params, $query_edit_cb, $options) = @_; |
| 538 |
+ $table ||= ''; |
|
| 527 | 539 |
$where_params ||= {};
|
| 528 | 540 |
|
| 529 | 541 |
# Where keys |
| 530 | 542 |
my @where_keys = keys %$where_params; |
| 531 | 543 |
|
| 532 | 544 |
# Not exists where keys |
| 533 |
- croak("Where key must be specified")
|
|
| 534 |
- if !@where_keys && !$options->{allow_update_all};
|
|
| 545 |
+ croak("Key-value pairs for where clause must be specified to 'delete' second argument")
|
|
| 546 |
+ if !@where_keys && !$options->{allow_delete_all};
|
|
| 535 | 547 |
|
| 536 | 548 |
# Where clause |
| 537 |
- my $where_clause = 'where '; |
|
| 538 |
- foreach my $where_key (@where_keys) {
|
|
| 539 |
- $where_clause .= "{= $where_key} && ";
|
|
| 549 |
+ my $where_clause = ''; |
|
| 550 |
+ if (@where_keys) {
|
|
| 551 |
+ $where_clause = 'where '; |
|
| 552 |
+ foreach my $where_key (@where_keys) {
|
|
| 553 |
+ $where_clause .= "{= $where_key} && ";
|
|
| 554 |
+ } |
|
| 555 |
+ $where_clause =~ s/ && $//; |
|
| 540 | 556 |
} |
| 541 |
- $where_clause =~ s/ && $//; |
|
| 542 | 557 |
|
| 543 | 558 |
# Template for delete |
| 544 | 559 |
my $template = "delete from $table $where_clause"; |
| ... | ... |
@@ -561,8 +576,8 @@ sub delete {
|
| 561 | 576 |
|
| 562 | 577 |
# Delete all rows |
| 563 | 578 |
sub delete_all {
|
| 564 |
- my ($self, $table, $query_edit_cb) = @_; |
|
| 565 |
- return $self->delete($table, {}, $query_edit_cb, {allow_delete_all => 1});
|
|
| 579 |
+ my ($self, $table) = @_; |
|
| 580 |
+ return $self->delete($table, {}, undef, {allow_delete_all => 1});
|
|
| 566 | 581 |
} |
| 567 | 582 |
|
| 568 | 583 |
sub _query_caches : ClassAttr { type => 'hash',
|
| ... | ... |
@@ -494,8 +494,136 @@ $result = $dbi->execute($SELECT_TMPL->{0});
|
| 494 | 494 |
$rows = $result->fetch_all_hash; |
| 495 | 495 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
|
| 496 | 496 |
|
| 497 |
+ |
|
| 498 |
+test 'insert error'; |
|
| 497 | 499 |
eval{$dbi->insert('table1')};
|
| 498 |
-like($@, qr/key-value pairs must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
|
| 500 |
+like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
|
| 499 | 501 |
|
| 500 | 502 |
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')};
|
| 501 | 503 |
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref"); |
| 504 |
+ |
|
| 505 |
+ |
|
| 506 |
+test 'update'; |
|
| 507 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 508 |
+$dbi->do($CREATE_TABLE->{1});
|
|
| 509 |
+$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 510 |
+$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 511 |
+$dbi->update('table1', {key2 => 11}, {key1 => 1});
|
|
| 512 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 513 |
+$rows = $result->fetch_all_hash; |
|
| 514 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 515 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 516 |
+ "$test : basic"); |
|
| 517 |
+ |
|
| 518 |
+$dbi->do("delete from table1");
|
|
| 519 |
+$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 520 |
+$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 521 |
+$dbi->update('table1', {key2 => 12}, {key2 => 2});
|
|
| 522 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 523 |
+$rows = $result->fetch_all_hash; |
|
| 524 |
+is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
|
| 525 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 526 |
+ "$test : update key same as search key"); |
|
| 527 |
+ |
|
| 528 |
+$dbi->do("delete from table1");
|
|
| 529 |
+$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 530 |
+$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 531 |
+$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
|
|
| 532 |
+ my $query = shift; |
|
| 533 |
+ $query->bind_filter(sub {
|
|
| 534 |
+ my ($key, $value) = @_; |
|
| 535 |
+ if ($key eq 'key2') {
|
|
| 536 |
+ return $value * 2; |
|
| 537 |
+ } |
|
| 538 |
+ return $value; |
|
| 539 |
+ }); |
|
| 540 |
+}); |
|
| 541 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 542 |
+$rows = $result->fetch_all_hash; |
|
| 543 |
+is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
|
|
| 544 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 545 |
+ "$test : query edit callback"); |
|
| 546 |
+ |
|
| 547 |
+ |
|
| 548 |
+test 'update error'; |
|
| 549 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 550 |
+$dbi->do($CREATE_TABLE->{1});
|
|
| 551 |
+eval{$dbi->update('table1')};
|
|
| 552 |
+like($@, qr/Key-value pairs for update must be specified to 'update' second argument/, |
|
| 553 |
+ "$test : update key-value pairs not specified"); |
|
| 554 |
+ |
|
| 555 |
+eval{$dbi->update('table1', {key2 => 1})};
|
|
| 556 |
+like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/, |
|
| 557 |
+ "$test : where key-value pairs not specified"); |
|
| 558 |
+ |
|
| 559 |
+eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, 'aaa')};
|
|
| 560 |
+like($@, qr/Query edit callback must be code reference/, |
|
| 561 |
+ "$test : query edit callback not code reference"); |
|
| 562 |
+ |
|
| 563 |
+ |
|
| 564 |
+test 'update_all'; |
|
| 565 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 566 |
+$dbi->do($CREATE_TABLE->{1});
|
|
| 567 |
+$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 568 |
+$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 569 |
+$dbi->update_all('table1', {key2 => 10}, sub {
|
|
| 570 |
+ my $query = shift; |
|
| 571 |
+ $query->bind_filter(sub {
|
|
| 572 |
+ my ($key, $value) = @_; |
|
| 573 |
+ return $value * 2; |
|
| 574 |
+ }) |
|
| 575 |
+}); |
|
| 576 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 577 |
+$rows = $result->fetch_all_hash; |
|
| 578 |
+is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
|
|
| 579 |
+ {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 580 |
+ "$test : query edit callback"); |
|
| 581 |
+ |
|
| 582 |
+ |
|
| 583 |
+test 'delete'; |
|
| 584 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 585 |
+$dbi->do($CREATE_TABLE->{0});
|
|
| 586 |
+$dbi->insert('table1', {key1 => 1, key2 => 2});
|
|
| 587 |
+$dbi->insert('table1', {key1 => 3, key2 => 4});
|
|
| 588 |
+$dbi->delete('table1', {key1 => 1});
|
|
| 589 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 590 |
+$rows = $result->fetch_all_hash; |
|
| 591 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
|
|
| 592 |
+ |
|
| 593 |
+$dbi->do("delete from table1;");
|
|
| 594 |
+$dbi->insert('table1', {key1 => 1, key2 => 2});
|
|
| 595 |
+$dbi->insert('table1', {key1 => 3, key2 => 4});
|
|
| 596 |
+$dbi->delete('table1', {key2 => 1}, sub {
|
|
| 597 |
+ my $query = shift; |
|
| 598 |
+ $query->bind_filter(sub {
|
|
| 599 |
+ my ($key, $value) = @_; |
|
| 600 |
+ return $value * 2; |
|
| 601 |
+ }); |
|
| 602 |
+}); |
|
| 603 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 604 |
+$rows = $result->fetch_all_hash; |
|
| 605 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
|
|
| 606 |
+ |
|
| 607 |
+test 'delete error'; |
|
| 608 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 609 |
+$dbi->do($CREATE_TABLE->{0});
|
|
| 610 |
+eval{$dbi->delete('table1')};
|
|
| 611 |
+like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
| 612 |
+ "$test : where key-value pairs not specified"); |
|
| 613 |
+ |
|
| 614 |
+eval{$dbi->delete('table1', {key1 => 1}, 'aaa')};
|
|
| 615 |
+like($@, qr/Query edit callback must be code reference/, |
|
| 616 |
+ "$test : query edit callback not code ref"); |
|
| 617 |
+ |
|
| 618 |
+ |
|
| 619 |
+test 'delete_all'; |
|
| 620 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
|
| 621 |
+$dbi->do($CREATE_TABLE->{0});
|
|
| 622 |
+$dbi->insert('table1', {key1 => 1, key2 => 2});
|
|
| 623 |
+$dbi->insert('table1', {key1 => 3, key2 => 4});
|
|
| 624 |
+$dbi->delete_all('table1');
|
|
| 625 |
+$result = $dbi->execute($SELECT_TMPL->{0});
|
|
| 626 |
+$rows = $result->fetch_all_hash; |
|
| 627 |
+is_deeply($rows, [], "$test : basic"); |
|
| 628 |
+ |
|
| 629 |
+ |