Xmldoom/Criteria/Search.pm untested

filter: Statementstmt 0.0% 0 / 176 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 284 CC 33 SCAR 54.2
CC
33
Cov
0%
generate_description
552.0
generate
20.0
add_attr
12.0
CRAP 226.7
1  
2 package Xmldoom::Criteria::Search;
3  
4 use Xmldoom::Criteria;
5 use Xmldoom::Criteria::Comparison;
6 use Xmldoom::Criteria::Attribute;
7 use Xmldoom::Criteria::Property;
8 use Xmldoom::Criteria::Literal;
9 use DBIx::Romani::Query::Select;
10 use DBIx::Romani::Query::Where;
11 use DBIx::Romani::Query::Comparison;
12 use DBIx::Romani::Query::SQL::Column;
13 use strict;
14  
15 use Data::Dumper;
16  
17 sub new
18 {
19 my $class = shift;
20 my $args = shift;
21  
22 my $type;
23  
24 if ( ref($args) eq 'HASH' )
25 {
26 $type = $args->{type};
27 }
28 else
29 {
30 $type = $args;
31 }
32
33 my $self = {
34 type => $type || $Xmldoom::Criteria::AND,
35 comparisons => [ ]
36 };
37  
38 bless $self, $class;
39 return $self;
40 }
41  
42 sub get_type { return shift->{type}; }
43 sub get_comparisons { return shift->{comparisons}; }
44  
45 sub set_type
46 {
47 my ($self, $type) = @_;
48 $self->{type} = $type;
49 }
50  
51 sub _add
52 {
53 my $self = shift;
54  
55 my $comp;
56  
57 if ( $_[0]->isa( 'Xmldoom::Criteria::Search' ) )
58 {
59 $comp = shift;
60 }
61 else
62 {
63 $comp = Xmldoom::Criteria::Comparison->new( @_ );
64 }
65  
66 push @{$self->{comparisons}}, $comp;
67 }
68  
69 sub add
70 {
71 my ($self, $lval, $rval, $type) = @_;
72  
73 if ( $lval->isa( 'Xmldoom::Criteria::Search' ) )
74 {
75 $self->_add( $lval );
76 }
77 else
78 {
79 $self->add_prop( $lval, $rval, $type );
80 }
81 }
82  
83 sub add_attr
84 {
85 my ($self, $attr, $value, $type) = @_;
86  
87 my $rval;
88  
89 if ( ref($value) eq 'ARRAY' )
90 {
91 $rval = [ ];
92 foreach my $i ( @$value )
93 {
94 push @$rval, Xmldoom::Criteria::Literal->new( $i );
95 }
96 }
97 elsif ( defined $value )
98 {
99 $rval = Xmldoom::Criteria::Literal->new( $value );
100 }
101  
102 $self->_add(
103 Xmldoom::Criteria::Attribute->new( $attr ),
104 $rval,
105 $type
106 );
107 }
108  
109 sub add_prop
110 {
111 my ($self, $prop, $value, $type) = @_;
112  
113 my $rval;
114  
115 if ( ref($value) eq 'ARRAY' )
116 {
117 $rval = [ ];
118 foreach my $i ( @$value )
119 {
120 push @$rval, Xmldoom::Criteria::Literal->new( $i );
121 }
122 }
123 elsif ( defined $value )
124 {
125 $rval = Xmldoom::Criteria::Literal->new( $value );
126 }
127  
128 $self->_add(
129 Xmldoom::Criteria::Property->new( $prop ),
130 $rval,
131 $type
132 );
133 }
134  
135 sub join_attr
136 {
137 my ($self, $attr1, $attr2, $type) = @_;
138  
139 $self->_add(
140 Xmldoom::Criteria::Attribute->new( $attr1 ),
141 Xmldoom::Criteria::Attribute->new( $attr2 ),
142 $type
143 );
144 }
145  
146 sub join_prop
147 {
148 my ($self, $prop1, $prop2, $type) = @_;
149  
150 $self->_add(
151 Xmldoom::Criteria::Property->new( $prop1 ),
152 Xmldoom::Criteria::Property->new( $prop2 ),
153 $type
154 );
155 }
156  
157 sub get_tables
158 {
159 my ($self, $database) = @_;
160  
161 my $table_hash = { };
162  
163 foreach my $comp ( @{$self->get_comparisons()} )
164 {
165 foreach my $table_name ( @{$comp->get_tables($database)} )
166 {
167 $table_hash->{$table_name} = 1;
168 }
169 }
170  
171 my @table_names = keys %$table_hash;
172  
173 return \@table_names;
174 }
175  
176 sub generate
177 {
178 my $self = shift;
179 my $args = shift;
180  
181 my $database;
182  
183 if ( ref($args) eq 'HASH' )
184 {
185 $database = $args->{database};
186 }
187 else
188 {
189 $database = $args;
190 }
191  
192 # actually make the where statment
193 my $where = DBIx::Romani::Query::Where->new( $self->get_type() );
194 foreach my $comp ( @{$self->get_comparisons()} )
195 {
196 $where->add( $comp->generate($database) );
197 }
198  
199 # reduce it if possible.
200 if ( scalar @{$where->get_values()} == 1 )
201 {
202 # if there is only one, then return only that
203 return $where->get_values()->[0];
204 }
205 elsif ( scalar @{$where->get_values()} == 0 )
206 {
207 # no search. we're grabbing everything?
208 return undef;
209 }
210  
211 return $where;
212 }
213  
214 sub generate_description
215 {
216 my $self = shift;
217 my $args = shift;
218  
219 my $database;
220 my $object_name;
221  
222 if ( ref($args) eq 'HASH' )
223 {
224 $database = $args->{database};
225 $object_name = $args->{object_name};
226 }
227 else
228 {
229 $database = $args;
230 $object_name = shift;
231 }
232  
233 my $object = $database->get_object( $object_name );
234 my %comps;
235  
236 # group by the lvalues
237 foreach my $comp ( @{$self->get_comparisons()} )
238 {
239 my $lval = $comp->get_lval();
240 my $name;
241  
242 my %res = (
243 type => $comp->get_type(),
244 rval => $comp->get_rval(),
245 lval => $lval,
246 );
247  
248 # name
249 if ( $lval->isa('Xmldoom::Criteria::Property') )
250 {
251 if ( $lval->get_object_name() eq $object_name )
252 {
253 $name = $object->get_property( $lval->get_property_name() )->get_description();
254 }
255 else
256 {
257 die "Unimplemented.";
258 }
259  
260 $res{prop} = $database->get_object( $lval->get_object_name() )->get_property( $lval->get_property_name() );
261 }
262 elsif ( $lval->isa('Xmldoom::Criteria::Attribute') )
263 {
264 $name = sprintf "%s.%s", $lval->get_table_name(), $lval->get_column_name();
265 }
266  
267 if ( not defined $comps{$name} )
268 {
269 $comps{$name} = [ ];
270 }
271  
272 push @{$comps{$name}}, \%res;
273 }
274  
275 my $text;
276  
277 if ( $self->get_type() eq 'AND' )
278 {
279 my @texts;
280 while( my ($name, $value) = each %comps )
281 {
282 my @chunks;
283  
284 foreach my $comp ( @$value )
285 {
286 my $value = $comp->{rval}->[0]->get_value( $database, $comp->{lval} );
287 my $op;
288  
289 my $desc = $comp->{prop}->get_value_description( $value );
290 if ( $desc )
291 {
292 $value = $desc;
293 }
294
295 if ( $comp->{type} eq $Xmldoom::Criteria::EQUAL )
296 {
297 $op = "is $value";
298 }
299 elsif ( $comp->{type} eq $Xmldoom::Criteria::NOT_EQUAL )
300 {
301 $op = "isn't $value";
302 }
303 elsif ( $comp->{type} eq $Xmldoom::Criteria::GREATER_THAN )
304 {
305 $op = "is greater than $value";
306 }
307 elsif ( $comp->{type} eq $Xmldoom::Criteria::GREATER_EQUAL )
308 {
309 $op = "is $value or greater";
310 }
311 elsif ( $comp->{type} eq $Xmldoom::Criteria::LESS_THAN )
312 {
313 $op = "is less than $value";
314 }
315 elsif ( $comp->{type} eq $Xmldoom::Criteria::LESS_EQUAL )
316 {
317 $op = "is $value or less";
318 }
319 elsif ( $comp->{type} eq $Xmldoom::Criteria::LIKE )
320 {
321 my $pure = $value;
322 $pure =~ s/\%//g;
323  
324 if ( $value =~ /^\%.*\%$/ )
325 {
326 $op = "contains $pure";
327 }
328 elsif ( $value =~ /^\%.*/ )
329 {
330 $op = "ends with $pure";
331 }
332 elsif ( $value =~ /^.*\%/ )
333 {
334 $op = "begins with $pure";
335 }
336 else
337 {
338 $op = "is similar to $pure";
339 }
340 }
341 elsif ( $comp->{type} eq $Xmldoom::Criteria::NOT_LIKE )
342 {
343 my $pure = $value;
344 $pure =~ s/\%//g;
345  
346 if ( $value =~ /^\%.*\%$/ )
347 {
348 $op = "doesn't contain $pure";
349 }
350 elsif ( $value =~ /^\%.*/ )
351 {
352 $op = "doesn't end with $pure";
353 }
354 elsif ( $value =~ /^.*\%/ )
355 {
356 $op = "doesn't begin with $pure";
357 }
358 else
359 {
360 $op = "isn't similar to $pure";
361 }
362 }
363 elsif ( $comp->{type} eq $Xmldoom::Criteria::BETWEEN )
364 {
365 my $value2 = $comp->{rval}->[1]->get_value( $database, $comp->{lval} );
366 $op = "is between $value and $value2";
367 }
368 else
369 {
370 die "Unimplemented.";
371 }
372  
373 push @chunks, $op;
374 }
375  
376 push @texts, sprintf( "%s %s", $name, join(' but ', @chunks ) );
377 }
378  
379 $text = join ', and ', @texts;
380 }
381 else
382 {
383 # TODO: we should group these as 'comp1 (or comp2 or comp3)' in the text.
384 die "Unimplemented.";
385 }
386  
387 return $text;
388 }
389  
390 sub clone
391 {
392 my $self = shift;
393  
394 my $search = Xmldoom::Criteria::Search->new( $self->get_type() );
395  
396 foreach my $comp ( @{$self->get_comparisons()} )
397 {
398 push @{$search->{comparisons}}, $comp->clone();
399 }
400  
401 return $search;
402 }
403  
404 1;
405