From 69bacbf38c9dedfab9612e7fe85174e488a77a57 Mon Sep 17 00:00:00 2001
From: Graham McVicker <mcvicker@sanger.ac.uk>
Date: Thu, 17 Apr 2003 09:34:46 +0000
Subject: [PATCH] DBEntryAdaptor::store changed so can no longer add
 external_db entries and refactored a bit

---
 modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm | 253 ++++++++------------
 1 file changed, 95 insertions(+), 158 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm
index 3392340eb1..5228ba073c 100644
--- a/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm
@@ -19,10 +19,7 @@ $dbEntry = $db_entry_adaptor->fetch_by_dbID($id);
 
 =head1 CONTACT
 
-  Arne Stabenau: stabenau@ebi.ac.uk
-  Ewan Birney  : birney@ebi.ac.uk
-
-=head1 APPENDIX
+Post questions to the EnsEMBL developer list <ensembl-dev@ebi.ac.uk>
 
 =cut
 
@@ -110,182 +107,122 @@ sub fetch_by_dbID {
 
 
 sub store {
-    my ( $self, $exObj, $ensObject, $ensType ) = @_;
-    my $dbJustInserted;
+  my ( $self, $exObj, $ensObject, $ensType ) = @_;
+  my $dbJustInserted;
 
-    # check if db exists
-    # urlPattern dbname release
-    my $sth = $self->prepare( "
+  #
+  # Check for the existance of the external_db, throw if it does not exist
+  #
+  my $sth = $self->prepare( "
      SELECT external_db_id
        FROM external_db
       WHERE db_name = ?
-        AND release = ?
-    " );
-    $sth->execute( $exObj->dbname(), $exObj->release() );
-    
-    my $dbRef;
-    
-    if(  ($dbRef) =  $sth->fetchrow_array() ) {
-        $dbJustInserted = 0;
-    } else {
-      # store it, get dbID for that
-      $sth = $self->prepare( "
-       INSERT ignore INTO external_db 
-       SET db_name = ?,
-           release = ?,
-           status  = ?
-     " );
-	
-      $sth->execute( $exObj->dbname(), $exObj->release(), $exObj->status);
-      
-      $dbJustInserted = 1;
-      $sth = $self->prepare( "SELECT LAST_INSERT_ID()" );
-      $sth->execute();
-      ( $dbRef ) = $sth->fetchrow_array();
-      if( ! defined $dbRef ) {
-	$self->throw( "Database entry failed." );
-      }
-    }
+        AND release = ?");
+
+  $sth->execute( $exObj->dbname(), $exObj->release() );
     
-    my $dbX;
+  my ($dbRef) =  $sth->fetchrow_array();
+
+  if(!$dbRef) {
+    $self->throw("external_db [" . $exObj->db_name . "] release [" .
+		 $exObj->release . "] does not exist");
+  }
     
-    if(  $dbJustInserted ) {
-      # dont have to check for existence; cannnot have been inserted at
-      # this point, so $dbX is certainly undefined
-      $dbX = undef;
-    } else {
-	$sth = $self->prepare( "
+  #
+  # Check for the existance of the external reference, add it if not present
+  #
+  $sth = $self->prepare( "
        SELECT xref_id
          FROM xref
         WHERE external_db_id = ?
           AND dbprimary_acc = ?
-          AND version = ?
-     " );
-	$sth->execute( $dbRef, $exObj->primary_id(), 
-		       $exObj->version() );
-	( $dbX ) = $sth->fetchrow_array();
-    }
+          AND version = ?" );
+
+  $sth->execute( $dbRef, $exObj->primary_id(),$exObj->version() );
+  my ($dbX) = $sth->fetchrow_array();
+  $sth->finish();
     
-    if( ! defined $dbX ) {
-	
-	$sth = $self->prepare( "
-      INSERT ignore INTO xref 
+  if(!$dbX) {
+      #
+      # store the new xref
+      #
+    $sth = $self->prepare( "
+       INSERT ignore INTO xref 
        SET dbprimary_acc = ?,
            display_label = ?,
            version = ?,
            description = ?,
-           external_db_id = ?
-     " );
-	$sth->execute( $exObj->primary_id(), $exObj->display_id(), $exObj->version(),
-		       $exObj->description(), $dbRef);
-	
-	$sth = $self->prepare( "
-      SELECT LAST_INSERT_ID()
-    " );
-	$sth->execute();
-	( $dbX ) = $sth->fetchrow_array();
+           external_db_id = ?" );
+    $sth->execute( $exObj->primary_id(), $exObj->display_id(), 
+		   $exObj->version(), $exObj->description(), $dbRef);
+    $dbX = $sth->{'mysql_insertid'};
+    $sth->finish();
 	
-	# synonyms
-	my $synonyms = $exObj->get_all_synonyms();
-	foreach my $syn ( @{$synonyms} ) {
-	    
-#Check if this synonym is already in the database for the given primary id
-	    my $sth = $self->prepare( "
-     SELECT xref_id,
-            synonym
-       FROM external_synonym
-      WHERE xref_id = ?
-        AND synonym = ?
-        " );
-	    $sth->execute($dbX, $syn);
-	    
-	    my ($dbSyn) = $sth->fetchrow_array();
-	    
-	    #print STDERR $dbSyn[0],"\n";
-	    
-	    if( ! $dbSyn ) {
-		$sth = $self->prepare( "
-        INSERT ignore INTO external_synonym
-         SET xref_id = ?,
-            synonym = ?
-        " );
-		$sth->execute($dbX, $syn);
-	    }
-	}
-		
-	$sth = $self->prepare( "
-   INSERT ignore INTO object_xref
-     SET xref_id = ?,
-         ensembl_object_type = ?,
-         ensembl_id = ?
-      " );
-	
-	$sth->execute( $dbX, $ensType, $ensObject );	
-	$exObj->dbID( $dbX );
-	$exObj->adaptor( $self );
-	
-	if ($exObj->isa('Bio::EnsEMBL::IdentityXref')) {
-	    $sth = $self->prepare( "
-      SELECT LAST_INSERT_ID()
-      " );
-	    $sth->execute();
-	    my ( $Xidt ) = $sth->fetchrow_array();
-	    
-	    $sth = $self->prepare( "
-             INSERT ignore INTO identity_xref
-             SET object_xref_id = ?,
-             query_identity = ?,
-             target_identity = ? 
-			 " );
-	    $sth->execute( $Xidt, $exObj->query_identity, $exObj->target_identity );
-	    
-	}
-    } else {
-	$sth = $self->prepare ( "
-              SELECT xref_id
-              FROM object_xref
-              WHERE xref_id = ?
-              AND   ensembl_object_type = ?
-              AND   ensembl_id = ?
-			  ");
+    #
+    # store the synonyms for the new xref
+    # 
+    my $synonym_check_sth = $self->prepare(
+              "SELECT xref_id, synonym
+               FROM external_synonym
+               WHERE xref_id = ?
+               AND synonym = ?");
+
+    my $synonym_store_sth = $self->prepare(
+        "INSERT ignore INTO external_synonym
+         SET xref_id = ?, synonym = ?");     
+
+    my $synonyms = $exObj->get_all_synonyms();
+    foreach my $syn ( @$synonyms ) {	    
+      $synonym_check_sth->execute($dbX, $syn);
+      my ($dbSyn) = $sth->fetchrow_array();
+      $synonym_store_sth->execute($dbX, $syn) if(!$dbSyn);
+    }
 	
-	$sth->execute($dbX, $ensType, $ensObject);
-	my ($tst) = $sth->fetchrow_array;
-
-	if (! defined $tst) {
-	# line is already in xref table. Need to add to object_xref
-	    $sth = $self->prepare( "
-             INSERT ignore INTO object_xref
-               SET xref_id = ?,
-               ensembl_object_type = ?,
-               ensembl_id = ?
-			   ");
+    $synonym_check_sth->finish();
+    $synonym_store_sth->finish();
+  }
+
+  #
+  # check if the object mapping was already stored
+  #
+  $sth = $self->prepare (
+           "SELECT xref_id
+            FROM object_xref
+            WHERE xref_id = ?
+            AND   ensembl_object_type = ?
+            AND   ensembl_id = ?");
+
+  $sth->execute($dbX, $ensType, $ensObject);
+  my ($tst) = $sth->fetchrow_array;
+  $sth->finish();
+    
+  if(!$tst) {
+    #
+    # Store the reference to the internal ensembl object
+    #
+    $sth = $self->prepare(
+         "INSERT ignore INTO object_xref
+          SET xref_id = ?, ensembl_object_type = ?, ensembl_id = ?");
 	
-	    $sth->execute( $dbX, $ensType, $ensObject );	
-	    $exObj->dbID( $dbX );
-	    $exObj->adaptor( $self );
-
-	    if ($exObj->isa('Bio::EnsEMBL::IdentityXref')) {
-		$sth = $self->prepare( "
-      SELECT LAST_INSERT_ID()
-      " );
-		
-		$sth->execute();
-		my ( $Xidt ) = $sth->fetchrow_array();
-		
-		$sth = $self->prepare( "
+    $sth->execute( $dbX, $ensType, $ensObject );	
+    $exObj->dbID( $dbX );
+    $exObj->adaptor( $self );
+      
+    my $Xidt = $sth->{'mysql_insertid'};
+
+    #
+    # If this is an IdentityXref need to store in that table too
+    #
+    if ($exObj->isa('Bio::EnsEMBL::IdentityXref')) {
+      $sth = $self->prepare( "
              INSERT ignore INTO identity_xref
              SET object_xref_id = ?,
              query_identity = ?,
-             target_identity = ?
-        " );
-		$sth->execute( $Xidt, $exObj->query_identity, $exObj->target_identity );
-		
-	    }
-	}
+             target_identity = ?" );
+      $sth->execute($Xidt, $exObj->query_identity, $exObj->target_identity);
     }
-        
-    return $dbX;    
+  } 
+  return $dbX;    
 }
 
 
-- 
GitLab