- added {key => ..., value => ...} syntax to DBIx...
...::Custom::Mapper map method
| ... | ... |
@@ -1,4 +1,7 @@ |
| 1 | 1 |
0.1728 |
| 2 |
+ |
|
| 3 |
+ - added {key => ..., value => ...} syntax to DBIx::Custom::Mapper map method
|
|
| 4 |
+ ,and argument of string and code reference is DEPRECATED! |
|
| 2 | 5 |
- update_param is DEPRECATED, use assing_clause instead. |
| 3 | 6 |
- assing_param is renamed to assing_clause, assing_param is DEPRECATED! |
| 4 | 7 |
- insert_param is renamed to values_clause, insert_param is DEPRECATED! |
| ... | ... |
@@ -18,30 +18,45 @@ has [qw/param/], |
| 18 | 18 |
sub map {
|
| 19 | 19 |
my ($self, %rule) = @_; |
| 20 | 20 |
my $param = $self->param; |
| 21 |
- $rule{$_} = $rule{$_} for @{$self->pass};
|
|
| 21 |
+ $rule{$_} = {key => $_} for @{$self->pass};
|
|
| 22 | 22 |
|
| 23 | 23 |
# Mapping |
| 24 | 24 |
my $new_param = {};
|
| 25 | 25 |
foreach my $key (keys %rule) {
|
| 26 | 26 |
|
| 27 |
- my $value_cb; |
|
| 28 |
- my $condition; |
|
| 29 |
- my $new_key; |
|
| 27 |
+ my $mapping = $rule{$key};
|
|
| 30 | 28 |
|
| 31 | 29 |
# Get mapping information |
| 32 |
- $rule{$key} = [$rule{$key}] if ref $rule{$key} ne 'ARRAY';
|
|
| 33 |
- foreach my $some (@{$rule{$key}}) {
|
|
| 34 |
- $new_key = $some unless ref $some; |
|
| 35 |
- $condition = $some->{condition} if ref $some eq 'HASH';
|
|
| 36 |
- $value_cb = $some if ref $some eq 'CODE'; |
|
| 30 |
+ my $new_key; |
|
| 31 |
+ my $value; |
|
| 32 |
+ my $condition; |
|
| 33 |
+ |
|
| 34 |
+ if (ref $mapping eq 'ARRAY') {
|
|
| 35 |
+ $new_key = $mapping->[0]; |
|
| 36 |
+ $value = $mapping->[1]; |
|
| 37 |
+ $condition = $mapping->[2]; |
|
| 37 | 38 |
} |
| 39 |
+ elsif (ref $mapping eq 'HASH') {
|
|
| 40 |
+ $new_key = $mapping->{key};
|
|
| 41 |
+ $value = $mapping->{value};
|
|
| 42 |
+ $condition = $mapping->{condition};
|
|
| 43 |
+ } |
|
| 44 |
+ elsif (!ref $mapping) {
|
|
| 45 |
+ $new_key = $mapping; |
|
| 46 |
+ warn qq/map method's string value "$mapping" is DEPRECATED. / . |
|
| 47 |
+ qq/use {key => ...} syntax instead/
|
|
| 48 |
+ } |
|
| 49 |
+ elsif (ref $mapping eq 'CODE') {
|
|
| 50 |
+ $value = $mapping; |
|
| 51 |
+ warn qq/map method's code reference value "$mapping" is DEPRECATED. / . |
|
| 52 |
+ qq/use {value => ...} syntax instead/
|
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 38 | 55 |
$new_key = $key unless defined $new_key; |
| 39 |
- $value_cb ||= sub { $_[0] };
|
|
| 40 | 56 |
$condition ||= $self->condition; |
| 41 | 57 |
$condition = $self->_condition_to_sub($condition); |
| 42 | 58 |
|
| 43 | 59 |
# Map parameter |
| 44 |
- my $value; |
|
| 45 | 60 |
if (ref $condition eq 'CODE') {
|
| 46 | 61 |
if (ref $param->{$key} eq 'ARRAY') {
|
| 47 | 62 |
$new_param->{$new_key} = [];
|
| ... | ... |
@@ -52,8 +67,11 @@ sub map {
|
| 52 | 67 |
} |
| 53 | 68 |
} |
| 54 | 69 |
else {
|
| 55 |
- $new_param->{$new_key} = $value_cb->($param->{$key})
|
|
| 56 |
- if $condition->($param->{$key});
|
|
| 70 |
+ if ($condition->($param->{$key})) {
|
|
| 71 |
+ $new_param->{$new_key} = defined $value
|
|
| 72 |
+ ? $value->($param->{$key})
|
|
| 73 |
+ : $param->{$key};
|
|
| 74 |
+ } |
|
| 57 | 75 |
} |
| 58 | 76 |
} |
| 59 | 77 |
elsif ($condition eq 'exists') {
|
| ... | ... |
@@ -66,8 +84,11 @@ sub map {
|
| 66 | 84 |
} |
| 67 | 85 |
} |
| 68 | 86 |
else {
|
| 69 |
- $new_param->{$new_key} = $value_cb->($param->{$key})
|
|
| 70 |
- if exists $param->{$key};
|
|
| 87 |
+ if (exists $param->{$key}) {
|
|
| 88 |
+ $new_param->{$new_key} = defined $value
|
|
| 89 |
+ ? $value->($param->{$key})
|
|
| 90 |
+ : $param->{$key};
|
|
| 91 |
+ } |
|
| 71 | 92 |
} |
| 72 | 93 |
} |
| 73 | 94 |
else { croak qq/Condition must be code reference or "exists" / . _subname }
|
| ... | ... |
@@ -179,12 +200,12 @@ and implements the following new ones. |
| 179 | 200 |
=head2 C<map> |
| 180 | 201 |
|
| 181 | 202 |
my $new_param = $mapper->map( |
| 182 |
- price => 'book.price', # Key |
|
| 183 |
- title => sub { '%' . $_[0] . '%'}, # Value
|
|
| 184 |
- author => ['book.author', sub { '%' . $_[0] . '%'}] # Key and value
|
|
| 203 |
+ price => {key => 'book.price'}
|
|
| 204 |
+ title => {value => sub { '%' . $_[0] . '%'}}
|
|
| 205 |
+ author => ['book.author' => sub { '%' . $_[0] . '%'}] # Key and value
|
|
| 185 | 206 |
); |
| 186 | 207 |
|
| 187 |
-Map C<param>'s key and value and return new parameter. |
|
| 208 |
+Map C<param> into new parameter. |
|
| 188 | 209 |
|
| 189 | 210 |
For example, if C<param> is set to |
| 190 | 211 |
|
| ... | ... |
@@ -217,15 +238,15 @@ You can set change mapping condition by C<condition> attribute. |
| 217 | 238 |
Or you can set C<condtion> option for each key. |
| 218 | 239 |
|
| 219 | 240 |
my $new_param = $mapper->map( |
| 220 |
- price => ['book.price', {condition => 'defined'}]
|
|
| 221 |
- title => [sub { '%' . $_[0] . '%'}, {condition => 'defined'}] # Value
|
|
| 222 |
- author => ['book.author', sub { '%' . $_[0] . '%'}, condtion => 'exists']
|
|
| 241 |
+ price => {key => 'book.price', condition => 'defined'}]
|
|
| 242 |
+ title => {value => sub { '%' . $_[0] . '%'}, condition => 'defined'}
|
|
| 243 |
+ author => ['book.author', sub { '%' . $_[0] . '%'}, 'exists']
|
|
| 223 | 244 |
); |
| 224 | 245 |
|
| 225 | 246 |
If C<pass> attrivute is set, the keys and value is copied without change. |
| 226 | 247 |
|
| 227 | 248 |
$mapper->pass([qw/title author/]); |
| 228 |
- my $new_param = $mapper->map(price => 'book.price'); |
|
| 249 |
+ my $new_param = $mapper->map(price => {key => 'book.price'});
|
|
| 229 | 250 |
|
| 230 | 251 |
The following hash reference |
| 231 | 252 |
|
| ... | ... |
@@ -13,7 +13,7 @@ plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
|
| 13 | 13 |
|
| 14 | 14 |
plan 'no_plan'; |
| 15 | 15 |
|
| 16 |
-#$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
|
|
| 16 |
+$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
|
|
| 17 | 17 |
sub test { print "# $_[0]\n" }
|
| 18 | 18 |
|
| 19 | 19 |
# Constant |
| ... | ... |
@@ -2551,60 +2551,59 @@ is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
|
| 2551 | 2551 |
test 'mapper'; |
| 2552 | 2552 |
$dbi = DBIx::Custom->connect; |
| 2553 | 2553 |
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
|
| 2554 |
- id => "$table1.id", |
|
| 2555 |
- author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2556 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2554 |
+ id => {key => "$table1.id"},
|
|
| 2555 |
+ author => ["$table1.author" => sub { '%' . $_[0] . '%' }],
|
|
| 2556 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2557 | 2557 |
); |
| 2558 | 2558 |
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
|
| 2559 | 2559 |
"$table1.price" => 1900}); |
| 2560 | 2560 |
|
| 2561 | 2561 |
$dbi = DBIx::Custom->connect; |
| 2562 | 2562 |
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
|
| 2563 |
- id => "$table1.id", |
|
| 2563 |
+ id => {key => "$table1.id"},
|
|
| 2564 | 2564 |
author => ["$table1.author" => $dbi->like_value], |
| 2565 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2565 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2566 | 2566 |
); |
| 2567 | 2567 |
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
|
| 2568 | 2568 |
"$table1.price" => 1900}); |
| 2569 | 2569 |
|
| 2570 | 2570 |
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
|
| 2571 |
- id => "$table1.id", |
|
| 2572 |
- author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2573 |
- price => ["$table1.price", sub { '%' . $_[0] . '%' },
|
|
| 2574 |
- {condition => sub { $_[0] eq 0 }}]
|
|
| 2571 |
+ id => {key => "$table1.id"},
|
|
| 2572 |
+ author => ["$table1.author" => sub { '%' . $_[0] . '%' }],
|
|
| 2573 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, sub { $_[0] eq 0 }]
|
|
| 2575 | 2574 |
); |
| 2576 | 2575 |
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
|
| 2577 | 2576 |
|
| 2578 | 2577 |
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
|
| 2579 |
- id => "$table1.id", |
|
| 2580 |
- author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
|
| 2581 |
- price => ["$table1.price", sub { '%' . $_[0] . '%' },
|
|
| 2582 |
- {condition => sub { $_[0] eq 1 }}]
|
|
| 2578 |
+ id => {key => "$table1.id"},
|
|
| 2579 |
+ author => ["$table1.author" => sub { '%' . $_[0] . '%' }],
|
|
| 2580 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, sub { $_[0] eq 1 }]
|
|
| 2583 | 2581 |
); |
| 2584 | 2582 |
is_deeply($param, {});
|
| 2585 | 2583 |
|
| 2586 | 2584 |
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
|
| 2587 |
- id => "$table1.id", |
|
| 2588 |
- price => ["$table1.price", {condition => 'exists'}]
|
|
| 2585 |
+ id => {key => "$table1.id"},
|
|
| 2586 |
+ price => {key => "$table1.price", condition => 'exists'}
|
|
| 2589 | 2587 |
); |
| 2590 | 2588 |
is_deeply($param, {"$table1.price" => undef});
|
| 2591 | 2589 |
|
| 2592 | 2590 |
$param = $dbi->mapper(param => {price => 'a'})->map(
|
| 2593 |
- id => ["$table1.id", {condition => 'exists'}],
|
|
| 2594 |
- price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
|
|
| 2591 |
+ id => {key => "$table1.id", condition => 'exists'},
|
|
| 2592 |
+ price => ["$table1.price", sub { '%' . $_[0] }, 'exists']
|
|
| 2595 | 2593 |
); |
| 2596 | 2594 |
is_deeply($param, {"$table1.price" => '%a'});
|
| 2597 | 2595 |
|
| 2598 | 2596 |
$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
|
| 2599 |
- id => ["$table1.id"], |
|
| 2597 |
+ id => {key => "$table1.id"},
|
|
| 2600 | 2598 |
price => ["$table1.price", sub { '%' . $_[0] }]
|
| 2601 | 2599 |
); |
| 2602 | 2600 |
is_deeply($param, {"$table1.price" => '%a'});
|
| 2603 | 2601 |
|
| 2604 |
-$param = $dbi->mapper(param => {price => 'a'})->map(
|
|
| 2605 |
- price => sub { '%' . $_[0] }
|
|
| 2602 |
+$param = $dbi->mapper(param => {price => 'a', author => 'b'})->map(
|
|
| 2603 |
+ price => sub { '%' . $_[0] },
|
|
| 2604 |
+ author => 'book.author' |
|
| 2606 | 2605 |
); |
| 2607 |
-is_deeply($param, {price => '%a'});
|
|
| 2606 |
+is_deeply($param, {price => '%a', 'book.author' => 'b'});
|
|
| 2608 | 2607 |
|
| 2609 | 2608 |
eval { $dbi->execute("drop table $table1") };
|
| 2610 | 2609 |
$dbi->execute($create_table1); |
| ... | ... |
@@ -2640,6 +2639,7 @@ $result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]});
|
| 2640 | 2639 |
$row = $result->all; |
| 2641 | 2640 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
| 2642 | 2641 |
|
| 2642 |
+ |
|
| 2643 | 2643 |
$where = $dbi->where; |
| 2644 | 2644 |
$where->clause(['and', ":${key1}{=}"]);
|
| 2645 | 2645 |
$param = $dbi->mapper(param => {$key1 => 0}, condition => 'length')
|
| ... | ... |
@@ -2666,6 +2666,7 @@ $result = $dbi->execute("select * from $table1 $where", {$key1 => 1});
|
| 2666 | 2666 |
$row = $result->all; |
| 2667 | 2667 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]);
|
| 2668 | 2668 |
|
| 2669 |
+ |
|
| 2669 | 2670 |
$where = $dbi->where; |
| 2670 | 2671 |
$where->clause(['and', ":${key1}{=}"]);
|
| 2671 | 2672 |
$param = $dbi->mapper(param => {$key1 => 7}, condition => sub { ($_[0] || '') eq 5 })->map;
|
| ... | ... |
@@ -2676,9 +2677,9 @@ is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]);
|
| 2676 | 2677 |
|
| 2677 | 2678 |
$where = $dbi->where; |
| 2678 | 2679 |
$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
|
| 2679 |
- id => "$table1.id", |
|
| 2680 |
+ id => {key => "$table1.id"},
|
|
| 2680 | 2681 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
| 2681 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2682 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2682 | 2683 |
); |
| 2683 | 2684 |
$where->param($param); |
| 2684 | 2685 |
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
|
| ... | ... |
@@ -2686,66 +2687,66 @@ is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
|
| 2686 | 2687 |
|
| 2687 | 2688 |
$where = $dbi->where; |
| 2688 | 2689 |
$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
|
| 2689 |
- id => "$table1.id", |
|
| 2690 |
+ id => {key => "$table1.id"},
|
|
| 2690 | 2691 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
| 2691 |
- price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 0 }}]
|
|
| 2692 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, sub { $_[0] eq 0 }]
|
|
| 2692 | 2693 |
); |
| 2693 | 2694 |
$where->param($param); |
| 2694 | 2695 |
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
|
| 2695 | 2696 |
|
| 2696 | 2697 |
$where = $dbi->where; |
| 2697 | 2698 |
$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
|
| 2698 |
- id => "$table1.id", |
|
| 2699 |
+ id => {key => "$table1.id"},
|
|
| 2699 | 2700 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
| 2700 |
- price => ["$table1.price", sub { '%' . $_[0] . '%' }, {condition => sub { $_[0] eq 1 }}]
|
|
| 2701 |
+ price => ["$table1.price", sub { '%' . $_[0] . '%' }, sub { $_[0] eq 1 }]
|
|
| 2701 | 2702 |
); |
| 2702 | 2703 |
$where->param($param); |
| 2703 | 2704 |
is_deeply($where->param, {});
|
| 2704 | 2705 |
|
| 2705 | 2706 |
$where = $dbi->where; |
| 2706 | 2707 |
$param = $dbi->mapper(param => {id => undef, author => undef, price => undef}, condition => 'exists')->map(
|
| 2707 |
- id => "$table1.id", |
|
| 2708 |
- price => ["$table1.price", {condition => 'exists'}]
|
|
| 2708 |
+ id => {key => "$table1.id"},
|
|
| 2709 |
+ price => {key => "$table1.price", condition => 'exists'}
|
|
| 2709 | 2710 |
); |
| 2710 | 2711 |
is_deeply($param, {"$table1.id" => undef,"$table1.price" => undef});
|
| 2711 | 2712 |
|
| 2712 | 2713 |
$where = $dbi->where; |
| 2713 | 2714 |
$param = $dbi->mapper(param => {price => 'a'})->map(
|
| 2714 |
- id => ["$table1.id", {condition => 'exists'}],
|
|
| 2715 |
- price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
|
|
| 2715 |
+ id => {key => "$table1.id", condition => 'exists'},
|
|
| 2716 |
+ price => ["$table1.price", sub { '%' . $_[0] }, 'exists']
|
|
| 2716 | 2717 |
); |
| 2717 | 2718 |
is_deeply($param, {"$table1.price" => '%a'});
|
| 2718 | 2719 |
|
| 2719 | 2720 |
$where = $dbi->where; |
| 2720 | 2721 |
$param = $dbi->mapper(param => {id => [1, 2], author => 'Ken', price => 1900})->map(
|
| 2721 |
- id => "$table1.id", |
|
| 2722 |
+ id => {key => "$table1.id"},
|
|
| 2722 | 2723 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
| 2723 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2724 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2724 | 2725 |
); |
| 2725 | 2726 |
is_deeply($param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%',
|
| 2726 | 2727 |
"$table1.price" => 1900}); |
| 2727 | 2728 |
|
| 2728 | 2729 |
$where = $dbi->where; |
| 2729 | 2730 |
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900}, condition => 'length')->map(
|
| 2730 |
- id => "$table1.id", |
|
| 2731 |
+ id => {key => "$table1.id"},
|
|
| 2731 | 2732 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }],
|
| 2732 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2733 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2733 | 2734 |
); |
| 2734 | 2735 |
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
|
| 2735 | 2736 |
"$table1.price" => 1900}); |
| 2736 | 2737 |
|
| 2737 | 2738 |
$where = $dbi->where; |
| 2738 | 2739 |
$param = $dbi->mapper(param => {id => ['', ''], author => 'Ken', price => 1900})->map(
|
| 2739 |
- id => ["$table1.id", {condition => 'length'}],
|
|
| 2740 |
- author => ["$table1.author", sub { '%' . $_[0] . '%' }, {condition => 'defined'}],
|
|
| 2741 |
- price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
|
|
| 2740 |
+ id => {key => "$table1.id", condition => 'length'},
|
|
| 2741 |
+ author => ["$table1.author", sub { '%' . $_[0] . '%' }, 'defined'],
|
|
| 2742 |
+ price => {key => "$table1.price", condition => sub { $_[0] eq 1900 }}
|
|
| 2742 | 2743 |
); |
| 2743 | 2744 |
is_deeply($param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%',
|
| 2744 | 2745 |
"$table1.price" => 1900}); |
| 2745 | 2746 |
|
| 2746 | 2747 |
$where = $dbi->where; |
| 2747 | 2748 |
$param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass => [qw/id author/])
|
| 2748 |
- ->map(price => 'book.price'); |
|
| 2749 |
+ ->map(price => {key => 'book.price'});
|
|
| 2749 | 2750 |
is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
|
| 2750 | 2751 |
|
| 2751 | 2752 |
test 'order'; |