- added EXPERIMENTAL pass attribute to DBIx::Custom...
...::Mapper
| ... | ... |
@@ -1,3 +1,6 @@ |
| 1 |
+0.1722 |
|
| 2 |
+ - added EXPERIMENTAL pass attribute to DBIx::Custom::Mapper |
|
| 3 |
+ - removed EXPERIMENTAL ignore attribute from DBIx::Custom::Mapper |
|
| 1 | 4 |
0.1721 |
| 2 | 5 |
- added EXPERIMENTAL DBIx::Custom::Mapper class |
| 3 | 6 |
- added EXPERIMENTAL mapper method to DBIx::Custom |
| ... | ... |
@@ -1,7 +1,7 @@ |
| 1 | 1 |
package DBIx::Custom; |
| 2 | 2 |
use Object::Simple -base; |
| 3 | 3 |
|
| 4 |
-our $VERSION = '0.1721'; |
|
| 4 |
+our $VERSION = '0.1722'; |
|
| 5 | 5 |
use 5.008001; |
| 6 | 6 |
|
| 7 | 7 |
use Carp 'croak'; |
| ... | ... |
@@ -15,6 +15,7 @@ use DBIx::Custom::Tag; |
| 15 | 15 |
use DBIx::Custom::Order; |
| 16 | 16 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
| 17 | 17 |
use DBIx::Custom::Mapper; |
| 18 |
+use DBIx::Custom::NotExists; |
|
| 18 | 19 |
use Encode qw/encode encode_utf8 decode_utf8/; |
| 19 | 20 |
use Scalar::Util qw/weaken/; |
| 20 | 21 |
|
| ... | ... |
@@ -854,8 +855,7 @@ sub new {
|
| 854 | 855 |
return $self; |
| 855 | 856 |
} |
| 856 | 857 |
|
| 857 |
-my $not_exists = bless {}, 'DBIx::Custom::NotExists';
|
|
| 858 |
-sub not_exists { $not_exists }
|
|
| 858 |
+sub not_exists { DBIx::Custom::NotExists->singleton }
|
|
| 859 | 859 |
|
| 860 | 860 |
sub order {
|
| 861 | 861 |
my $self = shift; |
| ... | ... |
@@ -2844,6 +2844,12 @@ You can get model object by C<model>. |
| 2844 | 2844 |
|
| 2845 | 2845 |
See L<DBIx::Custom::Model> to know model features. |
| 2846 | 2846 |
|
| 2847 |
+=head2 C<mapper EXPERIMETNAL> |
|
| 2848 |
+ |
|
| 2849 |
+ my $mapper = $dbi->mapper(param => $param); |
|
| 2850 |
+ |
|
| 2851 |
+Create a new L<DBIx::Custom::Mapper> object. |
|
| 2852 |
+ |
|
| 2847 | 2853 |
=head2 C<map_param> EXPERIMENTAL |
| 2848 | 2854 |
|
| 2849 | 2855 |
my $map_param = $dbi->map_param( |
| ... | ... |
@@ -1,25 +1,29 @@ |
| 1 | 1 |
package DBIx::Custom::Mapper; |
| 2 | 2 |
use Object::Simple -base; |
| 3 | 3 |
|
| 4 |
+use DBIx::Custom::NotExists; |
|
| 5 |
+ |
|
| 4 | 6 |
use Carp 'croak'; |
| 5 | 7 |
use DBIx::Custom::Util '_subname'; |
| 6 | 8 |
|
| 7 | 9 |
# Carp trust relationship |
| 8 | 10 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
| 9 | 11 |
|
| 10 |
-has [qw/param pass/], |
|
| 12 |
+has [qw/param/], |
|
| 11 | 13 |
condition => sub {
|
| 12 | 14 |
sub { defined $_[0] && length $_[0] }
|
| 13 |
- }; |
|
| 15 |
+ }, |
|
| 16 |
+ pass => sub { [] };
|
|
| 14 | 17 |
|
| 15 | 18 |
sub map {
|
| 16 | 19 |
my ($self, %rule) = @_; |
| 17 | 20 |
my $param = $self->param; |
| 21 |
+ $rule{$_} = $rule{$_} for @{$self->pass};
|
|
| 18 | 22 |
|
| 19 | 23 |
# Mapping |
| 20 | 24 |
my $new_param = {};
|
| 21 |
- foreach my $key (keys %$param) {
|
|
| 22 |
- |
|
| 25 |
+ foreach my $key (keys %rule) {
|
|
| 26 |
+ |
|
| 23 | 27 |
my $value_cb; |
| 24 | 28 |
my $condition; |
| 25 | 29 |
my $new_key; |
| ... | ... |
@@ -51,7 +55,7 @@ sub map {
|
| 51 | 55 |
for (my $i = 0; $i < @{$param->{$key}}; $i++) {
|
| 52 | 56 |
$new_param->{$new_key}->[$i]
|
| 53 | 57 |
= $condition->($param->{$key}->[$i]) ? $param->{$key}->[$i]
|
| 54 |
- : $self->dbi->not_exists; |
|
| 58 |
+ : DBIx::Custom::NotExists->singleton; |
|
| 55 | 59 |
} |
| 56 | 60 |
} |
| 57 | 61 |
else {
|
| ... | ... |
@@ -65,7 +69,7 @@ sub map {
|
| 65 | 69 |
for (my $i = 0; $i < @{$param->{$key}}; $i++) {
|
| 66 | 70 |
$new_param->{$new_key}->[$i]
|
| 67 | 71 |
= exists $param->{$key}->[$i] ? $param->{$key}->[$i]
|
| 68 |
- : $self->dbi->not_exists; |
|
| 72 |
+ : DBIx::Custom::NotExists->singleton; |
|
| 69 | 73 |
} |
| 70 | 74 |
} |
| 71 | 75 |
else {
|
| ... | ... |
@@ -131,12 +135,12 @@ DBIx::Custom::Mapper - Mapper of parameter EXPERIMENTAL |
| 131 | 135 |
|
| 132 | 136 |
Parameter. |
| 133 | 137 |
|
| 134 |
-=head2 C<ignore> |
|
| 138 |
+=head2 C<pass> |
|
| 135 | 139 |
|
| 136 |
- my $pass = $mapper->ignore; |
|
| 137 |
- $mapper = $mapper->ignore([qw/title author/]); |
|
| 140 |
+ my $pass = $mapper->pass; |
|
| 141 |
+ $mapper = $mapper->pass([qw/title author/]); |
|
| 138 | 142 |
|
| 139 |
-Ignored parameter keys when C<map> method is executed. |
|
| 143 |
+the key and value is copied without change when C<map> method is executed. |
|
| 140 | 144 |
|
| 141 | 145 |
=head2 C<condition> |
| 142 | 146 |
|
| ... | ... |
@@ -204,7 +208,6 @@ The following hash reference is returned. |
| 204 | 208 |
'book.price' => 1900, |
| 205 | 209 |
title => '%Perl%', |
| 206 | 210 |
'book.author' => '%Ken%', |
| 207 |
- issude_date => '2010-11-11' |
|
| 208 | 211 |
} |
| 209 | 212 |
|
| 210 | 213 |
By default, If the value has length, key and value is mapped. |
| ... | ... |
@@ -226,11 +229,17 @@ Or you can set C<condtion> option for each key. |
| 226 | 229 |
author => ['book.author', sub { '%' . $_[0] . '%'}, condtion => 'exists']
|
| 227 | 230 |
); |
| 228 | 231 |
|
| 229 |
-If C<ignore> is set, the keys is ignored. |
|
| 232 |
+If C<pass> attrivute is set, the keys and value is copied without change. |
|
| 230 | 233 |
|
| 231 |
- $mapper->ignore([qw/title author/]); |
|
| 234 |
+ $mapper->pass([qw/title author/]); |
|
| 235 |
+ my $new_param = $mapper->map(price => 'book.price'); |
|
| 236 |
+ |
|
| 237 |
+The following hash reference |
|
| 232 | 238 |
|
| 233 | 239 |
{title => 'Perl', author => 'Ken', price => 1900}
|
| 234 |
- is mapped to {price => 1900}
|
|
| 240 |
+ |
|
| 241 |
+is mapped to |
|
| 242 |
+ |
|
| 243 |
+ {title => 'Perl', author => 'Ken', 'book.price' => 1900}
|
|
| 235 | 244 |
|
| 236 | 245 |
=cut |
| ... | ... |
@@ -0,0 +1,26 @@ |
| 1 |
+package DBIx::Custom::NotExists; |
|
| 2 |
+ |
|
| 3 |
+use strict; |
|
| 4 |
+use warnings; |
|
| 5 |
+ |
|
| 6 |
+my $not_exists = bless {}, 'DBIx::Custom::NotExists';
|
|
| 7 |
+ |
|
| 8 |
+sub singleton { $not_exists }
|
|
| 9 |
+ |
|
| 10 |
+=head1 NAME |
|
| 11 |
+ |
|
| 12 |
+DBIx::Custom::NotExists |
|
| 13 |
+ |
|
| 14 |
+=head1 SYNOPSYS |
|
| 15 |
+ |
|
| 16 |
+ $not_exists = DBIx::Custom::NotExists->singleton; |
|
| 17 |
+ |
|
| 18 |
+=head1 METHODS |
|
| 19 |
+ |
|
| 20 |
+=head2 C<singleton> |
|
| 21 |
+ |
|
| 22 |
+ $not_exists = DBIx::Custom::NotExists->singleton; |
|
| 23 |
+ |
|
| 24 |
+L<DBIx::Custom::NotExists> singleton object. |
|
| 25 |
+ |
|
| 26 |
+=cut |
| ... | ... |
@@ -2597,8 +2597,8 @@ $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
|
| 2597 | 2597 |
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all; |
| 2598 | 2598 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
|
| 2599 | 2599 |
|
| 2600 |
+ |
|
| 2600 | 2601 |
test 'mapper'; |
| 2601 |
-$DB::single = 1; |
|
| 2602 | 2602 |
$dbi = DBIx::Custom->connect; |
| 2603 | 2603 |
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
|
| 2604 | 2604 |
id => "$table1.id", |
| ... | ... |
@@ -2624,7 +2624,6 @@ $param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
|
| 2624 | 2624 |
); |
| 2625 | 2625 |
is_deeply($param, {});
|
| 2626 | 2626 |
|
| 2627 |
-$DB::single = 1; |
|
| 2628 | 2627 |
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
|
| 2629 | 2628 |
id => "$table1.id", |
| 2630 | 2629 |
price => ["$table1.price", {condition => 'exists'}]
|
| ... | ... |
@@ -2643,6 +2642,148 @@ $param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
|
| 2643 | 2642 |
); |
| 2644 | 2643 |
is_deeply($param, {"$table1.price" => '%a'});
|
| 2645 | 2644 |
|
| 2645 |
+eval { $dbi->execute("drop table $table1") };
|
|
| 2646 |
+$dbi->execute($create_table1); |
|
| 2647 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
|
|
| 2648 |
+$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4});
|
|
| 2649 |
+ |
|
| 2650 |
+$where = $dbi->where; |
|
| 2651 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2652 |
+$param = $dbi->mapper(param => {$key1 => undef}, condition => 'defined')->map;
|
|
| 2653 |
+$where->param($param); |
|
| 2654 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
|
| 2655 |
+$row = $result->all; |
|
| 2656 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
|
| 2657 |
+ |
|
| 2658 |
+$where = $dbi->where; |
|
| 2659 |
+$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]);
|
|
| 2660 |
+$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'exists')->map;
|
|
| 2661 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
|
|
| 2662 |
+$row = $result->all; |
|
| 2663 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}]);
|
|
| 2664 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
|
|
| 2665 |
+$row = $result->all; |
|
| 2666 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}]);
|
|
| 2667 |
+ |
|
| 2668 |
+$where = $dbi->where; |
|
| 2669 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2670 |
+$param = $dbi->mapper(param => {$key1 => [undef, undef]}, condition => 'defined')->map;
|
|
| 2671 |
+$where->param($param); |
|
| 2672 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]});
|
|
| 2673 |
+$row = $result->all; |
|
| 2674 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
|
| 2675 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
|
|
| 2676 |
+$row = $result->all; |
|
| 2677 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
|
| 2678 |
+ |
|
| 2679 |
+$where = $dbi->where; |
|
| 2680 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2681 |
+$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
|
|
| 2682 |
+ ->pass([$key1, $key2])->map; |
|
| 2683 |
+$where->param($param); |
|
| 2684 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
|
| 2685 |
+$row = $result->all; |
|
| 2686 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}]);
|
|
| 2687 |
+ |
|
| 2688 |
+$where = $dbi->where; |
|
| 2689 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2690 |
+$param = $dbi->mapper(param => {$key1 => ''}, condition => 'length')->map;
|
|
| 2691 |
+$where->param($param); |
|
| 2692 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
|
| 2693 |
+$row = $result->all; |
|
| 2694 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
|
| 2695 |
+ |
|
| 2696 |
+$where = $dbi->where; |
|
| 2697 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2698 |
+$param = $dbi->mapper(param => {$key1 => 5}, condition => sub { ($_[0] || '') eq 5 })
|
|
| 2699 |
+ ->pass([$key1, $key2])->map; |
|
| 2700 |
+$where->param($param); |
|
| 2701 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
|
| 2702 |
+$row = $result->all; |
|
| 2703 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}]);
|
|
| 2704 |
+ |
|
| 2705 |
+$where = $dbi->where; |
|
| 2706 |
+$where->clause(['and', ":${key1}{=}"]);
|
|
| 2707 |
+$param = $dbi->mapper(param => {$key1 => 7}, condition => sub { ($_[0] || '') eq 5 })->map;
|
|
| 2708 |
+$where->param($param); |
|
| 2709 |
+$result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
|
| 2710 |
+$row = $result->all; |
|
| 2711 |
+is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
|
| 2712 |
+ |
|
| 2713 |
+$where = $dbi->where; |
|
| 2714 |
+$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
|
|
| 2715 |
+ id => "$table1.id", |
|
| 2716 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2717 |
+ price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2718 |
+); |
|
| 2719 |
+$where->param($param); |
|
| 2720 |
+is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
|
|
| 2721 |
+ "$table1.price" => 1900}); |
|
| 2722 |
+ |
|
| 2723 |
+$where = $dbi->where; |
|
| 2724 |
+$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
|
|
| 2725 |
+ id => "$table1.id", |
|
| 2726 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2727 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
|
|
| 2728 |
+); |
|
| 2729 |
+$where->param($param); |
|
| 2730 |
+is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
|
|
| 2731 |
+ |
|
| 2732 |
+$where = $dbi->where; |
|
| 2733 |
+$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
|
|
| 2734 |
+ id => "$table1.id", |
|
| 2735 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2736 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
|
|
| 2737 |
+); |
|
| 2738 |
+$where->param($param); |
|
| 2739 |
+is_deeply($where->param, {});
|
|
| 2740 |
+ |
|
| 2741 |
+$where = $dbi->where; |
|
| 2742 |
+$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
|
|
| 2743 |
+ id => "$table1.id", |
|
| 2744 |
+ price => ["$table1.price", {condition => 'exists'}]
|
|
| 2745 |
+); |
|
| 2746 |
+is_deeply($param, {"$table1.id" => undef,"$table1.price" => undef});
|
|
| 2747 |
+ |
|
| 2748 |
+$where = $dbi->where; |
|
| 2749 |
+$param = $dbi->mapper(param => {price => 'a'})->map(
|
|
| 2750 |
+ id => ["$table1.id", {condition => 'exists'}],
|
|
| 2751 |
+ price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
|
|
| 2752 |
+); |
|
| 2753 |
+is_deeply($param, {"$table1.price" => '%a'});
|
|
| 2754 |
+ |
|
| 2755 |
+$where = $dbi->where; |
|
| 2756 |
+$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
|
|
| 2757 |
+ id => "$table1.id", |
|
| 2758 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2759 |
+ price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2760 |
+); |
|
| 2761 |
+is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
|
|
| 2762 |
+ "$table1.price" => 1900}); |
|
| 2763 |
+ |
|
| 2764 |
+$where = $dbi->where; |
|
| 2765 |
+$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
|
|
| 2766 |
+ id => "$table1.id", |
|
| 2767 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2768 |
+ price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2769 |
+); |
|
| 2770 |
+is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
|
|
| 2771 |
+ "$table1.price" => 1900}); |
|
| 2772 |
+ |
|
| 2773 |
+$where = $dbi->where; |
|
| 2774 |
+$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
|
|
| 2775 |
+ id => ["$table1.id", {condition => 'length'}],
|
|
| 2776 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
|
|
| 2777 |
+ price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2778 |
+); |
|
| 2779 |
+is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
|
|
| 2780 |
+ "$table1.price" => 1900}); |
|
| 2781 |
+ |
|
| 2782 |
+$where = $dbi->where; |
|
| 2783 |
+$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
|
|
| 2784 |
+ ->map(price => 'book.price'); |
|
| 2785 |
+is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
|
|
| 2786 |
+ |
|
| 2646 | 2787 |
test 'map_param'; |
| 2647 | 2788 |
$dbi = DBIx::Custom->connect; |
| 2648 | 2789 |
$param = $dbi->map_param( |