... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1697 |
|
2 |
+ - added EXPERIMENTAL map_param method |
|
1 | 3 |
0.1696 |
2 | 4 |
- added new argument format update, delete, select method where option |
3 | 5 |
- create_query is DEPRECATED! use query option of each method instead. |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.1696'; |
|
4 |
+our $VERSION = '0.1670'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -590,6 +590,48 @@ sub include_model { |
590 | 590 |
return $self; |
591 | 591 |
} |
592 | 592 |
|
593 |
+sub map_param { |
|
594 |
+ my $self = shift; |
|
595 |
+ my $param = shift; |
|
596 |
+ my %map = @_; |
|
597 |
+ |
|
598 |
+ # Mapping |
|
599 |
+ my $map_param = {}; |
|
600 |
+ foreach my $key (keys %map) { |
|
601 |
+ my $value_cb; |
|
602 |
+ my $condition; |
|
603 |
+ my $map_key; |
|
604 |
+ |
|
605 |
+ # Get mapping information |
|
606 |
+ if (ref $map{$key} eq 'ARRAY') { |
|
607 |
+ foreach my $some (@{$map{$key}}) { |
|
608 |
+ $map_key = $some unless ref $some; |
|
609 |
+ $condition = $some->{if} if ref $some eq 'HASH'; |
|
610 |
+ $value_cb = $some if ref $some eq 'CODE'; |
|
611 |
+ } |
|
612 |
+ } |
|
613 |
+ else { |
|
614 |
+ $map_key = $map{$key}; |
|
615 |
+ } |
|
616 |
+ $value_cb ||= sub { $_[0] }; |
|
617 |
+ $condition ||= sub { defined $_[0] && length $_[0] }; |
|
618 |
+ |
|
619 |
+ # Map parameter |
|
620 |
+ my $value; |
|
621 |
+ if (ref $condition eq 'CODE') { |
|
622 |
+ $map_param->{$map_key} = $value_cb->($param->{$key}) |
|
623 |
+ if $condition->($param->{$key}); |
|
624 |
+ } |
|
625 |
+ elsif ($condition eq 'exists') { |
|
626 |
+ $map_param->{$map_key} = $value_cb->($param->{$key}) |
|
627 |
+ if exists $param->{$key}; |
|
628 |
+ } |
|
629 |
+ else { croak qq/Condition must be code reference or "exists" / . _subname } |
|
630 |
+ } |
|
631 |
+ |
|
632 |
+ return $map_param; |
|
633 |
+} |
|
634 |
+ |
|
593 | 635 |
sub merge_param { |
594 | 636 |
my ($self, @params) = @_; |
595 | 637 |
|
... | ... |
@@ -2337,6 +2379,54 @@ You can get model object by C<model>. |
2337 | 2379 |
|
2338 | 2380 |
See L<DBIx::Custom::Model> to know model features. |
2339 | 2381 |
|
2382 |
+=head2 C<map_param> EXPERIMENTAL |
|
2383 |
+ |
|
2384 |
+ my $map_param = $dbi->map_param( |
|
2385 |
+ {id => 1, authro => 'Ken', price => 1900}, |
|
2386 |
+ 'id' => 'book.id', |
|
2387 |
+ 'author' => ['book.author' => sub { '%' . $_[0] . '%' }], |
|
2388 |
+ 'price' => [ |
|
2389 |
+ 'book.price', {if => sub { length $_[0] }} |
|
2390 |
+ ] |
|
2391 |
+ ); |
|
2392 |
+ |
|
2393 |
+Map paramters to other key and value. First argument is original |
|
2394 |
+parameter. this is hash reference. Rest argument is mapping. |
|
2395 |
+By default, Mapping is done if the value length is not zero. |
|
2396 |
+ |
|
2397 |
+=over 4 |
|
2398 |
+ |
|
2399 |
+=item Key mapping |
|
2400 |
+ |
|
2401 |
+ 'id' => 'book.id' |
|
2402 |
+ |
|
2403 |
+This is only key mapping. Value is same as original one. |
|
2404 |
+ |
|
2405 |
+ (id => 1) is mapped to ('book.id' => 1) if value length is not zero. |
|
2406 |
+ |
|
2407 |
+=item Key and value mapping |
|
2408 |
+ |
|
2409 |
+ 'author' => ['book.author' => sub { '%' . $_[0] . '%' }] |
|
2410 |
+ |
|
2411 |
+This is key and value mapping. Frist element of array reference |
|
2412 |
+is mapped key name, second element is code reference to map the value. |
|
2413 |
+ |
|
2414 |
+ (author => 'Ken') is mapped to ('book.author' => '%Ken%') |
|
2415 |
+ if value length is not zero. |
|
2416 |
+ |
|
2417 |
+=item Condition |
|
2418 |
+ |
|
2419 |
+ 'price' => ['book.price', {if => 'exists'}] |
|
2420 |
+ 'price' => ['book.price', sub { '%' . $_[0] . '%' }, {if => 'exists'}] |
|
2421 |
+ 'price' => ['book.price', {if => sub { defined shift }}] |
|
2422 |
+ |
|
2423 |
+If you need condition, you can sepecify it. this is code reference |
|
2424 |
+or 'exists'. By default, condition is the following one. |
|
2425 |
+ |
|
2426 |
+ sub { defined $_[0] && length $_[0] } |
|
2427 |
+ |
|
2428 |
+=back |
|
2429 |
+ |
|
2340 | 2430 |
=head2 C<merge_param> |
2341 | 2431 |
|
2342 | 2432 |
my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2}); |
... | ... |
@@ -3204,4 +3204,48 @@ is($dbi->separator, '__'); |
3204 | 3204 |
eval { $dbi->separator('?') }; |
3205 | 3205 |
like($@, qr/Separator/); |
3206 | 3206 |
|
3207 |
+ |
|
3208 |
+test 'map_param'; |
|
3209 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
3210 |
+$param = $dbi->map_param( |
|
3211 |
+ {id => 1, author => 'Ken', price => 1900}, |
|
3212 |
+ id => 'book.id', |
|
3213 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3214 |
+ price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
3215 |
+); |
|
3216 |
+is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
|
3217 |
+ 'book.price' => 1900}); |
|
3218 |
+ |
|
3219 |
+$param = $dbi->map_param( |
|
3220 |
+ {id => 0, author => 0, price => 0}, |
|
3221 |
+ id => 'book.id', |
|
3222 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3223 |
+ price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
3224 |
+ {if => sub { $_[0] eq 0 }}] |
|
3225 |
+); |
|
3226 |
+is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
|
3227 |
+ |
|
3228 |
+$param = $dbi->map_param( |
|
3229 |
+ {id => '', author => '', price => ''}, |
|
3230 |
+ id => 'book.id', |
|
3231 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
3232 |
+ price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
3233 |
+ {if => sub { $_[0] eq 1 }}] |
|
3234 |
+); |
|
3235 |
+is_deeply($param, {}); |
|
3236 |
+ |
|
3237 |
+$param = $dbi->map_param( |
|
3238 |
+ {id => undef, author => undef, price => undef}, |
|
3239 |
+ id => 'book.id', |
|
3240 |
+ price => ['book.price', {if => 'exists'}] |
|
3241 |
+); |
|
3242 |
+is_deeply($param, {'book.price' => undef}); |
|
3243 |
+ |
|
3244 |
+$param = $dbi->map_param( |
|
3245 |
+ {price => 'a'}, |
|
3246 |
+ id => ['book.id', {if => 'exists'}], |
|
3247 |
+ price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
3248 |
+); |
|
3249 |
+is_deeply($param, {'book.price' => '%a'}); |
|
3250 |
+ |
|
3207 | 3251 |
=cut |