| ... | ... |
@@ -497,9 +497,9 @@ sub update {
|
| 497 | 497 |
if (@where_keys) {
|
| 498 | 498 |
$where_clause = 'where '; |
| 499 | 499 |
foreach my $where_key (@where_keys) {
|
| 500 |
- $where_clause .= "{= $where_key} && ";
|
|
| 500 |
+ $where_clause .= "{= $where_key} and ";
|
|
| 501 | 501 |
} |
| 502 |
- $where_clause =~ s/ && $//; |
|
| 502 |
+ $where_clause =~ s/ and $//; |
|
| 503 | 503 |
} |
| 504 | 504 |
|
| 505 | 505 |
# Template for update |
| ... | ... |
@@ -550,9 +550,9 @@ sub delete {
|
| 550 | 550 |
if (@where_keys) {
|
| 551 | 551 |
$where_clause = 'where '; |
| 552 | 552 |
foreach my $where_key (@where_keys) {
|
| 553 |
- $where_clause .= "{= $where_key} && ";
|
|
| 553 |
+ $where_clause .= "{= $where_key} and ";
|
|
| 554 | 554 |
} |
| 555 |
- $where_clause =~ s/ && $//; |
|
| 555 |
+ $where_clause =~ s/ and $//; |
|
| 556 | 556 |
} |
| 557 | 557 |
|
| 558 | 558 |
# Template for delete |
| ... | ... |
@@ -629,7 +629,7 @@ sub select {
|
| 629 | 629 |
foreach my $table (@$tables) {
|
| 630 | 630 |
$template .= "$table, "; |
| 631 | 631 |
} |
| 632 |
- $template =~ s/, / /; |
|
| 632 |
+ $template =~ s/, $/ /; |
|
| 633 | 633 |
|
| 634 | 634 |
# Where clause keys |
| 635 | 635 |
my @where_keys = keys %$where_params; |
| ... | ... |
@@ -638,15 +638,15 @@ sub select {
|
| 638 | 638 |
if (@where_keys) {
|
| 639 | 639 |
$template .= 'where '; |
| 640 | 640 |
foreach my $where_key (@where_keys) {
|
| 641 |
- $template .= "{= $where_key} && ";
|
|
| 641 |
+ $template .= "{= $where_key} and ";
|
|
| 642 | 642 |
} |
| 643 | 643 |
} |
| 644 |
- $template =~ s/ && $//; |
|
| 644 |
+ $template =~ s/ and $//; |
|
| 645 | 645 |
|
| 646 | 646 |
# Append something to last of statement |
| 647 | 647 |
if ($append_statement =~ s/^where //) {
|
| 648 | 648 |
if (@where_keys) {
|
| 649 |
- $template .= " && $append_statement"; |
|
| 649 |
+ $template .= " and $append_statement"; |
|
| 650 | 650 |
} |
| 651 | 651 |
else {
|
| 652 | 652 |
$template .= " where $append_statement"; |
| ... | ... |
@@ -937,7 +937,7 @@ add_filter add filter to filters |
| 937 | 937 |
$result = $dbi->query($template, $params); # Shorcut |
| 938 | 938 |
|
| 939 | 939 |
# Sample |
| 940 |
- $result = $dbi->query("select * from authors where {= name} && {= age}",
|
|
| 940 |
+ $result = $dbi->query("select * from authors where {= name} and {= age}",
|
|
| 941 | 941 |
{author => 'taro', age => 19});
|
| 942 | 942 |
|
| 943 | 943 |
while (my @row = $result->fetch) {
|
| ... | ... |
@@ -23,7 +23,8 @@ sub test {
|
| 23 | 23 |
# Constant varialbes for test |
| 24 | 24 |
my $CREATE_TABLE = {
|
| 25 | 25 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
| 26 |
- 1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));' |
|
| 26 |
+ 1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
| 27 |
+ 2 => 'create table table2 (key1 char(255), key3 char(255));' |
|
| 27 | 28 |
}; |
| 28 | 29 |
|
| 29 | 30 |
my $SELECT_TMPL = {
|
| ... | ... |
@@ -518,7 +519,7 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
| 518 | 519 |
$dbi->do("delete from table1");
|
| 519 | 520 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
| 520 | 521 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
| 521 |
-$dbi->update('table1', {key2 => 12}, {key2 => 2});
|
|
| 522 |
+$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
|
|
| 522 | 523 |
$result = $dbi->execute($SELECT_TMPL->{0});
|
| 523 | 524 |
$rows = $result->fetch_all_hash; |
| 524 | 525 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
| ... | ... |
@@ -604,6 +605,14 @@ $result = $dbi->execute($SELECT_TMPL->{0});
|
| 604 | 605 |
$rows = $result->fetch_all_hash; |
| 605 | 606 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
|
| 606 | 607 |
|
| 608 |
+$dbi->delete_all('table1');
|
|
| 609 |
+$dbi->insert('table1', {key1 => 1, key2 => 2});
|
|
| 610 |
+$dbi->insert('table1', {key1 => 3, key2 => 4});
|
|
| 611 |
+$dbi->delete('table1', {key1 => 1, key2 => 2});
|
|
| 612 |
+$rows = $dbi->select('table1')->fetch_all_hash;
|
|
| 613 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
|
|
| 614 |
+ |
|
| 615 |
+ |
|
| 607 | 616 |
test 'delete error'; |
| 608 | 617 |
$dbi = DBI::Custom->new($NEW_ARGS->{0});
|
| 609 | 618 |
$dbi->do($CREATE_TABLE->{0});
|
| ... | ... |
@@ -644,3 +653,26 @@ is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where
|
| 644 | 653 |
|
| 645 | 654 |
$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_all_hash;
|
| 646 | 655 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
|
| 656 |
+ |
|
| 657 |
+$rows = $dbi->select('table1', "order by key1 desc limit 1")->fetch_all_hash;
|
|
| 658 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
|
|
| 659 |
+ |
|
| 660 |
+$rows = $dbi->select('table1', {key1 => 2}, sub {
|
|
| 661 |
+ my $query = shift; |
|
| 662 |
+ $query->bind_filter(sub {
|
|
| 663 |
+ my ($key, $value) = @_; |
|
| 664 |
+ if ($key eq 'key1') {
|
|
| 665 |
+ return $value - 1; |
|
| 666 |
+ } |
|
| 667 |
+ return $value; |
|
| 668 |
+ }); |
|
| 669 |
+})->fetch_all_hash; |
|
| 670 |
+is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
|
|
| 671 |
+ |
|
| 672 |
+$dbi->do($CREATE_TABLE->{2});
|
|
| 673 |
+$dbi->insert('table2', {key1 => 1, key3 => 5});
|
|
| 674 |
+$rows = $dbi->select([qw/table1 table2/], |
|
| 675 |
+ ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
| 676 |
+ {'table1.key2' => 2},
|
|
| 677 |
+ "where table1.key1 = table2.key1")->fetch_all_hash; |
|
| 678 |
+is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
|