function tripal_core_edit_custom_table

2.x tripal_core.DEPRECATED.api.inc tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1)
3.x tripal_core.DEPRECATED.inc tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1)
1.x tripal_core_custom_tables.api.inc tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1)

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

Parameters

$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_creation: 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

1 call to tripal_core_edit_custom_table()
tripal_custom_tables_form_submit in tripal_core/includes/custom_tables.inc
Submit the Create/Edit Custom table form Implements hook_form_submit().

File

tripal_core/api/tripal_core_custom_tables.api.inc, line 30
Contains functions for the Custom Tables API

Code

function tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1) {

  // 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 = %d";
  $custom_table = db_fetch_object(db_query($sql, $table_id));

  // 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_creation = 0; // we want to create the table
  }

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

  // update the custom table record and re-create the table in Chado
  if (drupal_write_record('tripal_custom_tables', $record, 'table_id')) {

    // drop the table from chado if it exists
    if (!$skip_creation) {
      if (db_table_exists($custom_table->table_name)) {
        chado_query("DROP TABLE %s", $custom_table->table_name);
        drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
      }

      // re-create the table
      if (!tripal_core_create_custom_table($ret, $table_name, $schema)) {
        drupal_set_message(t("Could not create the custom table. Check Drupal error report logs."));
      }
      else {
        drupal_set_message(t("Custom table '%name' created", array('%name' => $table_name)));
      }
    }
    // TODO: add FK constraints
  }
}