File Coverage

blib/lib/SQL/OOP/Where.pm
Criterion Covered Total %
statement 62 82 75.6
branch 19 34 55.8
condition 10 25 40.0
subroutine 15 19 78.9
pod 14 14 100.0
total 120 174 68.9


line stmt bran cond sub pod time code
1             package SQL::OOP::Where;
2 15     15   2336 use strict;
  15         30  
  15         477  
3 15     15   72 use warnings;
  15         23  
  15         374  
4 15     15   82 use Scalar::Util qw(blessed);
  15         36  
  15         739  
5 15     15   23102 use SQL::OOP::ID;
  15         34  
  15         19842  
6              
7             ### ---
8             ### Constructor
9             ### ---
10             sub new {
11 24     24 1 18356 my $class = shift;
12 24         90 return bless {}, $class;
13             }
14              
15             ### ---
16             ### SQL::Abstract style AND factory
17             ### ---
18             sub and_hash {
19 0     0 1 0 warn 'Deprecated infavor of and_abstract';
20 0         0 my ($class, $hash_ref, $op) = @_;
21 0   0     0 return _append_hash($class->and, $hash_ref, $op || '=');
22             }
23              
24             ### ---
25             ### SQL::Abstract style OR factory
26             ### ---
27             sub or_hash {
28 0     0 1 0 warn 'Deprecated infavor of or_abstract';
29 0         0 my ($class, $hash_ref, $op) = @_;
30 0   0     0 return _append_hash($class->or, $hash_ref, $op || '=');
31             }
32              
33             ### ---
34             ### SQL::Abstract style AND factory
35             ### ---
36             sub and_abstract {
37 4     4 1 4726 my ($class, $array_ref, $op) = @_;
38 4   100     13 return _append_hash($class->and, $array_ref, $op || '=');
39             }
40              
41             ### ---
42             ### SQL::Abstract style OR factory
43             ### ---
44             sub or_abstract {
45 1     1 1 884 my ($class, $array_ref, $op) = @_;
46 1   50     4 return _append_hash($class->or, $array_ref, $op || '=');
47             }
48              
49             ### ---
50             ### SQL::Abstract style AND factory backend
51             ### ---
52             sub _append_hash {
53 5     5   9 my ($obj, $array_ref, $op) = @_;
54 5 50       18 my @copied = ref $array_ref eq 'HASH' ? %{$array_ref} : @{$array_ref};
  0         0  
  5         15  
55 5         23 while (my($key, $val) = splice @copied, 0, 2) {
56 8   50     28 $obj->append(__PACKAGE__->cmp($op || '=', $key, $val));
57             }
58 5         15 return $obj;
59             }
60              
61             ### ---
62             ### AND factory
63             ### ---
64             sub and {
65 16     16 1 55 my ($class, @array) = @_;
66 16         56 return SQL::OOP::Array->new(@array)->set_sepa(' AND ');
67             }
68              
69             ### ---
70             ### OR factory
71             ### ---
72             sub or {
73 5     5 1 14 my ($class, @array) = @_;
74 5         21 return SQL::OOP::Array->new(@array)->set_sepa(' OR ');
75             }
76              
77             ### ---
78             ### binary operator expression factory
79             ### ---
80             sub cmp {
81 61     61 1 4629 my ($self, $op, $key, $val) = @_;
82 61 50       167 if (scalar @_ != 4) {
83 0         0 die 'Not enough args given';
84             }
85 61 100 66     357 if ($key && defined $val) {
86 56         207 my $quoted = SQL::OOP::ID->new($key);
87 56 100       141 if (ref $val) {
88 10         41 return SQL::OOP::Array->new($quoted->to_string, $val)->set_sepa(" $op ");
89             }
90 46         184 return SQL::OOP::Base->new($quoted->to_string. qq( $op ?), [$val]);
91             }
92             }
93              
94             ### ---
95             ### binary operator expression factory with sub query in value [DEPRECATED]
96             ### ---
97             sub cmp_nested {
98 0     0 1 0 warn 'cmp_nested is deprecated! Use cmp instead';
99 0         0 my ($self, $op, $key, $val) = @_;
100 0 0       0 if (scalar @_ != 4) {
101 0         0 die 'Not enough args given';
102             }
103 0 0 0     0 if ($key && defined $val) {
104 0         0 my $quoted = SQL::OOP::ID->new($key);
105 0         0 return SQL::OOP::Array->new($quoted->to_string, $val)->set_sepa(" $op ");
106             }
107             }
108              
109             ### ---
110             ### IS NULL factory
111             ### ---
112             sub is_null {
113 3     3 1 737 my ($self, $key) = @_;
114 3 50       19 if ($key) {
115 3         12 my $quoted = SQL::OOP::ID->new($key);
116 3         14 return SQL::OOP::Base->new($quoted->to_string. qq( IS NULL));
117             }
118             }
119              
120             ### ---
121             ### IS NOT NULL factory
122             ### ---
123             sub is_not_null {
124 0     0 1 0 my ($self, $key) = @_;
125 0 0       0 if ($key) {
126 0         0 my $quoted = SQL::OOP::ID->new($key);
127 0         0 return SQL::OOP::Base->new($quoted->to_string. qq( IS NOT NULL));
128             }
129             }
130              
131             ### ---
132             ### BETWEEN ? AND ? factory
133             ### ---
134             sub between {
135 6     6 1 1304 my ($self, $key, $val1, $val2) = @_;
136 6 50       19 if ($key) {
137 6 100 66     38 if (defined $val1 and defined $val2) {
    50          
138 4         14 my $quoted = SQL::OOP::ID->new($key)->to_string;
139 4         31 my $str = $quoted. qq( BETWEEN ? AND ?);
140 4         17 return SQL::OOP::Base->new($str, [$val1, $val2]);
141             } elsif (defined $val1) {
142 2         9 return $self->cmp('>=', $key, $val1);
143             } else {
144 0         0 return $self->cmp('<=', $key, $val2);
145             }
146             }
147             }
148              
149             ### ---
150             ### IN factory
151             ### ---
152             sub in {
153 2     2 1 12 my ($self, $key, $val) = @_;
154 2 50       8 if ($key) {
155 2         9 my $quoted = SQL::OOP::ID->new($key)->to_string;
156 2 100 33     40 if (ref $val eq 'ARRAY') {
    50          
157 1         4 my $placeholder = '?, ' x scalar @$val;
158 1         3 $placeholder = substr($placeholder, 0, -2);
159 1         5 return SQL::OOP::Base->new("$quoted IN ($placeholder)", $val);
160             } elsif (blessed($val) && $val->isa('SQL::OOP::Base')) {
161 1         5 return SQL::OOP::Array->new($quoted, $val)->set_sepa(" IN ");
162             }
163             }
164             }
165              
166             ### ---
167             ### NOT IN factory
168             ### ---
169             sub not_in {
170 2     2 1 14 my ($self, $key, $val) = @_;
171 2 50       8 if ($key) {
172 2         10 my $quoted = SQL::OOP::ID->new($key)->to_string;
173 2 100 33     43 if (ref $val eq 'ARRAY') {
    50          
174 1         3 my $placeholder = '?, ' x scalar @$val;
175 1         3 $placeholder = substr($placeholder, 0, -2);
176 1         6 return SQL::OOP::Base->new("$quoted NOT IN ($placeholder)", $val);
177             } elsif (blessed($val) && $val->isa('SQL::OOP::Base')) {
178 1         4 return SQL::OOP::Array->new($quoted, $val)->set_sepa(" NOT IN ");
179             }
180             }
181             }
182              
183             1;
184              
185             __END__