| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Aniki::Plugin::RangeConditionMaker; | 
| 2 | 2 |  |  | 2 |  | 1141 | use 5.014002; | 
|  | 2 |  |  |  |  | 10 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 15 | use namespace::autoclean; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 5 | 2 |  |  | 2 |  | 805 | use Mouse::Role; | 
|  | 2 |  |  |  |  | 963 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 754 | use Carp qw/carp croak/; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 112 |  | 
| 8 | 2 |  |  | 2 |  | 361 | use SQL::QueryMaker qw/sql_gt sql_lt sql_and/; | 
|  | 2 |  |  |  |  | 2109 |  | 
|  | 2 |  |  |  |  | 462 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub make_range_condition { | 
| 11 | 11 |  |  | 11 | 0 | 80 | my ($self, $range) = @_; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 11 |  |  |  |  | 23 | my %total_range_condition; | 
| 14 | 11 |  |  |  |  | 35 | for my $type (qw/lower upper gt lt/) { | 
| 15 | 44 | 100 |  |  |  | 180 | next unless exists $range->{$type}; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 8 | 50 |  |  |  | 28 | ref $range->{$type} eq 'HASH' | 
| 18 |  |  |  |  |  |  | or croak "$type condition *MUST* be HashRef."; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 8 | 50 | 100 |  |  | 66 | my $func = $type eq 'lower' || $type eq 'gt' ? \&sql_gt | 
|  |  | 100 | 66 |  |  |  |  | 
| 21 |  |  |  |  |  |  | : $type eq 'upper' || $type eq 'lt' ? \&sql_lt | 
| 22 |  |  |  |  |  |  | : die "Unknown type: $type"; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 8 |  |  |  |  | 20 | my $range_condition = $range->{$type}; | 
| 25 | 8 |  |  |  |  | 28 | for my $column (keys %$range_condition) { | 
| 26 |  |  |  |  |  |  | croak "$column cannot be a reference value for range condition" | 
| 27 | 8 | 50 |  |  |  | 28 | if ref $range_condition->{$column}; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 8 |  |  |  |  | 36 | my $condition = $func->($range_condition->{$column}); | 
| 30 |  |  |  |  |  |  | $total_range_condition{$column} = | 
| 31 | 8 | 100 |  |  |  | 315 | exists $total_range_condition{$column} ? sql_and([$total_range_condition{$column}, $condition]) | 
| 32 |  |  |  |  |  |  | : $condition; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 11 | 100 |  |  |  | 60 | return %total_range_condition ? \%total_range_condition : undef; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | 1; | 
| 40 |  |  |  |  |  |  | __END__ | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =pod | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =encoding utf-8 | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =head1 NAME | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | Aniki::Plugin::RangeConditionMaker - range condition maker | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | package MyDB; | 
| 53 |  |  |  |  |  |  | use Mouse v2.4.5; | 
| 54 |  |  |  |  |  |  | extends qw/Aniki/; | 
| 55 |  |  |  |  |  |  | with qw/Aniki::Plugin::RangeConditionMaker/; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | package main; | 
| 58 |  |  |  |  |  |  | my $db = MyDB->new(...); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | my $where = $db->make_range_condition({ upper => { id => 10 } }); | 
| 61 |  |  |  |  |  |  | # => { id => { '<' => 10 } } | 
| 62 |  |  |  |  |  |  | $where = $db->make_range_condition({ lower => { id => 0 } }); | 
| 63 |  |  |  |  |  |  | # => { id => { '>' =>  0 } } | 
| 64 |  |  |  |  |  |  | $where = $db->make_range_condition({ upper => { id => 10 }, lower => { id => 0 } }); | 
| 65 |  |  |  |  |  |  | # => { id => [-and => { '>' => 0 }, { '<' => 10 }] } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head1 LICENSE | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | Copyright (C) karupanerura. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 72 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 AUTHOR | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | karupanerura E<lt>karupa@cpan.orgE<gt> | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =cut |