Browse Source

Fixed memory leaks and speed issues with OBO loader. Also added support for prepared statements for the tripal_core_chado_delete function

spficklin 12 years ago
parent
commit
bf6c2506ff
3 changed files with 332 additions and 110 deletions
  1. 184 40
      tripal_core/api/tripal_core.api.inc
  2. 28 14
      tripal_cv/api/tripal_cv.api.inc
  3. 120 56
      tripal_cv/includes/obo_loader.inc

+ 184 - 40
tripal_core/api/tripal_core.api.inc

@@ -85,9 +85,13 @@ require_once "tripal_core.schema_v1.11.api.inc";
  *  - skip_validation: TRUE or FALSE. If TRUE will skip all the validation steps and
  *     just try to insert as is. This is much faster but results in unhandled
  *     non user-friendly errors if the insert fails.
+ *  - return_record: by default, the function will return the record but with
+ *     the primary keys added after insertion.  To simply return TRUE on success
+ *     set this option to FALSE
  *
  * @return
- *  On success this function returns TRUE. On failure, it returns FALSE.
+ *  On success this function returns the inserted record with the new primary keys
+ *  add to the returned array. On failure, it returns FALSE.
  *
  * Example usage:
  * @code
@@ -131,6 +135,9 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   if (!array_key_exists('skip_validation', $options)) {
     $options['skip_validation'] = FALSE;
   }
+  if (!array_key_exists('return_record', $options)) {
+    $options['return_record'] = TRUE;
+  }
 
   $insert_values = array();
 
@@ -343,15 +350,26 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   }
 
   // if we have a result then add primary keys to return array
-  if ($result) {
+  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) {
-        $value =  db_result(chado_query("SELECT CURRVAL('" . $table . "_" . $field . "_seq')"));
-        $values[$field] = $value;
+        $psql = "PREPARE currval_$table AS SELECT CURRVAL('" . $table . "_" . $field . "_seq')";
+        $is_prepared = tripal_core_chado_prepare("currval_$table", $psql, array());
+        if ($is_prepared) {
+           $value = db_fetch_object(chado_query("EXECUTE currval_$table"));
+        }
+        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 {
     watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert record into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
     return FALSE;
@@ -539,8 +557,8 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   foreach ($update_values as $field => $value) {
 
     if (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
-    strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
-    strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
+        strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
+        strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
       if (strcmp($value, '__NULL__') == 0) {
         $sql .= " $field = %s, ";
         $ivalues[] = 'NULL';
@@ -608,8 +626,8 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   foreach ($update_matches as $field => $value) {
 
     if (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
-    strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
-    strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
+        strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
+        strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
       if (strcmp($value, '__NULL__')==0) {
         $sql .= " $field = %s AND ";
         $ivalues[] = 'NULL';
@@ -716,35 +734,44 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
  *  The name of the chado table for inserting
  * @param $match
  *  An associative array containing the values for locating a record to update.
- *
+ * @param $options
+ *  An array of options such as:
+ *  - statement_name: the name of the prepared statement to use. If the statement
+ *     has not yet been prepared it will be prepared automatically. On subsequent
+ *     calls with the same statement_name only an execute on the previously
+ *     prepared statement will occur.
+ *  - is_prepared: TRUE or FALSE. Whether or not the statement is prepared. By
+ *     default if the statement is not prepared it will be automatically.
+ *     However to avoid this check, which requires a database query you can
+ *     set this value to true and the check will not be performed.
  * @return
  *   On success this function returns TRUE. On failure, it returns FALSE.
  *
  * Example usage:
  * @code
  $umatch = array(
- 'organism_id' => array(
- 'genus' => 'Citrus',
- 'species' => 'sinensis',
- ),
- 'uniquename' => 'orange1.1g000034m.g7',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'gene',
- 'is_obsolete' => 0
- ),
+   'organism_id' => array(
+     'genus' => 'Citrus',
+     'species' => 'sinensis',
+   ),
+   'uniquename' => 'orange1.1g000034m.g7',
+   'type_id' => array (
+     'cv_id' => array (
+       'name' => 'sequence',
+     ),
+     'name' => 'gene',
+     'is_obsolete' => 0
+   ),
  );
  $uvalues = array(
- 'name' => 'orange1.1g000034m.g',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'mRNA',
- 'is_obsolete' => 0
- ),
+   'name' => 'orange1.1g000034m.g',
+   'type_id' => array (
+     'cv_id' => array (
+       'name' => 'sequence',
+     ),
+     'name' => 'mRNA',
+     'is_obsolete' => 0
+   ),
  );
  *   $result = tripal_core_chado_update('feature',$umatch,$uvalues);
  * @endcode
@@ -758,11 +785,45 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
  *
  * @ingroup tripal_chado_api
  */
-function tripal_core_chado_delete($table, $match) {
+function tripal_core_chado_delete($table, $match, $options = NULL) {
+  // set defaults for options. If we don't set defaults then
+  // we get memory leaks when we try to access the elements
+  if (!is_array($options)) {
+    $options = array();
+  }
+  if (!array_key_exists('is_prepared', $options)) {
+    $options['is_prepared'] = FALSE;
+  }
+  if (!array_key_exists('statement_name', $options)) {
+    $options['statement_name'] = FALSE;
+  }
+
+  // Determine plan of action
+  if ($options['statement_name']) {
+    // we have a prepared statment (or want to create one) so set $prepared = TRUE
+    $prepared = TRUE;
+
+    // we need to get a persistent connection.  If one exists this function
+    // 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 (update): $table\n";
+    //debug_print_backtrace();
+  }
+  
   $delete_matches = array();  // contains the values for the where clause
 
   // get the table description
   $table_desc = tripal_core_get_chado_table_schema($table);
+  $fields = $table_desc['fields'];
 
   // get the values needed for matching in the SQL statement
   foreach ($match as $field => $value) {
@@ -792,34 +853,117 @@ function tripal_core_chado_delete($table, $match) {
 
   // now build the SQL statement
   $sql = "DELETE FROM {$table} WHERE ";
+  $psql = $sql;
+  $uargs = array();
+  $idatatypes = array();
+  $pvalues = array();
+  $ivalues = array();
   $dargs = array();
+  $void_prepared = 0;
+  $i = 1;
   foreach ($delete_matches as $field => $value) {
+    
+    // if we have an array values then this is an "IN" clasue.
+    // we cannot use prepared statements with these
     if (count($value) > 1) {
       $sql .= "$field IN (" . db_placeholders($value, 'varchar') . ") AND ";
       foreach ($value as $v) {
         $dargs[] = $v;
       }
+      $void_prepared = 1;
+      continue;
     }
-    else {
+
+    if (strcasecmp($table_desc['fields'][$field]['type'], 'serial') == 0 OR
+        strcasecmp($table_desc['fields'][$field]['type'], 'int') == 0 OR
+        strcasecmp($table_desc['fields'][$field]['type'], 'integer') == 0) {
       if (strcmp($value, '__NULL__') == 0) {
         $sql .= " $field = NULL AND ";
-      }
-      elseif (strcmp($fields[$field]['type'], 'serial') == 0 or
-      strcmp($fields[$field]['type'], 'int') == 0) {
+        $ivalues[] = 'NULL';
+        $pvalues[] = '%s';
+        $uargs[] = 'NULL';
+      } 
+      else {
         $sql .= " $field = %d AND ";
+        $ivalues[] = $value;
+        $pvalues[] = '%d';
+        $uargs[] = $value;
+      }
+      $idatatypes[] = 'int';        
+    }
+    elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
+      $sql .= " $field = %s AND ";
+      $pvalues[] = '%s';
+      if (strcmp($value, '__NULL__')==0) {
+        $ivalues[] = 'NULL';
+        $uargs[] = 'NULL';
       }
       else {
-        $sql .= " $field = '%s' AND ";
+        $ivalues[] = $value;
+        $uargs[] = $value;
       }
-      array_push($dargs, $value);
+      $idatatypes[] = 'bool';
     }
+    elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
+      $sql .= " $field = %s AND ";
+      $pvalues[] = '%s';
+      if (strcmp($value, '__NULL__')==0) {
+        $ivalues[] = 'NULL';
+        $uargs[] = 'NULL';
+      }
+      else {
+        $ivalues[] = $value;
+        $uargs[] = $value;
+      }
+      $idatatypes[] = 'numeric';
+    }
+    else {
+      if (strcmp($value, '__NULL__')==0) {
+        $sql .= " $field = %s AND ";
+        $ivalues[] = 'NULL';
+        $uargs[] = 'NULL';
+        $pvalues[] = '%s';
+      }
+      else {
+        $sql .= " $field = '%s' AND ";
+        $ivalues[] = $value;
+        $uargs[] = $value;
+        $pvalues[] = "'%s'";
+      }
+      $idatatypes[] = 'text';
+    }    
+    array_push($dargs, $value);  
+    $psql .= "$field = \$" . $i . " AND ";
+    $i++;
   }
   $sql = drupal_substr($sql, 0, -4);  // get rid of the trailing 'AND'
+  $psql = drupal_substr($psql, 0, -4);  // get rid of the trailing 'AND'
 
-  // finally perform the delete.  If successful, return the updated record
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $result = db_query($sql, $dargs);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  // finish constructing the prepared SQL statement
+  $psql =  "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS " . $psql;
+  
+  // finally perform the update.  If successful, return the updated record
+  if ($prepared and !$void_prepared) {
+    // if this is the first time we've run this query
+    // then we need to do the prepare, otherwise just execute
+    if ($options['is_prepared'] != TRUE and
+    !tripal_core_is_sql_prepared($options['statement_name'])) {
+      $status = chado_query($psql);
+      if (!$status) {
+        watchdog('tripal_core', "tripal_core_chado_delete: not able to prepare '%name' statement for: %sql", array('%name' => $options['statement_name'], '%sql' => $sql), 'WATCHDOG ERROR');
+        return FALSE;
+      }
+    }
+    $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $pvalues) . ")";
+    $resource = chado_query($sql, $ivalues);
+  }
+  // if it's not a prepared statement then insert normally
+  else {
+    $resource = chado_query($sql, $uargs);
+  }
+  
+  // finally perform the delete.  If successful, return the updated record  
+  $result = chado_query($sql, $dargs);
   if ($result) {
     return TRUE;
   }

+ 28 - 14
tripal_cv/api/tripal_cv.api.inc

@@ -260,6 +260,7 @@ function tripal_cv_get_cvterm_options($cv_id = 0) {
  */
 function tripal_cv_add_cv($name, $definition) {
   
+  // insert/update values
   $ins_values = array(
     'name'       => $name,
     'definition' => $definition
@@ -344,11 +345,25 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
     
   // get the term properties
   $id = $term['id'];
-  $name = $term['name'];
-  $cvname = $term['namespace'];
-  $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
-  $is_obsolete = 0;
-  
+  if (isset($term['name'])) {
+    $name = $term['name'];  
+  }
+  else {
+    $name = $id;  
+  }  
+  if (isset($term['namespace'])) {
+    $cvname = $term['namespace'];
+  } 
+  else {
+    $cvname = $defaultcv;
+  }
+  if (isset($term['def'])) {
+    $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
+  }
+  else {
+    $definition = '';
+  }
+  $is_obsolete = 0;  
   if (isset($term['is_obsolete']) and strcmp($term['is_obsolete'], 'true') == 0) {
     $is_obsolete = 1;
   }  
@@ -359,12 +374,7 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
   if (!$id) {
     $id = $name;
   }
-  if (!$name) {
-    $name = $id;
-  }
-  if (!$cvname) {
-    $cvname = $defaultcv;
-  }
+
   
   // get the accession and the database from the cvterm id
   if ($dbname) {
@@ -483,7 +493,8 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
       // below recreate it with the correct info 
       else {          
         $match = array('cvterm_id' => $cvterm->cvterm_id);
-        tripal_core_chado_delete('cvterm', $match);
+        $options = array('statement_name' => 'del_cvterm_cv');
+        tripal_core_chado_delete('cvterm', $match, $options);
       }      
     }
     
@@ -512,7 +523,10 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
       
       // if the cvterm_dbxref record does not exists then add it 
       if (count($result)==0) {
-        $options = array('statement_name' => 'ins_cvtermdbxref_cvdbis');
+        $options = array(
+          'statement_name' => 'ins_cvtermdbxref_cvdbis',
+          'return_record' => FALSE,
+        );
         $success = tripal_core_chado_insert('cvterm_dbxref', $values, $options);
         if (!$success) {
           watchdog('tripal_cv', "Failed to find or insert the cvterm_dbxref record for a " . 
@@ -592,7 +606,7 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
     $success = tripal_core_chado_update('cvterm', $match, $upd_values, $upd_options);    
     if (!$success) {
       watchdog('tripal_cv', "Failed to update the term: $name", NULL, WATCHDOG_WARNING);
-      return FALSE;
+      return 0;
     }
     $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
   } 

+ 120 - 56
tripal_cv/includes/obo_loader.inc

@@ -199,7 +199,7 @@ function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$
   }
   
   // calculate the interval for updates
-  $interval = intval($count * 0.01);
+  $interval = intval($count * 0.0001);
   if ($interval < 1) {
     $interval = 1;
   }
@@ -259,8 +259,10 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
   if (!$cvterm) {
     tripal_cv_obo_quiterror("Cannot add the term " . $term['id']);
   }
-  if ($term['namespace']) {
-    $newcvs[$term['namespace']] = $cvterm->cv_id;
+  
+  
+  if (isset($term['namespace'])) {
+    $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
   }
   
   // now handle other properites
@@ -274,6 +276,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
       }
     }
   }
+  
   if (isset($term['subset'])) {
     //print "WARNING: unhandled tag: subset\n";
   }
@@ -310,6 +313,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
       tripal_cv_obo_quiterror("Cannot add/update synonyms");
     }
   }
+  
   // add the comment to the cvtermprop table
   if (isset($term['comment'])) {
     $comments = $term['comment'];
@@ -321,6 +325,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
       $j++;
     }
   }
+  
   // add any other external dbxrefs
   if (isset($term['xref'])) {
     foreach ($term['xref'] as $xref) {
@@ -329,6 +334,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
       }
     }
   }
+  
   if (isset($term['xref_analog'])) {
     foreach ($term['xref_analog'] as $xref) {
       if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
@@ -352,6 +358,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
       }
     }
   }
+  
   if (isset($term['intersection_of'])) {
     //print "WARNING: unhandled tag: intersection_of\n";
   }
@@ -400,7 +407,8 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
     'definition' => '',
     'is_obsolete' => 0,
   );
-  $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
+  $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0, $default_db);
+  
   if (!$relcvterm) {
     // if the relationship term couldn't be found in the default_db provided 
     // then do on more check to find it in the relationship ontology
@@ -410,7 +418,7 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
       'definition' => '',
       'is_obsolete' => 0,
     ); 
-    $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
+    $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0, 'OBO_REL');
     if (!$relcvterm) {
       tripal_cv_obo_quiterror("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
     }
@@ -421,15 +429,23 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
   if (!$oterm) {
     tripal_cv_obo_quiterror("Could not find object term $objname\n");
   }
-  $objterm = array(
-    'name' => $oterm['name'][0],
-    'id' => $oterm['id'][0],
-    'definition' => $oterm['def'][0],
-    'is_obsolete' => $oterm['is_obsolete'][0],
-  );
-  $objcvterm = tripal_cv_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1);
+  
+  $objterm = array(); 
+  $objterm['id']            = $oterm['id'][0];
+  $objterm['name']          = $oterm['name'][0];
+  $objterm['def']           = $oterm['def'][0];
+  if (isset($oterm['subset'])) {
+    $objterm['subset']      = $oterm['subset'][0];  
+  }  
+  if (isset($oterm['namespace'])) {
+    $objterm['namespace']   = $oterm['namespace'][0];
+  }
+  if (isset($oterm['is_obsolete'])) {
+    $objterm['is_obsolete'] = $oterm['is_obsolete'][0];
+  }
+  $objcvterm = tripal_cv_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1, $default_db);  
   if (!$objcvterm) {
-    tripal_cv_obo_quiterror("Cannot add/find cvterm");
+    tripal_cv_obo_quiterror("Cannot add cvterm " . $oterm['name'][0]);
   }
 
   // check to see if the cvterm_relationship already exists, if not add it
@@ -441,9 +457,10 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
   $options = array('statement_name' => 'sel_cvtermrelationship_tysuob');
   $result = tripal_core_chado_select('cvterm_relationship', array('*'), $values, $options);
   if (count($result) == 0) {
-    $options = array('statement_name' => 'ins_cvtermrelationship_tysuob');
-    $sql = "INSERT INTO {cvterm_relationship} ".
-           "(type_id,subject_id,object_id) VALUES (%d,%d,%d)";
+    $options = array(
+      'statement_name' => 'ins_cvtermrelationship_tysuob',
+      'return_record' => FALSE
+    );
     $success = tripal_core_chado_insert('cvterm_relationship', $values, $options);
     if (!$success) {
       tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
@@ -458,6 +475,7 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_get_term($obo, $id) {
+  /*
   foreach ($obo as $type) {
     foreach ($type as $term) {
       $accession = $term['id'][0];
@@ -465,6 +483,14 @@ function tripal_cv_obo_get_term($obo, $id) {
         return $term;
       }
     }
+  } */
+  // iterate through each of the types in the
+  // obo file (parsed into memory) and look
+  // for this term.  If found, return it.
+  foreach ($obo as $type) {
+    if (array_key_exists($id, $type)) {
+      return $type[$id];
+    }
   }
 
   return FALSE;
@@ -505,12 +531,12 @@ function tripal_cv_obo_add_synonyms($term, $cvterm) {
       if (!$results) {
         // build a 'term' object so we can add the missing term
         $term = array(
-           'name' => array($scope),
-           'id' => array("internal:$scope"),
-           'definition' => array(''),
-           'is_obsolete' => array(0),
+           'name' => $scope,
+           'id' => "internal:$scope",
+           'definition' => '',
+           'is_obsolete' => 0,
         );
-        $syntype = tripal_cv_add_cvterm($term, $syncv, 0, 1);
+        $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
         if (!$syntype) {
           tripal_cv_obo_quiterror("Cannot add synonym type: internal:$scope");
         }
@@ -532,7 +558,10 @@ function tripal_cv_obo_add_synonyms($term, $cvterm) {
           'synonym' => $def,
           'type_id' => $syntype->cvterm_id
         );
-        $options = array('statement_name' => 'ins_cvtermsynonym_cvsy');
+        $options = array(
+          'statement_name' => 'ins_cvtermsynonym_cvsy',
+          'return_record' => FALSE
+        );
         $success = tripal_core_chado_insert('cvtermsynonym', $values, $options);
         if (!$success) {
           tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
@@ -591,7 +620,7 @@ function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
 
     if (preg_match('/^\s*\[/', $line)) {  // at the first stanza we're out of header
       $in_header = 0;
-      // load the stanza we just finished reading
+      // store the stanza we just finished reading
       if (sizeof($stanza) > 0) {
         if (!isset($obo[$type])) {
           $obo[$type] = array();
@@ -689,12 +718,21 @@ function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
   }
 
   // finally add the cvterm_dbxref but first check to make sure it exists
-  $sql = "SELECT * from {cvterm_dbxref} WHERE cvterm_id = %d and dbxref_id = %d";
-  if (!db_fetch_object(db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id))) {
-    $sql = "INSERT INTO {cvterm_dbxref} (cvterm_id,dbxref_id)".
-           "VALUES (%d,%d)";
-    if (!db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id)) {
+  $values = array(
+    'cvterm_id' => $cvterm->cvterm_id,
+    'dbxref_id' => $dbxref->dbxref_id,
+  );
+  $options = array('statement_name' => 'sel_cvtermdbxref_cvdb');
+  $result = tripal_core_chado_select('cvterm_dbxref', array('*'), $values, $options);
+  if (count($result) == 0) {    
+    $ins_options = array(
+      'statement_name' => 'ins_cvtermdbxref_cvdb',
+      'return_record' => FALSE
+    );
+    $result = tripal_core_chado_insert('cvterm_dbxref', $values, $ins_options);
+    if (!$result){
       tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
+      return FALSE;
     }
   }
 
@@ -714,13 +752,13 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
   }
 
   // get the property type cvterm.  If it doesn't exist then we want to add it
-  $sql = "
-      SELECT *
-      FROM {cvterm} CVT INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
-      WHERE CVT.name = '%s' and CV.name = '%s'
-  ";
-  $cvproptype = db_fetch_object(db_query($sql, $property, 'cvterm_property_type'));
-  if (!$cvproptype) {
+  $values = array(
+    'name' => $property,
+    'cv_id' => $cv->cv_id,
+  );
+  $options = array('statement_name' => 'sel_cvterm_nacv_na');
+  $results = tripal_core_chado_select('cvterm', array('*'), $values, $options);
+  if (count($results) == 0) {    
     $term = array(
       'name' => $property,
       'id' => "internal:$property",
@@ -730,25 +768,41 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
     $cvproptype = tripal_cv_add_cvterm($term, $cv->name, 0, 0);
     if (!$cvproptype) {
       tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
+      return FALSE;
     }
   }
-
+  else {
+    $cvproptype = $results[0];
+  }
 
   // remove any properties that currently exist for this term.  We'll reset them
   if ($rank == 0) {
-    $sql = "DELETE FROM {cvtermprop} WHERE cvterm_id = %d";
-    db_query($sql, $cvterm->cvterm_id);
+    $values = array('cvterm_id' => $cvterm->cvterm_id);
+    $options = array('statement_name' => 'del_cvtermprop_cv');
+    $success = tripal_core_chado_delete('cvtermprop', $values, $options);
+    if (!$success) {
+       tripal_cv_obo_quiterror("Could not remove existing properties to update property $property for term\n");
+       return FALSE;
+    }    
   }
 
   // now add the property
-  $sql = "INSERT INTO {cvtermprop} (cvterm_id,type_id,value,rank) ".
-        "VALUES (%d, %d, '%s',%d)";
-  if (!db_query($sql, $cvterm->cvterm_id, $cvproptype->cvterm_id, $value, $rank)) {
+  $values = array(
+    'cvterm_id' => $cvterm->cvterm_id,
+    'type_id' => $cvproptype->cvterm_id,
+    'value' => $value,
+    'rank' => $rank,
+  );
+  $options = array(
+    'statement_name' => 'ins_cvtermprop_cvtyvara',
+    'return_record' => FALSE,
+  );
+  $result = tripal_core_chado_insert('cvtermprop', $values, $options);
+  if (!$result) {
     tripal_cv_obo_quiterror("Could not add property $property for term\n");
+    return FALSE;
   }
-
   return TRUE;
-
 }
 
 
@@ -759,20 +813,30 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
 function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
 
   // check to see if the dbxref exists if not, add it
-  $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'";
-  $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
-  if (!$dbxref) {
-    $sql = "
-       INSERT INTO {dbxref} (db_id, accession, version, description)
-       VALUES (%d,'%s','%s','%s')
-    ";
-    if (!db_query($sql, $db_id, $accession, $version, $description)) {
+  $values = array(
+    'db_id' => $db_id,
+    'accession' => $accession,
+  );
+  $options = array('statement_name' => 'sel_dbxref_idac');
+  $result = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);
+  if (count($result) == 0){
+    $ins_values = array(
+      'db_id'       => $db_id,
+      'accession'   => $accession,
+      'version'     => $version,
+      'description' => $description,
+    );
+    $ins_options = array(
+      'statement_name' => 'ins_dbxref_idacvede',
+      'return_record' => FALSE
+    );
+    $result = tripal_core_chado_insert('dbxref', $ins_values, $ins_options);
+    if (!$result) {
       tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
-    }
-    print "Added Dbxref accession: $accession\n";
-    $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
+      return FALSE;
+    } 
+    $result = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);    
   }
-  return $dbxref;
-
+  return $result[0];
 }