[MOBY-guts] biomoby commit

Dennis Wang dwang at pub.open-bio.org
Fri Jul 15 16:40:45 UTC 2005


dwang
Fri Jul 15 12:40:45 EDT 2005
Update of /home/repository/moby/moby-live/Perl/MOBY
In directory pub.open-bio.org:/tmp/cvs-serv30130/Perl/MOBY

Modified Files:
	OntologyServer.pm 
Log Message:
All SQL statements in OntologyServer.pm has been moved into Moby::Adaptor::moby::queryapi::mysql.pm
Note: Still may contain many syntactic errors

moby-live/Perl/MOBY OntologyServer.pm,1.50,1.51
===================================================================
RCS file: /home/repository/moby/moby-live/Perl/MOBY/OntologyServer.pm,v
retrieving revision 1.50
retrieving revision 1.51
diff -u -r1.50 -r1.51
--- /home/repository/moby/moby-live/Perl/MOBY/OntologyServer.pm	2005/07/13 21:53:12	1.50
+++ /home/repository/moby/moby-live/Perl/MOBY/OntologyServer.pm	2005/07/15 16:40:45	1.51
@@ -183,6 +183,10 @@
 
 sub objectExists {
 	my ( $self, %args ) = @_;
+
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	my $term = $args{term};
 	$term =~ s/^moby://;    # if the term is namespaced, then remove that
 	my $sth;
@@ -243,7 +247,8 @@
 
 sub createObject {
 	my ( $self, %args ) = @_;
-
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	#node => $term,
 	#desc => $desc,
 	#authURI => $auth,
@@ -304,6 +309,8 @@
 
 sub retrieveObject {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	my $term = $args{'node'};
 	return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' );
 	return ( 0, "requires a object type node as an argument", '' )
@@ -351,6 +358,9 @@
 
 sub deprecateObject {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY", '' ) unless ( $self->ontology eq 'object' );
 	my $term = $args{term};
 
@@ -361,31 +371,29 @@
 	unless ( $term =~ /urn\:lsid/ ) { $LSID = $self->getObjectURI($term) } else { $LSID = $term }
 	return ( 0, q{Object type $term cannot be resolved to an LSID}, "" )
 	  unless $LSID;
-	my ( $id, $lsid ) =
-	  $self->dbh->selectrow_array(
-			 q{select object_id, object_lsid from object where object_lsid = ?},
-			 undef, $LSID );
+	
+	my $result = $adaptor->query_object({object_lsid => $LSID});
+	my $row = shift(@$result);
+	my $id = $row->{object_id};
+	my $lsid = $row->{object_lsid};
 
 	# object1_id ISA object2_id?
-	my (@isa) =
-	  $self->dbh->selectrow_array(
-						 q{select * from object_term2term where object2_id = ?},
-						 undef, $id );
-	if ( scalar @isa ) {
+	my $isa = $adaptor->query_object_term2term({object2_id => $id});
+	if ( scalar @$isa ) {
 		return ( 0,
 				 qq{Object type $term has object dependencies in the ontology},
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from object where object_id = ?}, undef, $id );
-	if ( $self->dbh->err ) {
-		return ( 0, "Delete from Object Class table failed: $self->dbh->errstr",
+
+	my ($err, $errstr) = $adaptor->delete_object({object_id => $id});
+	if ( $err ) {
+		return ( 0, "Delete from Object Class table failed: $errstr",
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from object_term2term where object1_id = ?},
-					undef, $id );
-	if ( $self->dbh->err ) {
+	($err, $errstr) = $adaptor->delete_object_term2term({object1_id => $id});
+	if ( $err ) {
 		return ( 0,
-				"Delete from Object term2term table failed: $self->dbh->errstr",
+				"Delete from Object term2term table failed: $errstr",
 				$lsid );
 	}
 	return ( 1, "Object $term Deleted", $lsid );
@@ -409,6 +417,9 @@
 	# term => $term
 	# ontology => $ontology
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' )
 	  unless ( $self->ontology eq 'relationship' );
 	my $term = lc( $args{term} );
@@ -416,21 +427,22 @@
 	my $ont = $args{ontology};
 	return ( 0, "requires both term and ontology arguments\n", '' )
 	  unless ( defined($term) && defined($ont) );
-	my $sth;
-
+	my $result;
 	if ( $term =~ /^urn\:lsid/ ) {
-		$sth =
-		  $self->dbh->prepare(
-"select relationship_lsid, relationship_type, description, authority, contact_email from relationship where relationship_lsid = ? and ontology=?"
-		  );
+
+	$result = $adaptor->query_relationship({relationship_lsid => $term}, 'and', {ontology => $ont});	
+	
 	} else {
-		$sth =
-		  $self->dbh->prepare(
-"select relationship_lsid, relationship_type, description, authority, contact_email from relationship where relationship_type = ? and ontology=?"
-		  );
+	
+	$result = $adaptor->query_relationship({relationship_type => $term}, 'and', {ontology => $ont});
+	
 	}
-	$sth->execute( $term, $ont );
-	my ( $lsid, $type, $desc, $auth, $email ) = $sth->fetchrow_array;
+	my $row = shift(@$result);
+	my $lsid = $row->{relationship_lsid};
+	my $type = $row->{relationship_type};
+	my $desc = $row->{description};
+	my $auth = $row->{authority};
+	my $email = $row->{contact_email};
 	if ($lsid) {
 		return ( 1, $desc, $lsid );
 	} else {
@@ -456,43 +468,49 @@
 	#authority => $auth,
 	#contact_email => $email
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' );
 	my ( $subj_id, $subj_lsid, $obj_id, $obj_lsid );
 	if ( $args{subject_node} =~ /^urn:lsid/ ) {
-		( $subj_id, $subj_lsid ) = $self->dbh->selectrow_array(
-			 q{select object_id, object_lsid from object where object_lsid = ?},
-			 undef, $args{subject_node}
-		);
+
+		my $result = $adaptor->query_object({object_lsid => $args{subject_node}});
+		my $row = shift(@$result);
+	        $subj_id = $row->{object_id};
+	        $subj_lsid = $row->{object_lsid};
+
 	} else {
-		( $subj_id, $subj_lsid ) = $self->dbh->selectrow_array(
-			 q{select object_id, object_lsid from object where object_type = ?},
-			 undef, $args{subject_node}
-		);
+		my $result = $adaptor->query_object({object_type => $args{subject_node}});
+		my $row = shift(@$result);
+		$subj_id = $row->{object_id};
+		$subj_lsid = $row->{object_lsid};
+
 	}
 	return ( 0,
 			 qq{Object type $args{subject_node} does not exist in the ontology},
 			 '' )
 	  unless defined $subj_id;
 	if ( $args{object_node} =~ /^urn:lsid/ ) {
-		( $obj_id, $obj_lsid ) = $self->dbh->selectrow_array(
-			 q{select object_id, object_lsid from object where object_lsid = ?},
-			 undef, $args{object_node}
-		);
+
+		my $result = $adaptor->query_object({object_lsid => $args{object_node}});
+		my $row = shift(@$result);
+	        $obj_id = $row->{object_id};
+		$obj_lsid = $row->{object_lsid};
+		
 	} else {
-		( $obj_id, $obj_lsid ) = $self->dbh->selectrow_array(
-			 q{select object_id, object_lsid from object where object_type = ?},
-			 undef, $args{object_node}
-		);
+		my $result = $adaptor->query_object({object_type => $args{object_node}});
+		my $row = shift(@$result);
+		$obj_id = $row->{object_id};
+		$obj_lsid = $row->{object_lsid};
 	}
 	return ( 0,
 			 qq{Object type $args{object_node} does not exist in the ontology},
 			 '' )
 	  unless defined $obj_id;
-	my (@isa) =
-	  $self->dbh->selectrow_array(
-						 q{select * from object_term2term where object2_id = ?},
-						 undef, $subj_id );
-	if ( scalar @isa ) {
+	my $isa = $adaptor->query_object_term2term({object2_id => $subj_id});
+
+	if ( scalar @$isa ) {
 		return (
 			0,
 qq{Object type $args{subject_node} has existing object dependencies in the ontology.  It cannot be changed.},
@@ -507,12 +525,11 @@
 	  || return ( 0,
 			qq{Relationship $args{relationship} does not exist in the ontology},
 			'' );
-	$self->dbh->do(
-q{insert into object_term2term (relationship_type, object1_id, object2_id, object2_articlename) values (?,?,?,?)},
-		undef,
-		( $rel_lsid, $subj_id, $obj_id, $args{articleName} )
-	);
-	if ( $self->dbh->{mysql_insertid} ) {
+	my $insertid = $adaptor->insert_object_term2term({relationship_type => $rel_lsid}, 
+							 {object1_id => $subj_id},
+							 {object2_id => $obj_id},
+							 {object2_articlename => $args{articleName}});
+	if ($insertid ) {
 		return ( 1, "Object relationsihp created successfully", '' );
 	} else {
 		return ( 0, "Object relationship creation failed for unknown reasons",
@@ -534,32 +551,37 @@
 	#authority => $auth,
 	#contact_email => $email);
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' );
-	my ( $sbj_id, $sbj_lsid ) = $self->dbh->selectrow_array(
-		 q{select service_id, service_lsid from service where service_type = ?},
-		 undef, $args{subject_node}
-	);
+
+	my $result = $adaptor->query_service({service_type => $args{subject_node}});
+	my $row = shift(@$result);
+	my $sbj_id = $row->{service_id};
+	my $sbj_lsid = $row->{service_lsid};
+					    
 	return (
 		0,
 qq{Service type $args{subject_node} has object dependencies in the ontology.  It can not be changed},
 		$sbj_lsid
 	  )
 	  unless defined $sbj_id;
-	my (@isa) =
-	  $self->dbh->selectrow_array(
-					   q{select * from service_term2term where service2_id = ?},
-					   undef, $sbj_id );
-	if ( scalar @isa ) {
+
+	my $isa = $adaptor->query_service_term2term({service2_id => $sbj_id});
+	if ( scalar @$isa ) {
 		return (
 			0,
 qq{Service type $args{subject_node} has object dependencies in the ontology.  It can not be changed},
 			$sbj_lsid
 		);
 	}
-	my ( $obj_id, $obj_lsid ) = $self->dbh->selectrow_array(
-		 q{select service_id, service_lsid from service where service_type = ?},
-		 undef, $args{object_node}
-	);    # get ID of the related service
+	$result = $adaptor->query_service({service_type => $args{object_node}});
+	$row = shift(@$result);
+	my $obj_id = $row->{service_id};
+	my $obj_lsid = $row->{service_lsid};
+	# get ID of the related service
+	
 	defined $obj_id
 	  || return ( 0,
 		  qq{Service $args{object_node} does not exist in the service ontology},
@@ -572,12 +594,11 @@
 	  || return ( 0,
 			qq{Relationship $args{relationship} does not exist in the ontology},
 			'' );
-	$self->dbh->do(
-q{insert into service_term2term (relationship_type, service1_id, service2_id) values (?,?,?)},
-		undef,
-		( $rel_lsid, $sbj_id, $obj_id )
-	);
-	if ( $self->dbh->{mysql_insertid} ) {
+
+	my $insertid = $adaptor->insert_service_term2term({relationship_type => $rel_lsid}, 
+							  {service1_id => $sbj_id},
+							  {service2_id => $obj_id});
+	if ( $insertid ) {
 		return ( 1, "Service relationship created successfully", '' );
 	} else {
 		return ( 0, "Service relationship creation failed for unknown reasons",
@@ -591,6 +612,9 @@
 
 sub serviceExists {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' );
 	my $term = $args{term};
 	$term =~ s/^moby://;    # if the term is namespaced, then remove that
@@ -599,20 +623,22 @@
 	{
 		return ( 1, "external ontology", $term );
 	}
-	my $sth;
+	my $result;
 	if ( $term =~ /^urn\:lsid/ ) {
-		$sth =
-		  $self->dbh->prepare(
-"select service_id, service_type, service_lsid, description, authority, contact_email from service where service_lsid =  ?"
-		  );
+	$result = $adaptor->query_service({service_lsid => $term});
+
 	} else {
-		$sth =
-		  $self->dbh->prepare(
-"select service_id, service_type, service_lsid, description, authority, contact_email from service where service_type = ?"
-		  );
+	$result = $adaptor->query_service({service_type => $term});
+
 	}
-	$sth->execute($term);
-	my ( $id, $type, $lsid, $desc, $auth, $email ) = $sth->fetchrow_array;
+	my $row = shift(@$result);
+	my $id = $row->{service_id};
+	my $type = $row->{service_type};
+	my $lsid = $row->{service_lsid};
+	my $desc = $row->{description};
+	my $auth = $row->{authority};
+	my $email = $row->{contact_email};
+
 	if ($id) {
 		return ( 1, $desc, $lsid );
 	} else {
@@ -630,6 +656,8 @@
 
 sub createServiceType {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 
 	#node => $term,
 	#descrioption => $desc,
@@ -653,15 +681,14 @@
 	  ? $args{'node'}
 	  : $self->setURI( $args{'node'} );
 	unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) }
-	$self->dbh->do(
-q{insert into service (service_type, service_lsid, description, authority,contact_email) values (?,?,?,?,?)},
-		undef,
-		(
-		   $args{'node'},      $LSID, $args{'description'},
-		   $args{'authority'}, $args{'contact_email'}
-		)
-	);
-	unless ( $self->dbh->{mysql_insertid} ) {
+
+	my $insertid = $adaptor->insert_service({service_type => $args{'node'}},
+						{service_lsid => $LSID},
+						{description => $args{'description'},
+						{authority => $args{'authority'},
+						{contact_email => $args{'contact_email'});
+
+	unless ( $insertid ) {
 		return ( 0, "Service creation failed for unknown reasons", '' );
 	}
 	return ( 1, "Service creation succeeded", $LSID );
@@ -673,6 +700,9 @@
 
 sub deleteServiceType {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' );
 	my $term = $args{term};
 	if ( $term =~ /^urn:lsid/
@@ -692,35 +722,37 @@
 		""
 	  )
 	  unless $LSID;
-	my ( $id, $lsid ) = $self->dbh->selectrow_array(
-		 q{select service_id, service_lsid from service where service_lsid = ?},
-		 undef, $LSID
-	);
+
+	my $result = $adaptor->query_service({service_lsid => $LSID});
+	my $row = shift(@$result);
+	my $id = $row->{service_id};
+	my $lsid = $row->{service_lsid};
+
 	if ( !defined $id ) {
 		return ( 0, q{Service type $term does not exist in the ontology},
 				 $lsid );
 	}
 
 	# service1_id ISA service2_id?
-	my (@isa) =
-	  $self->dbh->selectrow_array(
-					   q{select * from service_term2term where service2_id = ?},
-					   undef, $id );
-	if ( scalar @isa ) {
+	my $isa = $adaptor->query_service_term2term({service2_id => $id});
+
+	if ( scalar @$isa ) {
 		return ( 0, qq{Service type $term has dependencies in the ontology},
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from service where service_id = ?}, undef, $id );
-	if ( $self->dbh->err ) {
-		return ( 0, "Delete from Service Type table failed: $self->dbh->errstr",
+	my ($err, $errstr) = $adaptor->delete_service({service_id => $id});
+
+	if ( $err ) {
+		return ( 0, "Delete from Service Type table failed: $errstr",
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from service_term2term where service1_id = ?},
-					undef, $id );
-	if ( $self->dbh->err ) {
+
+	($err, $errstr) = $adaptor->delete_service_term2term({service1_id => $id});
+
+	if ( $err ) {
 		return (
 			0,
-"Delete from Service Type Term2Term table failed: $self->dbh->errstr",
+"Delete from Service Type Term2Term table failed: $errstr",
 			$lsid
 		);
 	}
@@ -733,29 +765,34 @@
 
 sub namespaceExists {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+
 	return ( 0, "WRONG ONTOLOGY!", '' )
 	  unless ( $self->ontology eq 'namespace' );
 	my $term = $args{term};
 	$term =~ s/^moby://;    # if the term is namespaced, then remove that
-	my $sth;
 	if ( $term =~ /^urn:lsid/
 		 && !( $term =~ /^urn:lsid:biomoby.org:namespacetype/ ) )
 	{
 		return ( 1, "external ontology", $term );
 	}
+	my $result;
 	if ( $term =~ /^urn:lsid:biomoby.org:namespacetype/ ) {
-		$sth =
-		  $self->dbh->prepare(
-"select namespace_id, namespace_type, namespace_lsid,description, authority, contact_email from namespace where namespace_lsid = ?"
-		  );
+	$result = $adaptor->query_namespace({namespace_lsid => $term});
+
 	} else {
-		$sth =
-		  $self->dbh->prepare(
-"select namespace_id, namespace_type, namespace_lsid,description, authority, contact_email from namespace where namespace_type = ?"
-		  );
+	$result = $adaptor->query_namespace({namespace_type => $term});
+
 	}
-	$sth->execute($term);
-	my ( $id, $type, $lsid, $desc, $auth, $email ) = $sth->fetchrow_array;
+	my $row = shift(@$result);
+	my $id = $row->{namespace_id};
+	my $type = $row->{namespace_type};
+	my $lsid = $row->{namespace_lsid};
+	my $desc = $row->{description};
+	my $auth = $row->{authority};
+	my $email = $row->{contact_email};
+
 	if ($id) {
 		return ( 1, $desc, $lsid );
 	} else {
@@ -773,7 +810,8 @@
 
 sub createNamespace {
 	my ( $self, %args ) = @_;
-
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	#node => $term,
 	#descrioption => $desc,
 	#authority => $auth,
@@ -797,15 +835,14 @@
 	  ? $args{'node'}
 	  : $self->setURI( $args{'node'} );
 	unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) }
-	$self->dbh->do(
-q{insert into namespace (namespace_type, namespace_lsid,description, authority,contact_email) values (?,?,?,?,?)},
-		undef,
-		(
-		   $args{'node'},      $LSID, $args{'description'},
-		   $args{'authority'}, $args{'contact_email'}
-		)
-	);
-	unless ( $self->dbh->{mysql_insertid} ) {
+
+	my $insertid = $adaptor->insert_namespace({namespace_type => $args{'node'}}, 
+						{namespace_lsid => $LSID},
+						{description => $args{'description'}},
+						{authority => $args{'authority'}},
+						{contact_email => $args{'contact_email'}});
+
+	unless ( $insertid ) {
 		return ( 0, "Namespace creation failed for unknown reasons", '' );
 	}
 	return ( 1, "Namespace creation succeeded", $LSID );
@@ -817,6 +854,8 @@
 
 sub deleteNamespace {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return ( 0, "WRONG ONTOLOGY!", '' )
 	  unless ( $self->ontology eq 'namespace' );
 	my $term = $args{term};
@@ -829,36 +868,38 @@
 	{
 		return ( 0, "cannot delete a term from an external ontology", $term );
 	}
-	my ( $id, $lsid ) = $self->dbh->selectrow_array(
-q{select namespace_id, namespace_lsid from namespace where namespace_lsid = ?},
-		undef, $LSID
-	);
+
+	my $result = $adaptor->query_namespace({namespace_lsid => $LSID});
+	my $row = shift(@$result);
+	my $id = $row->{namespace_id};
+	my $lsid = $row->{namespace_lsid};
+
 	unless ($id) {
 		return ( 0, q{Namespace type $term does not exist in the ontology},
 				 $lsid );
 	}
 
 	# service1_id ISA service2_id?
-	my (@isa) =
-	  $self->dbh->selectrow_array(
-				   q{select * from namespace_term2term where namespace2_id = ?},
-				   undef, $id );
-	if ( scalar @isa ) {
+	my $isa = $adaptor->query_namespace_term2term({namespace2_id => $id});
+
+	if ( scalar @$isa ) {
 		return ( 0, qq{Namespace type $term has dependencies in the ontology},
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from namespace where namespace_id = ?},
-					undef, $id );
-	if ( $self->dbh->err ) {
-		return ( 0, "Delete from namespace table failed: $self->dbh->errstr",
+
+	my ($err, $errstr) = $adaptor->delete_namespace({namespace_id => $id});
+
+	if ( $err ) {
+		return ( 0, "Delete from namespace table failed: $errstr",
 				 $lsid );
 	}
-	$self->dbh->do( q{delete from namespace_term2term where namespace1_id = ?},
-					undef, $id );
-	if ( $self->dbh->err ) {
+
+	($err, $errstr) = $adaptor->delete_namespace_term2term({namespace1_id => $id});
+
+	if ( $err ) {
 		return (
 			 0,
-			 "Delete from namespace term2term table failed: $self->dbh->errstr",
+			 "Delete from namespace term2term table failed: $errstr",
 			 $lsid
 		);
 	}
@@ -871,13 +912,13 @@
 
 sub retrieveAllServiceTypes {
 	my ($self) = @_;
-	my $types =
-	  $self->dbh->selectall_arrayref(
-							  q{select service_type, description from service});
-	my @types = @{$types};
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	my $types = $adaptor->query_service();
+
 	my %response;
-	foreach (@types) {
-		$response{ $_->[0] } = $_->[1];
+	foreach (@$types) {
+		$response{ $_->{service_type} } = $_->{description};
 	}
 	return \%response;
 }
@@ -888,13 +929,13 @@
 
 sub retrieveAllNamespaceTypes {
 	my ($self) = @_;
-	my $types =
-	  $self->dbh->selectall_arrayref(
-						  q{select namespace_type, description from namespace});
-	my @types = @{$types};
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	my $types = $adaptor->query_namespace();
+
 	my %response;
-	foreach (@types) {
-		$response{ $_->[0] } = $_->[1];
+	foreach (@$types) {
+		$response{ $_->{namespace_type} } = $_->{description};
 	}
 	return \%response;
 }
@@ -905,13 +946,13 @@
 
 sub retrieveAllObjectClasses {
 	my ($self) = @_;
-	my $types =
-	  $self->dbh->selectall_arrayref(
-								q{select object_type, description from object});
-	my @types = @{$types};
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	my $types = $adaptor->query_object();
+
 	my %response;
-	foreach (@types) {
-		$response{ $_->[0] } = $_->[1];
+	foreach (@$types) {
+		$response{ $_->{object_type} } = $_->{description};
 	}
 	return \%response;
 }
@@ -924,11 +965,13 @@
 
 sub getObjectCommonName {
 	my ( $self, $URI ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return undef unless $URI =~ /urn\:lsid/;
-	my ($name) =
-	  $self->dbh->selectrow_array(
-						q{select object_type from object where object_lsid = ?},
-						undef, $URI );
+	my $result = $adaptor->query_object({object_lsid => $URI});
+	my $row = shift(@$result);
+	my $name = $row->{object_type};
+
 	return $name ? $name : $URI;
 }
 
@@ -938,11 +981,13 @@
 
 sub getNamespaceCommonName {
 	my ( $self, $URI ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return undef unless $URI =~ /urn\:lsid/;
-	my ($name) =
-	  $self->dbh->selectrow_array(
-			   q{select namespace_type from namespace where namespace_lsid = ?},
-			   undef, $URI );
+	my $result = $adaptor->query_namespace({namespace_lsid => $URI});
+	my $row = shift(@$result);
+	my $name = $row->{namespace_type};
+	
 	return $name ? $name : $URI;
 }
 
@@ -952,11 +997,13 @@
 
 sub getServiceCommonName {
 	my ( $self, $URI ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return undef unless $URI =~ /urn\:lsid/;
-	my ($name) =
-	  $self->dbh->selectrow_array(
-					 q{select service_type from service where service_lsid = ?},
-					 undef, $URI );
+	my $result = $adaptor->query_service({service_lsid => $URI});
+	my $row = shift(@$result);
+	my $name = $row->{service_type};
+
 	return $name ? $name : $URI;
 }
 
@@ -966,11 +1013,14 @@
 
 sub getServiceURI {
 	my ( $self, $term ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return $term if $term =~ /urn\:lsid/;
-	my ($id) =
-	  $self->dbh->selectrow_array(
-					 q{select service_lsid from service where service_type = ?},
-					 undef, $term );
+	
+	my $result = $adaptor->query_service({service_type => $term});
+	my $row = shift(@$result);
+	my $id = $row->{service_lsid};
+
 	return $id;
 }
 
@@ -980,11 +1030,14 @@
 
 sub getObjectURI {
 	my ( $self, $term ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	return $term if $term =~ /urn\:lsid/;
-	my ($id) =
-	  $self->dbh->selectrow_array(
-						q{select object_lsid from object where object_type = ?},
-						undef, $term );
+
+	my $result = $adaptor->query_object({object_type => $term});
+	my $row = shift(@$result);
+	my $id = $row->{object_lsid};
+
 	return $id;
 }
 
@@ -994,11 +1047,15 @@
 
 sub getNamespaceURI {
 	my ( $self, $term ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	
 	return $term if $term =~ /urn\:lsid/;
-	my ($id) =
-	  $self->dbh->selectrow_array(
-			   q{select namespace_lsid from namespace where namespace_type = ?},
-			   undef, $term );
+
+	my $result = $adaptor->query_namespace({namespace_type => $term});
+	my $row = shift(@$result);
+	my $id = $row->{namespace_lsid};
+
 	return $id;
 }
 
@@ -1008,11 +1065,15 @@
 
 sub getRelationshipURI {
 	my ( $self, $ontology, $term ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	
 	return $term if $term =~ /urn\:lsid/;
-	my ($id) = $self->dbh->selectrow_array(
-q{select relationship_lsid from relationship where relationship_type = ? and ontology = ?},
-		undef, $term, $ontology
-	);
+
+	my $result = $adaptor->query_relationship({relationship_type => $term}, 'and' {ontology => $ontology});
+	my $row = shift(@$result);
+	my $id = $row->{relationship_lsid};
+
 	return $id;
 }
 
@@ -1022,15 +1083,17 @@
 
 sub getRelationshipTypes {
 	my ( $self, %args ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
+	
 	my $ontology = $args{'ontology'};
 	my $OS = MOBY::OntologyServer->new( ontology => "relationship" );
-	my $defs = $OS->dbh->selectall_arrayref(
-q{select relationship_lsid, relationship_type, authority, description from relationship where ontology = ?},
-		undef, $ontology
-	);
+
+	my $defs = $adaptor->query_relationship({ontology => $ontology});
+
 	my %result;
-	foreach ( @{$defs} ) {
-		$result{ $_->[0] } = [ $_->[1], $_->[2], $_->[3] ];
+	foreach ( @$defs ) {
+		$result{ $_->{relationship_lsid} } = [ $_->{relationship_type}, $_->{authority}, $_->{description} ];
 	}
 	return \%result;
 }
@@ -1086,54 +1149,12 @@
 
 sub _doRelationshipsQuery {
 	my ( $self, $ontology, $term, $relationship, $direction ) = @_;
+	$CONFIG ||= MOBY::Config->new;    # exported by Config.pm
+	my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
 	my $defs;
-	if ( $direction eq 'root' ) {
-		unless ( defined $relationship ) {
-			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid, relationship_type from
-                ${ontology}_term2term as t2t,
-                $ontology as s1,
-                $ontology as s2  
-            where
-                s1.${ontology}_id = t2t.${ontology}1_id and
-                s2.${ontology}_id = t2t.${ontology}2_id and
-                s1.${ontology}_lsid = ?", undef, $term );    # ")
-		} else {
-			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid, relationship_type from
-                ${ontology}_term2term as t2t,
-                $ontology as s1,
-                $ontology as s2  
-            where
-                relationship_type = ? and 
-                s1.${ontology}_id = t2t.${ontology}1_id and
-                s2.${ontology}_id = t2t.${ontology}2_id and
-                s1.${ontology}_lsid = ?", undef, $relationship, $term );    # ")
-		}
-	} else {
-		unless ( defined $relationship ) {
-			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid, relationship_type from
-                ${ontology}_term2term as t2t,
-                $ontology as s1,
-                $ontology as s2  
-            where
-                s1.${ontology}_id = t2t.${ontology}1_id and
-                s2.${ontology}_id = t2t.${ontology}2_id and
-                s2.${ontology}_lsid = ?", undef, $term );                   # ")
-		} else {
-			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid, relationship_type from
-                ${ontology}_term2term as t2t,
-                $ontology as s1,
-                $ontology as s2  
-            where
-                relationship_type = ? and 
-                s1.${ontology}_id = t2t.${ontology}1_id and
-                s2.${ontology}_id = t2t.${ontology}2_id and
-                s2.${ontology}_lsid = ?", undef, $relationship, $term );    # ")
-		}
-	}
+	# query returns a reference to an array containing array references
+	$defs = $adaptor->getRelationship({direction => $direction}, {ontology => $ontology}, {term => $term}, {relationship => $relationship});
+	# a very long piece of SQL statements have been refactored into Moby::Adaptor::moby::queryapi::mysql.pm
 	return $defs;
 }
 




More information about the MOBY-guts mailing list