function chado_column_exists

2.x tripal_core.chado_schema.api.inc chado_column_exists($table, $column)
3.x tripal_chado.schema.api.inc chado_column_exists($table, $column)

Check that any given column in a Chado table exists.

This function is necessary because Drupal's db_field_exists() will not look in any other schema but the one were Drupal is installed

Parameters

$table: The name of the chado table.

$column: The name of the column in the chado table.

Return value

TRUE if the column exists for the table in the chado schema and FALSE if it does not.

Related topics

3 calls to chado_column_exists()
tripal_chado_fix_v1_3_custom_tables in tripal_chado/includes/setup/tripal_chado.setup.inc
Many of the custom tables created for Chado v1.2 are now in Chado v1.3.
tripal_chado_update_7324 in tripal_chado/tripal_chado.install
Updating the db2cv materialized view.
tripal_chado_upgrade_chado_1_2_to_1_3_pre_alter in tripal_chado/includes/tripal_chado.install.inc
Upgrade custom tables that may match the tables now in Chado v1.3.

File

tripal_chado/api/tripal_chado.schema.api.inc, line 105
Provides an application programming interface (API) for describing Chado tables.

Code

function chado_column_exists($table, $column) {
  global $databases;

  $default_db = $databases['default']['default']['database'];
  $cached_obj = cache_get('chado_table_columns', 'cache');
  if ($cached_obj) {
    $cached_cols = $cached_obj->data;
    if (is_array($cached_cols) and 
      array_key_exists($table, $cached_cols) and 
      array_key_Exists($column, $cached_cols[$table])) {
      return $cached_cols[$table][$column]['exists'];
    }
  }

  $sql = "
    SELECT 1
    FROM information_schema.columns
    WHERE
      table_name = :table_name AND
      column_name = :column_name AND
      table_schema = :chado AND
      table_catalog = :default_db
  ";
  $args = array(
    ':table_name' => $table,
    ':column_name' => $column,
    ':chado' => chado_get_schema_name('chado'),
    ':default_db' => $default_db
  );
  $results = db_query($sql, $args);
  $exists = $results->fetchField();
  if (!$exists) {
    $cached_cols[$table][$column]['exists'] = FALSE;
    cache_set('chado_table_columns', $cached_cols, 'cache', CACHE_TEMPORARY);
    return FALSE;
  }
  $cached_cols[$table][$column]['exists'] = TRUE;
  cache_set('chado_table_columns', $cached_cols, 'cache', CACHE_TEMPORARY);
  return TRUE;
}