blib/lib/MOBY/Adaptor/moby/queryapi/mysql.pm untested

filter: Statementstmt 0.0% 0 / 733 Branchbran 0.0% 0 / 186 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 76 Podpod 0.0% 0 / 69 totaltotal 0.0% 0 / 1151 CC 94 SCAR 30.3
CC
94
Cov
0%
get_all_relationships
90.0
get_relationship
56.0
new
42.0
CRAP 20.6
1 package MOBY::Adaptor::moby::queryapi::mysql;
2  
3 use strict;
4 use vars qw($AUTOLOAD @ISA);
5 use Carp;
6 use MOBY::Adaptor::moby::queryapi;
7 use DBI;
8 use DBD::mysql;
9  
10 use vars qw /$VERSION/;
11 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /: (\d+)\.(\d+)/;
12  
13 @ISA = qw{MOBY::Adaptor::moby::queryapi}; # implements the interface
14  
15 {
16 #Encapsulated class data
17
18 #___________________________________________________________
19 #ATTRIBUTES
20 my %_attr_data = # DEFAULT ACCESSIBILITY
21 (
22 driver => ["DBI:mysql", 'read/write'],
23 dbh => [undef, 'read/write'],
24
25 );
26  
27 #_____________________________________________________________
28  
29 # METHODS, to operate on encapsulated class data
30  
31 # Is a specified object attribute accessible in a given mode
32 sub _accessible {
33 my ($self, $attr, $mode) = @_;
34 $_attr_data{$attr}[1] =~ /$mode/
35 }
36  
37 # Classwide default value for a specified object attribute
38 sub _default_for {
39 my ($self, $attr) = @_;
40 $_attr_data{$attr}[0];
41 }
42  
43 # List of names of all specified object attributes
44 sub _standard_keys {
45 keys %_attr_data;
46 }
47  
48 sub driver {
49 my ($self, $arg) = @_;
50 $self->{driver} = $arg if defined $arg;
51 return $self->{driver};
52 }
53 sub dbh {
54 my ($self, $arg) = @_;
55 $self->{dbh} = $arg if defined $arg;
56 return $self->{dbh};
57 }
58  
59 }
60  
61 sub _getDBHandle {
62 my ($ontology) = @_;
63 my $CONF = MOBY::Config->new;
64 my $adap = $CONF->getDataAdaptor(source => $ontology);
65 return $adap->dbh;
66 }
67  
68 sub new {
69 my ($caller, %args) = @_;
70 my $self = $caller->SUPER::new(%args);
71  
72 my $caller_is_obj = ref($caller);
73 my $class = $caller_is_obj || $caller;
74  
75 foreach my $attrname ( $self->_standard_keys ) {
76 if (exists $args{$attrname} && defined $args{$attrname}) {
77 $self->{$attrname} = $args{$attrname} }
78 elsif ($caller_is_obj) {
79 $self->{$attrname} = $caller->{$attrname} }
80 else {
81 $self->{$attrname} = $self->_default_for($attrname) }
82 }
83  
84 return unless $self->driver;
85 my $driver = $self->driver; # inherited from the adaptorI (queryapi)
86 my $username = $self->username;
87 my $password = $self->password;
88 my $port = $self->port;
89 my $url = $self->url;
90 my $dbname = $self->dbname;
91
92 my ($dsn) = "$driver:$dbname:$url:$port";
93 my $dbh = DBI->connect($dsn, $username, $password, {RaiseError => 1}) or die "can't connect to database";
94  
95  
96 ##############################################################
97 unless ($dbh) {
98 print STDERR "Couldn't connect to the datasource \n",($self->_dump()),"\n\n";
99 return undef;
100 }
101
102 $self->dbh($dbh);
103 #############################################################
104  
105 return undef unless $self->dbh;
106 return $self;
107
108 }
109  
110 sub _add_condition{
111 my ($statement, @params) = @_;
112 my @bindvalues = ();
113 my $condition = "where ";
114
115 foreach my $param (@params )
116 {
117 if (($param eq 'and') || ($param eq 'or'))
118 {
119 $condition .= $param . " ";
120 }
121 else
122 {
123 my %pair = %$param;
124  
125 for my $key (keys %pair)
126 {
127 if (defined $pair{$key}){
128 #added a check for servicename to support case sensitivity
129 if ($key eq "servicename") {
130 $condition .= $key . " LIKE binary ? ";
131 push(@bindvalues, $pair{$key});
132 } elsif ($pair{$key} eq "IS NOT NULL"){
133 $condition .= $key . " IS NOT NULL ";
134 } else {
135 $condition .= $key . " = ? ";
136 push(@bindvalues, $pair{$key});
137 }
138 } else{
139 $condition .= $key . " IS NULL "
140 }
141 }
142 }
143 }
144 $statement .= $condition;
145 return ($statement, @bindvalues);
146 }
147  
148 # preforms query but returns a reference to an array containing hash references
149 sub do_query{
150 my ($dbh, $statement, @bindvalues) = @_;
151 my $sth = $dbh -> prepare($statement);
152 if (@bindvalues < 1)
153 {
154 $sth->execute;
155 }
156 else
157 {
158 $sth->execute(@bindvalues);
159 }
160 # returns an array of hash references
161 my $arrayHashRef = $sth->fetchall_arrayref({});
162 return $arrayHashRef;
163 }
164  
165 sub get_value{
166 my ($key, @params) = @_;
167
168 foreach my $param (@params )
169 {
170 my %pair = %$param;
171 for my $tmp (keys %pair)
172 {
173 if ($tmp eq $key){
174 return $pair{$key};
175 }
176 }
177 }
178 }
179  
180 sub _getSIIDFromLSID {
181 my ($self, $lsid) = @_;
182 my $dbh = $self->dbh;
183 my $sth = $dbh->prepare("select service_instance_id from service_instance where lsid = ?");
184 $sth->execute($lsid);
185 my ($siid) = $sth->fetchrow_array();
186 return $siid;
187 }
188  
189 # this should NOT retun a collection ID... needs more work...
190 # args passed in: service_lsid
191 sub query_collection_input{
192 my ($self, %args) = @_;
193 my $dbh = $self->dbh;
194 my $serv_lsid = $args{'service_instance_lsid'};
195
196 my $statement = "select
197 collection_input_id,
198 article_name
199 from collection_input as c, service_instance as si where si.service_instance_id = c.service_instance_id and si.lsid = ?";
200 my $result = do_query($dbh, $statement, ($serv_lsid));
201 return $result;
202 }
203  
204 # args passed in: service_instance_lsid, article_name
205 sub insert_collection_input {
206 my ($self, %args) = @_;
207 my $article = $args{article_name};
208 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
209
210 $self->dbh->do("insert into collection_input (service_instance_id, article_name) values (?,?)",
211 undef, $siid, $article);
212 my $id=$self->dbh->{mysql_insertid};
213 return $id;
214 }
215  
216 # pass in service_instance_lsid
217 sub delete_collection_input{
218 my ($self, %args) = @_;
219 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
220
221 my $statement = "delete from collection_input where service_instance_id = ?";
222 $self->dbh->do( $statement, undef, $siid);
223
224 if ($self->dbh->err){
225 return (1, $self->dbh->errstr);
226 }
227 else{
228 return 0;
229 }
230 }
231  
232 # pass service_instance_lsid
233 sub query_collection_output{
234 my ($self, %args) = @_;
235 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
236 my $dbh = $self->dbh;
237
238 my $statement = "select
239 collection_output_id,
240 article_name,
241 service_instance_id
242 from collection_output where service_instance_id = ? ";
243 my $result = do_query($dbh, $statement, ($siid));
244 return $result;
245 }
246  
247 # pass service_instance_lsid, article_name
248 sub insert_collection_output {
249 my ($self, %args) = @_;
250 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
251 my $dbh = $self->dbh;
252 $self->dbh->do("insert into collection_output (service_instance_id, article_name) values (?,?)",
253 undef, $siid,$args{'article_name'});
254 my $id=$self->dbh->{mysql_insertid};
255 return $id;
256 }
257  
258 # pass argument service_instance_lsid
259 sub delete_collection_output{
260 my ($self, %args) = @_;
261 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
262 my $dbh = $self->dbh;
263 my $statement = "delete from collection_output where service_instance_id = ?";
264 my @bindvalues = ();
265 $dbh->do( $statement, undef, ($siid));
266  
267 if ($dbh->err){
268 return (1, $dbh->errstr);
269 }
270 else{
271 return 0;
272 }
273 }
274  
275 # pass service_instance_lsid
276 sub query_simple_input{
277 my ($self, %args) = @_;
278 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
279 my $collid = $args{collection_input_id};
280 my $id_to_use = $siid?$siid:$collid;
281
282 my $dbh = $self->dbh;
283
284 my $statement = "select
285 simple_input_id,
286 object_type_uri,
287 namespace_type_uris,
288 article_name,
289 service_instance_id,
290 collection_input_id
291 from simple_input where ";
292
293 my $condition;
294 $siid && ($condition = " service_instance_id = ? and collection_input_id IS NULL");
295 $collid && ($condition = " collection_input_id = ?");
296 $statement .= $condition;
297
298 my $result = do_query($dbh, $statement, ($id_to_use));
299 return $result;
300 }
301
302 # pass service_instance_lsid, object_type_uri, namespace_type_uris, article_name, collection_input_id
303 sub insert_simple_input {
304 my ($self, %args) = @_;
305 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
306 my $dbh = $self->dbh;
307 $dbh->do("insert into simple_input
308 (object_type_uri,
309 namespace_type_uris,
310 article_name,
311 service_instance_id,
312 collection_input_id)
313 values (?,?,?,?,?)",
314 undef,
315 $args{'object_type_uri'},
316 $args{'namespace_type_uris'},
317 $args{'article_name'},
318 $siid,
319 $args{'collection_input_id'});
320 my $id=$dbh->{mysql_insertid};
321 return $id;
322 }
323  
324 # pass service_instance_lsid
325 sub delete_simple_input{
326 my ($self, %args) = @_;
327 my $dbh = $self->dbh;
328 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
329 my ($collid) = $args{collection_input_id};
330 my $statement1; my $statement2;
331 $siid && ($statement1 = "delete from simple_input where service_instance_id = ?");
332 $collid && ($statement2 = "delete from simple_input where collection_input_id = ?");
333
334 $siid && ($dbh->do( $statement1, undef,($siid)));
335 $collid && ($dbh->do($statement2, undef,($collid)));
336 if ($dbh->err){
337 return (1, $dbh->errstr);
338 }
339 else{
340 return 0;
341 }
342 }
343  
344 sub delete_inputs { # this should replace all other delete_*_input
345 my ($self, %args) = @_;
346 my $dbh = $self->dbh;
347 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
348 my $result_ids = $self->query_collection_input(service_instance_lsid => $self->lsid);
349  
350 my $statement = "delete from simple_input where service_instance_id = ?";
351  
352 $dbh->do( $statement, undef,($siid));
353 if ($dbh->err){
354 return (1, $dbh->errstr);
355 }
356 else{
357 return 0;
358 }
359
360 }
361  
362 sub delete_output { # this should replace all other delete_*_output
363
364 }
365  
366 # UGH this has to know too much bout the underlying database structure e.g. that one is null and other is full
367 # this problem is in MOBY::Central line 3321 3346 and 3374
368 #****** FIX
369 # send service_instance_lsid, collection_input_id
370 sub query_simple_output{
371 my ($self, %args) = @_;
372 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
373 my $collid = $args{collection_output_id};
374 my $dbh = $self->dbh;
375 my $id_to_use = $siid?$siid:$collid;
376
377 my $statement = "select
378 simple_output_id,
379 object_type_uri,
380 namespace_type_uris,
381 article_name,
382 service_instance_id,
383 collection_output_id
384 from simple_output where ";
385 my $condition;
386 $siid && ($condition = " service_instance_id = ? and collection_output_id IS NULL");
387 $collid && ($condition = " collection_output_id = ?");
388 $statement .= $condition;
389  
390  
391 my $result = do_query($dbh, $statement, ($id_to_use));
392 return $result;
393 }
394  
395 # pass args service_instance_id and collection_output_id
396 sub insert_simple_output {
397 my ($self, %args) = @_;
398 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
399 my $dbh = $self->dbh;
400  
401 $dbh->do("insert into simple_output
402 (object_type_uri,
403 namespace_type_uris,
404 article_name,
405 service_instance_id,
406 collection_output_id)
407 values (?,?,?,?,?)",
408 undef,(
409 $args{'object_type_uri'},
410 $args{'namespace_type_uris'},
411 $args{'article_name'},
412 $siid,
413 $args{'collection_output_id'}));
414 my $id=$dbh->{mysql_insertid};
415 return $id;
416  
417 }
418  
419 # pass service_instance_id or collection_output_id
420 sub delete_simple_output{
421 my ($self, %args) = @_;
422 my $dbh = $self->dbh;
423 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
424 my ($collid) = $args{collection_output_id};
425 my $statement1; my $statement2;
426 $siid && ($statement1 = "delete from simple_output where service_instance_id = ?");
427 $collid && ($statement2 = "delete from simple_output where collection_output_id = ?");
428
429 $siid && ($dbh->do( $statement1, undef,($siid)));
430 $collid && ($dbh->do($statement2, undef,($collid)));
431 if ($dbh->err){
432 return (1, $dbh->errstr);
433 }
434 else{
435 return 0;
436 }
437 }
438  
439 # pass service_instance_lsid
440 sub query_secondary_input{
441 my ($self, %args) = @_;
442 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
443 my $dbh = $self->dbh;
444
445 my $statement = "select
446 secondary_input_id,
447 default_value,
448 maximum_value,
449 minimum_value,
450 enum_value,
451 datatype,
452 article_name,
453 description,
454 service_instance_id
455 from secondary_input where service_instance_id = ?";
456 my $result = do_query($dbh, $statement, ($siid));
457 return $result;
458 }
459  
460 # pass default_value, maximum_value minimum_value enum_value datatype article_name service_instance_lsid
461 sub insert_secondary_input{
462 my ($self, %args) = @_;
463 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
464 my $dbh = $self->dbh;
465 $dbh->do(q{insert into secondary_input (default_value,maximum_value,minimum_value,enum_value,datatype,article_name,description,service_instance_id) values (?,?,?,?,?,?,?,?)},
466 undef,
467 (
468 $args{'default_value'}, $args{'maximum_value'},
469 $args{'minimum_value'}, $args{'enum_value'},
470 $args{'datatype'}, $args{'article_name'}, $args{'description'},$siid)
471 );
472 return $dbh->{mysql_insertid};
473 }
474  
475 # pass service_instance_lsid
476 sub delete_secondary_input{
477 my ($self, %args) = @_;
478 my ($siid) = $self->_getSIIDFromLSID($args{service_instance_lsid});
479 my $dbh = $self->dbh;
480 my $statement = "delete from secondary_input where service_instance_id=?";
481  
482 $dbh->do( $statement, undef, ($siid));
483 if ($dbh->err){
484 return (1, $dbh->errstr);
485 }
486 else{
487 return 0;
488 }
489 }
490  
491  
492 # receives argument "type", that may be either an LSID or a type term
493 sub query_object {
494 my ($self, %args) = @_;
495 my $type = $args{type};
496 my $condition = "";
497 if ($type =~ /^urn\:lsid/){
498 $condition = "where object_lsid = ?";
499 } elsif ($type) {
500 $condition = "where object_type = ?";
501 }
502 my $statement = "select
503 object_id,
504 object_lsid,
505 object_type,
506 description,
507 authority,
508 contact_email
509 from object $condition";
510  
511 my $dbh = _getDBHandle("mobyobject");
512 my $result;
513 if ($type){
514 $result = do_query($dbh, $statement, ($type));
515 } else {
516 $result = do_query($dbh, $statement);
517 }
518 return $result;
519 }
520  
521 # inserts a new tuple into object table
522 # pass object_type object_lsid description authority contact_email
523 sub insert_object{
524 my ($self, %args) = @_;
525 my $dbh = $self->dbh;
526 $dbh->do("insert into object
527 (object_type,
528 object_lsid,
529 description,
530 authority,
531 contact_email)
532 values (?,?,?,?,?)",
533 undef,
534 $args{'object_type'},
535 $args{'object_lsid'},
536 $args{'description'},
537 $args{'authority'},
538 $args{'contact_email'});
539 my $id=$dbh->{mysql_insertid};
540 return $id;
541 }
542  
543 # pass 'type' which is either an LSID or a term
544 sub delete_object{
545 my ($self, %args) = @_;
546 my $dbh = $self->dbh;
547 my $term = $args{type};
548 return 0 unless $term;
549 my $result = $self->query_object(type => $term);
550 my $row = shift(@$result);
551 my $id = $row->{object_id};
552 my $lsid = $row->{object_lsid};
553 my $statement = "delete from object where object_lsid = ?";
554 $dbh->do( $statement,undef, ($lsid) );
555
556 $self->_delete_object_term2term(id => $id);
557 if ($dbh->err){
558 return (1, $dbh->errstr);
559 }
560 else{
561 return 0;
562 }
563 }
564  
565 # pass "type" here, should be an LSID, preferably...
566 sub query_object_term2term{
567 my ($self, %args) = @_;
568 my $type = $args{type};
569 my $result = $self->query_object(type => $type);
570 my $row = shift(@$result);
571 my $id = $row->{object_id};
572 return [{}] unless $id;
573 my $dbh = $self->dbh;
574
575 my $statement = "select
576 assertion_id,
577 relationship_type,
578 object1_id,
579 object2_id,
580 object2_articlename
581 from object_term2term where object2_id = ?";
582 my $result2 = do_query($dbh, $statement, ($id));
583 return $result2;
584 }
585  
586 # pass object1_type, object2_type, object2_articlename, relationship_type
587 sub insert_object_term2term{
588 my ($self, %args) = @_;
589 my $type1 = $args{object1_type};
590 my $result = $self->query_object(type => $type1);
591 my $row = shift(@$result);
592 my $id1 = $row->{object_id};
593 my $type2 = $args{object2_type};
594 $result = $self->query_object(type => $type2);
595 $row = shift(@$result);
596 my $id2 = $row->{object_id};
597 my $relationship_type = $args{relationship_type};
598 my $object2_articlename = $args{object2_articlename};
599  
600 my $dbh = $self->dbh;
601 $dbh->do(
602 q{insert into object_term2term (relationship_type, object1_id, object2_id, object2_articlename) values (?,?,?,?)},
603 undef,
604 $relationship_type,
605 $id1,
606 $id2,
607 $object2_articlename
608 );
609
610 return $dbh->{mysql_insertid};
611 }
612  
613 # pass object 'type' as term or lsid
614 # this should be a private routine, not a public one.
615 # SHOULD NOT BE DOCUMENTED IN THE API
616 sub _delete_object_term2term{
617 my ($self, %args) = @_;
618 my $o1id = $args{id};
619 return 0 unless defined($o1id);
620 my $dbh = $self->dbh;
621 my $statement = "delete from object_term2term where object1_id=?";
622 $dbh->do( $statement,undef, ($o1id));
623
624 if ($dbh->err){
625 return (1, $dbh->errstr);
626 }
627 else{
628 return 0;
629 }
630 }
631  
632 # pass servicename and authority_uri
633 # TODO added LIKE binary here
634 sub query_service_existence {
635 my ($self, %args) = @_;
636 my $dbh = $self->dbh;
637  
638 my $servicename = $args{'servicename'};
639 my $authURI = $args{'authority_uri'};
640 my $result = $self->_query_authority(authority_uri => $authURI);
641 return 0 unless @$result[0];
642 my $id = @$result[0]->{authority_id};
643 return 0 unless $id;
644 my $statement = "select
645 service_instance_id,
646 category,
647 servicename,
648 service_type_uri,
649 authority_id,
650 url,
651 contact_email,
652 authoritative,
653 description,
654 signatureURL,
655 lsid
656 from service_instance where servicename LIKE binary ? and authority_id = ?";
657 my $final = do_query($dbh, $statement, ($servicename, $id));
658 if (@$final[0]){return 1} else {return 0}
659
660 }
661 # selects all the columns from service_instance table
662 # PAY ATTENTION to what this returns. Not auth_id but auth_uri!!
663 # IMPORTANT: must use quotes for the keys of the hash (eg. 'authority.authority_uri' => $value )
664 sub query_service_instance {
665 my ($self, %args) = @_;
666 my $dbh = $self->dbh;
667
668 my @args;
669 while (my ($k, $v) = each %args){
670 push @args, ({$k => $v}, "and"); # format for the_add_condition subroutine
671 # but too bad won't be scalable for "or"
672 }
673
674 if (keys(%args)){ pop @args;} # remove final "and"
675
676 my $statement = "select
677 service_instance_id,
678 category,
679 servicename,
680 service_type_uri,
681 authority.authority_uri,
682 url,
683 service_instance.contact_email,
684 authoritative,
685 description,
686 signatureURL,
687 lsid
688 from service_instance, authority ";
689 my @bindvalues;
690 ($statement, @bindvalues) =_add_condition($statement, @args);
691 if (keys(%args)){
692 $statement .= " and authority.authority_id = service_instance.authority_id";
693 } else {
694 $statement .= " where authority.authority_id = service_instance.authority_id";
695 }
696 my $final = do_query($dbh, $statement, @bindvalues);
697 return $final;
698 }
699  
700 # custom query for Moby::Central.pm->findService()
701 # hmmmmmmm.... I'm not sure that this routine should exist...
702 # it is redundant to the routine above, if the routine above were executed
703 # multiple times. I think that is the more correct (though less efficient)
704 # way to go, since it is "scalable" to every possible underlying data source
705 # ********FIX change this later...
706 sub match_service_type_uri{
707 my ($self, %args) = @_;
708 my $dbh = $self->dbh;
709 my $uri_list = $args{'service_type_uri'};
710 my $statement = "select service_instance_id,category, servicename, service_type_uri, authority_id, url, contact_email, authoritative, description, signatureURL, lsid from service_instance where service_type_uri in ($uri_list)";
711 my @bindvalues = ();
712 my $result = do_query($dbh, $statement, @bindvalues);
713 return $result;
714 }
715  
716 # passs........ blah blah.....
717 sub insert_service_instance {
718 my ($self, %args) = @_;
719 my $dbh = $self->dbh;
720 my $authority_id;
721 if ($args{'authority_uri'}){ # need to transform URI to a row ID
722 my $result = $self->_query_authority(authority_uri => $args{'authority_uri'});
723 return undef unless @$result[0];
724 $authority_id = @$result[0]->{authority_id};
725 return undef unless $authority_id;
726 }
727  
728 $dbh->do(q{insert into service_instance (category, servicename, service_type_uri, authority_id, url, contact_email, authoritative, description, signatureURL, lsid) values (?,?,?,?,?,?,?,?,?,?)},
729 undef,(
730 $args{'category'},
731 $args{'servicename'},
732 $args{'service_type_uri'},
733 $authority_id,
734 $args{'url'},
735 $args{'contact_email'},
736 $args{'authoritative'},
737 $args{'description'},
738 $args{'signatureURL'},
739 $args{'lsid'}));
740
741 my $id = $dbh->{mysql_insertid};
742 return $id;
743 }
744  
745 # pass service_instance_lsid
746 sub delete_service_instance{
747 my ($self, %args) = @_;
748 my $dbh = $self->dbh;
749 my $statement = "delete from service_instance where lsid = ?";
750 $dbh->do( $statement,undef, ($args{service_instance_lsid}) );
751 if ($dbh->err){
752 return (1, $dbh->errstr);
753 }
754 else{
755 return 0;
756 }
757 }
758
759 # Selects all columns EXCEPT authority_id
760 # pass authority_uri
761 sub query_authority {
762 my ($self, %args) = @_;
763 my $authURI = $args{authority_uri};
764 my $dbh = $self->dbh;
765
766 my $statement = "select
767 authority_common_name,
768 authority_uri,
769 contact_email
770 from authority where authority_uri = ?";
771 my $result = do_query($dbh, $statement, ($authURI));
772 return $result;
773 }
774  
775 # Selects all columns including authority_id
776 # pass authority_uri. NOTE THAT THIS IS A PRIVATE ROUTINE
777 # SHOULD NOT BE DOCUMENTED IN THE API
778 sub _query_authority {
779 my ($self, %args) = @_;
780 my $authURI = $args{authority_uri};
781 my $dbh = $self->dbh;
782
783 my $statement = "select
784 authority_common_name,
785 authority_uri,
786 authority_id,
787 contact_email
788 from authority where authority_uri = ?";
789 my $result = do_query($dbh, $statement, ($authURI));
790 return $result;
791 }
792  
793 # custom query routine used in Moby::Central.pm -> retrieveServiceProviders()
794 # no args passed
795 sub get_all_authorities{
796 my ($self, @args) = @_;
797 my $dbh = $self->dbh;
798 my $statement = "select distinct authority.authority_uri from service_instance right join authority on authority.authority_id = service_instance.authority_id where servicename IS NOT NULL order by authority.authority_uri;";
799 my @bindvalues = ();
800 my $result = do_query($dbh, $statement, @bindvalues);
801 return $result;
802 }
803  
804 # pass authority_common_name, authority_uri, contact_email, return ID of some sort
805 sub insert_authority{
806 my ($self, %args) = @_;
807 my $dbh = $self->dbh;
808 $dbh->do("insert into authority
809 (authority_common_name,
810 authority_uri,
811 contact_email)
812 values (?,?,?)",
813 undef,
814 ($args{'authority_common_name'},
815 $args{'authority_uri'},
816 $args{'contact_email'}));
817 my $id = $dbh->{mysql_insertid};
818 return $id;
819 }
820  
821 # pass service_type, as term or LSID
822 sub query_service{
823 my ($self, %args) = @_;
824 my $type = $args{type}||"";
825 my $condition = "";
826 if ($type =~ /^urn\:lsid/){
827 $condition = "where service_lsid = ?";
828 } elsif ($type) {
829 $condition = "where service_type = ?";
830 } else {
831 $condition = "";
832 }
833
834 my $dbh = _getDBHandle("mobyservice");
835  
836 my $statement = "select
837 service_id,
838 service_lsid,
839 service_type,
840 description,
841 authority,
842 contact_email
843 from
844 service
845 $condition";
846 my $result;
847 if ($type){
848 $result = do_query($dbh, $statement, ($type));
849 } else {
850 $result = do_query($dbh, $statement);
851 }
852 return $result;
853 }
854  
855 sub new_query_service{
856 my ($self, %args) = @_;
857 my $type = $args{type}||"";
858 my $condition = "";
859 if ($type =~ /^urn\:lsid/){
860 $condition = "where s1.service_lsid = ?";
861 } elsif ($type) {
862 $condition = "where s1.service_type = ?";
863 } else {
864 $condition = "";
865 }
866
867 my $dbh = _getDBHandle("mobyservice");
868  
869 my $statement = "select
870 s1.service_id as service_id,
871 s1.service_lsid as service_lsid,
872 s1.service_type as service_type,
873 s1.description as description,
874 s1.authority as authority,
875 s1.contact_email as contact_email,
876 s2.service_type as parent_type,
877 s2.service_lsid as parent_lsid
878 from
879 service as s1
880 left join service_term2term as t
881 on s1.service_id= t.service1_id
882 left join service as s2
883 on s2.service_id=t.service2_id
884 $condition";
885 my $result;
886 if ($type){
887 $result = do_query($dbh, $statement, ($type));
888 } else {
889 $result = do_query($dbh, $statement);
890 }
891 return $result;
892 }
893  
894  
895  
896 # pass in ....
897 sub insert_service{
898 my ($self, %args) = @_;
899 my $dbh = $self->dbh;
900 $dbh->do(q{insert into service (service_type, service_lsid, description, authority, contact_email) values (?,?,?,?,?)},
901 undef,
902 (
903 $args{'service_type'}, $args{'service_lsid'}, $args{'description'},
904 $args{'authority'}, $args{'contact_email'}
905 )
906 );
907 return $dbh->{mysql_insertid};
908 }
909  
910 # pass in 'type' as a term or lsid
911 sub delete_service{
912 my ($self, %args) = @_;
913 my $type = $args{type};
914 my $result = $self->query_service(type => $type);
915 my $row = shift(@$result);
916 my $id = $row->{service_id};
917 my $lsid = $row->{service_lsid};
918 return 0 unless $lsid;
919 my $dbh = $self->dbh;
920 my $statement = "delete from service where service_lsid = ?";
921 $dbh->do( $statement, undef, ($lsid));
922 $self->_delete_service_term2term(id => $id);
923 if ($dbh->err){
924 return (1, $dbh->errstr);
925 }
926 else{
927 return 0;
928 }
929 }
930  
931 sub query_service_term2term{
932 my ($self, %args) = @_;
933 my $type = $args{type};
934 my $result = $self->query_service(type => $type);
935 my $row = shift(@$result);
936 my $id = $row->{service_id};
937 return [{}] unless $id;
938 my $dbh = $self->dbh;
939
940 my $statement = "select
941 assertion_id,
942 relationship_type,
943 service1_id,
944 service2_id
945 from service_term2term where service2_id = ?";
946 my $result2 = do_query($dbh, $statement, ($id));
947 return $result2;
948 }
949  
950 #pass relationshiptype, servce1_type, service2_type
951 sub insert_service_term2term{
952 my ($self, %args) = @_;
953 my $type1 = $args{service1_type};
954 my $result = $self->query_service(type => $type1);
955 my $row = shift(@$result);
956 my $id1 = $row->{service_id};
957 my $type2 = $args{service2_type};
958 $result = $self->query_service(type => $type2);
959 $row = shift(@$result);
960 my $id2 = $row->{service_id};
961 my $relationship_type = $args{relationship_type};
962  
963 my $dbh = $self->dbh;
964 $dbh->do(q{insert into service_term2term (relationship_type, service1_id, service2_id) values (?,?,?)},
965 undef,
966 ($relationship_type,
967 $id1,
968 $id2)
969 );
970
971 return $dbh->{mysql_insertid};
972 }
973  
974  
975 # NOTE THAT THIS IS A PRIVATE FUNCTION AND SHOULD
976 # NOT BE DOCUMENTED IN THE API.
977 sub _delete_service_term2term{
978 my ($self, %args) = @_;
979 my $id = $args{id};
980 return 0 unless (defined($id));
981 my $dbh = $self->dbh;
982 my $statement = "delete from service_term2term where service1_id=?";
983 $dbh->do( $statement,undef, ($id));
984 if ($dbh->err){
985 return (1, $dbh->errstr);
986 }
987 else{
988 return 0;
989 }
990 }
991  
992  
993 sub query_relationship{
994 my ($self, %args) = @_;
995 my $type = $args{type} || "";
996 # return [{}] unless $type;
997 my $condition = "";
998 if ($type =~ /^urn\:lsid/){
999 $condition = " relationship_lsid = ? and ";
1000 } elsif ($type) {
1001 $condition = " relationship_type = ? and ";
1002 }
1003 my $ont = $args{ontology};
1004
1005 my $dbh = $self->dbh;
1006
1007 my $statement = "select
1008 relationship_id,
1009 relationship_lsid,
1010 relationship_type,
1011 container,
1012 description,
1013 authority,
1014 contact_email,
1015 ontology
1016 from relationship where $condition ontology = ?";
1017
1018 if ($type){
1019 return do_query($dbh, $statement, ($type, $ont));
1020 } else {
1021 return do_query($dbh, $statement, ($ont));
1022 }
1023 }
1024  
1025 sub query_namespace{
1026 my ($self, %args) = @_;
1027 my $type = $args{type};
1028 my $condition = "";
1029 if ($type =~ /^urn\:lsid/){
1030 $condition = " where namespace_lsid = ?";
1031 } elsif ($type) {
1032 $condition = " where namespace_type = ?";
1033 } else {
1034 $condition = "";
1035 }
1036
1037 my $dbh = _getDBHandle("mobynamespace");
1038
1039 my $statement = "select
1040 namespace_id,
1041 namespace_lsid,
1042 namespace_type,
1043 description,
1044 authority,
1045 contact_email
1046 from namespace $condition";
1047 my $result;
1048 if ($type){
1049 $result = do_query($dbh, $statement, ($type));
1050 } else {
1051 $result = do_query($dbh, $statement);
1052 }
1053 return $result;
1054 }
1055  
1056  
1057 sub insert_namespace{
1058 my ($self, %args) = @_;
1059 my $dbh = $self->dbh;
1060 $dbh->do(q{insert into namespace (namespace_type, namespace_lsid, description, authority,contact_email) values (?,?,?,?,?)},
1061 undef,
1062 (
1063 $args{'namespace_type'}, $args{'namespace_lsid'},$args{'description'},$args{'authority'},$args{'contact_email'}
1064 )
1065 );
1066 return $dbh->{mysql_insertid};
1067 }
1068  
1069 # pass namesapce_lsid
1070 sub delete_namespace{
1071 my ($self, %args) = @_;
1072 my $type = $args{type};
1073 my $result = $self->query_namespace(type => $type);
1074 my $row = shift(@$result);
1075 my $id = $row->{namespace_id};
1076 my $lsid = $row->{namespace_lsid};
1077 return 0 unless $lsid;
1078 my $dbh = $self->dbh;
1079 my $statement = "delete from namespace where namespace_lsid = ?";
1080 $dbh->do( $statement, undef, ($lsid));
1081 $self->_delete_namespace_term2term(id => $id);
1082 if ($dbh->err){
1083 return (1, $dbh->errstr);
1084 }
1085 else{
1086 return 0;
1087 }
1088 }
1089  
1090 sub query_namespace_term2term{
1091 my ($self, %args) = @_;
1092 my $type = $args{type};
1093 my $result = $self->query_namespace(type => $type);
1094 my $row = shift(@$result);
1095 my $id = $row->{namespace_id};
1096 return [{}] unless $id;
1097 my $dbh = $self->dbh;
1098
1099 my $statement = "select
1100 assertion_id,
1101 relationship_type,
1102 namespace1_id,
1103 namespace2_id
1104 from namespace_term2term where namespace2_id = ?";
1105 my $result2 = do_query($dbh, $statement, ($id));
1106 return $result2;
1107 }
1108  
1109 # PRIVATE, NOT PART OF API!
1110 sub _delete_namespace_term2term{
1111 my ($self, %args) = @_;
1112 my $id = $args{id};
1113 return 0 unless defined($id);
1114 my $dbh = $self->dbh;
1115 my $statement = "delete from namespace_term2term where namespace1_id=?";
1116 $dbh->do( $statement,undef, ($id));
1117 if ($dbh->err){
1118 return (1, $dbh->errstr);
1119 }
1120 else{
1121 return 0;
1122 }
1123 }
1124 # pass type as LSID or term
1125 sub check_object_usage{
1126 my ($self, %args) = @_;
1127 my $dbh = $self->dbh;
1128 my $errorMsg = 1;
1129 my $type = $args{type};
1130 return 0 unless $type;
1131 my $result = $self->query_object(type => $type);
1132 my $row = shift @$result;
1133 my $lsid = $row->{object_lsid};
1134
1135 my ($id) = $dbh->selectrow_array(q{select service_instance.service_instance_id from service_instance natural join simple_input where object_type_uri = ?},
1136 undef, $lsid
1137 );
1138 return $errorMsg
1139 if ($id);
1140
1141 ($id) = $dbh->selectrow_array(q{select service_instance.service_instance_id from service_instance natural join simple_output where object_type_uri = ?},
1142 undef, $lsid
1143 );
1144 return $errorMsg
1145 if ($id);
1146
1147 ($id) = $dbh->selectrow_array(q{select service_instance.service_instance_id from service_instance natural join collection_input natural join simple_input where object_type_uri = ?},
1148 undef, $lsid
1149 );
1150 return $errorMsg
1151 if ($id);
1152
1153 ($id) = $dbh->selectrow_array(q{select service_instance.service_instance_id from service_instance natural join collection_output natural join simple_output where object_type_uri = ?},
1154 undef, $lsid
1155 );
1156 return $errorMsg
1157 if ($id);
1158
1159 return 0;
1160 }
1161  
1162 # custom query routine for Moby::Central.pm -> deregisterNamespace()
1163 sub check_namespace_usage{
1164 my ($self, %args) = @_;
1165 my $dbh = $self->dbh;
1166 my $errorMsg = 1;
1167 my $type = $args{type};
1168 return 0 unless $type;
1169 my $result = $self->query_namespace(type => $type);
1170 my $row = shift @$result;
1171 my $lsid = $row->{namespace_lsid};
1172
1173 my $sth = $dbh->prepare("select service_instance.service_instance_id, namespace_type_uris from service_instance natural join simple_input where INSTR(namespace_type_uris,'$lsid')"
1174 );
1175 $sth->execute;
1176  
1177 while ( my ( $id, $ns ) = $sth->fetchrow_array() ) {
1178 my @nss = split ",", $ns;
1179 foreach (@nss) {
1180 $_ =~ s/\s//g;
1181 my $errstr = "Namespace Type $type ($_) is used by a service (service ID number $id) and may not be deregistered";
1182 return (1, $errstr)
1183 if ( $_ eq $lsid );
1184 }
1185 }
1186 $sth = $dbh->prepare("select service_instance.service_instance_id, namespace_type_uris from service_instance natural join simple_output where INSTR(namespace_type_uris,'$lsid')"
1187 );
1188 $sth->execute;
1189 while ( my ( $id, $ns ) = $sth->fetchrow_array() ) {
1190 my @nss = split ",", $ns;
1191 foreach (@nss) {
1192 $_ =~ s/\s//g;
1193 my $errstr = "Namespace Type $type ($_) is used by a service (service ID number $id) and may not be deregistered";
1194 return (1, $errstr)
1195 if ( $_ eq $lsid );
1196 }
1197 }
1198 $sth =
1199 $dbh->prepare("select service_instance.service_instance_id, namespace_type_uris from service_instance natural join collection_input natural join simple_input where INSTR(namespace_type_uris, '$lsid')"
1200 );
1201 $sth->execute;
1202 while ( my ( $id, $ns ) = $sth->fetchrow_array() ) {
1203 my @nss = split ",", $ns;
1204 foreach (@nss) {
1205 $_ =~ s/\s//g;
1206 my $errstr = "Namespace Type $type ($_) is used by a service (service ID number $id) and may not be deregistered";
1207 return (1, $errstr)
1208 if ( $_ eq $lsid );
1209 }
1210 }
1211 $sth =
1212 $dbh->prepare("select service_instance.service_instance_id, namespace_type_uris from service_instance natural join collection_output natural join simple_output where INSTR(namespace_type_uris, '$lsid')"
1213 );
1214 $sth->execute;
1215 while ( my ( $id, $ns ) = $sth->fetchrow_array() ) {
1216 my @nss = split ",", $ns;
1217 foreach (@nss) {
1218 $_ =~ s/\s//g;
1219 my $errstr = "Namespace Type $type ($_) is used by a service (service ID number $id) and may not be deregistered";
1220 return (1, $errstr)
1221 if ( $_ eq $lsid );
1222 }
1223 }
1224 return (0, "");
1225 }
1226  
1227 # custom query routine for Moby::Central.pm -> findService()
1228 sub check_keywords{
1229 my ($self, %args) = @_;
1230 my $dbh = $self->dbh;
1231 my $param = $args{keywords};
1232 return ([{}]) unless (ref($param) =~ /ARRAY/);
1233 my @keywords = @$param;
1234 #my %findme = %$param;
1235 my $searchstring;
1236 foreach my $kw ( @keywords ) {
1237 $kw =~ s/\*//g;
1238 $kw = $dbh->quote("%$kw%");
1239 $searchstring .= " OR description like $kw ";
1240 }
1241 $searchstring =~ s/OR//; # remove just the first OR in the longer statement
1242  
1243 my $statement = "select service_instance_id,category, servicename, service_type_uri, authority_id, url, contact_email, authoritative, description, signatureURL, lsid from service_instance where $searchstring";
1244 my @bindvalues = ();
1245
1246 my $ids = do_query($dbh, $statement, @bindvalues);
1247 return ($ids);
1248 }
1249
1250 # custom query subroutine for Moby::Central.pm->_searchForSimple()
1251 sub find_by_simple{
1252 my ($self, %args) = @_;
1253 my $dbh = $self->dbh;
1254 my $inout = $args{'inout'};
1255 my $ancestor_string = $args{'ancestor_string'};
1256 my $namespaceURIs = $args{'namespaceURIs'};
1257
1258 my $query ="select service_instance_id, namespace_type_uris from simple_$inout where object_type_uri in ($ancestor_string) and collection_${inout}_id IS NULL "
1259 ; # if service_instance_id is null then it must be a collection input.
1260 my $nsquery;
1261 foreach my $ns ( @{$namespaceURIs} ) { # namespaces are already URI's
1262 $nsquery .= " OR INSTR(namespace_type_uris, '$ns') ";
1263 }
1264 if ($nsquery) {
1265 $nsquery =~ s/OR//; # just the first
1266 $nsquery .= " OR namespace_type_uris IS NULL";
1267 $query .= " AND ($nsquery) ";
1268 }
1269
1270 my $result = do_query($dbh, $query, ());
1271 return $result;
1272 }
1273  
1274 # custom query subroutine for Moby::Central.pm->_searchForCollection()
1275 sub find_by_collection{
1276 my ($self, %args) = @_;
1277 my $dbh = $self->dbh;
1278 my $inout = $args{'inout'};
1279 my $objectURI = $args{'objectURI'};
1280 my $namespaceURIs = $args{'namespaceURIs'};
1281
1282 my $query = "select
1283 c.service_instance_id,
1284 s.namespace_type_uris
1285 from
1286 simple_$inout as s,
1287 collection_$inout as c
1288 where
1289 s.collection_${inout}_id IS NOT NULL
1290 AND s.collection_${inout}_id = c.collection_${inout}_id
1291 AND object_type_uri = '$objectURI' ";
1292 my $nsquery;
1293 foreach my $ns ( @{$namespaceURIs} ) { # namespaces are already URI's
1294 $nsquery .= " OR INSTR(namespace_type_uris, '$ns') ";
1295 }
1296 if ($nsquery) {
1297 $nsquery =~ s/^\sOR//; # just the first
1298 $nsquery .= " OR namespace_type_uris IS NULL";
1299 $query .= " AND ($nsquery) "; # add the AND clause
1300 }
1301
1302 my $result = do_query($dbh, $query, ());
1303 return $result;
1304 }
1305  
1306 # custom query subroutine for Moby::Central.pm->RetrieveServiceNames
1307 sub get_service_names{
1308 my ($self, %args) = @_;
1309 my $dbh = $self->dbh;
1310 my $statement = "select authority_uri, servicename, lsid from authority as a, service_instance as s where s.authority_id = a.authority_id";
1311 my @bindvalues = ();
1312
1313 my $result = do_query($dbh, $statement, @bindvalues);
1314 return $result;
1315 }
1316  
1317 # custom query for Moby::Central.pm->_flatten
1318 sub get_parent_terms{
1319 my ($self, %args) = @_;
1320 my $dbh = $self->dbh;
1321
1322 my $type_id = $args{'relationship_type_id'};
1323 my $statement = "
1324 select
1325 OE1.term
1326 from
1327 OntologyEntry as OE1,
1328 OntologyEntry as OE2,
1329 Term2Term as TT
1330 where
1331 ontologyentry2_id = OE2.id
1332 and ontologyentry1_id = OE1.id
1333 and relationship_type_id = $type_id
1334 and OE2.term = ?";
1335
1336 my @bindvalues = ();
1337 push(@bindvalues, $args{'term'});
1338
1339 my $result = do_query($dbh, $statement, @bindvalues);
1340 return $result;
1341 }
1342  
1343 # custom query subroutine for selecting from object_term2term and object tables
1344 # used in Moby::OntologyServer.pm->retrieveObject()
1345 sub get_object_relationships{
1346 my ($self, %args) = @_;
1347 my $dbh = $self->dbh;
1348 my $type = $args{type};
1349 return 0 unless $type;
1350 my $result = $self->query_object(type => $type);
1351 my $row = shift @$result;
1352 my $id = $row->{object_id};
1353  
1354 my $statement = "select
1355 relationship_type,
1356 object_type,
1357 object_lsid,
1358 description,
1359 authority,
1360 contact_email,
1361 object2_articlename
1362 from object_term2term, object
1363 where object1_id = ? and object2_id = object_id";
1364
1365 my $result2 = do_query($dbh, $statement, ($id));
1366 return $result2;
1367 }
1368  
1369 # relationship query for any table used in Moby::OntologyServer->_doRelationshipQuery()
1370 # note: returns a reference to an array containing ARRAY references
1371 sub get_relationship{
1372 my ($self, %args) = @_;
1373 my $dbh = $self->dbh;
1374 my $direction = $args{'direction'};
1375 my $ontology = $args{'ontology'};
1376 my $relationship = $args{'relationship'}; # this is assumed to be an LSID
1377  
1378 my $type = $args{'term'};
1379 return 0 unless $type;
1380 my $lsid;
1381 if ($ontology eq "service"){
1382 my $result = $self->query_service(type => $type);
1383 my $row = shift @$result;
1384 $lsid = $row->{service_lsid};
1385 } else {
1386 my $result = $self->query_object(type => $type);
1387 my $row = shift @$result;
1388 $lsid = $row->{object_lsid};
1389 }
1390 my $defs;
1391 my $extra_columns;
1392 $extra_columns = ", relationship_type ";
1393 if ($ontology eq "object"){$extra_columns .=", object2_articlename ";}
1394 if ( $direction eq 'root' ) {
1395 unless ( defined $relationship ) {
1396 $defs = $self->dbh->selectall_arrayref( "
1397 select distinct s2.${ontology}_lsid $extra_columns from
1398 ${ontology}_term2term as t2t,
1399 $ontology as s1,
1400 $ontology as s2
1401 where
1402 s1.${ontology}_id = t2t.${ontology}1_id and
1403 s2.${ontology}_id = t2t.${ontology}2_id and
1404 s1.${ontology}_lsid = ?", undef, $lsid ); # ")
1405 } else {
1406 $defs = $self->dbh->selectall_arrayref( "
1407 select distinct s2.${ontology}_lsid $extra_columns from
1408 ${ontology}_term2term as t2t,
1409 $ontology as s1,
1410 $ontology as s2
1411 where
1412 relationship_type = ? and
1413 s1.${ontology}_id = t2t.${ontology}1_id and
1414 s2.${ontology}_id = t2t.${ontology}2_id and
1415 s1.${ontology}_lsid = ?", undef, $relationship, $lsid ); # ")
1416 }
1417 } else {
1418 unless ( defined $relationship ) {
1419 $defs = $self->dbh->selectall_arrayref( "
1420 select distinct s1.${ontology}_lsid $extra_columns from
1421 ${ontology}_term2term as t2t,
1422 $ontology as s1,
1423 $ontology as s2
1424 where
1425 s1.${ontology}_id = t2t.${ontology}1_id and
1426 s2.${ontology}_id = t2t.${ontology}2_id and
1427 s2.${ontology}_lsid = ?", undef, $lsid); # ")
1428 } else {
1429 $defs = $self->dbh->selectall_arrayref( "
1430 select distinct s1.${ontology}_lsid $extra_columns from
1431 ${ontology}_term2term as t2t,
1432 $ontology as s1,
1433 $ontology as s2
1434 where
1435 relationship_type = ? and
1436 s1.${ontology}_id = t2t.${ontology}1_id and
1437 s2.${ontology}_id = t2t.${ontology}2_id and
1438 s2.${ontology}_lsid = ?", undef, $relationship, $lsid ); # ")
1439 }
1440 }
1441 return $defs;
1442 }
1443  
1444 # Get all relationships in the queried database in one go. The
1445 # complete table ${ontology}_term2term is transferred into a hash
1446 # whose reference is finally returned. Important: note that the hash
1447 # is built 'direction aware', that is for objects 'object1_id' is used
1448 # as key when direction is 'root' and 'object2_id' as value. Vice
1449 # versa for the 'leaves' direction. Likewise for services.
1450 # Returns a hash reference.
1451 sub get_all_relationships {
1452  
1453 my ($self, %args) = @_;
1454 my $direction = $args{'direction'};
1455 my $ontology = $args{'ontology'};
1456 # my $relationship = $args{'relationship'}; # has to be lsid!
1457  
1458 my $relHash;
1459 my $dbh = _getDBHandle("moby$ontology");
1460  
1461 my $statement = "select ${ontology}1_id, ${ontology}2_id, relationship_type";
1462 $statement .= ", object2_articlename, assertion_id " if $ontology eq 'object';
1463 $statement .= " from ${ontology}_term2term";
1464 # my $relationship_lsid = "urn:lsid:biomoby.org:${ontology}relation:isa";
1465 my $defs = $dbh->selectall_arrayref($statement);
1466  
1467 return {} unless @$defs;
1468 foreach my $def (@$defs) {
1469 my $relationship = $def->[2];
1470 if ( $relationship =~ /has/i ) {
1471 # HAS or HASA
1472 # >1 has/hasa child possible; also store articlename and assertion_id
1473 # hash structure: $relHash->{has/a-lsid}->{object1_id}->[object2_id,articlename,assertion_id]
1474 push @{$relHash->{$relationship}->{$def->[0]}}, [$def->[1],$def->[3],$def->[4]] if $direction eq 'root';
1475 push @{$relHash->{$relationship}->{$def->[1]}}, [$def->[0],$def->[3],$def->[4]] if $direction eq 'leaves';
1476 }
1477 elsif ( $relationship =~ /isa/i ) {
1478 # ISA
1479 push @{$relHash->{$relationship}->{$def->[1]}}, $def->[0] if $direction eq 'leaves'; # >1 child possible!
1480 $relHash->{$relationship}->{$def->[0]} = $def->[1] if $direction eq 'root'; # no multi parents!
1481 }
1482 else { return {}; }
1483 }
1484 return $relHash;
1485 }
1486  
1487 # retrieve details for a number of entities from table $ontology
1488 # represented by a list of ${ontology}_id's;
1489 # used in MOBY::OntologyServer::Relationships
1490 sub get_details_for_id_list {
1491 my ($self, $ontology, $fields, $idList) = @_;
1492  
1493 return {} unless @$idList;
1494 return {} unless @$fields;
1495  
1496 my $dbh = _getDBHandle("moby$ontology");
1497 my $result = {};
1498  
1499 # avoid errors due to wrong field names:
1500 my %existingFields;
1501 my @queryFields = ();
1502 my $resArray = $dbh->selectall_arrayref("SHOW COLUMNS FROM $ontology");
1503 foreach my $row ( @$resArray ) {
1504 $existingFields{$row->[0]}++;
1505 }
1506 foreach my $field ( @$fields ) {
1507 next if $field eq "${ontology}_id";
1508 if ( exists $existingFields{$field} ) {
1509 push @queryFields, $field;
1510 }
1511 else {
1512 warn "Requested field $field does not exist in table $ontology!";
1513 }
1514 }
1515  
1516 #
1517 my $statement = "select ${ontology}_id, ". join(",", @queryFields).
1518 " from $ontology where ${ontology}_id in (" .
1519 join(",", @$idList) . ")";
1520 $resArray = $dbh->selectall_arrayref($statement);
1521 foreach my $row ( @$resArray ) {
1522 my $entityId = shift @$row;
1523 foreach my $field (@queryFields) {
1524 my $value = shift @$row;
1525 $result->{$entityId}->{$field} = $value ? $value : '';
1526 }
1527 }
1528 return $result;
1529 }
1530  
1531 sub _checkURI {
1532
1533 # my $uri = "http://www.ics.uci.edu/pub/ietf/uri/#Related";
1534 #print "$1, $2, $3, $4, $5, $6, $7, $8, $9" if
1535 # $uri =~ m{^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?};
1536 #
1537 #The license for this recipe is available here.
1538 #
1539 #Discussion:
1540 #
1541 #If the match is successful, a URL such as
1542 #
1543 #http://www.ics.uci.edu/pub/ietf/uri/#Related
1544 #
1545 #will be broken down into the following group match variables:
1546 #
1547 #$1 = http:
1548 #$2 = http
1549 #$3 = //www.ics.uci.edu
1550 #$4 = www.ics.uci.edu
1551 #$5 = /pub/ietf/uri/
1552 #$6 =
1553 #$7 =
1554 #$8 = #Related
1555 #$9 = Related
1556 #
1557 #In general, this regular expression breaks a URI down into the following parts,
1558 #as defined in the RFC:
1559 #
1560 #scheme = $2
1561 #authority = $4
1562 #path = $5
1563 #query = $7
1564 #fragment = $9
1565  
1566 }
1567  
1568 sub DESTROY {}
1569  
1570 1;