File Coverage

blib/lib/Aniki/Plugin/RangeConditionMaker.pm
Criterion Covered Total %
statement 32 32 100.0
branch 16 20 80.0
condition 6 6 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 60 65 92.3


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