function chado_edit_custom_table

2.x chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exists = 1)

Edits a custom table in the chado database. It supports using the Drupal Schema API array.


$table_id: The table_id of the table to edit

$table_name: The name of the custom table

$schema: Use the Schema API array to define the custom table.

$skip_if_exists: Set as TRUE to skip dropping and re-creation of the table. This is useful if the table was already created through another means and you simply want to make Tripal aware of the table schema.

Related topics

2 calls to chado_edit_custom_table()
tripal_core_edit_custom_table in tripal_core/api/
tripal_custom_tables_form_submit in tripal_core/includes/
Submit the Create/Edit Custom table form Implements hook_form_submit().
1 string reference to 'chado_edit_custom_table'


tripal_core/api/, line 32
Provides an API to manage custom tables in Chado.


function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exists = 1) {

  $transaction = db_transaction();
  try {
    // Create a new record
    $record = new stdClass();
    $record->table_id = $table_id;
    $record->table_name = $table_name;
    $record->schema = serialize($schema);

    // get the current custom table record
    $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 this is a materialized view then don't allow editing with this function
    if ($custom_table->mview_id) {
      tripal_report_error('tripal_core', TRIPAL_ERROR, "Please use the tripal_edit_mview() function to edit this custom table as it is a materialized view.", array());
      drupal_set_message("This custom table is a materialized view. Please use the " . l('Materialized View', 'admin/tripal/schema/mviews') . " interface to edit it.", 'error');
      return FALSE;

    // if the user changed the table name, we want to drop the old one and force
    // creation of the new one.
    if ($custom_table->table_name != $table_name) {
      chado_query("DROP TABLE %s", $custom_table->table_name);
      $skip_if_exists = 0; // we want to create the table

    // if skip creation is not set, then drop the table from chado if it exists
    if (!$skip_if_exists) {
      if (db_table_exists($custom_table->table_name)) {
        chado_query("DROP TABLE %s", $custom_table->table_name);
        drupal_set_message(t("Custom Table " . $custom_table->table_name . " dropped"));

    // update the custom table record and run the create custom table function
    drupal_write_record('tripal_custom_tables', $record, 'table_id');
    $success = chado_create_custom_table($table_name, $schema, $skip_if_exists);
  catch (Exception $e) {
    watchdog_exception('tripal_core', $e);
    $error = _drupal_decode_exception($e);
    drupal_set_message(t("Could not update custom table '%table_name': %message.", 
    array('%table_name' => $table, '%message' => $error['!message'])), 'error');
    return FALSE;
  return TRUE;