Browse Source

When syncing features repetitive error logs were entered into the Drupal report log for every synced feature stating that a duplicate feature could not be inserted. This was fixed

spficklin 12 years ago
parent
commit
6cfe5c467e
2 changed files with 48 additions and 17 deletions
  1. 28 6
      tripal_core/api/tripal_core.api.inc
  2. 20 11
      tripal_feature/tripal_feature.module

+ 28 - 6
tripal_core/api/tripal_core.api.inc

@@ -867,6 +867,10 @@ function tripal_core_chado_delete($table, $match) {
  * @return
  *  A database query result resource, FALSE if the query was not executed
  *  correctly, or the number of records in the dataset if $has_record is set.
+ *  If the option 'is_duplicate' is provided and the record is a duplicate it
+ *  will return the duplicated record.  If the 'has_record' option is provided
+ *  a value of TRUE will be returned if a record exists and FALSE will bee
+ *  returned if there are not records.
  *
  * Example usage:
  * @code
@@ -943,12 +947,18 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
     // will not recreate it, but if not it will create one and store it in
     // a Drupal variable for reuse later.
     $connection = tripal_db_persistent_chado();
+    
+    // if we cannot get a connection the abandon the prepared statement
+    if(!$connection){
+       $prepared = FALSE;
+       unset($options['statement_name']);
+    }
   }
   else {
     //print "NO STATEMENT (select): $table\n";
     //debug_print_backtrace();
   }
-
+  
   // check that our columns and values arguments are proper arrays
   if (!is_array($columns)) {
     watchdog('tripal_core', 'the $columns argument for tripal_core_chado_select must be an array.');
@@ -994,6 +1004,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
 	      }
 	    }
 	    
+	    // recreate the $values and $columns arrays 
       foreach ($fields as $field) {
         if (array_key_exists($field, $values)) {
           $new_values[$field] = $values[$field];
@@ -1003,6 +1014,8 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
             array_push($new_columns, $field);
           }
         }
+        // if the field doesn't exist in the values array then 
+        // substitute any default values
         elseif (array_key_exists('default', $table_desc['fields'][$field])) {
           $new_values[$field] = $table_desc['fields'][$field]['default'];
           $uq_sname .= substr($field, 0, 2);
@@ -1010,6 +1023,9 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
             array_push($new_columns, $field);
           }
         }
+        // if the array key doesn't exist in the values given by the caller
+        // and there is no default value then we cannot check if the record
+        // is a duplicate so return FALSE
         else {
           return FALSE;
         }
@@ -1024,7 +1040,12 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
       unset($new_values);
       unset($new_options);
     }
-    return $results;
+    if($options['has_record'] and $has_results){
+    	return TRUE;
+    } 
+    else {
+      return $results;
+    }
   }
 
   foreach ($values as $field => $value) {
@@ -2540,10 +2561,10 @@ function tripal_core_chado_prepare($statement_name, $psql, $args) {
     return FALSE;
   }
   else {
-    watchdog('tripal_core', "tripal_core_chado_select: prepared '%name' statement as %sql", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_NOTICE);
-    $_SESSION[$connection][] = $statement_name;
-    $_SESSION['prepared_args'][$connection][$statement_name] = $args;
-    $_SESSION['prepared_sql'][$connection][$statement_name] = $psql;
+    //watchdog('tripal_core', "tripal_core_chado_select: prepared '%name' statement as %sql", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_NOTICE);
+    //  $_SESSION[$connection][] = $statement_name;
+    //  $_SESSION['prepared_args'][$connection][$statement_name] = $args;
+    //  $_SESSION['prepared_sql'][$connection][$statement_name] = $psql;
     return TRUE;
   }
 }
@@ -2570,6 +2591,7 @@ function tripal_core_chado_execute_prepared($statement_name, $sql, $values) {
   $required_values = $_SESSION['prepared_args'][$connection][$statement_name];
   if (!$required_values){
     watchdog('tripal_core', "tripal_core_chado_execute_prepared: missing prepare arguments for this statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
+    return FALSE;
   }
 
   if (sizeof($required_values) == sizeof($values)) {

+ 20 - 11
tripal_feature/tripal_feature.module

@@ -444,36 +444,45 @@ function chado_feature_insert($node) {
     'md5checksum' => md5($residues)
   );
 
-  $istatus = tripal_core_chado_insert('feature', $values);
-  if (!$istatus) {
-    drupal_set_message(t('Unable to add feature.'), 'warning');
-    watchdog('tripal_feature',
-    'Insert feature: Unable to create feature where values: %values',
-    array('%values' => print_r($values, TRUE)),
-    WATCHDOG_WARNING
-  );
+  
+  // check to see if we are inserting a duplicate record. 
+  $options = array('is_duplicate' => TRUE, 'has_record' => TRUE);
+  $exists = tripal_core_chado_select('feature', array('*'), $values, $options);
+  
+  // if the record is not a duplicate then add it
+  if(!$exists){
+	  $istatus = tripal_core_chado_insert('feature', $values);
+	  if (!$istatus) {
+	    drupal_set_message(t('Unable to add feature.'), 'warning');
+	    watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
+	      array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
+	  }
   }
+  
+  // now get the newly added record
   $values = array(
     'organism_id' => $node->organism_id,
     'uniquename' => $node->uniquename,
     'type_id' => $type[0]->cvterm_id,
   );
   $feature = tripal_core_chado_select('feature', array('feature_id'), $values);
+  
   // add the genbank accession and synonyms
   chado_feature_add_synonyms($node->synonyms, $feature[0]->feature_id);
 
   // make sure the entry for this feature doesn't already exist in the chado_feature table
   // if it doesn't exist then we want to add it.
   $node_check_sql = "SELECT * FROM {chado_feature} " .
-                   "WHERE feature_id = '%s'";
+                    "WHERE feature_id = '%s'";
   $node_check = db_fetch_object(db_query($node_check_sql, $feature[0]->feature_id));
   if (!$node_check) {
     // next add the item to the drupal table
     $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
            "VALUES (%d, %d, %d, " . time() . ")";
-    db_query($sql, $node->nid, $node->vid, $feature[0]->feature_id);
-  }
+      db_query($sql, $node->nid, $node->vid, $feature[0]->feature_id);
   }
+}
+  
 /**
  *
  *