name = $name; $record->modulename = $modulename; $record->mv_schema = 'DUMMY'; $record->mv_table = $mv_table; $record->mv_specs = $mv_specs; $record->indexed = $indexed; $record->query = $query; $record->special_index = $special_index; $record->comment = $comment; // add the record to the tripal_mviews table and if successful // create the new materialized view in the chado schema if(drupal_write_record('tripal_mviews',$record)){ // drop the table from chado if it exists $previous_db = tripal_db_set_active('chado'); // use chado database if (db_table_exists($mv_table)) { $sql = "DROP TABLE $mv_table"; db_query($sql); } tripal_db_set_active($previous_db); // now use drupal database // now construct the indexes $index = ''; if($indexed){ // add to the array of values $vals = preg_split("/[\n,]+/",$indexed); $index = ''; foreach ($vals as $field){ $field = trim($field); $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);"; } } // add the table to the database $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index"; $previous_db = tripal_db_set_active('chado'); // use chado database $results = db_query($sql); tripal_db_set_active($previous_db); // now use drupal database if($results){ drupal_set_message(t("View '$name' created")); } else { // if we failed to create the view in chado then // remove the record from the tripal_jobs table $sql = "DELETE FROM {tripal_mviews} ". "WHERE mview_id = $record->mview_id"; db_query($sql); } } } /** * Edits a materialized view to the chado database to help speed data access. * * @param $mview_id * The mview_id of the materialized view to edit * @param $name * The name of the materialized view. * @param $modulename * The name of the module submitting the materialized view (e.g. 'tripal_library') * @param $mv_table * The name of the table to add to chado. This is the table that can be queried. * @param $mv_specs * The table definition * @param $indexed * The columns that are to be indexed * @param $query * The SQL query that loads the materialized view with data * @param $special_index * function * * @ingroup tripal_mviews_api */ function tripal_edit_mview ($mview_id,$name,$modulename,$mv_table,$mv_specs,$indexed,$query,$special_index,$comment){ $record = new stdClass(); $record->mview_id = $mview_id; $record->name = $name; $record->modulename = $modulename; $record->mv_schema = 'DUMMY'; $record->mv_table = $mv_table; $record->mv_specs = $mv_specs; $record->indexed = $indexed; $record->query = $query; $record->special_index = $special_index; $record->last_update = 0; $record->status = ''; $record->comment = $comment; // drop the table from chado if it exists $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id "; $mview = db_fetch_object(db_query($sql)); $previous_db = tripal_db_set_active('chado'); // use chado database if (db_table_exists($mview->mv_table)) { $sql = "DROP TABLE $mview->mv_table"; db_query($sql); } tripal_db_set_active($previous_db); // now use drupal database // update the record to the tripal_mviews table and if successful // create the new materialized view in the chado schema if(drupal_write_record('tripal_mviews',$record,'mview_id')){ // drop the table from chado if it exists $previous_db = tripal_db_set_active('chado'); // use chado database if (db_table_exists($mv_table)) { $sql = "DROP TABLE $mv_table"; db_query($sql); } tripal_db_set_active($previous_db); // now use drupal database // now construct the indexes $index = ''; if($indexed){ // add to the array of values $vals = preg_split("/[\n,]+/",$indexed); $index = ''; foreach ($vals as $field){ $field = trim($field); $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);"; } } // recreate the view $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index"; $previous_db = tripal_db_set_active('chado'); // use chado database $results = db_query($sql); tripal_db_set_active($previous_db); // now use drupal database if($results){ drupal_set_message(t("View '$name' edited and saved. All results cleared. Please re-populate the view.")); } else { // if we failed to create the view in chado then // remove the record from the tripal_jobs table $sql = "DELETE FROM {tripal_mviews} ". "WHERE mview_id = $record->mview_id"; db_query($sql); } } } /** * Retrieve the materialized view_id given the name * * @param $view_name * The name of the materialized view * * @return * The unique identifier for the given view * * @ingroup tripal_mviews_api */ function tripal_mviews_get_mview_id ($view_name){ $sql = "SELECT * FROM {tripal_mviews} ". "WHERE name = '%s'"; if(db_table_exists('tripal_mviews')){ $mview = db_fetch_object(db_query($sql,$view_name)); if($mview){ return $mview->mview_id; } } return 0; } /** * * * @ingroup tripal_core */ function tripal_mviews_action ($op,$mview_id,$redirect=0){ global $user; $args = array("$mview_id"); if(!$mview_id){ return ''; } // get this mview details $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id "; $mview = db_fetch_object(db_query($sql)); // add a job or perform the action based on the given operation if($op == 'update'){ tripal_add_job("Populate materialized view '$mview->name'",'tripal_core', 'tripal_update_mview',$args,$user->uid); } if($op == 'delete'){ // remove the mview from the tripal_mviews table $sql = "DELETE FROM {tripal_mviews} ". "WHERE mview_id = $mview_id"; db_query($sql); // drop the table from chado if it exists $previous_db = tripal_db_set_active('chado'); // use chado database if (db_table_exists($mview->mv_table)) { $sql = "DROP TABLE $mview->mv_table"; db_query($sql); } tripal_db_set_active($previous_db); // now use drupal database } if($redirect){ drupal_goto("admin/tripal/mviews"); } } /** * Update a Materialized View * * @param $mview_id * The unique identifier for the materialized view to be updated * * @return * True if successful, false otherwise * * @ingroup tripal_mviews_api */ function tripal_update_mview ($mview_id){ $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d "; $mview = db_fetch_object(db_query($sql,$mview_id)); if($mview){ $previous_db = tripal_db_set_active('chado'); // use chado database $results = db_query("DELETE FROM {$mview->mv_table}"); $results = db_query("INSERT INTO $mview->mv_table ($mview->query)"); tripal_db_set_active($previous_db); // now use drupal database if($results){ $sql = "SELECT count(*) as cnt FROM $mview->mv_table"; $count = db_fetch_object(db_query($sql)); $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'); return 1; } else { # print and save the error message $record = new stdClass(); $record->mview_id = $mview_id; $record->status = "ERROR populating. See Drupal's recent log entries for details."; print $record->status . "\n"; drupal_write_record('tripal_mviews',$record,'mview_id'); return 0; } } } /** * * * @ingroup tripal_mviews_api */ function tripal_mview_report ($mview_id) { // get this mview details $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id "; $mview = db_fetch_object(db_query($sql)); // create a table with each row containig stats for // an individual job in the results set. $return_url = url("admin/tripal/mviews/"); $output .= "
Return to table of materialized views.
"; $output .= "Details for $mview->name:
"; $output .= "View Name | ". "$mview->name | ". "
---|---|
Module Name | ". "$mview->modulename | ". "
Table Name | ". "$mview->mv_table | ". "
Table Field Definitions | ". "$mview->mv_specs | ". "
Query | ". "$mview->query | ".
"
Indexed Fields | ". "$mview->indexed | ". "
Special Indexed Fields | ". "$mview->speical_index | ". "
Last Update | ". "$update | ". "
Actions | ". "Populate, ". " Edit, ". " Delete |