Browse Source

API: Consolidated chado_delete_property/ID and chado_update_property/ID

Lacey Sanderson 11 years ago
parent
commit
00d77d137e

+ 12 - 7
tripal_core/api/tripal_core.DEPRECATED.inc

@@ -622,12 +622,18 @@ function tripal_update_mview($mview_id) {
 /**
  * @deprecated Restructured API to make naming more readable and consistent.
  * Function was deprecated in Tripal 2.0 and will be removed 2 releases from now.
- * This function has been replaced by @see chado_delete_property().
+ * This function is considered to be no easier to use than chado_delete_record directly
+ * @see chado_delete_record().
  */
-function tripal_core_delete_property_by_id($basetable, $record_id) {
-  //New API Function
-  // D7 @todo: change parameters
-  return chado_delete_property($basetable, $record_id);
+function tripal_core_delete_property_by_id($basetable, $property_id) {
+
+  // construct the array that will match the exact record to update
+  $match = array(
+    $basetable.'prop_id' => $property_id,
+  );
+
+  return chado_delete_record($basetable.'prop', $match);
+
 }
 
 /**
@@ -680,8 +686,7 @@ $cv_name, $value, $update_if_present = 0) {
 function tripal_core_update_property_by_id($basetable, $record_id, $property,
 $cv_name, $value) {
   //New API Function
-  // D7 @todo: change parameters
-  return chado_update_property($basetable, $record_id, $property, $cv_name, $value);
+  return chado_update_property($basetable, FALSE, $property, $cv_name, $value, FALSE, $record_id);
 }
 
 /**

+ 26 - 31
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -118,7 +118,7 @@
  *
  * @ingroup tripal_chado_node_api
  */
-function chado_get_property($basetable, $record_id, $property, $cv_name) {
+function chado_get_property($basetable, $record_id, $property, $cv_name, $property_id = FALSE) {
   // get the foreign key for this property table
   $table_desc = chado_get_schema($basetable . 'prop');
   $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
@@ -134,6 +134,11 @@ function chado_get_property($basetable, $record_id, $property, $cv_name) {
       'is_obsolete' => 0
     ),
   );
+  // if we have the unique property_id make sure to add that to the values
+  if ($property_id) {
+    $property_pkey = $table_desc['primary key'][0];
+    $values[$property_pkey] = $property_id;
+  }
   $results = chado_generate_var($basetable . 'prop', $values);
   if ($results) {
     $results = chado_expand_var($results, 'field', $basetable . 'prop.value');
@@ -272,10 +277,10 @@ $cv_name, $value, $update_if_present = 0) {
  * @ingroup tripal_chado_node_api
  */
 function chado_update_property($basetable, $record_id, $property,
-$cv_name, $value, $insert_if_missing = 0) {
+$cv_name, $value, $insert_if_missing = FALSE, $property_id = FALSE) {
 
   // first see if the property is missing (we can't update a missing property
-  $prop = chado_get_property($basetable, $record_id, $property, $cv_name);
+  $prop = chado_get_property($basetable, $record_id, $property, $cv_name, $property_id);
   if (count($prop)==0) {
     if ($insert_if_missing) {
       return chado_insert_property($basetable, $record_id, $property, $cv_name, $value);
@@ -299,11 +304,29 @@ $cv_name, $value, $insert_if_missing = 0) {
       'name' => $property,
     ),
   );
+  // If we have the unique property_id, make sure to use it in the match to ensure
+  // we get the exact record. Doesn't rely on there only being one property of that type
+  if ($property_id) {
+    $property_pkey = $table_desc['primary key'][0];
+    $match = array(
+      $property_pkey => $property_id
+    );
+  }
 
   // construct the array of values to be updated
   $values = array(
     'value' => $value,
   );
+  // If we have the unique property_id then we can also update the type
+  // thus add it to the values to be updated
+  if ($property_id) {
+    $values['type_id'] = array(
+      'cv_id' => array(
+        'name' => $cv_name,
+      ),
+      'name' => $property,
+    );
+  }
 
   return chado_update_record($basetable . 'prop', $match, $values);
 }
@@ -398,34 +421,6 @@ function chado_delete_property($basetable, $record_id, $property, $cv_name) {
   return chado_delete_record($basetable . 'prop', $match);
 }
 
-/**
- * Deletes a property using the property ID
- *
- * @param $basetable
- *   The base table for which the property should be deleted. Thus to deleted a property
- *   for a feature the basetable=feature and property is deleted from featureprop
- * @param $record_id
- *   The primary key of the basetable to delete a property for. This should be in integer.
- *
- * @return
- *   Return True on Delete and False otherwise
- *
- * @ingroup tripal_chado_node_api
- */
-function chado_delete_propertyID($basetable, $record_id) {
-
-  // get the foreign key for this property table
-  $table_desc = chado_get_schema($basetable . 'prop');
-  $pkcol = $table_desc['primary key'][0];
-
-  // construct the array that will match the exact record to update
-  $match = array(
-    $pkcol => $record_id,
-  );
-
-  return chado_delete_record($basetable . 'prop', $match);
-}
-
 /**
  * @section
  * Properties Form to be added to node forms

+ 19 - 6
tripal_feature/api/tripal_feature.api.inc

@@ -199,8 +199,8 @@ function tripal_feature_analysis_update_property_by_id($analysisfeatureprop_id,
   $property, $value, $cv_name = 'tripal') {
 
     // update the property.
-    return chado_update_propertyID('analysisfeature',
-      $analysisfeatureprop_id, $property, $cv_name, $value);
+    return chado_update_property('analysisfeature',
+      NULL, $property, $cv_name, $value, FALSE, $analysisfeatureprop_id);
 }
 /**
  * Delete an analysis feature property using the property name.  Use this
@@ -267,8 +267,14 @@ function tripal_feature_analysis_delete_property($analysis_id = NULL, $feature_i
  * @ingroup tripal_feature_api
  */
 function tripal_feature_analysis_delete_property_by_id($analysisfeatureprop_id) {
-  // get the property.
-  return chado_delete_propertyID('analysisfeature', $analysisfeatureprop_id);
+
+  // construct the array that will match the exact record to update
+  $match = array(
+    'analysisfeatureprop_id' => $analysisfeatureprop_id,
+  );
+
+  return chado_delete_record('analysisfeatureprop', $match);
+
 }
 /**
  * Retrieve properties of a given type for a given feature
@@ -364,7 +370,7 @@ function tripal_feature_update_property($feature_id, $property,
  */
 function tripal_feature_update_property_by_id($featureprop_id, $property,
   $value, $cv_name = 'tripal') {
-    return chado_update_propertyID('feature', $featureprop_id, $property, $cv_name, $value);
+    return chado_update_property('feature', NULL, $property, $cv_name, $value, FALSE, $featureprop_id);
 }
 
 /**
@@ -402,7 +408,14 @@ function tripal_feature_delete_property($feature_id, $property, $cv_name='tripal
  * @ingroup tripal_feature_api
  */
 function tripal_feature_delete_property_by_id($featureprop_id) {
-  return chado_delete_propertyID('feature', $featureprop_id);
+
+  // construct the array that will match the exact record to update
+  $match = array(
+    'featureprop_id' => $featureprop_id,
+  );
+
+  return chado_delete_record('featureprop', $match);
+
 }
 
 /**