File Coverage

blib/lib/DBIx/Romani/Query/Comparison.pm
Criterion Covered Total %
statement 3 58 5.1
branch 0 24 0.0
condition 0 6 0.0
subroutine 1 10 10.0
pod 0 9 0.0
total 4 107 3.7


line stmt bran cond sub pod time code
1              
2             package DBIx::Romani::Query::Comparison;
3              
4 1     1   4 use strict;
  1         1  
  1         811  
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 0     0 0   my $class = shift;
26 0           my $args = shift;
27              
28 0           my $rvalues_max;
29             my $type;
30              
31 0 0         if ( ref($args) eq 'HASH' )
32             {
33 0           $type = $args->{type};
34             }
35             else
36             {
37 0           $type = $args;
38             }
39              
40 0 0         if ( not defined $type )
41             {
42 0           $type = $EQUAL;
43             }
44              
45 0 0 0       if ( $type eq $BETWEEN )
    0 0        
    0          
46             {
47 0           $rvalues_max = 2;
48             }
49             elsif ( $type eq $IS_NULL or $type eq $IS_NOT_NULL )
50             {
51 0           $rvalues_max = 0;
52             }
53             elsif ( $type eq $IN or $type eq $NOT_IN )
54             {
55             # no limit!
56 0           $rvalues_max = undef;
57             }
58             else
59             {
60 0           $rvalues_max = 1;
61             }
62              
63 0           my $self = {
64             lvalue => undef,
65             type => $type,
66             rvalues_max => $rvalues_max,
67             rvalues => [ ],
68             };
69              
70 0           bless $self, $class;
71 0           return $self;
72             }
73              
74 0     0 0   sub get_type { return shift->{type}; }
75              
76 0     0 0   sub get_lvalue { return shift->{lvalue}; }
77             sub get_rvalue
78             {
79 0     0 0   my $self = shift;
80              
81             # deal with camparisons with strict limits
82 0 0         if ( defined $self->{rvalues_max} )
83             {
84 0 0         if ( $self->{rvalues_max} == 1 )
85             {
86 0 0         if ( scalar @{$self->{rvalues}} == 0 )
  0            
87             {
88 0           return undef;
89             }
90             else
91             {
92 0           return $self->{rvalues}->[0];
93             }
94             }
95 0 0         if ( $self->{rvalues_max} == 0 )
96             {
97 0           return undef;
98             }
99             }
100              
101 0           return $self->{rvalues};
102             }
103              
104             sub get_values
105             {
106 0     0 0   my $self = shift;
107 0           return [ $self->{lvalue}, @{$self->{rvalues}} ];
  0            
108             }
109              
110             sub add
111             {
112 0     0 0   my ($self, $val) = @_;
113            
114 0 0         if ( not defined $self->{lvalue} )
115             {
116 0           $self->{lvalue} = $val;
117             }
118             else
119             {
120 0 0         if ( defined $self->{rvalues_max} )
121             {
122 0 0         if ( scalar @{$self->{rvalues}} == $self->{rvalues_max} )
  0            
123             {
124 0           my $name;
125 0           $name = ref($self);
126 0           $name =~ s/.*:://;
127              
128 0           die "Cannot add more than $self->{rvalues_max} rvalues to the $name comparison";
129             }
130             }
131              
132 0           push @{$self->{rvalues}}, $val;
  0            
133             }
134             }
135              
136             sub visit
137             {
138 0     0 0   my ($self, $visitor) = (shift, shift);
139 0           return $visitor->visit_comparison( $self, @_ );
140             }
141              
142             sub copy_values
143             {
144 0     0 0   my ($self, $other) = @_;
145              
146 0           foreach my $value ( @{$other->get_values()} )
  0            
147             {
148 0           $self->add( $value->clone() );
149             }
150             }
151              
152             sub clone
153             {
154 0     0 0   my $self = shift;
155 0           my $class = ref($self);
156              
157 0           my $clone;
158 0           $clone = $class->new();
159 0           $clone->copy_values( $self );
160              
161 0           return $clone;
162             }
163              
164             1;
165