function chado_query

2.x tripal_core.chado_query.api.inc chado_query($sql, $args = array())
3.x tripal_chado.query.api.inc chado_query($sql, $args = array())
1.x tripal_core_chado.api.inc chado_query($sql)

Use this function instead of db_query() to avoid switching databases when making query to the chado database

Will use a chado persistent connection if it already exists

@returns A database query result resource or FALSE if the query was not executed correctly

Parameters

$sql: The sql statement to execute

Related topics

192 calls to chado_query()
chado_analysis_delete in tripal_analysis/tripal_analysis.module
Removes analysis from the chado database
chado_analysis_form in tripal_analysis/includes/tripal_analysis.form.inc
When editing or creating a new node of type 'chado_analysis' we need a form. This function creates the form that will be used for this.
chado_analysis_insert in tripal_analysis/tripal_analysis.module
When a new chado_analysis node is created we also need to add information to our chado_analysis table. This function is called on insert of a new node of type 'chado_analysis' and inserts the necessary information.
chado_analysis_node_form_add_analysisprop_table_props in tripal_analysis/includes/tripal_analysis.form.inc
chado_analysis_update in tripal_analysis/tripal_analysis.module
Update analyses

... See full list

File

tripal_core/api/tripal_core_chado.api.inc, line 2436
The Tripal Core API

Code

function chado_query($sql) {
  global $persistent_chado;

  $is_local = tripal_core_is_chado_local();

  $args = func_get_args();
  array_shift($args); // remove the $sql from the argument list
  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
    $args = $args[0];
  }

  // run the Drupal command to clean up the SQL
  _db_query_callback($args, TRUE);
  $sql = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $sql);

  // add the chado schema to the table names if Chado is local to the Drupal database
  if ($is_local) {
    $sql = preg_replace('/\n/', '', $sql); // remove carriage returns
    $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
  }

  // let Drupal add any prefixes to tables
  $sql = db_prefix_tables($sql);

  // Execute the query on the chado database/schema
  // Use the persistent chado connection if it already exists
  if ($persistent_chado) {

    $query = $sql;
    // Duplicate the _db_query code in order to ensure that the drupal
    // $active_db variable is not used in the pg_query command
    // thus changed $active_db to $persistent_chado
    // START COPY FROM _db_query in database.pgsql.inc
    if (variable_get('dev_query', 0)) {
      list($usec, $sec) = explode(' ', microtime());
      $timer = (float) $usec + (float) $sec;
    }
    // if we're local we can just run the query
    if ($is_local) {
      //dpm($query);
      $last_result = pg_query($persistent_chado, $query);
    }
    else {
      $previous_db = tripal_db_set_active('chado');
      $last_result = pg_query($persistent_chado, $query);
      tripal_db_set_active($previous_db);
    }

    if (variable_get('dev_query', 0)) {
      $bt = debug_backtrace();
      $query = $bt[2]['function'] . "\n" . $query;
      list($usec, $sec) = explode(' ', microtime());
      $stop = (float) $usec + (float) $sec;
      $diff = $stop - $timer;
      $queries[] = array($query, $diff);
    }

    if ($last_result !== FALSE) {
      return $last_result;
    }
    else {
      // Indicate to drupal_error_handler that this is a database error.
      ${DB_ERROR}= TRUE;
      trigger_error(check_plain(pg_last_error($persistent_chado) . "\nquery: " . $query), E_USER_WARNING);
      return FALSE;
    }
    // END COPY FROM _db_query in database.pgsql.inc
  }
  else {
    // before running the query we want to prefix the table names with
    // the chado schema.  Previously use had to make changes to the
    // search_path but that caused a lot of database calls and wasted
    // resources during long jobs.
    if ($is_local) {
      $results = _db_query($sql);
    }
    else {
      $previous_db = tripal_db_set_active('chado');
      $results = _db_query($sql);
      tripal_db_set_active($previous_db);
    }
  }
  return $results;
}