Romani/Query/Comparison.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 128 CC 13 SCAR 31.2
CC
13
Cov
0%
new
42.0
get_rvalue
30.0
add
20.0
CRAP 22.5
1  
2 package DBIx::Romani::Query::Comparison;
3  
4 use strict;
5  
6 # comparison types
7 our $EQUAL = '=';
8 our $NOT_EQUAL = '<>';
9 our $GREATER_THAN = '>';
10 our $GREATER_EQUAL = '>=';
11 our $LESS_THAN = '<';
12 our $LESS_EQUAL = '<=';
13 our $LIKE = 'LIKE';
14 our $NOT_LIKE = 'NOT LIKE';
15 our $ILIKE = 'ILIKE';
16 our $NOT_ILIKE = 'NOT ILIKE';
17 our $BETWEEN = 'BETWEEN';
18 our $IN = 'IN';
19 our $NOT_IN = 'NOT IN';
20 our $IS_NULL = 'IS NULL';
21 our $IS_NOT_NULL = 'IS NOT NULL';
22  
23 sub new
24 {
25 my $class = shift;
26 my $args = shift;
27  
28 my $rvalues_max;
29 my $type;
30  
31 if ( ref($args) eq 'HASH' )
32 {
33 $type = $args->{type};
34 }
35 else
36 {
37 $type = $args;
38 }
39  
40 if ( not defined $type )
41 {
42 $type = $EQUAL;
43 }
44  
45 if ( $type eq $BETWEEN )
46 {
47 $rvalues_max = 2;
48 }
49 elsif ( $type eq $IS_NULL or $type eq $IS_NOT_NULL )
50 {
51 $rvalues_max = 0;
52 }
53 elsif ( $type eq $IN or $type eq $NOT_IN )
54 {
55 # no limit!
56 $rvalues_max = undef;
57 }
58 else
59 {
60 $rvalues_max = 1;
61 }
62  
63 my $self = {
64 lvalue => undef,
65 type => $type,
66 rvalues_max => $rvalues_max,
67 rvalues => [ ],
68 };
69  
70 bless $self, $class;
71 return $self;
72 }
73  
74 sub get_type { return shift->{type}; }
75  
76 sub get_lvalue { return shift->{lvalue}; }
77 sub get_rvalue
78 {
79 my $self = shift;
80  
81 # deal with camparisons with strict limits
82 if ( defined $self->{rvalues_max} )
83 {
84 if ( $self->{rvalues_max} == 1 )
85 {
86 if ( scalar @{$self->{rvalues}} == 0 )
87 {
88 return undef;
89 }
90 else
91 {
92 return $self->{rvalues}->[0];
93 }
94 }
95 if ( $self->{rvalues_max} == 0 )
96 {
97 return undef;
98 }
99 }
100  
101 return $self->{rvalues};
102 }
103  
104 sub get_values
105 {
106 my $self = shift;
107 return [ $self->{lvalue}, @{$self->{rvalues}} ];
108 }
109  
110 sub add
111 {
112 my ($self, $val) = @_;
113
114 if ( not defined $self->{lvalue} )
115 {
116 $self->{lvalue} = $val;
117 }
118 else
119 {
120 if ( defined $self->{rvalues_max} )
121 {
122 if ( scalar @{$self->{rvalues}} == $self->{rvalues_max} )
123 {
124 my $name;
125 $name = ref($self);
126 $name =~ s/.*:://;
127  
128 die "Cannot add more than $self->{rvalues_max} rvalues to the $name comparison";
129 }
130 }
131  
132 push @{$self->{rvalues}}, $val;
133 }
134 }
135  
136 sub visit
137 {
138 my ($self, $visitor) = (shift, shift);
139 return $visitor->visit_comparison( $self, @_ );
140 }
141  
142 sub copy_values
143 {
144 my ($self, $other) = @_;
145  
146 foreach my $value ( @{$other->get_values()} )
147 {
148 $self->add( $value->clone() );
149 }
150 }
151  
152 sub clone
153 {
154 my $self = shift;
155 my $class = ref($self);
156  
157 my $clone;
158 $clone = $class->new();
159 $clone->copy_values( $self );
160  
161 return $clone;
162 }
163  
164 1;
165