- insert, insert_at, update, update_at can receive...
...odd number arguments,
... | ... |
@@ -1,3 +1,6 @@ |
1 |
+0.1685 |
|
2 |
+ - insert, insert_at, update, update_at can receive odd number arguments, |
|
3 |
+ first one is parameter. |
|
1 | 4 |
0.1684 |
2 | 5 |
- added DBIx::Custom::Result all method, this is alias for fetch_hash_all |
3 | 6 |
- added DBIx::Custom::Result one method, this is alias for fetch_hash_first |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1684'; |
|
3 |
+our $VERSION = '0.1685'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -574,13 +574,17 @@ sub execute { |
574 | 574 |
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/; |
575 | 575 |
|
576 | 576 |
sub insert { |
577 |
- my ($self, %args) = @_; |
|
577 |
+ my $self = shift; |
|
578 | 578 |
|
579 | 579 |
# Arguments |
580 |
+ my $param; |
|
581 |
+ $param = shift if @_ % 2; |
|
582 |
+ my %args = @_; |
|
580 | 583 |
my $table = delete $args{table}; |
581 | 584 |
croak qq{"table" option must be specified } . _subname |
582 | 585 |
unless $table; |
583 |
- my $param = delete $args{param} || {}; |
|
586 |
+ my $p = delete $args{param} || {}; |
|
587 |
+ $param ||= $p; |
|
584 | 588 |
my $append = delete $args{append} || ''; |
585 | 589 |
my $query_return = delete $args{query}; |
586 | 590 |
|
... | ... |
@@ -615,13 +619,17 @@ sub insert { |
615 | 619 |
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1); |
616 | 620 |
|
617 | 621 |
sub insert_at { |
618 |
- my ($self, %args) = @_; |
|
622 |
+ my $self = shift; |
|
619 | 623 |
|
620 | 624 |
# Arguments |
625 |
+ my $param; |
|
626 |
+ $param = shift if @_ % 2; |
|
627 |
+ my %args = @_; |
|
621 | 628 |
my $primary_keys = delete $args{primary_key}; |
622 | 629 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
623 | 630 |
my $where = delete $args{where}; |
624 |
- my $param = delete $args{param}; |
|
631 |
+ my $p = delete $args{param} || {}; |
|
632 |
+ $param ||= $p; |
|
625 | 633 |
|
626 | 634 |
# Check arguments |
627 | 635 |
foreach my $name (keys %args) { |
... | ... |
@@ -1001,13 +1009,17 @@ our %UPDATE_ARGS |
1001 | 1009 |
= map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all where_param/; |
1002 | 1010 |
|
1003 | 1011 |
sub update { |
1004 |
- my ($self, %args) = @_; |
|
1012 |
+ my $self = shift; |
|
1005 | 1013 |
|
1006 | 1014 |
# Arguments |
1015 |
+ my $param; |
|
1016 |
+ $param = shift if @_ % 2; |
|
1017 |
+ my %args = @_; |
|
1007 | 1018 |
my $table = delete $args{table} || ''; |
1008 | 1019 |
croak qq{"table" option must be specified } . _subname |
1009 | 1020 |
unless $table; |
1010 |
- my $param = delete $args{param} || {}; |
|
1021 |
+ my $p = delete $args{param} || {}; |
|
1022 |
+ $param ||= $p; |
|
1011 | 1023 |
my $where = delete $args{where} || {}; |
1012 | 1024 |
my $where_param = delete $args{where_param} || {}; |
1013 | 1025 |
my $append = delete $args{append} || ''; |
... | ... |
@@ -1069,14 +1081,18 @@ sub update_all { shift->update(allow_update_all => 1, @_) }; |
1069 | 1081 |
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1); |
1070 | 1082 |
|
1071 | 1083 |
sub update_at { |
1072 |
- my ($self, %args) = @_; |
|
1084 |
+ my $self = shift; |
|
1073 | 1085 |
|
1074 | 1086 |
# Arguments |
1087 |
+ my $param; |
|
1088 |
+ $param = shift if @_ % 2; |
|
1089 |
+ my %args = @_; |
|
1075 | 1090 |
my $primary_keys = delete $args{primary_key}; |
1076 | 1091 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
1077 | 1092 |
my $where = delete $args{where}; |
1093 |
+ my $p = delete $args{param} || {}; |
|
1094 |
+ $param ||= $p; |
|
1078 | 1095 |
|
1079 |
- |
|
1080 | 1096 |
# Check arguments |
1081 | 1097 |
foreach my $name (keys %args) { |
1082 | 1098 |
croak qq{"$name" is wrong option } . _subname |
... | ... |
@@ -1086,7 +1102,7 @@ sub update_at { |
1086 | 1102 |
# Create where parameter |
1087 | 1103 |
my $where_param = $self->_create_where_param($where, $primary_keys); |
1088 | 1104 |
|
1089 |
- return $self->update(where => $where_param, %args); |
|
1105 |
+ return $self->update(where => $where_param, param => $param, %args); |
|
1090 | 1106 |
} |
1091 | 1107 |
|
1092 | 1108 |
sub update_param { |
... | ... |
@@ -2062,28 +2078,32 @@ Place holder is set to 5. |
2062 | 2078 |
=head2 C<insert> |
2063 | 2079 |
|
2064 | 2080 |
$dbi->insert( |
2065 |
- table => 'book', |
|
2066 |
- param => {title => 'Perl', author => 'Ken'} |
|
2081 |
+ param => {title => 'Perl', author => 'Ken'}, |
|
2082 |
+ table => 'book' |
|
2067 | 2083 |
); |
2068 |
- |
|
2084 |
+ |
|
2069 | 2085 |
Insert statement. |
2070 | 2086 |
|
2071 | 2087 |
The following opitons are currently available. |
2072 | 2088 |
|
2073 | 2089 |
=over 4 |
2074 | 2090 |
|
2075 |
-=item C<table> |
|
2076 |
- |
|
2077 |
-Table name. |
|
2078 |
- |
|
2079 |
- $dbi->insert(table => 'book'); |
|
2080 |
- |
|
2081 | 2091 |
=item C<param> |
2082 | 2092 |
|
2083 | 2093 |
Insert data. This is hash reference. |
2084 | 2094 |
|
2085 | 2095 |
$dbi->insert(param => {title => 'Perl'}); |
2086 | 2096 |
|
2097 |
+If arguments is odd numbers, first argument is received as C<param>. |
|
2098 |
+ |
|
2099 |
+ $dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
|
2100 |
+ |
|
2101 |
+=item C<table> |
|
2102 |
+ |
|
2103 |
+Table name. |
|
2104 |
+ |
|
2105 |
+ $dbi->insert(table => 'book'); |
|
2106 |
+ |
|
2087 | 2107 |
=item C<append> |
2088 | 2108 |
|
2089 | 2109 |
Append statement to last of SQL. This is string. |
... | ... |
@@ -2607,18 +2627,26 @@ The following opitons are currently available. |
2607 | 2627 |
|
2608 | 2628 |
=over 4 |
2609 | 2629 |
|
2610 |
-=item C<table> |
|
2611 |
- |
|
2612 |
-Table name. |
|
2613 |
- |
|
2614 |
- $dbi->update(table => 'book'); |
|
2615 |
- |
|
2616 | 2630 |
=item C<param> |
2617 | 2631 |
|
2618 | 2632 |
Update data. This is hash reference. |
2619 | 2633 |
|
2620 | 2634 |
$dbi->update(param => {title => 'Perl'}); |
2621 | 2635 |
|
2636 |
+If arguments is odd numbers, first argument is received as C<param>. |
|
2637 |
+ |
|
2638 |
+ $dbi->update( |
|
2639 |
+ {title => 'Perl'}, |
|
2640 |
+ table => 'book', |
|
2641 |
+ where => {author => 'Ken'} |
|
2642 |
+ ); |
|
2643 |
+ |
|
2644 |
+=item C<table> |
|
2645 |
+ |
|
2646 |
+Table name. |
|
2647 |
+ |
|
2648 |
+ $dbi->update(table => 'book'); |
|
2649 |
+ |
|
2622 | 2650 |
=item C<where> |
2623 | 2651 |
|
2624 | 2652 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object |
... | ... |
@@ -285,6 +285,14 @@ $result = $dbi->execute('select * from "table"'); |
285 | 285 |
$rows = $result->fetch_hash_all; |
286 | 286 |
is_deeply($rows, [{select => 2}], "reserved word"); |
287 | 287 |
|
288 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
289 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
290 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
291 |
+$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
292 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
293 |
+$rows = $result->fetch_hash_all; |
|
294 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
295 |
+ |
|
288 | 296 |
test 'update'; |
289 | 297 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
290 | 298 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -399,6 +407,17 @@ $result = $dbi->execute('select * from "table"'); |
399 | 407 |
$rows = $result->fetch_hash_all; |
400 | 408 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
401 | 409 |
|
410 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
411 |
+$dbi->execute($CREATE_TABLE->{1}); |
|
412 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
413 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
414 |
+$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1}); |
|
415 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
416 |
+$rows = $result->fetch_hash_all; |
|
417 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
418 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
419 |
+ "basic"); |
|
420 |
+ |
|
402 | 421 |
test 'update_all'; |
403 | 422 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
404 | 423 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -1621,6 +1640,18 @@ eval { |
1621 | 1640 |
}; |
1622 | 1641 |
like($@, qr/must be/); |
1623 | 1642 |
|
1643 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1644 |
+$dbi->execute($CREATE_TABLE->{1}); |
|
1645 |
+$dbi->insert_at( |
|
1646 |
+ {key3 => 3}, |
|
1647 |
+ primary_key => ['key1', 'key2'], |
|
1648 |
+ table => 'table1', |
|
1649 |
+ where => [1, 2], |
|
1650 |
+); |
|
1651 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
1652 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1653 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3); |
|
1654 |
+ |
|
1624 | 1655 |
test 'update_at'; |
1625 | 1656 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
1626 | 1657 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -1647,6 +1678,19 @@ is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
1647 | 1678 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
1648 | 1679 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4); |
1649 | 1680 |
|
1681 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
1682 |
+$dbi->execute($CREATE_TABLE->{1}); |
|
1683 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1684 |
+$dbi->update_at( |
|
1685 |
+ {key3 => 4}, |
|
1686 |
+ table => 'table1', |
|
1687 |
+ primary_key => ['key1', 'key2'], |
|
1688 |
+ where => [1, 2] |
|
1689 |
+); |
|
1690 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
1691 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2); |
|
1692 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4); |
|
1693 |
+ |
|
1650 | 1694 |
test 'select_at'; |
1651 | 1695 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
1652 | 1696 |
$dbi->execute($CREATE_TABLE->{1}); |