Tripal v1.0 (6.x-1.0)
tripal_core_chado.api.inc File Reference

Go to the source code of this file.

Functions

 tripal_core_chado_insert ($table, $values, $options=array())
 tripal_core_chado_update ($table, $match, $values, $options=NULL)
 tripal_core_chado_delete ($table, $match, $options=NULL)
 tripal_core_chado_select ($table, $columns, $values, $options=NULL)
 tripal_core_chado_get_foreign_key ($table_desc, $field, $values, $options=NULL)
 tripal_core_generate_chado_var ($table, $values, $base_options=array())
 tripal_core_expand_chado_vars ($object, $type, $to_expand, $table_options=array())
 tripal_core_exclude_type_by_default ()
 tripal_core_exclude_field_from_feature_by_default ()
 chado_pager_query ($query, $limit, $element, $count)
 chado_query_range ($query)
 chado_query ($sql)
 chado_get_id_for_node ($table, $node)
 chado_get_node_id ($table, $id)
 tripal_core_get_property ($basetable, $record_id, $property, $cv_name)
 tripal_core_insert_property ($basetable, $record_id, $property, $cv_name, $value, $update_if_present=0)
 tripal_core_update_property ($basetable, $record_id, $property, $cv_name, $value, $insert_if_missing=0)
 tripal_core_update_property_by_id ($basetable, $record_id, $property, $cv_name, $value)
 tripal_core_delete_property ($basetable, $record_id, $property, $cv_name)
 tripal_core_delete_property_by_id ($basetable, $record_id)
 tripal_db_set_active ($dbname= 'default')
 tripal_db_get_search_path ()
 tripal_db_set_chado_search_path ($dbname)
 tripal_db_set_default_search_path ()
 tripal_core_is_sql_prepared ($statement_name)
 tripal_core_chado_prepare ($statement_name, $psql, $args)
 tripal_core_chado_execute_prepared ($statement_name, $sql, $values)
 tripal_core_chado_clear_prepared ($statement_name_regex=NULL)
 tripal_db_persistent_chado ()
 tripal_db_release_persistent_chado ()
 tripal_db_start_transaction ()
 tripal_db_set_savepoint_transaction ($savepoint, $release=FALSE)
 tripal_db_commit_transaction ()
 tripal_db_rollback_transaction ($savepoint=NULL, $commit=TRUE)
 tripal_get_max_chado_rank ($tablename, $where_options)
 tripal_get_chado_custom_schema ($table)
 tripal_core_chado_schema_exists ()
 tripal_core_schema_exists ($schema)
 tripal_core_get_chado_tables ($include_custom=NULL)
 tripal_core_set_chado_version ()
 tripal_core_get_chado_version ($exact=FALSE, $warn_if_unsupported=FALSE)
 tripal_core_get_chado_table_schema ($table)
 tripal_core_clean_orphaned_nodes ($table, $job_id)
 tripal_core_is_chado_installed ()
 tripal_core_is_chado_local ()
 tripal_core_is_tripal_node_type ($chado_table)

Detailed Description

The Tripal Core API

This file provides the API needed for all other Tripal and Tripal dependent modules.

Definition in file tripal_core_chado.api.inc.


Function Documentation

tripal_core_chado_clear_prepared ( statement_name_regex = NULL)

Clears prepared statements to avoid conflicts

If no statement_name_regex is supplied then it clears ALL prepared statements; Otherwise, it clears prepared statement names that match the regex provided

Definition at line 3187 of file tripal_core_chado.api.inc.

                                                                         {
  global $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)) {
      $k = array_search($r->name, $prepared_statements);
      unset($prepared_statements[$k]);
      chado_query('DEALLOCATE PREPARE %s',$r->name);
    }
  }
  else {
    $prepared_statements = array();
    chado_query('DEALLOCATE PREPARE ALL');
  }
}
tripal_core_chado_execute_prepared ( statement_name,
sql,
values 
)

Execute a prepared statement with the supplied values

Parameters:
$statement_nameThe name of the prepared statement
$sqlThe SQL to execute using chado query. Should be of the form EXECUTE [statement_name] ([arg1],[arg2]...[argn])
$valuesAn array of values in the execute sql statement

Definition at line 3102 of file tripal_core_chado.api.inc.

                                                                            {
  global $prepared_statements;

  if (!tripal_core_is_sql_prepared($statement_name)) {
    watchdog('tripal_core', "tripal_core_chado_execute_prepared: Cannot execute an unprepared statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
    return FALSE;
  }

  // Before Executing, Ensure that all the values are supplied
  $required_values = $prepared_statements[$statement_name]['prepared_args'];
  if (!$required_values) {
    watchdog('tripal_core', "tripal_core_chado_execute_prepared: missing prepare arguments for this statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
    return FALSE;
  }

  if (sizeof($required_values) == sizeof($values)) {

    $error = FALSE;
    foreach ($values as $k => $v) {
      if (isset($required_values[$k])) {
        switch ($required_values[$k]) {
          case 'text':
            $check = is_string($v);
            if ($v != '' and !$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          case 'int':
            $check = is_numeric($v);
            if (!$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          case 'bool':
            if ($v != 'TRUE' and $v != 'FALSE') {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          case 'numeric':
            $check = is_numeric($v);
            if (!$check) {
              watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
                array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
              return FALSE;
            }
            break;
          default:
            watchdog('tripal_core', "chado_execute_prepared: unsupported argument type (supplied for '%name' statement %type)",

            array('%name' => $statement_name, '%type' => $required_values[$k]), WATCHDOG_WARNING);
            break;
        }
      }
      else {
        watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. Expected %required but recieved %values",
          array('%name' => $statement_name, '%required' => print_r($required_values, TRUE), '%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
        return FALSE;
      }
    }

    // Since all values are supplied, execute
    $resource = chado_query($sql, $values);
    return $resource;
  }
  else {
    watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. ' .
      'Expected %required but recieved %values. Statement: %statement.",
      array('%name' => $statement_name, '%required' => print_r($required_values, TRUE),
        '%values' => print_r($values, TRUE), '%statement' => $prepared_statements[$statement_name]['prepared_sql']), WATCHDOG_ERROR);
    return FALSE;
  }
}
tripal_core_chado_prepare ( statement_name,
psql,
args 
)

Prepare a chado query

Parameters:
$statement_nameThe name of the prepared statement
$psqlThe SQL statement to be executed via chado_query. Should be of the form PREPARE [statement name] AS [SQL Statement to be prepared]
$argsAn array of arguements required to execute the prepared statement. The keys of the array should correspond to the variables in the prepared statement and the value should be the type of value needed (ie: text, int, etc.)

Definition at line 3050 of file tripal_core_chado.api.inc.

                                                                  {
  global $persistent_chado;
  global $prepared_statements;

  if (!$persistent_chado) {
    watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement as no persistent connection is available", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
    return FALSE;
  }

  // Check to see if this statement was already prepared
  if (tripal_core_is_sql_prepared($statement_name)) {
    // check that the arguments are the same
    $prepared_args = $prepared_statements[$statement_name]['prepared_args'];
    $prepared_sql = $prepared_statements[$statement_name]['prepared_sql'];
    if ($prepared_args == $args) {
      // This statement is already prepared
      return TRUE;
    }
    else {
      // Although a statement with this name is already prepared it is not the same!
      watchdog('tripal_core', "chado_prepare: '%name' statement already prepared with different arguments! ".
        "You want to prepare \n%sql\n with \n%values\n and the existing statement is \n%esql\n with \n%existing",
        array('%name' => $statement_name, '%sql' => $psql, '%values' => print_r($args, TRUE), '%esql' => $prepared_sql,
          '%existing' => print_r($prepared_args, TRUE)), WATCHDOG_ERROR);
      return FALSE;
    }
  }

  $status = chado_query($psql);
  if (!$status) {
    watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement for: %sql", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
    return FALSE;
  }
  else {
    $prepared_statements[$statement_name] = array();
    $prepared_statements[$statement_name]['prepared_args'] = $args;
    $prepared_statements[$statement_name]['prepared_sql'] = $psql;
    return TRUE;
  }
}
tripal_core_chado_schema_exists ( )

Check that the Chado schema exists

Returns:
TRUE/FALSE depending upon whether it exists

Definition at line 3404 of file tripal_core_chado.api.inc.

                                           {

  $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
    $sql = "SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname = 'chado'";
    if (db_fetch_object(db_query($sql))) {
      variable_set('chado_schema_exists', TRUE);
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  return TRUE;
}
tripal_core_is_sql_prepared ( statement_name)

Indicates if the SQL statement is prepapred

Parameters:
$statement_nameThe name of the statement to check if it is prepared.
Returns:
TRUE if the statement is preapred, FALSE otherwise

Definition at line 3013 of file tripal_core_chado.api.inc.

                                                      {
  global $prepared_statements;

  if (!is_array($prepared_statements)) {
    watchdog('tripal_core', "tripal_core_is_sql_prepared: argument must be an array", array(), WATCHDOG_ERROR);
      return FALSE;
  }

  // check to see if the statement is prepared already
  if (in_array($statement_name, $prepared_statements)) {
    return TRUE;
  }

  // @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));

  if ($result) {
    return TRUE;
  }
  return FALSE;
}
tripal_core_is_tripal_node_type ( chado_table)

Determine whether a given chado table is directly linked to a node

Parameters:
$chado_tableThe name of a chado table to check (ie: feature)
Returns:
TRUE if it is linked to a node and FALSE otherwise

Definition at line 3799 of file tripal_core_chado.api.inc.

                                                       {
  $linking_table = 'chado_' . $chado_table;
  if (db_table_exists($linking_table)) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}
tripal_db_commit_transaction ( )

A simple function to commit a database transaction

Returns:
nothing

Definition at line 3285 of file tripal_core_chado.api.inc.

                                        {
  chado_query("COMMIT");
}
tripal_db_persistent_chado ( )

Instantiate or Return a persistent chado connection. This should not be confused with PHP persistent connections. Here we use the drupal db_connect function to

NOTE: cannot use $active_db since a new connection is created each time db_set_active() is called

Returns:
A postgresql connection object which can be used by pg_prepare, pg_execute, etc.

Definition at line 3214 of file tripal_core_chado.api.inc.

                                      {
  global $db_url;
  global $persistent_chado;

  // get connection if it already exists otherwise we need to set it
  if ($persistent_chado) {
    return $persistent_chado;
  }
  else {
    if (is_array($db_url) && isset($db_url['chado'])) {
      $connection = db_connect($db_url['chado']);
      if (!$connection) {
        watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
        return FALSE;
      }
      $persistent_chado = $connection;
    }
    else {
      if (is_array($db_url)) {
        $connection = db_connect($db_url['default']);
      }
      else {
        $connection = db_connect($db_url);
      }
      if (!$connection) {
        $persistent_chado = NULL;
        watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
        return FALSE;
      }
      $persistent_chado = $connection;
    }
    return $connection;
  }
  return FALSE;
}
tripal_db_release_persistent_chado ( )

Release a persistent chado connection

Definition at line 3253 of file tripal_core_chado.api.inc.

                                              {
  $persistent_chado = NULL;
}
tripal_db_rollback_transaction ( savepoint = NULL,
commit = TRUE 
)

Rollback changes.

If $savepoint is NULL then rollback to the beginning of the transaction, Otherwise, rollback to the point at which the named $savepoint was created

Parameters:
$savepointThe name of the saved point in the transaction to rollback to
$commitThe transcation will only be committed if this value is TRUE. The default is TRUE.
Returns:
nothing

Definition at line 3304 of file tripal_core_chado.api.inc.

                                                                           {

  if ($savepoint) {
    chado_query("ROLLBACK TO SAVEPOINT %s", $savepoint);
  }
  else {
    chado_query("ROLLBACK");
  }

  if ($commit) {
    tripal_db_commit_transaction();
  }

}
tripal_db_set_savepoint_transaction ( savepoint,
release = FALSE 
)

Set a savepoint to roll the current transaction back to if an error is encountered

Definition at line 3272 of file tripal_core_chado.api.inc.

                                                                           {
  // Postgresql requires a savepoint of the same name to be unset before re-use
  if ($release) {
    chado_query("RELEASE SAVEPOINT %s", $savepoint);
  }
  chado_query("SAVEPOINT %s", $savepoint);
}
tripal_db_start_transaction ( )

Start a transaction block. Ensures the use of a persistent chado connection

Definition at line 3260 of file tripal_core_chado.api.inc.

                                       {
  $connection = tripal_db_persistent_chado();
  if ($connection) {
    chado_query("BEGIN");
    return $connection;
  }
  return FALSE;
}
 All Classes Files Functions Variables