function chado_populate_mview

3.x tripal_chado.mviews.api.inc chado_populate_mview($mview_id)

Update a Materialized View.

Parameters

$mview_id: The unique identifier for the materialized view to be updated.

Return value

True if successful, FALSE otherwise.

Related topics

6 calls to chado_populate_mview()
drush_tripal_chado_trp_refresh_mview in tripal_chado/tripal_chado.drush.inc
Updates the specified materialized view
OBOImporter::postRun in tripal_chado/includes/TripalImporter/OBOImporter.inc
tripal_chado_prepare_chado in tripal_chado/includes/setup/tripal_chado.setup.inc
Prepares Chado for use by Tripal.
tripal_chado_update_7323 in tripal_chado/tripal_chado.install
Adding the db2cv materialized view.
tripal_chado_update_7324 in tripal_chado/tripal_chado.install
Updating the db2cv materialized view.

... See full list

2 string references to 'chado_populate_mview'
chado_refresh_mview in tripal_chado/api/tripal_chado.mviews.api.inc
Populates the specified Materialized View.
tripal_chado_job_describe_args in tripal_chado/tripal_chado.module
Implements hook_job_describe_args().

File

tripal_chado/api/tripal_chado.mviews.api.inc, line 357
Provides an application programming interface (API) to manage materialized views in Chado.

Code

function chado_populate_mview($mview_id) {
  $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  $results = db_query($sql, array(':mview_id' => $mview_id));
  $mview = $results->fetchObject();
  if ($mview) {
    // Execute the query inside a transaction so that it doesn't destroy 
    // existing data that may leave parts of the site unfunctional.
    $transaction = db_transaction();
    $previous_db = chado_set_active('chado'); // use chado database
    try {
      $success = chado_query("DELETE FROM {" . $mview->mv_table . "}");
      $success = chado_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");
      // If success get the number of results and update the table record.
      if ($success) {
        $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
        $results = chado_query($sql);
        $count = $results->fetchObject();
        $record = new stdClass();
        $record->mview_id = $mview_id;
        $record->last_update = time();
        $record->status = "Populated with " . number_format($count->cnt) . " rows";
        drupal_write_record('tripal_mviews', $record, 'mview_id');
      }
      // If not success then throw an error.
      else {
        throw new Exception("ERROR populating the materialized view " . $mview->mv_table . ". See Drupal's recent log entries for details.");
      }
      chado_set_active($previous_db);
    }
    catch (Exception $e) {
      $transaction->rollback();
      chado_set_active($previous_db);
      // Print and save the error message.
      $record = new stdClass();
      $record->mview_id = $mview_id;
      $record->status = "ERROR populating $mview->mv_table. See Drupal's recent log entries for details.\n";
      drupal_write_record('tripal_mviews', $record, 'mview_id');
      watchdog_exception('tripal_mviews', $e);
      return FALSE;
    }
    print "Done.\n";
    return TRUE;
  }
}