Xmldoom/Definition/Object.pm untested

filter: Statementstmt 0.0% 0 / 306 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 465 CC 40 SCAR 26.2
CC
40
Cov
0%
load
72.0
count
20.0
new
12.0
CRAP 13.8
1  
2 package Xmldoom::Definition::Object;
3  
4 use Xmldoom::Threads;
5 use Exception::Class::TryCatch;
6 use DBIx::Romani::Query::Select;
7 use DBIx::Romani::Query::Insert;
8 use DBIx::Romani::Query::Update;
9 use DBIx::Romani::Query::Delete;
10 use DBIx::Romani::Query::Where;
11 use DBIx::Romani::Query::Comparison;
12 use DBIx::Romani::Query::Variable;
13 use DBIx::Romani::Query::SQL::Column;
14 use DBIx::Romani::Query::SQL::Literal;
15 use Module::Runtime qw(use_module);
16 use strict;
17  
18 use Data::Dumper;
19  
20 sub new
21 {
22 my $class = shift;
23 my $args = shift;
24  
25 my $database;
26 my $object_name;
27 my $table_name;
28 my $shared = 0;
29
30 if ( ref($args) eq 'HASH' )
31 {
32 $database = $args->{definition};
33 $object_name = $args->{object_name};
34 $table_name = $args->{table_name};
35 $shared = $args->{shared};
36 }
37 else
38 {
39 $database = $args;
40 $object_name = shift;
41 $table_name = shift;
42 }
43  
44 my $table = $database->get_table( $table_name );
45 if ( not defined $table )
46 {
47 die "Cannot bind an object to a non-existant table.";
48 }
49  
50 my $self = {
51 database => $database,
52 object_name => $object_name,
53 table_name => $table_name,
54 table => $table,
55 props => [ ],
56 class => undef,
57  
58 # generate on demand
59 select_query => undef,
60 select_by_key_query => undef,
61 insert_query => undef,
62 update_query => undef,
63 delete_query => undef,
64 };
65  
66 bless $self, $class;
67 return Xmldoom::Threads::make_shared($self, $shared);
68 }
69  
70 sub get_database { return shift->{database}; }
71 sub get_table_name { return shift->{table_name}; }
72 sub get_table { return shift->{table}; }
73 sub get_name { return shift->{object_name}; }
74 sub get_properties { return shift->{props}; }
75 sub get_class { return shift->{class}; }
76  
77 sub get_property
78 {
79 my ($self, $prop_name) = @_;
80  
81 foreach my $prop ( @{$self->{props}} )
82 {
83 if ( $prop->get_name() eq $prop_name )
84 {
85 return $prop;
86 }
87 }
88  
89 die sprintf "Unknown property '%s' on object '%s'", $prop_name, $self->get_name();
90 }
91  
92 sub get_reportable_properties
93 {
94 my $self = shift;
95 my @list = grep { $_->get_reportable() } @{$self->{props}};
96 return wantarray ? @list : \@list;
97 }
98  
99 sub get_searchable_properties
100 {
101 my $self = shift;
102 my @list = grep { $_->get_searchable() } @{$self->{props}};
103 return wantarray ? @list : \@list;
104 }
105  
106 sub has_property
107 {
108 my ($self, $prop_name) = @_;
109  
110 try eval
111 {
112 $self->get_property( $prop_name );
113 };
114  
115 my $error = catch;
116 if ( $error )
117 {
118 return 0;
119 }
120  
121 return 1;
122 }
123  
124 sub set_class
125 {
126 my ($self, $class) = @_;
127  
128 if ( defined $self->{class} )
129 {
130 die "You are trying to redefine an object's class! Why would anyone want to do that?";
131 }
132  
133 $self->{class} = $class;
134 }
135  
136 sub add_property
137 {
138 my ($self, $prop) = @_;
139  
140 # TODO: make sure that this property will actually work, ie. are there
141 # any autoload name conflicts.
142  
143 # poses problems for running in shared memory because conceivably
144 # the object definition could be shared and the property not, which will
145 # cause it to be copied and be a different object than what was passed in.
146 if ( Xmldoom::Threads::is_shared($self) and not Xmldoom::Threads::is_shared($prop) )
147 {
148 die "Cannot add a non-shared memory proproperty to a shared memory object definition";
149 }
150  
151 if ( $self->has_property( $prop->get_name() ) )
152 {
153 die "Cannot add two properties with the same name";
154 }
155  
156 push @{$self->{props}}, $prop;
157 }
158  
159 sub set_custom_property
160 {
161 my ($self, $name, $prop_class) = @_;
162  
163 my $index = 0;
164 foreach my $prop ( @{$self->get_properties()} )
165 {
166 if ( $prop->get_name() eq $name )
167 {
168 if ( $prop->isa('Xmldoom::Definition::Property::PlaceHolder') )
169 {
170 # All is thrill chillin
171 $self->{props}->[$index] = $prop_class->new( $prop->get_prop_args() );
172 return;
173 }
174 else
175 {
176 die "Property '$name' exists, but is not designated to be a custom property";
177 }
178 }
179  
180 $index ++;
181 }
182  
183 die "No such property '$name'";
184 }
185  
186 sub class_new
187 {
188 my $self = shift;
189  
190 my $class = $self->get_class();
191  
192 use_module($class);
193  
194 return $class->new( @_ );
195 }
196  
197 sub class_load
198 {
199 my $self = shift;
200  
201 my $class = $self->get_class();
202  
203 use_module($class);
204  
205 return $class->load( @_ );
206 }
207  
208 sub get_select_query
209 {
210 my $self = shift;
211  
212 if ( not defined $self->{select_query} )
213 {
214 my $query = DBIx::Romani::Query::Select->new();
215 $query->add_from( $self->{table_name} );
216  
217 # add all the columns
218 foreach my $column ( @{$self->{table}->get_columns()} )
219 {
220 $query->add_result( DBIx::Romani::Query::SQL::Column->new( $self->{table_name}, $column->{name}) );
221 }
222  
223 $self->{select_query} = $query;
224 }
225  
226 return $self->{select_query};
227 }
228  
229 sub get_select_by_key_query
230 {
231 my $self = shift;
232  
233 if ( not defined $self->{select_by_key_query} )
234 {
235 my $query = $self->get_select_query()->clone();
236 my $where = DBIx::Romani::Query::Where->new( $DBIx::Romani::Query::Where::AND );
237  
238 foreach my $column ( @{$self->{table}->get_columns()} )
239 {
240 if ( $column->{primary_key} )
241 {
242 my $op = DBIx::Romani::Query::Comparison->new( $DBIx::Romani::Query::Comparison::EQUAL );
243 $op->add( DBIx::Romani::Query::SQL::Column->new( $self->{table_name}, $column->{name} ) );
244 $op->add( DBIx::Romani::Query::Variable->new( "$self->{table_name}.$column->{name}" ) );
245 $where->add( $op );
246 }
247 }
248  
249 $query->set_where( $where );
250 $self->{select_by_key_query} = $query;
251 }
252  
253 return $self->{select_by_key_query};
254 }
255  
256 sub get_insert_query
257 {
258 my $self = shift;
259  
260 if ( not defined $self->{insert_query} )
261 {
262 my $query = DBIx::Romani::Query::Insert->new( $self->{table_name} );
263  
264 foreach my $column ( @{$self->{table}->get_columns()} )
265 {
266 $query->set_value( $column->{name}, DBIx::Romani::Query::Variable->new($column->{name}) );
267 }
268  
269 $self->{insert_query} = $query;
270 }
271  
272 return $self->{insert_query};
273 }
274  
275 sub get_update_query
276 {
277 my $self = shift;
278  
279 if ( not defined $self->{update_query} )
280 {
281 my $query = DBIx::Romani::Query::Update->new( $self->{table_name} );
282 my $where = DBIx::Romani::Query::Where->new( $DBIx::Romani::Query::Where::AND );
283  
284 foreach my $column ( @{$self->{table}->get_columns()} )
285 {
286 # add the primary key to the where section
287 if ( $column->{primary_key} )
288 {
289 my $op = DBIx::Romani::Query::Comparison->new( $DBIx::Romani::Query::Comparison::EQUAL );
290 $op->add( DBIx::Romani::Query::SQL::Column->new( undef, $column->{name} ) );
291 $op->add( DBIx::Romani::Query::Variable->new( "key.$column->{name}" ) );
292 $where->add($op);
293 }
294  
295 # set all the column values
296 $query->set_value( $column->{name}, DBIx::Romani::Query::Variable->new( $column->{name} ) );
297 }
298 $query->set_where( $where );
299  
300 $self->{update_query} = $query;
301 }
302  
303 return $self->{update_query};
304 }
305  
306 sub get_delete_query
307 {
308 my $self = shift;
309  
310 if ( not defined $self->{delete_query} )
311 {
312 my $query = DBIx::Romani::Query::Delete->new( $self->{table_name} );
313 my $where = DBIx::Romani::Query::Where->new( $DBIx::Romani::Query::Where::AND );
314  
315 foreach my $column ( @{$self->{table}->get_columns()} )
316 {
317 if ( $column->{primary_key} )
318 {
319 my $op = DBIx::Romani::Query::Comparison->new( $DBIx::Romani::Query::Comparison::EQUAL );
320 $op->add( DBIx::Romani::Query::SQL::Column->new( undef, $column->{name} ) );
321 $op->add( DBIx::Romani::Query::Variable->new( $column->{name} ) );
322 $where->add( $op );
323 }
324 }
325 $query->set_where( $where );
326  
327 $self->{delete_query} = $query;
328 }
329  
330 return $self->{delete_query};
331 }
332  
333 # A convenience function
334 sub find_links
335 {
336 my ($self, $object_name) = @_;
337  
338 my $database = $self->get_database();
339 my $object = $database->get_object( $object_name );
340  
341 return $database->find_links( $self->get_table_name(), $object->get_table_name() );
342 }
343  
344 # A convenience function
345 sub create_db_connection
346 {
347 my $self = shift;
348  
349 my $factory = $self->get_database()->get_connection_factory();
350 if ( not defined $factory )
351 {
352 # Programmer error
353 die "This database doesn't have a DBIx::Romani::Connection::Factory registered";
354 }
355  
356 return $factory->create();
357 }
358  
359 #
360 # The following allow you to perform all of the basic database operations that
361 # CS3::Object performs, except without an actual object, just the raw queries.
362 #
363  
364 sub load
365 {
366 my $self = shift;
367  
368 # Convenience.
369 my $table = $self->get_table();
370 my $table_name = $self->get_table_name();
371 my $query = $self->get_select_by_key_query();
372  
373 my %values;
374  
375 my $args;
376 if ( ref($_[0]) eq 'HASH' )
377 {
378 $args = shift;
379 }
380  
381 # parse the arguments into values for the SQL generator
382 foreach my $column ( @{$table->get_columns()} )
383 {
384 if ( $column->{primary_key} )
385 {
386 my $col_name = $column->{name};
387 my $val_name = "$table_name.$col_name";
388 my $val;
389  
390 if ( $args )
391 {
392 $val = $args->{$col_name};
393 }
394 else
395 {
396 $val = shift;
397 }
398  
399 if ( not defined $val )
400 {
401 die "Missing required key value \"$col_name\"";
402 }
403  
404 $values{$val_name} = DBIx::Romani::Query::SQL::Literal->new( $val );
405 }
406 }
407  
408 my $conn;
409  
410 my $data = try eval
411 {
412 $conn = $self->create_db_connection();
413  
414 my $stmt = $conn->prepare( $query );
415 my $rs = $stmt->execute( \%values );
416  
417 if ( $rs->next() )
418 {
419 return $rs->get_row();
420 }
421 else
422 {
423 # uh oh!
424 die "Can't find an object with that primary key!";
425 }
426 };
427  
428 do
429 {
430 $conn->disconnect() if defined $conn;
431 };
432  
433 catch my $err;
434 $err->rethrow() if $err;
435  
436 return $data;
437 }
438  
439 sub search_rs
440 {
441 my $self = shift;
442 my $criteria = shift;
443  
444 my $query = $criteria->generate_query_for_object( $self->get_database(), $self->get_name() );
445  
446 my $conn;
447 my $rs;
448  
449 # connect and query
450 try eval
451 {
452 $conn = $self->create_db_connection();
453 #printf STDERR "Search(): %s\n", $conn->generate_sql($query);
454 $rs = $conn->prepare( $query )->execute();
455 };
456  
457 catch my $err;
458 if ( $err )
459 {
460 $conn->disconnect() if defined $conn;
461 $err->rethrow();
462 }
463  
464 return $rs;
465 }
466  
467 sub search
468 {
469 my $self = shift;
470 my $rs = $self->search_rs( @_ );
471
472 my @ret;
473  
474 # unravel our result set
475 while ( $rs->next() )
476 {
477 push @ret, $rs->get_row();
478 }
479  
480 return wantarray ? @ret : \@ret;
481 }
482  
483 sub search_attrs_rs
484 {
485 my $self = shift;
486 my $criteria = shift;
487  
488 my @attrs;
489  
490 my $table_name = $self->get_table_name();
491  
492 # build object specific attrs
493 foreach my $attr ( @_ )
494 {
495 push @attrs, "$table_name/$attr";
496 }
497  
498 my $query = $criteria->generate_query_for_attrs( $self->get_database(), \@attrs );
499  
500 my $conn;
501 my $rs;
502  
503 # connect and query
504 try eval
505 {
506 $conn = $self->create_db_connection();
507 #printf STDERR "Search(): %s\n", $conn->generate_sql($query);
508 $rs = $conn->prepare( $query )->execute();
509 };
510  
511 if ( my $err = catch )
512 {
513 $conn->disconnect() if defined $conn;
514 $err->rethrow();
515 }
516  
517 return $rs;
518 }
519  
520 sub search_attrs
521 {
522 my $self = shift;
523 my $rs = $self->search_attrs_rs( @_ );
524
525 my @ret;
526  
527 # unravel our result set
528 while ( $rs->next() )
529 {
530 push @ret, $rs->get_row();
531 }
532  
533 return wantarray ? @ret : \@ret;
534 }
535  
536 sub search_distinct_attrs_rs
537 {
538 my $self = shift;
539 my $criteria = shift;
540  
541 my @attrs;
542  
543 my $table_name = $self->get_table_name();
544  
545 # build object specific attrs
546 foreach my $attr ( @_ )
547 {
548 push @attrs, "$table_name/$attr";
549 }
550  
551 my $query = $criteria->generate_query_for_attrs( $self->get_database(), \@attrs );
552  
553 # we are searching distinctly...
554 $query->set_distinct(1);
555  
556 my $conn;
557 my $rs;
558  
559 # connect and query
560 try eval
561 {
562 $conn = $self->create_db_connection();
563 #printf STDERR "SearchDistinct(): %s\n", $conn->generate_sql($query);
564 $rs = $conn->prepare( $query )->execute();
565 };
566  
567 catch my $err;
568 if ( $err )
569 {
570 $conn->disconnect() if defined $conn;
571 $err->rethrow();
572 }
573  
574 return $rs;
575 }
576  
577 sub search_distinct_attrs
578 {
579 my $self = shift;
580 my $rs = $self->search_attrs_rs( @_ );
581
582 my @ret;
583  
584 # unravel our result set
585 while ( $rs->next() )
586 {
587 push @ret, $rs->get_row();
588 }
589  
590 return wantarray ? @ret : \@ret;
591 }
592  
593 sub count
594 {
595 my $self = shift;
596 my $criteria = shift;
597  
598 my $query = $criteria->generate_query_for_object_count( $self->get_database(), $self->get_name() );
599  
600 my $conn;
601 my $ret;
602  
603 try eval
604 {
605 $conn = $self->create_db_connection();
606
607 #printf "Search(): %s\n", $conn->generate_sql($query);
608 my $stmt = $conn->prepare( $query );
609 my $rs = $stmt->execute();
610  
611 if ( $rs->next() )
612 {
613 my $t = $rs->get_row();
614 $ret = $t->{count};
615 }
616 };
617  
618 do
619 {
620 $conn->disconnect() if defined $conn;
621 };
622  
623 catch my $err;
624 $err->rethrow() if $err;
625  
626 return $ret;
627 }
628  
629 1;
630