Browse Source

Chado installer is fully functional in Drupla 7

spficklin 11 years ago
parent
commit
9530be9e77

+ 3 - 3
tripal_core/api/get_FKs.php

@@ -53,7 +53,7 @@ function get_chado_fk_relationships($version) {
         information_schema.table_constraints AS tc 
         JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
         JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
-    WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='%s'
+    WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name=:table_name
   ";
     
   // iterate through the tables and get the foreign keys
@@ -108,8 +108,8 @@ function get_chado_fk_relationships($version) {
     $table_arr['table'] = $table;
     
     // get the foreign keys and add them to the array
-    $fks = db_query($sql,$table);
-    while ($fk = db_fetch_object($fks)) {
+    $fks = db_query($sql, array(':table_name' => $table));
+    foreach ($fks as $fk) {
       $table_arr['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name;
       $table_arr['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name;
       $reffering[$fk->foreign_table_name][] = $table;

+ 82 - 215
tripal_core/api/tripal_core_chado.api.inc

@@ -290,7 +290,7 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   $i = 1;
   foreach ($insert_values as $field => $value) {
     $ifields[] = $field;
-    $ivalues[] = $value;
+    $ivalues[":$field"] = $value;
     $iplaceholders[] = '$' . $i;
     $i++;
     if (strcmp($value, '__NULL__')==0) {
@@ -351,7 +351,8 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
         $is_prepared = tripal_core_chado_prepare("currval_" . $table . "_" . $field, $psql, array());
         $value = '';
         if ($is_prepared) {
-           $value = db_result(chado_query("EXECUTE currval_" . $table . "_" . $field));
+           $results = db_result(chado_query("EXECUTE currval_" . $table . "_" . $field));
+           $value = $results->fetchObject();
            if (!$value) {
             watchdog('tripal_core', "tripal_core_chado_insert: not able to retrieve primary key after insert: %sql",
               array('%sql' => $psql), WATCHDOG_ERROR);
@@ -360,7 +361,8 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
         }
         else {
           $sql = "SELECT CURRVAL('{" . $table . "_" . $field . "_seq}')";
-          $value =  db_result(chado_query($sql));
+          $results =  db_result(chado_query($sql));
+          $value = $results->fetchObject();
           if (!$value) {
             watchdog('tripal_core', "tripal_core_chado_insert: not able to retrieve primary key after insert: %sql",
               array('%sql' => $sql), WATCHDOG_ERROR);
@@ -1543,7 +1545,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
 
   // format results into an array
   $results = array();
-  while ($r = db_fetch_object($resource)) {
+  foreach ($resource as $r) {
     $results[] = $r;
   }
   if ($options['has_record']) {
@@ -1880,9 +1882,12 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
       if (db_table_exists('chado_' . $table)) {
         // that has a foreign key to this one ($table_desc['primary key'][0]
         // and to the node table (nid)
-        $sql = "SELECT %s, nid FROM {chado_%s} WHERE %s=%d";
-        $mapping = db_fetch_object(db_query($sql, $table_primary_key, $table,
-          $table_primary_key, $object->{$table_primary_key}));
+        $sql = "
+          SELECT $table_primary_key, nid 
+          FROM {chado_$table} 
+          WHERE $table_primary_key = :$table_primary_key";
+        $results = db_query($sql, array(":$table_primary_key" . $object->{$table_primary_key}));
+        $mapping = $results->fetchObject();
         if ($mapping->{$table_primary_key}) {
           $object->nid = $mapping->nid;
           $object->expandable_nodes[] = $table;
@@ -2387,6 +2392,8 @@ function chado_pager_query($query, $limit, $element, $count_query) {
  * @param $sql
  *   The SQL statement to execute, this is followed by a variable number of args
  *   used as substitution values in the SQL statement.
+ * @param $args
+ *   The SQL arguments
  * @param $from
  *   The first result row to return..
  * @param $count
@@ -2398,16 +2405,8 @@ function chado_pager_query($query, $limit, $element, $count_query) {
  *
  * @ingroup tripal_chado_api
  */
-function chado_query_range($query) {
-  $args = func_get_args();
-  $count = array_pop($args);
-  $from = array_pop($args);
-  array_shift($args);
-
+function chado_query_range($query, $args, $from, $count) {
 
-  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
-    $args = $args[0];
-  }
   $query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;
 
   return chado_query($query, $args);
@@ -2431,8 +2430,9 @@ function chado_query_range($query) {
  * @ingroup tripal_chado_api
  */
 function chado_query($sql, $args = array()) {
-  global $_tripal_core_persistent_chado;
-  
+	
+  $is_local = tripal_core_is_chado_local();
+    
   // if Chado is local to the database then prefix the Chado table
   // names with 'chado'.
   if ($is_local) {
@@ -2443,9 +2443,9 @@ function chado_query($sql, $args = array()) {
   // if Chado is not local to the Drupal database then we have to 
   // switch to another database
   else {
-    $previous_db = tripal_db_set_active('chado') ;
-    $results = _db_query($sql);
-    tripal_db_set_active($previous_db);
+    $previous_db = db_set_active('chado') ;
+    $results = db_query($sql);
+    db_set_active($previous_db);
   }
   
   return $results;
@@ -2462,7 +2462,9 @@ function chado_query($sql, $args = array()) {
  * @ingroup tripal_chado_api
  */
 function chado_get_id_for_node($table, $node) {
-  return db_result(db_query("SELECT %s_id FROM {chado_%s} WHERE nid = %d", $table, $table, $node->nid));
+	$sql = "SELECT " . $table . "_id FROM {chado_$table} WHERE nid = :nid";
+	$results = db_query($sql, array(':nid' => $node->nid));
+	return $results->fetchObject();
 }
 
 /**
@@ -2476,7 +2478,9 @@ function chado_get_id_for_node($table, $node) {
  *  @ingroup tripal_chado_api
  */
 function chado_get_node_id($table, $id) {
-  return db_result(db_query("SELECT nid FROM {chado_%s} WHERE %s_id = %d", $table, $table, $id));
+	$sql = "SELECT nid FROM {chado_$table} WHERE " . $table . "_id = :" . $table . "_id";
+  $results = db_query($sql, array(":" . $table . "_id" => $id));
+  return $results->fetchObject();
 }
 
 /**
@@ -2808,101 +2812,6 @@ function tripal_core_delete_property_by_id($basetable, $record_id) {
   return tripal_core_chado_delete($basetable . 'prop', $match);
 }
 
-
-
-
-
-/**
- * Set the Tripal Database
- *
- * The tripal_db_set_active function is used to prevent namespace collisions
- * when chado and drupal are installed in the same database but in different
- * schemas.  It is also used for backwards compatibility with older versions
- * of tripal or in cases where chado is located outside of the Drupal database.
- *
- * @ingroup tripal_chado_api
- */
-function tripal_db_set_active($dbname  = 'default') {
-  global $db_url, $db_type, $active_db;
-  $chado_exists = 0;
-
-  // only postgres can support search paths.  So if this is MysQL then
-  // just run the normal tripal_db_set_active function.
-  if (strcmp($db_type, 'pgsql')==0) {
-
-    // if the 'chado' database is in the $db_url variable then chado is
-    // not in the same Drupal database, so we don't need to set any
-    // search_path and can just change the database
-    if (is_array($db_url)) {
-      if (isset($db_url[$dbname])) {
-        return db_set_active($dbname);
-      }
-    }
-
-    // if this is the default database then set the search path and return
-    if (strcmp($dbname, 'default')==0) {
-      return db_set_active($dbname);
-    }
-    // if the user requests a database other than the default
-    // then we need to try and set the chado search path.  This
-    // only works if Chado is local to the Drpual database. If it
-    // fails then do nothing.
-    else {
-      if (tripal_db_set_chado_search_path($dbname)) {
-         // if the chado schema is local to Drupal then
-         // just return the active database.
-         return 'default';
-      }
-      else {
-        watchdog('tripal_core', "Cannot set 'search_path' variable for Postgres to %dbname",
-          array('%dbname' => $dbname), WATCHDOG_ERROR);
-      }
-    }
-  }
-  // a non postgres database
-  else {
-    return db_set_active($dbname);
-  }
-}
-
-/**
- * Gets the current search_path for PostgreSQL
- *
- * @ingroup tripal_chado_api
- */
-function tripal_db_get_search_path() {
-  $path = db_fetch_object(db_query("show search_path"));
-  return $path->search_path;
-}
-
-/**
- * Set the chado search_path for PostgreSQL
- *
- * Sets the database search_path for postgreSQL to the
- * chado schema.
- *
- * @ingroup tripal_chado_api
- */
-function tripal_db_set_chado_search_path($dbname) {
-
-  // check to make sure the chado schema exists
-  $chado_exists = variable_get('chado_schema_exists', FALSE);
-  if (!$chado_exists) {
-    $chado_exists = tripal_core_chado_schema_exists();
-  }
-
-  // here we make the assumption that the default database schema is
-  // 'public'.  This will most likely always be the case but if not,
-  // then this code will break
-  if ($chado_exists) {
-    db_query('set search_path to %s', "$dbname,public");
-    return TRUE;
-  }
-  else {
-    return FALSE;
-  }
-}
-
 /**
  * Indicates if the SQL statement is prepapred
  *
@@ -2926,9 +2835,9 @@ function tripal_core_is_sql_prepared($statement_name) {
   }
 
   // @coder-ignore: acting on postgres tables rather then drupal schema therefore, table prefixing does not apply
-  $sql = "SELECT name FROM pg_prepared_statements WHERE name = '%s'";
-  // do not use 'chado_query' here or it causes memory-leaks
-  $result = db_fetch_object(db_query($sql, $statement_name));
+  $sql = "SELECT name FROM pg_prepared_statements WHERE name = :name";
+  $results = db_query($sql, array(':name' => $statement_name));
+  $result = $results->fetchObject();
 
   if ($result) {
     return TRUE;
@@ -3093,11 +3002,12 @@ function tripal_core_chado_clear_prepared($statement_name_regex = NULL) {
   global $_tripal_core_prepared_statements;
 
   if ($statement_name_regex) {
-    $resource = chado_query("SELECT * FROM pg_catalog.pg_prepared_statements WHERE name~'%s'", $statement_name_regex);
-    while ($r = db_fetch_object($resource)) {
+  	$sql = "SELECT * FROM pg_catalog.pg_prepared_statements WHERE name~:name_regex";
+    $resource = chado_query($sql, array(':name_regex' => $statement_name_regex));
+    foreach ($resource as $r) {
       $k = array_search($r->name, $_tripal_core_prepared_statements);
       unset($_tripal_core_prepared_statements[$k]);
-      chado_query('DEALLOCATE PREPARE %s', $r->name);
+      chado_query('DEALLOCATE PREPARE :statement_name', array(':statement_name' => $r->name));
     }
   }
   else {
@@ -3177,9 +3087,9 @@ function tripal_db_start_transaction() {
 function tripal_db_set_savepoint_transaction($savepoint, $release = FALSE) {
   // Postgresql requires a savepoint of the same name to be unset before re-use
   if ($release) {
-    chado_query("RELEASE SAVEPOINT %s", $savepoint);
+    chado_query("RELEASE SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
   }
-  chado_query("SAVEPOINT %s", $savepoint);
+  chado_query("SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
 }
 
 /**
@@ -3209,7 +3119,7 @@ function tripal_db_commit_transaction() {
 function tripal_db_rollback_transaction($savepoint = NULL, $commit = TRUE) {
 
   if ($savepoint) {
-    chado_query("ROLLBACK TO SAVEPOINT %s", $savepoint);
+    chado_query("ROLLBACK TO SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
   }
   else {
     chado_query("ROLLBACK");
@@ -3221,58 +3131,6 @@ function tripal_db_rollback_transaction($savepoint = NULL, $commit = TRUE) {
 
 }
 
-/**
- * Purpose: Get max rank for a given set of criteria
- *   This function was developed with the many property tables in chado in mind
- *
- * @param $tablename
- *    The name of the chado table you want to select the max rank from this table must contain a
- *    rank column of type integer
- * @param $where_options
- *   where options should include the id and type for that table to correctly
- *     group a set of records together where the only difference are the value and rank
- * @code
- *  array(
- *     <column_name> => array(
- *        'type' => <type of column: INT/STRING>,
- *       'value' => <the value you want to filter on>,
- *      'exact' => <if TRUE use =; if FALSE use ~>,
- *    )
- *  )
- * @endcode
- * @return the maximum rank
- *
- * @ingroup tripal_chado_api
- */
-function tripal_get_max_chado_rank($tablename, $where_options) {
-  $where= array();
-
-  //generate the where clause from supplied options
-  // the key is the column name
-  foreach ($where_options as $key => $val_array) {
-    if (preg_match('/INT/', $val_array['type'])) {
-      $where[] = $key . "=" . $val_array['value'];
-    }
-    else {
-      if ($val_array['exact']) {
-        $operator='=';
-      }
-      else {
-        $operator='~';
-      }
-      $where[] = $key . $operator . "'" . $val_array['value'] . "'";
-    }
-  }
-  $sql = "SELECT max(rank) as max_rank, count(rank) as count FROM {%s} WHERE %s";
-  $result = db_fetch_object(chado_query($sql, $tablename,  implode(' AND ', $where)));
-  if ($result->count > 0) {
-    return $result->max_rank;
-  }
-  else {
-    return -1;
-  }
-}
-
 /**
  * Retrieves the schema in an array for the specified custom table.
  *
@@ -3287,8 +3145,9 @@ function tripal_get_max_chado_rank($tablename, $where_options) {
  */
 function tripal_get_chado_custom_schema($table) {
 
-  $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = '%s'";
-  $custom = db_fetch_object(db_query($sql, $table));
+  $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
+  $results = db_query($sql, array(':table_name' => $table));
+  $custom = $results->fetchObject();
   if (!$custom) {
     return FALSE;
   }
@@ -3306,7 +3165,7 @@ function tripal_get_chado_custom_schema($table) {
 function tripal_core_chado_schema_exists() {
 
   $exists = variable_get('chado_schema_exists', FALSE);
-
+  
   if (!$exists) {
     // 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
@@ -3398,7 +3257,7 @@ function tripal_core_get_chado_tables($include_custom = NULL) {
     $sql = "SELECT table_name FROM {tripal_custom_tables}";
     $resource = db_query($sql);
 
-    while ($r = db_fetch_object($resource)) {
+    foreach ($resource as $r) {
       $tables[$r->table_name] = $r->table_name;
     }
   }
@@ -3417,6 +3276,7 @@ function tripal_core_get_chado_tables($include_custom = NULL) {
  */
 function tripal_core_set_chado_version() {
   global $databases;
+  $is_local = 0;  
 
   // check that Chado is installed if not return 'uninstalled as the version'
   $chado_exists = tripal_core_chado_schema_exists();
@@ -3428,20 +3288,21 @@ function tripal_core_set_chado_version() {
       // return uninstalled as the version
       return 'not installed';
     }
+    $previous_db = db_set_active('chado');
+    $prop_exists = db_table_exists('chadoprop');
+    db_set_active($previous_db);
   }
-
+  else {
+  	$is_local = 1;
+  	$prop_exists = db_table_exists('chado.chadoprop');
+  }
+  
   // if the table doesn't exist then we don't know what version but we know
   // it must be 1.11 or older.
-  $previous_db = tripal_db_set_active('chado');
-  $prop_exists = db_table_exists('chadoprop');
-  tripal_db_set_active($previous_db);
   if (!$prop_exists) {
     return "1.11 or older";
   }
 
-  // we can't use the Tripal API to query this table
-  // because the Tripal API depends on this function to
-  // tell it the version. So, we need a typical SQL statement
   $sql = "
     SELECT value 
     FROM {chadoprop} CP 
@@ -3449,14 +3310,19 @@ function tripal_core_set_chado_version() {
       INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id 
     WHERE CV.name = 'chado_properties' and CVT.name = 'version'
   ";
-  $previous_db = tripal_db_set_active('chado');
-  $resultes = db_query($sql);
-  tripal_db_set_active($previous_db);
+  if (!$is_local) {
+	  $previous_db = db_set_active('chado');
+	  $results = db_query($sql);
+	  db_set_active($previous_db);
+  }
+  else {
+    $results = chado_query($sql);
+  }
   $v = $results->fetchObject();
 
   // if we don't have a version in the chadoprop table then it must be
   // v1.11 or older
-  if (!$v->value) {
+  if (!$v) {
     variable_set('chado_version', "1.11 or older");
     return "1.11 or older";
   }
@@ -3564,17 +3430,17 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
   $count = 0;
 
   // build the SQL statments needed to check if nodes point to valid analyses
-  $dsql = "SELECT * FROM {node} WHERE type = 'chado_%s' order by nid";
-  $nsql = "SELECT * FROM {node} WHERE nid = %d";
-  $csql = "SELECT * FROM {chado_%s} WHERE nid = %d ";
-  $clsql= "SELECT * FROM {chado_%s}";
-  $lsql = "SELECT * FROM {%s} where %s_id = %d ";
+  $dsql = "SELECT * FROM {node} WHERE type = 'chado_" . $table . "' order by nid";
+  $nsql = "SELECT * FROM {node} WHERE nid = :nid";
+  $csql = "SELECT * FROM {chado_" . $table . "} WHERE nid = :nid ";
+  $clsql= "SELECT * FROM {chado_" . $table . "}";
+  $lsql = "SELECT * FROM {" . $table . "} where " . $table . "_id = :" . $table . "_id ";
 
   // load into nodes array
   print "Getting nodes\n";
   $nodes = array();
-  $res = db_query($dsql, $table);
-  while ($node = db_fetch_object($res)) {
+  $res = db_query($dsql);
+  foreach ($res as $node) {  
     $nodes[$count] = $node;
     $count++;
   }
@@ -3582,8 +3448,8 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
   // load the chado_$table into an array
   print "Getting chado_$table\n";
   $cnodes = array();
-  $res = db_query($clsql, $table);
-  while ($node = db_fetch_object($res)) {
+  $res = db_query($clsql);
+  foreach ($res as $node) {
     $cnodes[$count] = $node;
     $count++;
   }
@@ -3604,20 +3470,23 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
     }
 
     // see if the node exits, if not remove the entry from the chado_$table table
-    $node = db_fetch_object(db_query($nsql, $nid->nid));
+    $results = db_query($nsql, array(':nid' => $nid->nid));
+    $node = $results->fetchObject();
     if (!$node) {
       $deleted++;
-      db_query("DELETE FROM {chado_%s} WHERE nid = %d", $table, $nid->nid);
+      db_query("DELETE FROM {chado_" . $table . "} WHERE nid = :nid", array(':nid' => $nid->nid));
       $message = "chado_$table missing node.... DELETING: $nid->nid";
       watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
     }
 
     // see if the record in chado exist, if not remove the entry from the chado_$table
     $table_id = $table . "_id";
-    $record = db_fetch_object(chado_query($lsql, $table, $table, $nid->$table_id));
+    $results = chado_query($lsql, array(":" . $table . "_id " => $nid->$table_id));
+    $record = $results->fetchObject();
     if (!$record) {
       $deleted++;
-      db_query("DELETE FROM {chado_%s} WHERE %s_id = '%d'", $table, $table, $nid->$table_id);
+      $sql = "DELETE FROM {chado_" . $table . "} WHERE " . $table . "_id = :" . $table . "_id";
+      db_query($sql, array(":" . $table . "_id" => $nid->$table_id));
       $message = "chado_$table missing $table.... DELETING entry.";
       watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
     }
@@ -3637,7 +3506,8 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
 
     // check to see if the node has a corresponding entry
     // in the chado_$table table. If not then delete the node.
-    $link = db_fetch_object(db_query($csql, $table, $node->nid));
+    $results = db_query($csql, array(":nid" => $node->nid));
+    $link = $results->fetchObject();
     if (!$link) {
       if (node_access('delete', $node)) {
         $deleted++;
@@ -3666,14 +3536,11 @@ function tripal_core_clean_orphaned_nodes($table, $job_id) {
  * @ingroup tripal_chado_api
  */
 function tripal_core_is_chado_installed() {
-  global $db_url, $db_type;
+  global $databases;
 
-  // first check if chado is in the db_url of the
-  // settings.php file
-  if (is_array($db_url)) {
-    if (isset($db_url['chado'])) {
-      return TRUE;
-    }
+  // first check if chado is in the $databases variable of the settings.php file
+  if (array_key_exists('chado', $databases)) {
+    return TRUE;
   }
 
   // check to make sure the chado schema exists

+ 17 - 15
tripal_core/api/tripal_core_custom_tables.api.inc

@@ -36,8 +36,9 @@ function tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_cr
   $record->schema = serialize($schema);
 
   // get the current custom table record
-  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
-  $custom_table = db_fetch_object(db_query($sql, $table_id));
+  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
+  $results = db_query($sql, array(':table_id' => $table_id));
+  $custom_table = $results->fetchObject();
   
   // if the user changed the table name, we want to drop the old one and force
   // creation of the new one.
@@ -107,20 +108,21 @@ function tripal_core_create_custom_table(&$ret, $table, $schema, $skip_creation
   $ret = array();
       
   // see if the table entry already exists in the tripal_custom_tables table.
-  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = '%s'";
-  $centry = db_fetch_object(db_query($sql, $table));
+  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
+  $results = db_query($sql, array(':table_name' => $table));
+  $centry = $results->fetchObject();
   
   // check to see if the table already exists in the chado schema  
-  $previous_db = tripal_db_set_active('chado');  // use chado database
+  $previous_db = db_set_active('chado');  // use chado database
   $exists = db_table_exists($table);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  db_set_active($previous_db);  // now use drupal database
 
   
   // if the table does not exist then create it
   if (!$exists) {
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $previous_db = db_set_active('chado');  // use chado database
     db_create_table($ret, $table, $schema);
-    tripal_db_set_active($previous_db);  // now use drupal database
+    db_set_active($previous_db);  // now use drupal database
     if (count($ret)==0) {
       watchdog('tripal_core', "Error adding custom table '!table_name'.",
         array('!table_name' => $table), WATCHDOG_ERROR);
@@ -133,10 +135,10 @@ function tripal_core_create_custom_table(&$ret, $table, $schema, $skip_creation
   if ($exists and is_object($centry) and !$skip_creation) {    
     
     // drop the table we'll recreate it with the new schema
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $previous_db = db_set_active('chado');  // use chado database
     db_drop_table($ret, $table);
     db_create_table($ret, $table, $schema);
-    tripal_db_set_active($previous_db);  // now use drupal database
+    db_set_active($previous_db);  // now use drupal database
   }
 
   // add an entry in the tripal_custom_table
@@ -146,8 +148,8 @@ function tripal_core_create_custom_table(&$ret, $table, $schema, $skip_creation
 
   // if an entry already exists then remove it
   if ($centry) {
-    $sql = "DELETE FROM {tripal_custom_tables} WHERE table_name = '%s'";
-    db_query($sql, $table);
+    $sql = "DELETE FROM {tripal_custom_tables} WHERE table_name = :table_name";
+    db_query($sql, array(':table_name' => $table));
   }
   $success = drupal_write_record('tripal_custom_tables', $record);
   if (!$success) {
@@ -193,10 +195,10 @@ function tripal_core_create_custom_table(&$ret, $table, $schema, $skip_creation
  * @ingroup tripal_custom_tables_api
  */
 function tripal_custom_tables_get_table_id($table_name) {
-  $sql = "SELECT * FROM {tripal_custom_tables} " .
-         "WHERE table_name = '%s'";
   if (db_table_exists('tripal_custom_tables')) {
-    $custom_table = db_fetch_object(db_query($sql, $table_name));
+    $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
+    $results = db_query($sql, array(':table_name' => $table_name));
+    $custom_table = $results->fetchObject();
     if ($custom_table) {
       return $custom_table->table_id;
     }

+ 2 - 2
tripal_core/api/tripal_core_jobs.api.inc

@@ -258,9 +258,9 @@ function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) {
   // they are processed in a FIFO manner.
   if ($job_id) {
     $sql =  "SELECT * FROM {tripal_jobs} TJ " .
-            "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = %d " .
+            "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = :job_id " .
             "ORDER BY priority ASC,job_id ASC";
-    $job_res = db_query($sql, $job_id);
+    $job_res = db_query($sql, array(':job_id', $job_id));
   }
   else {
     $sql =  "SELECT * FROM {tripal_jobs} TJ " .

+ 29 - 27
tripal_core/api/tripal_core_mviews.api.inc

@@ -78,12 +78,12 @@ function tripal_add_mview($name, $modulename, $mv_table, $mv_specs, $indexed,
   if (drupal_write_record('tripal_mviews', $record)) {
 
     // drop the table from chado if it exists
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $previous_db = db_set_active('chado');  // use chado database
     if (db_table_exists($mv_table)) {
       $sql = "DROP TABLE $mv_table";
       db_query($sql);
     }
-    tripal_db_set_active($previous_db);  // now use drupal database
+    db_set_active($previous_db);  // now use drupal database
 
     // now construct the indexes
     $index = '';
@@ -110,9 +110,9 @@ function tripal_add_mview($name, $modulename, $mv_table, $mv_specs, $indexed,
     else {
       // add the table to the database
       $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
-      $previous_db = tripal_db_set_active('chado');  // use chado database
+      $previous_db = db_set_active('chado');  // use chado database
       $results = db_query($sql);
-      tripal_db_set_active($previous_db);  // now use drupal database
+      db_set_active($previous_db);  // now use drupal database
       if ($results) {
         drupal_set_message(t("View '%name' created", array('%name' => $name)));
       }
@@ -178,8 +178,9 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
   // changed. IF so, then we want to drop and recreate the table. If not, then
   // just save the updated SQL.
   $create_table = 1;  
-  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
-  $mview = db_fetch_object(db_query($sql, $mview_id));  
+  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
+  $results = db_query($sql, array(':mview_id' => $mview_id));
+  $mview = $results->fetchObject();  
   if ($mview->mv_schema == $mv_schema and $mview->mv_table == $mv_table and 
      $mview->mv_specs == $mv_specs and $mview->indexed == $indexed and 
      $mview->special_index == $special_index) {
@@ -198,13 +199,13 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
   
   // if we are going to create the table then we must first drop it if it exists
   if ($create_table) {
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $previous_db = db_set_active('chado');  // use chado database
     if (db_table_exists($mview->mv_table)) {
-      $sql = "DROP TABLE %s";
-      db_query($sql, $mview->mv_table);
+      $sql = "DROP TABLE :table_name";
+      db_query($sql, array(':table_name' => $mview->mv_table));
       drupal_set_message(t("View '%name' dropped", array('%name' => $name)));
     }
-    tripal_db_set_active($previous_db);  // now use drupal database
+    db_set_active($previous_db);  // now use drupal database
   }
 
   // update the record to the tripal_mviews table and if successful
@@ -270,10 +271,10 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  * @ingroup tripal_mviews_api
  */
 function tripal_mviews_get_mview_id($view_name) {
-  $sql = "SELECT * FROM {tripal_mviews} " .
-        "WHERE name = '%s'";
   if (db_table_exists('tripal_mviews')) {
-    $mview = db_fetch_object(db_query($sql, $view_name));
+  	$sql = "SELECT * FROM {tripal_mviews} WHERE name = :name";
+  	$results = db_query($sql, array(':name' => $view_name));
+    $mview = $results->fetchObject();
     if ($mview) {
       return $mview->mview_id;
     }
@@ -303,8 +304,9 @@ function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
   }
 
   // get this mview details
-  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
-  $mview = db_fetch_object(db_query($sql, $mview_id));
+  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
+  $results = db_query($sql, array(':mview_id' => $mview_id));
+  $mview = $results->fetchObject();
 
   // add a job or perform the action based on the given operation
   if ($op == 'update') {
@@ -317,12 +319,12 @@ function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
            "WHERE mview_id = $mview_id";
     db_query($sql);
     // drop the table from chado if it exists
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $previous_db = db_set_active('chado');  // use chado database
     if (db_table_exists($mview->mv_table)) {
       $sql = "DROP TABLE $mview->mv_table";
       db_query($sql);
     }
-    tripal_db_set_active($previous_db);  // now use drupal database
+    db_set_active($previous_db);  // now use drupal database
   }
 
   // Redirect the user
@@ -343,23 +345,23 @@ function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
  * @ingroup tripal_mviews_api
  */
 function tripal_update_mview($mview_id) {
-  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
-  $mview = db_fetch_object(db_query($sql, $mview_id));
+  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
+  $results = db_query($sql, array(':mview_id' => $mview_id));
+  $mview = $results->fetchObject();
   if ($mview) {    
     // execute the query inside a transaction so that it doesn't destroy existing data
     // that may leave parts of the site unfunctional
     tripal_db_start_transaction();
-    $previous_db = tripal_db_set_active('chado');  // use chado database
-    $results = db_query("DELETE FROM {%s}", $mview->mv_table);
-    $results = db_query("INSERT INTO {%s} ($mview->query)", $mview->mv_table);    
-    tripal_db_set_active($previous_db);  // now use drupal database
+    $previous_db = db_set_active('chado');  // use chado database
+    $results = db_query("DELETE FROM {" . $mview->mv_table . "}");
+    $results = db_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");    
+    db_set_active($previous_db);  // now use drupal database
     if ($results) {
       // commit the transaction
       tripal_db_commit_transaction();
-      $sql = "SELECT count(*) as cnt FROM {%s}";
-      $previous_db = tripal_db_set_active('chado');  // use chado database
-      $count = db_fetch_object(db_query($sql, $mview->mv_table));
-      tripal_db_set_active($previous_db);  // now use drupal database
+      $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
+      $results = chado_query($sql);
+      $count = $results->fetchObject();
       $record = new stdClass();
       $record->mview_id = $mview_id;
       $record->last_update = REQUEST_TIME;

+ 14 - 12
tripal_core/includes/chado_install.inc

@@ -77,12 +77,15 @@ function tripal_core_chado_load_form_submit($form, &$form_state) {
  */
 function tripal_core_install_chado($action) {
 
-  $vsql = "INSERT INTO {chadoprop} (type_id, value) VALUES  " .
-          "((SELECT cvterm_id " .
-          "FROM {cvterm} CVT " .
-          " INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id " .
-          "WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), " .
-          "'%s') ";
+  $vsql = "
+    INSERT INTO {chadoprop} (type_id, value) 
+      VALUES (
+       (SELECT cvterm_id 
+        FROM {cvterm} CVT 
+          INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id 
+         WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), 
+       :version) 
+  ";
 
   if ($action == 'Install Chado v1.2') {
     $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.2.sql';
@@ -104,7 +107,7 @@ function tripal_core_install_chado($action) {
         print "Installation (Step 2 of 2) Problems!  Please check output above for errors.\n";
         exit;
       }
-      chado_query($vsql, '1.2'); # set the version
+      chado_query($vsql, array(':version' => '1.2')); # set the version
     }
     else {
       print "ERROR: cannot install chado.  Please check database permissions\n";
@@ -130,7 +133,7 @@ function tripal_core_install_chado($action) {
       print "Upgrade (Step 2 of 2) problems!  Please check output above for errors.\n";
       exit;
     }
-    chado_query($vsql, '1.2'); # set the version
+    chado_query($vsql, array(':version' => '1.2')); # set the version
   }
   elseif ($action == 'Install Chado v1.11') {
     $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11.sql';
@@ -361,11 +364,11 @@ function tripal_core_install_sql($sql_file) {
       }
       
       if (!$chado_local) {
-        $previous = tripal_db_set_active('chado');
+        $previous = db_set_active('chado');
       }
       $result = db_query($query);
       if (!$chado_local) {
-        tripal_db_set_active($previous);
+        db_set_active($previous);
       }
       if (!$result) {
         $error  = pg_last_error();
@@ -388,6 +391,5 @@ function tripal_core_install_sql($sql_file) {
  */
 function tripal_core_chado_install_done() {
 
-  // return the search path to normal
-  db_query("set search_path to public");
+  db_query("set search_path to default");
 }

+ 15 - 11
tripal_core/includes/custom_tables.inc

@@ -19,8 +19,9 @@
 function tripal_custom_table_view($table_id) {
 
   // get this custom_table details
-  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
-  $custom_table = db_fetch_object(db_query($sql, $table_id));
+  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
+  $results = db_query($sql, array(':table_id' => $table_id));
+  $custom_table = $results->fetchObject();
 
   // create a table with each row containig stats for
   // an individual job in the results set.
@@ -66,7 +67,7 @@ function tripal_custom_tables_list() {
   $rows = array();
   $custom_tables = db_query("SELECT * FROM {tripal_custom_tables} ORDER BY table_name");
 
-  while ($custom_table = db_fetch_object($custom_tables)) {
+  foreach ($custom_tables as $custom_table) {
  
     $rows[] = array(
       l(t('View'), "admin/tripal/custom_tables/view/$custom_table->table_id") . " | " .
@@ -112,8 +113,9 @@ function tripal_custom_tables_form(&$form_state = NULL, $table_id = NULL) {
 
   // get this requested table
   if (strcmp($action, 'Edit')==0) {
-    $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d ";
-    $custom_table = db_fetch_object(db_query($sql, $table_id));
+    $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id ";
+    $results = db_query($sql, array(':table_id' => $table_id));
+    $custom_table = $results->fetchObject();
 
     // set the default values.  If there is a value set in the
     // form_state then let's use that, otherwise, we'll pull
@@ -271,12 +273,13 @@ function tripal_custom_tables_form_validate($form, &$form_state) {
       if ($action == 'Edit') {
         // see if the table name has changed. If so, then check to make sure
         // it doesn't already exists. We don't want to drop a table we didn't mean to
-        $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
-        $ct = db_fetch_object(db_query($sql, $table_id));
+        $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
+        $results = db_query($sql, array(':table_id' => $table_id));
+        $ct = $results->fetchObject();
         if ($ct->table_name != $schema_array['table']) {
-          $previous_db = tripal_db_set_active('chado');
+          $previous_db = db_set_active('chado');
           $exists = db_table_exists($schema_array['table']);
-          tripal_db_set_active($previous_db);        
+          db_set_active($previous_db);        
           if ($exists) {
             form_set_error($form_state['values']['schema'],
               t("The table name already exists, please choose a different name."));  
@@ -344,8 +347,9 @@ function tripal_custom_tables_action($op, $table_id, $redirect = FALSE) {
   }
 
   // get this table details
-  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
-  $custom_table = db_fetch_object(db_query($sql, $table_id));
+  $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
+  $results = db_query($sql, array(':table_id' => $table_id));
+  $custom_table = $results->fetchObject();
 
   if ($op == 'delete') {
   

+ 7 - 5
tripal_core/includes/mviews.inc

@@ -17,8 +17,9 @@
 function tripal_mview_report($mview_id) {
 
   // get this mview details
-  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
-  $mview = db_fetch_object(db_query($sql, $mview_id));
+  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
+  $results = db_query($sql, array(':mview_id' => $mview_id));
+  $mview = $results->fetchObject();
   
   $rows = array();
 
@@ -83,7 +84,7 @@ function tripal_mviews_report() {
   $rows = array();
   $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
 
-  while ($mview = db_fetch_object($mviews)) {
+  foreach ($mviews as $mview) {
     if ($mview->last_update > 0) {
       $update = format_date($mview->last_update);
     }
@@ -155,8 +156,9 @@ function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
   
   // get this requested view
   if (strcmp($action, 'Edit')==0) {
-    $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
-    $mview = db_fetch_object(db_query($sql, $mview_id));
+    $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
+    $results = db_query($sql, array(':mview_id' => $mview_id));
+    $mview = $results->fetchObject();
 
     // set the default values.  If there is a value set in the
     // form_state then let's use that, otherwise, we'll pull

+ 9 - 6
tripal_core/tripal_core.drush.inc

@@ -178,10 +178,12 @@ function drush_tripal_core_tripal_jobs_launch($username) {
   $job_id = drush_get_option('job_id');
 
   if ($username) {
-    $sql = "SELECT uid FROM {user} WHERE name = '%s'";
-    $u = db_fetch_object(db_query($sql, $username));
+    $sql = "SELECT uid FROM {users} WHERE name = :name";
+    $results = db_query($sql, array(':name' => $username));
+    $u = $results->fetchObject();
     if (!$u) {
-      drush_print('ERROR: Please provide a valid username for running this job.');  
+      drush_print('ERROR: Please provide a valid username for running this job.'); 
+      exit; 
     }
     global $user;
     $user = user_load($u->uid);
@@ -226,7 +228,7 @@ function drush_tripal_core_tripal_jobs_current() {
   $sql =  "SELECT * FROM {tripal_jobs} TJ " .
             "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
   $jobs = db_query($sql);
-  while ($job = db_fetch_object($jobs)) {
+  foreach ($jobs as $job) {
     $job_pid = $job->pid;
     $output = "Name: " . $job->job_name . "\n"
              . "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n"
@@ -262,8 +264,9 @@ function drush_tripal_core_tripal_update_mview() {
   if (!$mview_id) {
     if ($table_name) {
       // if table_name supplied use that to get mview_id
-      $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
-      $r = db_fetch_object(db_query($sql, $table_name));
+      $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
+      $results = db_query($sql, array(':mv_table' => $table_name));      
+      $r = $resuls->fetchObject();
       if (!$r->mview_id) {
         drush_set_error('No Materialized View associated with that table_name.');
       }

+ 3 - 2
tripal_core/tripal_core.module

@@ -331,8 +331,9 @@ function tripal_core_job_describe_args($callback, $args) {
   $new_args = array();
   if ($callback == 'tripal_update_mview') {
     // get this mview details
-    $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
-    $mview = db_fetch_object(db_query($sql, $args[0]));
+    $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
+    $results = db_query($sql, array(':mview_id' => $args[0]));
+    $mview = $results->fetchObject();
     $new_args['View Name'] = $mview->name;
   }
 

+ 4 - 2
tripal_core/tripal_launch_jobs.php

@@ -29,13 +29,15 @@ drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 // check to make sure the username is valid
 $username = $argv[1];
 $do_parallel = $argv[2];
-if (!db_fetch_object(db_query("SELECT * FROM {users} WHERE name = '%s'", $username))) {
+$results = db_query("SELECT * FROM {users} WHERE name = :name", array(':name' => $username));
+$u = $results->fetchObject();
+if (!$u) {
   fwrite($stdout, "'$username' is not a valid Drupal username. exiting...\n");
   exit;
 }
 
 global $user;
-$user = user_load(array('name' => $username));
+$user = user_load($u->uid);
 
 
 fwrite($stdout, "Tripal Job Launcher\n");

+ 10 - 7
tripal_core/tripal_launch_jobs_multi.php

@@ -99,13 +99,16 @@ function runjob($sitename, $username) {
 
   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
-  if (!db_fetch_object(db_query("SELECT * FROM {users} WHERE name = '%s'", $username))) {
-    fwrite(STDOUT, "'$username' is not a valid Drupal username. exiting...\n");
-    exit;
-  }
-
-  $user = $username;
-  $user = user_load(array('name' => $username));
+	// check to make sure the username is valid
+	$results = db_query("SELECT * FROM {users} WHERE name = :name", array(':name' => $username));
+	$u = $results->fetchObject();
+	if (!$u) {
+	  fwrite($stdout, "'$username' is not a valid Drupal username. exiting...\n");
+	  exit;
+	}
+	
+	global $user;
+	$user = user_load($u->uid);
 
   tripal_jobs_launch();
 }

+ 2 - 6
tripal_feature/tripal_feature.module

@@ -1629,9 +1629,7 @@ function tripal_feature_load_library_feature_browser($library) {
           "WHERE LF.library_id = %d and ($where_cvt) ".
           "GROUP BY L.library_id ";
 
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $org_features = pager_query($sql, 10, 0, $csql, $library->library_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $org_features = chado_pager_query($sql, 10, 0, $csql, $library->library_id);
   $pager = theme('pager');
 
   // prepare the query that will lookup node ids
@@ -1691,9 +1689,7 @@ function tripal_feature_load_analysis_feature_browser($analysis) {
           "WHERE A.analysis_id = %d and ($where_cvt) ".
           "GROUP BY A.analysis_id ";
 
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $org_features = pager_query($sql, 10, 0, $csql, $analysis->analysis_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $org_features = chado_pager_query($sql, 10, 0, $csql, $analysis->analysis_id);
   $pager = theme('pager');
 
   // prepare the query that will lookup node ids