Преглед изворни кода

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

spficklin пре 12 година
родитељ
комит
bd92f8335a
2 измењених фајлова са 74 додато и 10 уклоњено
  1. 45 2
      tripal_bulk_loader/tripal_bulk_loader.loader.inc
  2. 29 8
      tripal_core/api/tripal_core.api.inc

+ 45 - 2
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -593,7 +593,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
 
     // if this is a duplicate then substitute the values in the table_data array so
-    // that for future records that my depend on this one, they can get the values needed
+    // that for future records that may depend on this one, they can get the values needed
     if ($duplicate and is_array($duplicate) and count($duplicate) == 1) {
       $dup_record = $duplicate[0];
       // save the duplicate record for later.  If this is an update_if_duplicate
@@ -651,8 +651,51 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       if (array_key_exists('statement_name', $options)) {
         $options['statement_name'] = 'upd_' . $options['statement_name'];
       }
+      // This should have been set on the first round of inserts for this record
       $match = $data[$priority]['is_duplicate'];
-      $record = tripal_core_chado_update($table, $match, $values, $options);
+      // However, sometimes there is a pre-existing record before the loader starts
+			// Thus check that this value is set and if not, then generate a match array
+			// based on the unique keys for this record.
+			if (empty($match)) {
+				$match = array();
+				// First check to see if we have fields for the primary key
+				foreach ($table_desc['primary key'] as $k_field) {
+					if (!empty($values[$k_field])) {
+						$match[$k_field] = $values[$k_field];
+					}
+				}
+				// Otherwise check the fields that are part of the unique key
+				if (empty($match)) {
+					foreach ($table_desc['unique keys'] as $u_keys) {
+						foreach ($u_keys as $u_field) {
+							if (!empty($values[$u_field])) {
+								$match[$u_field] = $values[$u_field];
+							}
+						}
+					}
+				}
+			}
+			if (!empty($match)) {
+				// Now we need to check if it already exists via a select
+				$results = tripal_core_chado_select($table, array_keys($table_desc['fields']), $match);
+				// If not then insert
+				if (empty($results)) {
+					$options['statement_name'] = 'ins_'.$options['statement_name'];
+					$record = tripal_core_chado_insert($table, $values, $options);
+				}
+				else {
+  		    $record = tripal_core_chado_update($table, $match, $values, $options);
+  		  }
+  	  }
+  	  else {
+  	  	$msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
+        $table_data['mode'] . ') Unable to update record since none of the unique key or primary key fields were available ' .
+        ' where values:' . print_r($values, TRUE);
+
+      	watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
+      	$data[$priority]['error'] = TRUE;
+      	$no_errors = FALSE;
+  	  }
     }
     else {
       $record = tripal_core_chado_insert($table, $values, $options);

+ 29 - 8
tripal_core/api/tripal_core.api.inc

@@ -457,6 +457,9 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   if (!array_key_exists('statement_name', $options)) {
     $options['statement_name'] = FALSE;
   }
+  if (!array_key_exists('return_record', $options)) {
+    $options['return_record'] = TRUE;
+  }
 
   $update_values = array();   // contains the values to be updated
   $update_matches = array();  // contains the values for the where clause
@@ -705,14 +708,32 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
       }
     }
     $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $pvalues) . ")";
-    $resource = chado_query($sql, $ivalues);
+    $result = chado_query($sql, $ivalues);
   }
   // if it's not a prepared statement then insert normally
   else {
-    $resource = chado_query($sql, $uargs);
+    $result = chado_query($sql, $uargs);
   }
 
-  if ($resource) {
+    // if we have a result then add primary keys to return array
+  if ($options['return_record'] == TRUE and $result) {
+    if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
+      foreach ($table_desc['primary key'] as $field) {
+        $psql = "PREPARE currval_" . $table . "_" . $field . " AS SELECT CURRVAL('" . $table . "_" . $field . "_seq')";
+        $is_prepared = tripal_core_chado_prepare("currval_" . $table . "_" . $field, $psql, array());
+        if ($is_prepared) {
+           $value = db_result(chado_query("EXECUTE currval_". $table . "_" . $field));
+        }
+        else {
+          $sql = "SELECT CURRVAL('" . $table . "_" . $field . "_seq')";
+          $value =  db_result(chado_query($sql));
+        }
+        $values[$field] = $value;
+      }
+    }
+    return $values;
+  }
+  elseif ($options['return_record'] == FALSE and $result) {
     return TRUE;
   }
   else {
@@ -1025,7 +1046,7 @@ function tripal_core_chado_delete($table, $match, $options = NULL) {
  *
  * @return
  *  A database query result resource, FALSE if the query was not executed
- *  correctly, an empty array if no records were matched, or the number of records 
+ *  correctly, an empty array if no records were matched, 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
@@ -1389,10 +1410,10 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
       $i++;
     } // end foreach item in where clause
     $sql = drupal_substr($sql, 0, -4);  // get rid of the trailing 'AND '
-    $psql = drupal_substr($psql, 0, -4);  // get rid of the trailing 'AND '    
-    
+    $psql = drupal_substr($psql, 0, -4);  // get rid of the trailing 'AND '
+
   } // end if(empty($where)){ } else {
-      
+
   // finally add any ordering of the results to the SQL statement
   if (count($options['order_by']) > 0) {
     $sql .= " ORDER BY ";
@@ -1403,7 +1424,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
     }
     $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing ', '
     $psql = drupal_substr($psql, 0, -2);  // get rid of the trailing ', '
-  }  
+  }
 
   // finish constructing the prepared SQL statement
   if ($options['statement_name']) {