瀏覽代碼

Fixed some chado_query calls that still passed strings rather then an array of arguments. Also added a watchdog call to chado_query to warn when this happens.

Lacey Sanderson 11 年之前
父節點
當前提交
4ae89fbd2b
共有 2 個文件被更改,包括 119 次插入112 次删除
  1. 61 56
      tripal_core/api/tripal_core_chado.api.inc
  2. 58 56
      tripal_cv/includes/tripal_cv_admin.inc

+ 61 - 56
tripal_core/api/tripal_core_chado.api.inc

@@ -190,7 +190,7 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
             $ukselect_vals[$field] = $insert_values[$field];
             $ukselect_vals[$field] = $insert_values[$field];
           }
           }
         }
         }
-        // now check the constraint        
+        // now check the constraint
         if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals)) {
         if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals)) {
           watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " .
           watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " .
             print_r($values, 1), array(), 'WATCHDOG_ERROR');
             print_r($values, 1), array(), 'WATCHDOG_ERROR');
@@ -274,7 +274,7 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
     return TRUE;
     return TRUE;
   }
   }
   else {
   else {
-    watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert record into '%table': " . print_r($values, 1), 
+    watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert record into '%table': " . print_r($values, 1),
       array('%table' => $table), 'WATCHDOG_ERROR');
       array('%table' => $table), 'WATCHDOG_ERROR');
     return FALSE;
     return FALSE;
   }
   }
@@ -409,7 +409,7 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
 
 
   // get the values needed for matching in the SQL statement
   // get the values needed for matching in the SQL statement
   foreach ($match as $field => $value) {
   foreach ($match as $field => $value) {
-    if (is_array($value)) {   
+    if (is_array($value)) {
       $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
       $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
       if (sizeof($results) > 1) {
       if (sizeof($results) > 1) {
         watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)), WATCHDOG_ERROR);
         watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)), WATCHDOG_ERROR);
@@ -430,7 +430,7 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   foreach ($values as $field => $value) {
   foreach ($values as $field => $value) {
     if (is_array($value)) {
     if (is_array($value)) {
       $foreign_options = array();
       $foreign_options = array();
-      // select the value from the foreign key relationship for this value   
+      // select the value from the foreign key relationship for this value
       $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
       $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
       if (sizeof($results) > 1) {
       if (sizeof($results) > 1) {
         watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)), WATCHDOG_ERROR);
         watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)), WATCHDOG_ERROR);
@@ -450,14 +450,14 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   // now build the SQL statement
   // now build the SQL statement
   $sql  = 'UPDATE {' . $table . '} SET ';
   $sql  = 'UPDATE {' . $table . '} SET ';
   $args = array();        // arguments passed to chado_query
   $args = array();        // arguments passed to chado_query
-  foreach ($update_values as $field => $value) {    
+  foreach ($update_values as $field => $value) {
     if (strcmp($value, '__NULL__') == 0) {
     if (strcmp($value, '__NULL__') == 0) {
       $sql .= " $field = NULL, ";
       $sql .= " $field = NULL, ";
     }
     }
     else {
     else {
       $sql .= " $field = :$field, ";
       $sql .= " $field = :$field, ";
       $args[":$field"] = $value;
       $args[":$field"] = $value;
-    }    
+    }
   }
   }
   $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing comma & space
   $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing comma & space
 
 
@@ -469,7 +469,7 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
     else {
     else {
       $sql .= " $field = :$field AND ";
       $sql .= " $field = :$field AND ";
       $args[":$field"] = $value;
       $args[":$field"] = $value;
-    }    
+    }
   }
   }
   $sql = drupal_substr($sql, 0, -4);  // get rid of the trailing 'AND'
   $sql = drupal_substr($sql, 0, -4);  // get rid of the trailing 'AND'
 
 
@@ -611,7 +611,7 @@ function tripal_core_chado_delete($table, $match, $options = NULL) {
   foreach ($delete_matches as $field => $value) {
   foreach ($delete_matches as $field => $value) {
     // if we have an array values then this is an "IN" clasue.
     // if we have an array values then this is an "IN" clasue.
     // we cannot use prepared statements with these
     // we cannot use prepared statements with these
-    
+
     if (count($value) > 1) {
     if (count($value) > 1) {
       $sql .= "$field IN (";
       $sql .= "$field IN (";
       $index = 0;
       $index = 0;
@@ -943,7 +943,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
     }
     }
   }
   }
 
 
-  
+
   // now build the SQL and prepared SQL statements. We may not use
   // now build the SQL and prepared SQL statements. We may not use
   // the prepared statement if it wasn't requested in the options or if the
   // the prepared statement if it wasn't requested in the options or if the
   // argument in a where statement has multiple values.
   // argument in a where statement has multiple values.
@@ -987,14 +987,14 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
         $operator = '=';
         $operator = '=';
         if (in_array($field, $options['regex_columns'])) {
         if (in_array($field, $options['regex_columns'])) {
           $operator = '~*';
           $operator = '~*';
-        }        
+        }
         if (in_array($field, $options['case_insensitive_columns'])) {
         if (in_array($field, $options['case_insensitive_columns'])) {
           $sql .= "lower($field) $operator lower(:$field) AND ";
           $sql .= "lower($field) $operator lower(:$field) AND ";
           $args[":$field"] = $value[0];
           $args[":$field"] = $value[0];
         }
         }
         else {
         else {
           $sql .= "$field $operator :$field AND ";
           $sql .= "$field $operator :$field AND ";
-          $args[":$field"] = $value[0];          
+          $args[":$field"] = $value[0];
         }
         }
       }
       }
     } // end foreach item in where clause
     } // end foreach item in where clause
@@ -1009,7 +1009,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
     }
     }
     $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing ', '
     $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing ', '
   }
   }
-  
+
   // if the caller has requested the SQL rather than the results...
   // if the caller has requested the SQL rather than the results...
   // which happens in the case of wanting to use the Drupal pager, then do so
   // which happens in the case of wanting to use the Drupal pager, then do so
   if ($options['return_sql'] == TRUE) {
   if ($options['return_sql'] == TRUE) {
@@ -1338,7 +1338,7 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
 */
 */
   // get the values for the record in the current table---------------------------------------------
   // get the values for the record in the current table---------------------------------------------
   $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
   $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
-  
+
   if ($results) {
   if ($results) {
     foreach ($results as $key => $object) {
     foreach ($results as $key => $object) {
       // Add empty expandable_x arrays
       // Add empty expandable_x arrays
@@ -1354,8 +1354,8 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
         // that has a foreign key to this one ($table_desc['primary key'][0]
         // that has a foreign key to this one ($table_desc['primary key'][0]
         // and to the node table (nid)
         // and to the node table (nid)
         $sql = "
         $sql = "
-          SELECT $table_primary_key, nid 
-          FROM {chado_$table} 
+          SELECT $table_primary_key, nid
+          FROM {chado_$table}
           WHERE $table_primary_key = :$table_primary_key";
           WHERE $table_primary_key = :$table_primary_key";
         $mapping = db_query($sql, array(":$table_primary_key" => $object->{$table_primary_key}))->fetchObject();
         $mapping = db_query($sql, array(":$table_primary_key" => $object->{$table_primary_key}))->fetchObject();
         if ($mapping->{$table_primary_key}) {
         if ($mapping->{$table_primary_key}) {
@@ -1437,9 +1437,9 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
       }
       }
     }
     }
   }
   }
-  
+
   // convert the results into an array
   // convert the results into an array
-  $results_arr = array(); 
+  $results_arr = array();
   foreach ($results as $record) {
   foreach ($results as $record) {
     $results_arr[] = $record;
     $results_arr[] = $record;
   }
   }
@@ -1615,11 +1615,11 @@ function tripal_core_expand_chado_vars($object, $type, $to_expand, $table_option
           // if a prepared statement is provided generate a new statement_name so that
           // if a prepared statement is provided generate a new statement_name so that
           // we don't conflict when we recurse.
           // we don't conflict when we recurse.
           $new_options = $table_options;
           $new_options = $table_options;
-/*          
+/*
           if (array_key_exists('statement_name', $table_options)) {
           if (array_key_exists('statement_name', $table_options)) {
              $new_options['statement_name'] = "exp_" . $foreign_table . "_" . substr($left, 0, 2) . substr($right, 0, 2);
              $new_options['statement_name'] = "exp_" . $foreign_table . "_" . substr($left, 0, 2) . substr($right, 0, 2);
           }
           }
-*/          
+*/
           $foreign_object = tripal_core_generate_chado_var($foreign_table, array($left => $object->{$right}), $new_options);
           $foreign_object = tripal_core_generate_chado_var($foreign_table, array($left => $object->{$right}), $new_options);
 
 
           // if the generation of the object was successful, update the base object to include it.
           // if the generation of the object was successful, update the base object to include it.
@@ -1889,9 +1889,9 @@ function chado_query_range($query, $args, $from, $count) {
  *
  *
  * @param $sql
  * @param $sql
  *   The sql statement to execute
  *   The sql statement to execute
- *   
+ *
  * @param $args
  * @param $args
- *   The array of arguments, with the same structure as passed to 
+ *   The array of arguments, with the same structure as passed to
  *   the db_query() function of Drupal.
  *   the db_query() function of Drupal.
  *
  *
  * @return
  * @return
@@ -1900,9 +1900,14 @@ function chado_query_range($query, $args, $from, $count) {
  * @ingroup tripal_chado_api
  * @ingroup tripal_chado_api
  */
  */
 function chado_query($sql, $args = array()) {
 function chado_query($sql, $args = array()) {
-  
+
   $is_local = tripal_core_is_chado_local();
   $is_local = tripal_core_is_chado_local();
-    
+
+  if (!is_array($args)) {
+    watchdog('tripal_core', 'Need to pass an array to chado_query, ":value" passed instead. Query: :query', array(':value' => $args, ':query' => $sql), WATCHDOG_ERROR);
+    $args = array($args);
+  }
+
   // if Chado is local to the database then prefix the Chado table
   // if Chado is local to the database then prefix the Chado table
   // names with 'chado'.
   // names with 'chado'.
   if ($is_local) {
   if ($is_local) {
@@ -1910,14 +1915,14 @@ function chado_query($sql, $args = array()) {
     $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
     $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
     $results = db_query($sql, $args);
     $results = db_query($sql, $args);
   }
   }
-  // if Chado is not local to the Drupal database then we have to 
+  // if Chado is not local to the Drupal database then we have to
   // switch to another database
   // switch to another database
   else {
   else {
     $previous_db = tripal_db_set_active('chado') ;
     $previous_db = tripal_db_set_active('chado') ;
     $results = db_query($sql);
     $results = db_query($sql);
     tripal_db_set_active($previous_db);
     tripal_db_set_active($previous_db);
   }
   }
-  
+
   return $results;
   return $results;
 }
 }
 
 
@@ -1931,13 +1936,13 @@ function chado_query($sql, $args = array()) {
  *
  *
  * @param $table
  * @param $table
  * @param $nid
  * @param $nid
- * 
+ *
  * @ingroup tripal_chado_api
  * @ingroup tripal_chado_api
  */
  */
 function chado_get_id_for_node($table, $nid) {
 function chado_get_id_for_node($table, $nid) {
   $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
   $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
   $result = db_query($sql, array(':nid' => $nid))->fetchObject();
   $result = db_query($sql, array(':nid' => $nid))->fetchObject();
-  
+
   return $result->id;
   return $result->id;
 }
 }
 
 
@@ -2039,7 +2044,7 @@ $cv_name, $value, $update_if_present = 0) {
   if (!is_array($props) and $props) {
   if (!is_array($props) and $props) {
     $props = array($props);
     $props = array($props);
   }
   }
-  
+
   $rank = 0;
   $rank = 0;
   if (count($props) > 0) {
   if (count($props) > 0) {
     if ($update_if_present) {
     if ($update_if_present) {
@@ -2053,14 +2058,14 @@ $cv_name, $value, $update_if_present = 0) {
         if ($p->rank > $rank) {
         if ($p->rank > $rank) {
           $rank = $p->rank;
           $rank = $p->rank;
         }
         }
-        if (strcmp($p->value, $value) == 0) {          
+        if (strcmp($p->value, $value) == 0) {
           return TRUE;
           return TRUE;
         }
         }
       }
       }
       // now add 1 to the rank
       // now add 1 to the rank
       $rank++;
       $rank++;
     }
     }
-  } 
+  }
 
 
   // make sure the cvterm exists.  Otherwise we'll get an error with
   // make sure the cvterm exists.  Otherwise we'll get an error with
   // prepared statements not matching
   // prepared statements not matching
@@ -2068,17 +2073,17 @@ $cv_name, $value, $update_if_present = 0) {
     'cv_id' => array(
     'cv_id' => array(
       'name' => $cv_name,
       'name' => $cv_name,
     ),
     ),
-    'name' => $property,    
+    'name' => $property,
   );
   );
-  
+
   $options = array('statement_name' => 'sel_cvterm_cv');
   $options = array('statement_name' => 'sel_cvterm_cv');
-  $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);  
+  $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
   if (!$term or count($term) == 0) {
   if (!$term or count($term) == 0) {
     watchdog('tripal_core', "Cannot find property '%prop_name'.",
     watchdog('tripal_core', "Cannot find property '%prop_name'.",
       array('%prop_name' => $property), WATCHDOG_ERROR);
       array('%prop_name' => $property), WATCHDOG_ERROR);
     return FALSE;
     return FALSE;
   }
   }
-  
+
   // get the foreign key for this property table
   // get the foreign key for this property table
   $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
   $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
   $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
   $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
@@ -2385,16 +2390,16 @@ function tripal_get_chado_custom_schema($table) {
  */
  */
 function chado_table_exists($table) {
 function chado_table_exists($table) {
   global $databases;
   global $databases;
-  
+
   $default_db = $databases['default']['default']['database'];
   $default_db = $databases['default']['default']['database'];
 
 
   $sql = "
   $sql = "
     SELECT 1
     SELECT 1
-    FROM information_schema.tables 
-    WHERE 
-      table_name = :table_name AND 
-      table_schema = 'chado' AND 
-      table_catalog = '$default_db' 
+    FROM information_schema.tables
+    WHERE
+      table_name = :table_name AND
+      table_schema = 'chado' AND
+      table_catalog = '$default_db'
   ";
   ";
   $results = db_query($sql, array(':table_name' => $table));
   $results = db_query($sql, array(':table_name' => $table));
   $exists = $results->fetchObject();
   $exists = $results->fetchObject();
@@ -2412,13 +2417,13 @@ function chado_table_exists($table) {
 function tripal_core_chado_schema_exists() {
 function tripal_core_chado_schema_exists() {
 
 
   $exists = variable_get('chado_schema_exists', FALSE);
   $exists = variable_get('chado_schema_exists', FALSE);
-  
+
   if (!$exists) {
   if (!$exists) {
     // This is postgresql-specific code to check the existence of the chado schema
     // This is postgresql-specific code to check the existence of the chado schema
     // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
     // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
     $sql = "
     $sql = "
-      SELECT nspname 
-      FROM pg_catalog.pg_namespace 
+      SELECT nspname
+      FROM pg_catalog.pg_namespace
       WHERE nspname = 'chado'
       WHERE nspname = 'chado'
     ";
     ";
     $results = db_query($sql);
     $results = db_query($sql);
@@ -2450,8 +2455,8 @@ function tripal_core_schema_exists($schema) {
   $sql = "
   $sql = "
     SELECT nspname
     SELECT nspname
     FROM pg_namespace
     FROM pg_namespace
-    WHERE 
-      has_schema_privilege(nspname, 'USAGE') AND 
+    WHERE
+      has_schema_privilege(nspname, 'USAGE') AND
       nspname = :nspname
       nspname = :nspname
     ORDER BY nspname
     ORDER BY nspname
   ";
   ";
@@ -2523,14 +2528,14 @@ function tripal_core_get_chado_tables($include_custom = NULL) {
  */
  */
 function tripal_core_set_chado_version() {
 function tripal_core_set_chado_version() {
   global $databases;
   global $databases;
-  $is_local = 0;  
+  $is_local = 0;
 
 
   // check that Chado is installed if not return 'uninstalled as the version'
   // check that Chado is installed if not return 'uninstalled as the version'
   $chado_exists = tripal_core_chado_schema_exists();
   $chado_exists = tripal_core_chado_schema_exists();
-  if (!$chado_exists) {    
+  if (!$chado_exists) {
     // if it's not in the drupal database check to see if it's specified in the $db_url
     // if it's not in the drupal database check to see if it's specified in the $db_url
-    // in the settings.php    
-    if (!array_key_exists('chado', $databases)) {      
+    // in the settings.php
+    if (!array_key_exists('chado', $databases)) {
       // if it's not in the drupal database or specified in the $db_url then
       // if it's not in the drupal database or specified in the $db_url then
       // return uninstalled as the version
       // return uninstalled as the version
       return 'not installed';
       return 'not installed';
@@ -2543,7 +2548,7 @@ function tripal_core_set_chado_version() {
     $is_local = 1;
     $is_local = 1;
     $prop_exists = db_table_exists('chado.chadoprop');
     $prop_exists = db_table_exists('chado.chadoprop');
   }
   }
-  
+
   // if the table doesn't exist then we don't know what version but we know
   // if the table doesn't exist then we don't know what version but we know
   // it must be 1.11 or older.
   // it must be 1.11 or older.
   if (!$prop_exists) {
   if (!$prop_exists) {
@@ -2551,10 +2556,10 @@ function tripal_core_set_chado_version() {
   }
   }
 
 
   $sql = "
   $sql = "
-    SELECT value 
-    FROM {chadoprop} CP 
-      INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id 
-      INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id 
+    SELECT value
+    FROM {chadoprop} CP
+      INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
+      INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
     WHERE CV.name = 'chado_properties' and CVT.name = 'version'
     WHERE CV.name = 'chado_properties' and CVT.name = 'version'
   ";
   ";
   if (!$is_local) {
   if (!$is_local) {
@@ -2687,7 +2692,7 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
   print "Getting nodes\n";
   print "Getting nodes\n";
   $nodes = array();
   $nodes = array();
   $res = db_query($dsql);
   $res = db_query($dsql);
-  foreach ($res as $node) {  
+  foreach ($res as $node) {
     $nodes[$count] = $node;
     $nodes[$count] = $node;
     $count++;
     $count++;
   }
   }
@@ -2849,7 +2854,7 @@ function tripal_core_is_tripal_node_type($chado_table) {
  */
  */
 function tripal_db_set_active($dbname  = 'default') {
 function tripal_db_set_active($dbname  = 'default') {
   global $databases, $active_db;
   global $databases, $active_db;
-  
+
   $chado_exists = variable_get('chado_schema_exists', FALSE);
   $chado_exists = variable_get('chado_schema_exists', FALSE);
   if ($chado_exists) {
   if ($chado_exists) {
     if ($dbname == 'chado') {
     if ($dbname == 'chado') {

+ 58 - 56
tripal_cv/includes/tripal_cv_admin.inc

@@ -68,8 +68,9 @@ function tripal_ajax_cv_edit() {
  */
  */
 function tripal_cv_edit_form(&$form_state = NULL, $cvid = NULL) {
 function tripal_cv_edit_form(&$form_state = NULL, $cvid = NULL) {
 
 
-  $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
-  $cv = db_fetch_object(chado_query($sql, $cvid));
+  $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id ";
+  $cv = chado_query($sql, array(':cv_id' => $cvid));
+  $cv = $cv->fetch_object();
 
 
   // set the default values.  If there is a value set in the
   // set the default values.  If there is a value set in the
   // form_state then let's use that, otherwise, we'll pull
   // form_state then let's use that, otherwise, we'll pull
@@ -141,11 +142,11 @@ function tripal_cv_edit_form_submit($form, &$form_state) {
   if (strcmp($op, 'Update') == 0) {
   if (strcmp($op, 'Update') == 0) {
     $sql = "
     $sql = "
        UPDATE {cv} SET
        UPDATE {cv} SET
-         name = '%s',
-         definition = '%s'
-       WHERE cv_id = %d
+         name = :name,
+         definition = :description
+       WHERE cv_id = :cv_id
     ";
     ";
-    $db = chado_query($sql, $name, $desc, $cvid);
+    $db = chado_query($sql, array(':name' => $name, ':description' => $desc, ':cv_id' => $cvid));
     if ($db) {
     if ($db) {
       drupal_set_message(t("Controlled vocabulary updated"));
       drupal_set_message(t("Controlled vocabulary updated"));
     }
     }
@@ -156,9 +157,9 @@ function tripal_cv_edit_form_submit($form, &$form_state) {
   if (strcmp($op, 'Delete')==0) {
   if (strcmp($op, 'Delete')==0) {
     $sql = "
     $sql = "
        DELETE FROM {cv}
        DELETE FROM {cv}
-       WHERE cv_id = %d
+       WHERE cv_id = :cv_id
     ";
     ";
-    $db = chado_query($sql, $cvid);
+    $db = chado_query($sql, array(':cv_id' => $cvid));
     if ($db) {
     if ($db) {
       drupal_set_message(t("Controlled vocabulary deleted"));
       drupal_set_message(t("Controlled vocabulary deleted"));
     }
     }
@@ -225,9 +226,9 @@ function tripal_cv_add_form_submit($form, &$form_state) {
     INSERT INTO {cv}
     INSERT INTO {cv}
      (name,definition)
      (name,definition)
     VALUES
     VALUES
-     ('%s','%s')
+     (:name, :description)
   ";
   ";
-  $db = chado_query($sql, $name, $desc);
+  $db = chado_query($sql, array(':name' => $name, ':description' => $desc));
   if ($db) {
   if ($db) {
     drupal_set_message(t("Controlled vocabulary added"));
     drupal_set_message(t("Controlled vocabulary added"));
   }
   }
@@ -245,19 +246,19 @@ function tripal_cv_add_form_submit($form, &$form_state) {
  */
  */
 function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
 function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
   tripal_core_ahah_init_form();
   tripal_core_ahah_init_form();
-  
+
   $form = array();
   $form = array();
 
 
-  // get defaults  
+  // get defaults
   $cv_id = $form_state['values']['cv_id'] ? $form_state['values']['cv_id'] : FALSE;
   $cv_id = $form_state['values']['cv_id'] ? $form_state['values']['cv_id'] : FALSE;
   $name  = $form_state['values']['name'] ? $form_state['values']['name'] : '';
   $name  = $form_state['values']['name'] ? $form_state['values']['name'] : '';
-  
+
   // if we have a cv_id and a term name then get the rest of the term details
   // if we have a cv_id and a term name then get the rest of the term details
   if ($cv_id and $name) {
   if ($cv_id and $name) {
      $values = array(
      $values = array(
        'cv_id' => $cv_id,
        'cv_id' => $cv_id,
        'name' => $name,
        'name' => $name,
-     );     
+     );
      $results = tripal_core_chado_select('cvterm', array('*'), $values);
      $results = tripal_core_chado_select('cvterm', array('*'), $values);
      if (!$results or count($results) == 0) {
      if (!$results or count($results) == 0) {
        // we can't find the cvterm so reset the name to blank
        // we can't find the cvterm so reset the name to blank
@@ -268,7 +269,7 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
        $definition = $cvterm->definition;
        $definition = $cvterm->definition;
        $is_relationshiptype = $cvterm->is_relationshiptype;
        $is_relationshiptype = $cvterm->is_relationshiptype;
        $is_obsolete = $cvterm->is_obsolete;
        $is_obsolete = $cvterm->is_obsolete;
-       
+
        // now get the database
        // now get the database
        $values = array('dbxref_id' => $cvterm->dbxref_id);
        $values = array('dbxref_id' => $cvterm->dbxref_id);
        $results = tripal_core_chado_select('dbxref', array('*'), $values);
        $results = tripal_core_chado_select('dbxref', array('*'), $values);
@@ -277,7 +278,7 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
        $db_id = $dbxref->db_id;
        $db_id = $dbxref->db_id;
      }
      }
   }
   }
-  
+
   $values = array();
   $values = array();
   $columns = array('cv_id', 'name');
   $columns = array('cv_id', 'name');
   $options = array('order_by' => array('name' => 'ASC'));
   $options = array('order_by' => array('name' => 'ASC'));
@@ -290,14 +291,14 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
 
 
   $form['wrapper-top'] = array(
   $form['wrapper-top'] = array(
     '#type' => 'markup',
     '#type' => 'markup',
-    '#value' => '<div id="cvterm-form">', 
+    '#value' => '<div id="cvterm-form">',
   );
   );
-  
+
   $form['form_action'] = array(
   $form['form_action'] = array(
     '#type' => 'hidden',
     '#type' => 'hidden',
-    '#value' => $action, 
-  );   
-  
+    '#value' => $action,
+  );
+
   $form['cv_id'] = array(
   $form['cv_id'] = array(
     '#title' => t('Controlled Vocabulary (Ontology) Name'),
     '#title' => t('Controlled Vocabulary (Ontology) Name'),
     '#type' => 'select',
     '#type' => 'select',
@@ -309,9 +310,9 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
        'wrapper' => 'cvterm-form',
        'wrapper' => 'cvterm-form',
        'event'   => 'change',
        'event'   => 'change',
        'method'  => 'replace',
        'method'  => 'replace',
-    ),    
+    ),
   );
   );
-  
+
   if ($cv_id) {
   if ($cv_id) {
     $form['add_cvterm'] = array(
     $form['add_cvterm'] = array(
       '#type'           => 'fieldset',
       '#type'           => 'fieldset',
@@ -334,7 +335,7 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
       if ($name) {
       if ($name) {
         $form['add_cvterm']['name']['#attributes'] = array('readonly' => 'readonly');
         $form['add_cvterm']['name']['#attributes'] = array('readonly' => 'readonly');
         $form['add_cvterm']['name']['#description'] = 'The term name cannot be changed. If the name is incorrect, please create a new term and make this one as obsolete.';
         $form['add_cvterm']['name']['#description'] = 'The term name cannot be changed. If the name is incorrect, please create a new term and make this one as obsolete.';
-      } 
+      }
       else {
       else {
         $form['add_cvterm']['name']['#autocomplete_path'] = "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id";
         $form['add_cvterm']['name']['#autocomplete_path'] = "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id";
         $form['add_cvterm']['name']['#ahah'] = array(
         $form['add_cvterm']['name']['#ahah'] = array(
@@ -343,30 +344,30 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
            'method'  => 'replace',
            'method'  => 'replace',
         );
         );
       }
       }
-    }   
+    }
+
+    if ($action == 'add' or $name) {
+
 
 
-    if ($action == 'add' or $name) { 
-      
-      
       $form['add_cvterm']['definition']= array(
       $form['add_cvterm']['definition']= array(
         '#type'          => 'textarea',
         '#type'          => 'textarea',
         '#title'         => t('Description'),
         '#title'         => t('Description'),
         '#description'   => t('Please enter a description for this term'),
         '#description'   => t('Please enter a description for this term'),
         '#default_value' => $definition,
         '#default_value' => $definition,
       );
       );
-  
+
       $form['add_cvterm']['is_relationshiptype'] = array(
       $form['add_cvterm']['is_relationshiptype'] = array(
         '#type'          => 'checkbox',
         '#type'          => 'checkbox',
         '#title'         => t('This term describes a relationship?'),
         '#title'         => t('This term describes a relationship?'),
         '#default_value' => $is_relationshiptype,
         '#default_value' => $is_relationshiptype,
       );
       );
-  
+
       $form['add_cvterm']['is_obsolete'] = array(
       $form['add_cvterm']['is_obsolete'] = array(
         '#type'          => 'checkbox',
         '#type'          => 'checkbox',
         '#title'         => t('This term is obsolete?'),
         '#title'         => t('This term is obsolete?'),
         '#default_value' => $is_obsolete,
         '#default_value' => $is_obsolete,
       );
       );
-  
+
       $values = array();
       $values = array();
       $columns = array('db_id', 'name');
       $columns = array('db_id', 'name');
       $options = array('order_by' => array('name' => 'ASC'));
       $options = array('order_by' => array('name' => 'ASC'));
@@ -391,14 +392,14 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
         $form['add_cvterm']['db_id']['#disabled'] = TRUE;
         $form['add_cvterm']['db_id']['#disabled'] = TRUE;
         $form['add_cvterm']['db_id']['#description'] = 'The database to which this term belongs cannot be changed.';
         $form['add_cvterm']['db_id']['#description'] = 'The database to which this term belongs cannot be changed.';
       }
       }
-      
+
       $form['add_cvterm']['accession']= array(
       $form['add_cvterm']['accession']= array(
         '#type'          => 'textfield',
         '#type'          => 'textfield',
         '#title'         => t("Accession"),
         '#title'         => t("Accession"),
-        '#description'   => t('If this term has an existing accession (unique identifier) in the database 
+        '#description'   => t('If this term has an existing accession (unique identifier) in the database
            please enter that here.  If the accession is numeric with a database prefix (e.g. GO:003023), please
            please enter that here.  If the accession is numeric with a database prefix (e.g. GO:003023), please
-           enter just the numeric value.  The database prefix will be appended whenever the term is displayed. 
-           If the accession is not numeric then enter it as is.  If no value is provied, the term name 
+           enter just the numeric value.  The database prefix will be appended whenever the term is displayed.
+           If the accession is not numeric then enter it as is.  If no value is provied, the term name
            provided above will be used as the accession.'),
            provided above will be used as the accession.'),
         '#required'      => FALSE,
         '#required'      => FALSE,
         '#default_value' => $accession,
         '#default_value' => $accession,
@@ -417,8 +418,8 @@ function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
       );
       );
     } // end if name selected (or action == 'add')
     } // end if name selected (or action == 'add')
   } //end of if cv selected
   } //end of if cv selected
-    
-  
+
+
   $form['wrapper-bottom'] = array(
   $form['wrapper-bottom'] = array(
     '#type' => 'markup',
     '#type' => 'markup',
     '#value' => '</div>',
     '#value' => '</div>',
@@ -453,7 +454,7 @@ function tripal_cv_cvterm_form_submit($form, &$form_state) {
   if (!empty($form_state['ahah_submission'])) {
   if (!empty($form_state['ahah_submission'])) {
     return;
     return;
   }
   }
-    
+
   // get the database
   // get the database
   $values = array('db_id' => $form_state['values']['db_id']);
   $values = array('db_id' => $form_state['values']['db_id']);
   $results = tripal_core_chado_select('db', array('name'), $values);
   $results = tripal_core_chado_select('db', array('name'), $values);
@@ -462,7 +463,7 @@ function tripal_cv_cvterm_form_submit($form, &$form_state) {
     return;
     return;
   }
   }
   $db = $results[0];
   $db = $results[0];
-  
+
   // get the cv
   // get the cv
   $values = array('cv_id' => $form_state['values']['cv_id']);
   $values = array('cv_id' => $form_state['values']['cv_id']);
   $results = tripal_core_chado_select('cv', array('name'), $values);
   $results = tripal_core_chado_select('cv', array('name'), $values);
@@ -471,31 +472,31 @@ function tripal_cv_cvterm_form_submit($form, &$form_state) {
     return;
     return;
   }
   }
   $cv = $results[0];
   $cv = $results[0];
-  
+
   // get the accession for this term
   // get the accession for this term
   $accession = $form_state['values']['accession'];
   $accession = $form_state['values']['accession'];
   if (!$accession) {
   if (!$accession) {
     $accession = $form_state['values']['name'];
     $accession = $form_state['values']['name'];
-  }  
+  }
   if (is_numeric($accession)) {
   if (is_numeric($accession)) {
     $accession = $db->name . ":" . $accession;
     $accession = $db->name . ":" . $accession;
   }
   }
-  
-  
+
+
   $update = 0;
   $update = 0;
   if ($form_state['values']['form_action'] == 'edit') {
   if ($form_state['values']['form_action'] == 'edit') {
     $update = 1;
     $update = 1;
   }
   }
-  
+
   // now add the term
   // now add the term
-  $term = array(    
+  $term = array(
     'name' => $form_state['values']['name'],
     'name' => $form_state['values']['name'],
     'namespace' => $cv->name,
     'namespace' => $cv->name,
     'id' => $accession,
     'id' => $accession,
     'def' => $form_state['values']['definition'],
     'def' => $form_state['values']['definition'],
     'is_obsolete' => $form_state['values']['is_obsolete'],
     'is_obsolete' => $form_state['values']['is_obsolete'],
-  );  
-  
+  );
+
   $is_relationship = $form_state['values']['is_relationshiptype'];
   $is_relationship = $form_state['values']['is_relationshiptype'];
   $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, $update, $db->name);
   $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, $update, $db->name);
   if ($cvterm) {
   if ($cvterm) {
@@ -505,9 +506,9 @@ function tripal_cv_cvterm_form_submit($form, &$form_state) {
     else {
     else {
       drupal_set_message('Term updated successfully.');
       drupal_set_message('Term updated successfully.');
     }
     }
-  } 
+  }
   else {
   else {
-    drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');  
+    drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
   }
   }
 
 
 }
 }
@@ -523,20 +524,20 @@ function tripal_cv_cvterm_callback() {
   $status = TRUE;
   $status = TRUE;
 
 
   // prepare and render the form
   // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();   
-  $data = drupal_render($form);  
+  $form = tripal_core_ahah_prepare_form();
+  $data = drupal_render($form);
 
 
-  // bind javascript events to the new objects that will be returned 
+  // bind javascript events to the new objects that will be returned
   // so that AHAH enabled elements will work.
   // so that AHAH enabled elements will work.
   $settings = tripal_core_ahah_bind_events();
   $settings = tripal_core_ahah_bind_events();
-   
+
   // return the updated JSON
   // return the updated JSON
   drupal_json(
   drupal_json(
     array(
     array(
-      'status'   => $status, 
+      'status'   => $status,
       'data'     => $data,
       'data'     => $data,
       'settings' => $settings,
       'settings' => $settings,
-    )  
+    )
   );
   );
 }
 }
 
 
@@ -551,7 +552,8 @@ function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
   $cvid =  $form_state['values']['cvid'];
   $cvid =  $form_state['values']['cvid'];
 
 
   // first get the controlled vocabulary name:
   // first get the controlled vocabulary name:
-  $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
+  $cv = chado_query("SELECT * FROM {cv} WHERE cv_id = :cv_id", array(':cv_id' => $cvid));
+  $cv = $cv->fetch_object();
 
 
   // Submit a job to update cvtermpath
   // Submit a job to update cvtermpath
   $job_args = array($cvid);
   $job_args = array($cvid);