| ... | ... |
@@ -1,5 +1,6 @@ |
| 1 | 1 |
0.1687 |
| 2 | 2 |
- added EXPERIMENTAL type_rule method |
| 3 |
+ - added EXPERIMENTAL execute() type_rule_off option |
|
| 3 | 4 |
0.1686 |
| 4 | 5 |
- select() column option can receive array reference in array. |
| 5 | 6 |
This is EXPERIMENTAL |
| ... | ... |
@@ -22,7 +22,7 @@ use Encode qw/encode encode_utf8 decode_utf8/; |
| 22 | 22 |
use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
|
| 23 | 23 |
use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
|
| 24 | 24 |
|
| 25 |
-our @COMMON_ARGS = qw/table query filter type id primary_key/; |
|
| 25 |
+our @COMMON_ARGS = qw/table query filter type id primary_key type_rule_off/; |
|
| 26 | 26 |
|
| 27 | 27 |
__PACKAGE__->attr( |
| 28 | 28 |
[qw/connector dsn password user/], |
| ... | ... |
@@ -442,6 +442,7 @@ sub execute {
|
| 442 | 442 |
$filter = _array_to_hash($filter); |
| 443 | 443 |
my $type = delete $args{type};
|
| 444 | 444 |
$type = _array_to_hash($type); |
| 445 |
+ my $type_rule_off = delete $args{type_rule_off};
|
|
| 445 | 446 |
|
| 446 | 447 |
# Check argument names |
| 447 | 448 |
foreach my $name (keys %args) {
|
| ... | ... |
@@ -489,21 +490,23 @@ sub execute {
|
| 489 | 490 |
|
| 490 | 491 |
# Type rule |
| 491 | 492 |
my $applied_filter = {};
|
| 492 |
- foreach my $name (keys %$param) {
|
|
| 493 |
- my $table; |
|
| 494 |
- my $column; |
|
| 495 |
- if ($name =~ /(?:(.+)\.)?(.+)/) {
|
|
| 496 |
- $table = $1; |
|
| 497 |
- $column = $2; |
|
| 498 |
- } |
|
| 499 |
- $table ||= $main_table; |
|
| 500 |
- |
|
| 501 |
- my $into = $self->{_into} || {};
|
|
| 502 |
- if (defined $table && $into->{$table} &&
|
|
| 503 |
- (my $rule = $into->{$table}->{$column}))
|
|
| 504 |
- {
|
|
| 505 |
- $applied_filter->{$column} = $rule;
|
|
| 506 |
- $applied_filter->{"$table.$column"} = $rule;
|
|
| 493 |
+ unless ($type_rule_off) {
|
|
| 494 |
+ foreach my $name (keys %$param) {
|
|
| 495 |
+ my $table; |
|
| 496 |
+ my $column; |
|
| 497 |
+ if ($name =~ /(?:(.+)\.)?(.+)/) {
|
|
| 498 |
+ $table = $1; |
|
| 499 |
+ $column = $2; |
|
| 500 |
+ } |
|
| 501 |
+ $table ||= $main_table; |
|
| 502 |
+ |
|
| 503 |
+ my $into = $self->{_into} || {};
|
|
| 504 |
+ if (defined $table && $into->{$table} &&
|
|
| 505 |
+ (my $rule = $into->{$table}->{$column}))
|
|
| 506 |
+ {
|
|
| 507 |
+ $applied_filter->{$column} = $rule;
|
|
| 508 |
+ $applied_filter->{"$table.$column"} = $rule;
|
|
| 509 |
+ } |
|
| 507 | 510 |
} |
| 508 | 511 |
} |
| 509 | 512 |
|
| ... | ... |
@@ -591,6 +594,7 @@ sub execute {
|
| 591 | 594 |
filter => $filter->{in} || {},
|
| 592 | 595 |
end_filter => $filter->{end} || {},
|
| 593 | 596 |
type_rule => $self->type_rule, |
| 597 |
+ type_rule_off => $type_rule_off |
|
| 594 | 598 |
); |
| 595 | 599 |
|
| 596 | 600 |
return $result; |
| ... | ... |
@@ -2050,6 +2054,28 @@ filter name registerd by C<register_filter()>. |
| 2050 | 2054 |
); |
| 2051 | 2055 |
|
| 2052 | 2056 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
| 2057 |
+C<filter> option is also available |
|
| 2058 |
+by C<insert()>, C<update()>, C<delete()>, C<select()> |
|
| 2059 |
+ |
|
| 2060 |
+=item C<type> |
|
| 2061 |
+ |
|
| 2062 |
+Specify database data type. |
|
| 2063 |
+ |
|
| 2064 |
+ type => [image => DBI::SQL_BLOB] |
|
| 2065 |
+ type => [[qw/image audio/] => DBI::SQL_BLOB] |
|
| 2066 |
+ |
|
| 2067 |
+This is used to bind paramter by C<bind_param()> of statment handle. |
|
| 2068 |
+ |
|
| 2069 |
+ $sth->bind_param($pos, $value, DBI::SQL_BLOB); |
|
| 2070 |
+ |
|
| 2071 |
+C<type> option is also available |
|
| 2072 |
+by C<insert()>, C<update()>, C<delete()>, C<select()>. |
|
| 2073 |
+ |
|
| 2074 |
+=item C<type_rule_off> EXPERIMENTAL |
|
| 2075 |
+ |
|
| 2076 |
+ type_rule_off => 1 |
|
| 2077 |
+ |
|
| 2078 |
+Trun type rule off. |
|
| 2053 | 2079 |
|
| 2054 | 2080 |
=back |
| 2055 | 2081 |
|
| ... | ... |
@@ -2098,34 +2124,7 @@ Append statement to last of SQL. This is string. |
| 2098 | 2124 |
|
| 2099 | 2125 |
=item C<filter> |
| 2100 | 2126 |
|
| 2101 |
-Filter, executed before data is send to database. This is array reference. |
|
| 2102 |
-Filter value is code reference or |
|
| 2103 |
-filter name registerd by C<register_filter()>. |
|
| 2104 |
- |
|
| 2105 |
- # Basic |
|
| 2106 |
- $dbi->delete( |
|
| 2107 |
- filter => {
|
|
| 2108 |
- title => sub { uc $_[0] }
|
|
| 2109 |
- author => sub { uc $_[0] }
|
|
| 2110 |
- } |
|
| 2111 |
- ); |
|
| 2112 |
- |
|
| 2113 |
- # At once (use array reference) |
|
| 2114 |
- $dbi->delete( |
|
| 2115 |
- filter => [ |
|
| 2116 |
- [qw/title author/] => sub { uc $_[0] }
|
|
| 2117 |
- ] |
|
| 2118 |
- ); |
|
| 2119 |
- |
|
| 2120 |
- # Filter name |
|
| 2121 |
- $dbi->delete( |
|
| 2122 |
- filter => {
|
|
| 2123 |
- title => 'upper_case', |
|
| 2124 |
- author => 'upper_case' |
|
| 2125 |
- } |
|
| 2126 |
- ); |
|
| 2127 |
- |
|
| 2128 |
-These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
| 2127 |
+Same as C<execute> method's C<filter> option. |
|
| 2129 | 2128 |
|
| 2130 | 2129 |
=item C<query> |
| 2131 | 2130 |
|
| ... | ... |
@@ -2162,6 +2161,14 @@ The above is same as the followin ones. |
| 2162 | 2161 |
|
| 2163 | 2162 |
See C<id> option. |
| 2164 | 2163 |
|
| 2164 |
+=item C<type> |
|
| 2165 |
+ |
|
| 2166 |
+Same as C<execute> method's C<type> option. |
|
| 2167 |
+ |
|
| 2168 |
+=item C<type_rule_off> EXPERIMENTAL |
|
| 2169 |
+ |
|
| 2170 |
+Same as C<execute> method's C<type_rule_off> option. |
|
| 2171 |
+ |
|
| 2165 | 2172 |
=back |
| 2166 | 2173 |
|
| 2167 | 2174 |
=head2 C<delete_all> |
| ... | ... |
@@ -2208,45 +2215,56 @@ Append statement to last of SQL. This is string. |
| 2208 | 2215 |
|
| 2209 | 2216 |
=item C<filter> |
| 2210 | 2217 |
|
| 2211 |
-Filter, executed before data is send to database. This is array reference. |
|
| 2212 |
-Filter value is code reference or |
|
| 2213 |
-filter name registerd by C<register_filter()>. |
|
| 2218 |
+Same as C<execute> method's C<filter> option. |
|
| 2219 |
+ |
|
| 2220 |
+=item C<query> |
|
| 2221 |
+ |
|
| 2222 |
+Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
| 2223 |
+This is true or false value. |
|
| 2224 |
+ |
|
| 2225 |
+ my $query = $dbi->insert(query => 1); |
|
| 2226 |
+ |
|
| 2227 |
+You can check SQL. |
|
| 2228 |
+ |
|
| 2229 |
+ my $sql = $query->sql; |
|
| 2230 |
+ |
|
| 2231 |
+=item C<id> |
|
| 2232 |
+ |
|
| 2233 |
+Insert using primary_key. |
|
| 2214 | 2234 |
|
| 2215 |
- # Basic |
|
| 2216 | 2235 |
$dbi->insert( |
| 2217 |
- filter => {
|
|
| 2218 |
- title => sub { uc $_[0] }
|
|
| 2219 |
- author => sub { uc $_[0] }
|
|
| 2220 |
- } |
|
| 2236 |
+ primary_key => 'id', |
|
| 2237 |
+ id => 4, |
|
| 2238 |
+ param => {title => 'Perl', author => 'Ken'}
|
|
| 2221 | 2239 |
); |
| 2222 |
- |
|
| 2223 |
- # At once (use array reference) |
|
| 2240 |
+ |
|
| 2224 | 2241 |
$dbi->insert( |
| 2225 |
- filter => [ |
|
| 2226 |
- [qw/title author/] => sub { uc $_[0] }
|
|
| 2227 |
- ] |
|
| 2242 |
+ primary_key => ['id1', 'id2'], |
|
| 2243 |
+ id => [4, 5], |
|
| 2244 |
+ param => {title => 'Perl', author => 'Ken'}
|
|
| 2228 | 2245 |
); |
| 2229 |
- |
|
| 2230 |
- # Filter name |
|
| 2246 |
+ |
|
| 2247 |
+The above is same as the followin ones. |
|
| 2248 |
+ |
|
| 2231 | 2249 |
$dbi->insert( |
| 2232 |
- filter => {
|
|
| 2233 |
- title => 'upper_case', |
|
| 2234 |
- author => 'upper_case' |
|
| 2235 |
- } |
|
| 2250 |
+ param => {id => 4, title => 'Perl', author => 'Ken'}
|
|
| 2236 | 2251 |
); |
| 2237 | 2252 |
|
| 2238 |
-These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
| 2253 |
+ $dbi->insert( |
|
| 2254 |
+ param => {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'}
|
|
| 2255 |
+ ); |
|
| 2239 | 2256 |
|
| 2240 |
-=item C<query> |
|
| 2257 |
+=item C<primary_key> |
|
| 2241 | 2258 |
|
| 2242 |
-Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
| 2243 |
-This is true or false value. |
|
| 2259 |
+See C<id> description. |
|
| 2244 | 2260 |
|
| 2245 |
- my $query = $dbi->insert(query => 1); |
|
| 2261 |
+=item C<type> |
|
| 2246 | 2262 |
|
| 2247 |
-You can check SQL. |
|
| 2263 |
+Same as C<execute> method's C<type> option. |
|
| 2248 | 2264 |
|
| 2249 |
- my $sql = $query->sql; |
|
| 2265 |
+=item C<type_rule_off> EXPERIMENTAL |
|
| 2266 |
+ |
|
| 2267 |
+Turn type rule off. |
|
| 2250 | 2268 |
|
| 2251 | 2269 |
=back |
| 2252 | 2270 |
|
| ... | ... |
@@ -2576,34 +2594,7 @@ This option is for Oracle and SQL Server paging process. |
| 2576 | 2594 |
|
| 2577 | 2595 |
=item C<filter> |
| 2578 | 2596 |
|
| 2579 |
-Filter, executed before data is send to database. This is array reference. |
|
| 2580 |
-Filter value is code reference or |
|
| 2581 |
-filter name registerd by C<register_filter()>. |
|
| 2582 |
- |
|
| 2583 |
- # Basic |
|
| 2584 |
- $dbi->select( |
|
| 2585 |
- filter => {
|
|
| 2586 |
- title => sub { uc $_[0] }
|
|
| 2587 |
- author => sub { uc $_[0] }
|
|
| 2588 |
- } |
|
| 2589 |
- ); |
|
| 2590 |
- |
|
| 2591 |
- # At once (use array reference) |
|
| 2592 |
- $dbi->select( |
|
| 2593 |
- filter => [ |
|
| 2594 |
- [qw/title author/] => sub { uc $_[0] }
|
|
| 2595 |
- ] |
|
| 2596 |
- ); |
|
| 2597 |
- |
|
| 2598 |
- # Filter name |
|
| 2599 |
- $dbi->select( |
|
| 2600 |
- filter => {
|
|
| 2601 |
- title => 'upper_case', |
|
| 2602 |
- author => 'upper_case' |
|
| 2603 |
- } |
|
| 2604 |
- ); |
|
| 2605 |
- |
|
| 2606 |
-These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
| 2597 |
+Same as C<execute> method's C<filter> option. |
|
| 2607 | 2598 |
|
| 2608 | 2599 |
=item C<query> |
| 2609 | 2600 |
|
| ... | ... |
@@ -2618,14 +2609,11 @@ You can check SQL. |
| 2618 | 2609 |
|
| 2619 | 2610 |
=item C<type> |
| 2620 | 2611 |
|
| 2621 |
-Specify database data type. |
|
| 2622 |
- |
|
| 2623 |
- $dbi->select(type => [image => DBI::SQL_BLOB]); |
|
| 2624 |
- $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]); |
|
| 2612 |
+Same as C<execute> method's C<type> option. |
|
| 2625 | 2613 |
|
| 2626 |
-This is used to bind paramter by C<bind_param()> of statment handle. |
|
| 2614 |
+=item C<type_rule_off> EXPERIMENTAL |
|
| 2627 | 2615 |
|
| 2628 |
- $sth->bind_param($pos, $value, DBI::SQL_BLOB); |
|
| 2616 |
+Same as C<execute> method's C<type_rule_off> option. |
|
| 2629 | 2617 |
|
| 2630 | 2618 |
=back |
| 2631 | 2619 |
|
| ... | ... |
@@ -2693,34 +2681,7 @@ Append statement to last of SQL. This is string. |
| 2693 | 2681 |
|
| 2694 | 2682 |
=item C<filter> |
| 2695 | 2683 |
|
| 2696 |
-Filter, executed before data is send to database. This is array reference. |
|
| 2697 |
-Filter value is code reference or |
|
| 2698 |
-filter name registerd by C<register_filter()>. |
|
| 2699 |
- |
|
| 2700 |
- # Basic |
|
| 2701 |
- $dbi->update( |
|
| 2702 |
- filter => {
|
|
| 2703 |
- title => sub { uc $_[0] }
|
|
| 2704 |
- author => sub { uc $_[0] }
|
|
| 2705 |
- } |
|
| 2706 |
- ); |
|
| 2707 |
- |
|
| 2708 |
- # At once (use array reference) |
|
| 2709 |
- $dbi->update( |
|
| 2710 |
- filter => [ |
|
| 2711 |
- [qw/title author/] => sub { uc $_[0] }
|
|
| 2712 |
- ] |
|
| 2713 |
- ); |
|
| 2714 |
- |
|
| 2715 |
- # Filter name |
|
| 2716 |
- $dbi->update( |
|
| 2717 |
- filter => {
|
|
| 2718 |
- title => 'upper_case', |
|
| 2719 |
- author => 'upper_case' |
|
| 2720 |
- } |
|
| 2721 |
- ); |
|
| 2722 |
- |
|
| 2723 |
-These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
| 2684 |
+Same as C<execute> method's C<filter> option. |
|
| 2724 | 2685 |
|
| 2725 | 2686 |
=item C<query> |
| 2726 | 2687 |
|
| ... | ... |
@@ -2733,30 +2694,6 @@ You can check SQL. |
| 2733 | 2694 |
|
| 2734 | 2695 |
my $sql = $query->sql; |
| 2735 | 2696 |
|
| 2736 |
-Insert using primary_key. |
|
| 2737 |
- |
|
| 2738 |
- $dbi->insert( |
|
| 2739 |
- primary_key => 'id', |
|
| 2740 |
- id => 4, |
|
| 2741 |
- param => {title => 'Perl', author => 'Ken'}
|
|
| 2742 |
- ); |
|
| 2743 |
- |
|
| 2744 |
- $dbi->insert( |
|
| 2745 |
- primary_key => ['id1', 'id2'], |
|
| 2746 |
- id => [4, 5], |
|
| 2747 |
- param => {title => 'Perl', author => 'Ken'}
|
|
| 2748 |
- ); |
|
| 2749 |
- |
|
| 2750 |
-The above is same as the followin ones. |
|
| 2751 |
- |
|
| 2752 |
- $dbi->insert( |
|
| 2753 |
- param => {id => 4, title => 'Perl', author => 'Ken'}
|
|
| 2754 |
- ); |
|
| 2755 |
- |
|
| 2756 |
- $dbi->insert( |
|
| 2757 |
- param => {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'}
|
|
| 2758 |
- ); |
|
| 2759 |
- |
|
| 2760 | 2697 |
=item C<id> |
| 2761 | 2698 |
|
| 2762 | 2699 |
update using primary_key. |
| ... | ... |
@@ -2789,6 +2726,14 @@ The above is same as the followin ones. |
| 2789 | 2726 |
|
| 2790 | 2727 |
See C<id> option. |
| 2791 | 2728 |
|
| 2729 |
+=item C<type> |
|
| 2730 |
+ |
|
| 2731 |
+Same as C<execute> method's C<type> option. |
|
| 2732 |
+ |
|
| 2733 |
+=item C<type_rule_off> EXPERIMENTAL |
|
| 2734 |
+ |
|
| 2735 |
+Turn type rule off. |
|
| 2736 |
+ |
|
| 2792 | 2737 |
=back |
| 2793 | 2738 |
|
| 2794 | 2739 |
=head2 C<update_all> |
| ... | ... |
@@ -9,7 +9,7 @@ use Carp 'croak'; |
| 9 | 9 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
| 10 | 10 |
|
| 11 | 11 |
__PACKAGE__->attr( |
| 12 |
- [qw/filters sth type_rule/], |
|
| 12 |
+ [qw/filters sth type_rule type_rule_off/], |
|
| 13 | 13 |
stash => sub { {} }
|
| 14 | 14 |
); |
| 15 | 15 |
|
| ... | ... |
@@ -111,7 +111,7 @@ sub fetch {
|
| 111 | 111 |
|
| 112 | 112 |
for (my $i = 0; $i < @$columns; $i++) {
|
| 113 | 113 |
|
| 114 |
- if ($type_rule->{$types->[$i]} &&
|
|
| 114 |
+ if (!$self->type_rule_off && $type_rule->{$types->[$i]} &&
|
|
| 115 | 115 |
(my $rule = $type_rule->{$types->[$i]}->{from}))
|
| 116 | 116 |
{
|
| 117 | 117 |
$row[$i] = $rule->($row[$i]); |
| ... | ... |
@@ -181,7 +181,7 @@ sub fetch_hash {
|
| 181 | 181 |
for (my $i = 0; $i < @$columns; $i++) {
|
| 182 | 182 |
|
| 183 | 183 |
# Type rule |
| 184 |
- if ($type_rule->{$types->[$i]} &&
|
|
| 184 |
+ if (!$self->type_rule_off && $type_rule->{$types->[$i]} &&
|
|
| 185 | 185 |
(my $rule = $type_rule->{$types->[$i]}->{from}))
|
| 186 | 186 |
{
|
| 187 | 187 |
$row->[$i] = $rule->($row->[$i]); |
| ... | ... |
@@ -389,6 +389,13 @@ Resistered filters. |
| 389 | 389 |
|
| 390 | 390 |
Statement handle of L<DBI>. |
| 391 | 391 |
|
| 392 |
+=head2 C<type_rule_off> EXPERIMENTAL |
|
| 393 |
+ |
|
| 394 |
+ my $type_rule_off = $result->type_rule_off; |
|
| 395 |
+ $result = $result->type_rule_off(1); |
|
| 396 |
+ |
|
| 397 |
+Turn type rule off. |
|
| 398 |
+ |
|
| 392 | 399 |
=head1 METHODS |
| 393 | 400 |
|
| 394 | 401 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
| ... | ... |
@@ -2636,4 +2636,30 @@ $row = $result->one; |
| 2636 | 2636 |
is($row->{key1}, 'A');
|
| 2637 | 2637 |
is($row->{key2}, 'B');
|
| 2638 | 2638 |
|
| 2639 |
+ |
|
| 2640 |
+test 'type_rule_off'; |
|
| 2641 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2642 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2643 |
+$dbi->type_rule( |
|
| 2644 |
+ Date => {
|
|
| 2645 |
+ from => sub { $_[0] * 2 },
|
|
| 2646 |
+ into => sub { $_[0] * 3 },
|
|
| 2647 |
+ } |
|
| 2648 |
+); |
|
| 2649 |
+$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
|
|
| 2650 |
+$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
| 2651 |
+is($result->fetch->[0], 2); |
|
| 2652 |
+ |
|
| 2653 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2654 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2655 |
+$dbi->type_rule( |
|
| 2656 |
+ Date => {
|
|
| 2657 |
+ from => sub { $_[0] * 2 },
|
|
| 2658 |
+ into => sub { $_[0] * 3 },
|
|
| 2659 |
+ } |
|
| 2660 |
+); |
|
| 2661 |
+$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
|
|
| 2662 |
+$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
| 2663 |
+is($result->one->{key1}, 2);
|
|
| 2664 |
+ |
|
| 2639 | 2665 |
=cut |