mviews.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * @defgroup tripal_mviews_api Core Module Materalized Views API
  4. * @{
  5. * Provides an application programming interface (API) to manage materialized views in Chado.
  6. * The Perl-based chado comes with an interface for managing materialzed views. This
  7. * API provides an alternative Drupal-based method.
  8. * @}
  9. * @ingroup tripal_api
  10. */
  11. /**
  12. * Add a materialized view to the chado database to help speed data access.
  13. *
  14. * @param $name
  15. * The name of the materialized view.
  16. * @param $modulename
  17. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  18. * @param $mv_table
  19. * The name of the table to add to chado. This is the table that can be queried.
  20. * @param $mv_specs
  21. * The table definition
  22. * @param $indexed
  23. * The columns that are to be indexed
  24. * @param $query
  25. * The SQL query that loads the materialized view with data
  26. * @param $special_index
  27. * function
  28. *
  29. * @ingroup tripal_mviews_api
  30. */
  31. function tripal_add_mview ($name,$modulename,$mv_table,$mv_specs,$indexed,$query,$special_index,$comment=NULL){
  32. $record = new stdClass();
  33. $record->name = $name;
  34. $record->modulename = $modulename;
  35. $record->mv_schema = 'DUMMY';
  36. $record->mv_table = $mv_table;
  37. $record->mv_specs = $mv_specs;
  38. $record->indexed = $indexed;
  39. $record->query = $query;
  40. $record->special_index = $special_index;
  41. $record->comment = $comment;
  42. // add the record to the tripal_mviews table and if successful
  43. // create the new materialized view in the chado schema
  44. if(drupal_write_record('tripal_mviews',$record)){
  45. // drop the table from chado if it exists
  46. $previous_db = tripal_db_set_active('chado'); // use chado database
  47. if (db_table_exists($mv_table)) {
  48. $sql = "DROP TABLE $mv_table";
  49. db_query($sql);
  50. }
  51. tripal_db_set_active($previous_db); // now use drupal database
  52. // now construct the indexes
  53. $index = '';
  54. if($indexed){
  55. // remove extra spaces and index field names
  56. $indexed = preg_replace("/\s+,/",",",$indexed);
  57. $indexed = preg_replace("/\s+\n/","\n",$indexed);
  58. $indexed = preg_replace("/,\n/","\n",$indexed);
  59. // add to the array of values
  60. $vals = preg_split("/[\n,]+/",$indexed);
  61. $index = '';
  62. foreach ($vals as $field){
  63. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  64. }
  65. }
  66. // add the table to the database
  67. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  68. $previous_db = tripal_db_set_active('chado'); // use chado database
  69. $results = db_query($sql);
  70. tripal_db_set_active($previous_db); // now use drupal database
  71. if($results){
  72. drupal_set_message(t("View '$name' created"));
  73. } else {
  74. // if we failed to create the view in chado then
  75. // remove the record from the tripal_jobs table
  76. $sql = "DELETE FROM {tripal_mviews} ".
  77. "WHERE mview_id = $record->mview_id";
  78. db_query($sql);
  79. }
  80. }
  81. }
  82. /**
  83. * Edits a materialized view to the chado database to help speed data access.
  84. *
  85. * @param $mview_id
  86. * The mview_id of the materialized view to edit
  87. * @param $name
  88. * The name of the materialized view.
  89. * @param $modulename
  90. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  91. * @param $mv_table
  92. * The name of the table to add to chado. This is the table that can be queried.
  93. * @param $mv_specs
  94. * The table definition
  95. * @param $indexed
  96. * The columns that are to be indexed
  97. * @param $query
  98. * The SQL query that loads the materialized view with data
  99. * @param $special_index
  100. * function
  101. *
  102. * @ingroup tripal_mviews_api
  103. */
  104. function tripal_edit_mview ($mview_id,$name,$modulename,$mv_table,$mv_specs,$indexed,$query,$special_index,$comment){
  105. $record = new stdClass();
  106. $record->mview_id = $mview_id;
  107. $record->name = $name;
  108. $record->modulename = $modulename;
  109. $record->mv_schema = 'DUMMY';
  110. $record->mv_table = $mv_table;
  111. $record->mv_specs = $mv_specs;
  112. $record->indexed = $indexed;
  113. $record->query = $query;
  114. $record->special_index = $special_index;
  115. $record->last_update = 0;
  116. $record->status = '';
  117. $record->comment = $comment;
  118. // drop the table from chado if it exists
  119. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  120. $mview = db_fetch_object(db_query($sql));
  121. $previous_db = tripal_db_set_active('chado'); // use chado database
  122. if (db_table_exists($mview->mv_table)) {
  123. $sql = "DROP TABLE $mview->mv_table";
  124. db_query($sql);
  125. }
  126. tripal_db_set_active($previous_db); // now use drupal database
  127. // update the record to the tripal_mviews table and if successful
  128. // create the new materialized view in the chado schema
  129. if(drupal_write_record('tripal_mviews',$record,'mview_id')){
  130. // drop the table from chado if it exists
  131. $previous_db = tripal_db_set_active('chado'); // use chado database
  132. if (db_table_exists($mv_table)) {
  133. $sql = "DROP TABLE $mv_table";
  134. db_query($sql);
  135. }
  136. tripal_db_set_active($previous_db); // now use drupal database
  137. // now construct the indexes
  138. $index = '';
  139. if($indexed){
  140. // remove extra spaces and index field names
  141. $indexed = preg_replace("/\s+,/",",",$indexed);
  142. $indexed = preg_replace("/\s+\n/","\n",$indexed);
  143. $indexed = preg_replace("/,\n/","\n",$indexed);
  144. // add to the array of values
  145. $vals = preg_split("/[\n,]+/",$indexed);
  146. $index = '';
  147. foreach ($vals as $field){
  148. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  149. }
  150. }
  151. // recreate the view
  152. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  153. $previous_db = tripal_db_set_active('chado'); // use chado database
  154. $results = db_query($sql);
  155. tripal_db_set_active($previous_db); // now use drupal database
  156. if($results){
  157. drupal_set_message(t("View '$name' edited and saved. All results cleared. Please re-populate the view."));
  158. } else {
  159. // if we failed to create the view in chado then
  160. // remove the record from the tripal_jobs table
  161. $sql = "DELETE FROM {tripal_mviews} ".
  162. "WHERE mview_id = $record->mview_id";
  163. db_query($sql);
  164. }
  165. }
  166. }
  167. /**
  168. * Retrieve the materialized view_id given the name
  169. *
  170. * @param $view_name
  171. * The name of the materialized view
  172. *
  173. * @return
  174. * The unique identifier for the given view
  175. *
  176. * @ingroup tripal_mviews_api
  177. */
  178. function tripal_mviews_get_mview_id ($view_name){
  179. $sql = "SELECT * FROM {tripal_mviews} ".
  180. "WHERE name = '%s'";
  181. if(db_table_exists('tripal_mviews')){
  182. $mview = db_fetch_object(db_query($sql,$view_name));
  183. if($mview){
  184. return $mview->mview_id;
  185. }
  186. }
  187. return 0;
  188. }
  189. /**
  190. *
  191. *
  192. * @ingroup tripal_core
  193. */
  194. function tripal_mviews_action ($op,$mview_id,$redirect=0){
  195. global $user;
  196. $args = array("$mview_id");
  197. if(!$mview_id){
  198. return '';
  199. }
  200. // get this mview details
  201. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  202. $mview = db_fetch_object(db_query($sql));
  203. // add a job or perform the action based on the given operation
  204. if($op == 'update'){
  205. tripal_add_job("Populate materialized view '$mview->name'",'tripal_core',
  206. 'tripal_update_mview',$args,$user->uid);
  207. }
  208. if($op == 'delete'){
  209. // remove the mview from the tripal_mviews table
  210. $sql = "DELETE FROM {tripal_mviews} ".
  211. "WHERE mview_id = $mview_id";
  212. db_query($sql);
  213. // drop the table from chado if it exists
  214. $previous_db = tripal_db_set_active('chado'); // use chado database
  215. if (db_table_exists($mview->mv_table)) {
  216. $sql = "DROP TABLE $mview->mv_table";
  217. db_query($sql);
  218. }
  219. tripal_db_set_active($previous_db); // now use drupal database
  220. }
  221. if($redirect){
  222. drupal_goto("admin/tripal/mviews");
  223. }
  224. }
  225. /**
  226. * Update a Materialized View
  227. *
  228. * @param $mview_id
  229. * The unique identifier for the materialized view to be updated
  230. *
  231. * @return
  232. * True if successful, false otherwise
  233. *
  234. * @ingroup tripal_mviews_api
  235. */
  236. function tripal_update_mview ($mview_id){
  237. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
  238. $mview = db_fetch_object(db_query($sql,$mview_id));
  239. if($mview){
  240. $previous_db = tripal_db_set_active('chado'); // use chado database
  241. $results = db_query("DELETE FROM {$mview->mv_table}");
  242. $results = db_query("INSERT INTO $mview->mv_table ($mview->query)");
  243. tripal_db_set_active($previous_db); // now use drupal database
  244. if($results){
  245. $sql = "SELECT count(*) as cnt FROM $mview->mv_table";
  246. $count = db_fetch_object(db_query($sql));
  247. $record = new stdClass();
  248. $record->mview_id = $mview_id;
  249. $record->last_update = time();
  250. $record->status = "Populated with " . number_format($count->cnt) . " rows";
  251. drupal_write_record('tripal_mviews',$record,'mview_id');
  252. return 1;
  253. } else {
  254. # print and save the error message
  255. $record = new stdClass();
  256. $record->mview_id = $mview_id;
  257. $record->status = "ERROR populating. See Drupal's recent log entries for details.";
  258. print $record->status . "\n";
  259. drupal_write_record('tripal_mviews',$record,'mview_id');
  260. return 0;
  261. }
  262. }
  263. }
  264. /**
  265. *
  266. *
  267. * @ingroup tripal_mviews_api
  268. */
  269. function tripal_mview_report ($mview_id) {
  270. // get this mview details
  271. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  272. $mview = db_fetch_object(db_query($sql));
  273. // create a table with each row containig stats for
  274. // an individual job in the results set.
  275. $return_url = url("admin/tripal/mviews/");
  276. $output .= "<p><a href=\"$return_url\">Return to table of materialized views.</a></p>";
  277. $output .= "<br />";
  278. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  279. $output .= "<br />";
  280. $output .= "<table class=\"border-table\">";
  281. if($mview->name){
  282. $output .= " <tr>".
  283. " <th>View Name</th>".
  284. " <td>$mview->name</td>".
  285. " </tr>";
  286. }
  287. if($mview->modulename){
  288. $output .= " <tr>".
  289. " <th>Module Name</th>".
  290. " <td>$mview->modulename</td>".
  291. " </tr>";
  292. }
  293. if($mview->mv_table){
  294. $output .= " <tr>".
  295. " <th>Table Name</th>".
  296. " <td>$mview->mv_table</td>".
  297. " </tr>";
  298. }
  299. if($mview->mv_specs){
  300. $output .= " <tr>".
  301. " <th>Table Field Definitions</th>".
  302. " <td>$mview->mv_specs</td>".
  303. " </tr>";
  304. }
  305. if($mview->query){
  306. $output .= " <tr>".
  307. " <th>Query</th>".
  308. " <td><pre>$mview->query</pre></td>".
  309. " </tr>";
  310. }
  311. if($mview->indexed){
  312. $output .= " <tr>".
  313. " <th>Indexed Fields</th>".
  314. " <td>$mview->indexed</td>".
  315. " </tr>";
  316. }
  317. if($mview->special_index){
  318. $output .= " <tr>".
  319. " <th>Special Indexed Fields</th>".
  320. " <td>$mview->speical_index</td>".
  321. " </tr>";
  322. }
  323. if($mview->last_update > 0){
  324. $update = format_date($mview->last_update);
  325. } else {
  326. $update = 'Not yet populated';
  327. }
  328. $output .= " <tr>".
  329. " <th>Last Update</th>".
  330. " <td>$update</td>".
  331. " </tr>";
  332. // build the URLs using the url function so we can handle installations where
  333. // clean URLs are or are not used
  334. $update_url = url("admin/tripal/mviews/action/update/$mview->mview_id");
  335. $delete_url = url("admin/tripal/mviews/action/delete/$mview->mview_id");
  336. $edit_url = url("admin/tripal/mviews/edit/$mview->mview_id");
  337. $output .= "<tr><th>Actions</th>".
  338. "<td> <a href='$update_url'>Populate</a>, ".
  339. " <a href='$edit_url'>Edit</a>, ".
  340. " <a href='$delete_url'>Delete</a></td></tr>";
  341. $output .= "</table>";
  342. return $output;
  343. }
  344. /**
  345. *
  346. *
  347. * @ingroup tripal_mviews_api
  348. */
  349. function tripal_mviews_report () {
  350. $header = array('','MView Name','Last Update','Status','Description','');
  351. $rows = array();
  352. $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
  353. while($mview = db_fetch_object($mviews)){
  354. if($mview->last_update > 0){
  355. $update = format_date($mview->last_update);
  356. } else {
  357. $update = 'Not yet populated';
  358. }
  359. $rows[] = array(
  360. l('View',"admin/tripal/mviews/report/$mview->mview_id") ." | ".
  361. l('Edit',"admin/tripal/mviews/edit/$mview->mview_id") ." | ".
  362. l('Populate',"admin/tripal/mviews/action/update/$mview->mview_id"),
  363. $mview->name,
  364. $update,
  365. $mview->status,
  366. $mview->comment,
  367. l('Delete',"admin/tripal/mviews/action/delete/$mview->mview_id"),
  368. );
  369. }
  370. $rows[] = array(
  371. 'data' => array(
  372. array('data' => l('Create a new materialized view.',"admin/tripal/mviews/new"),
  373. 'colspan' => 6),
  374. )
  375. );
  376. $page = theme('table', $header, $rows);
  377. return $page;
  378. }
  379. /**
  380. *
  381. *
  382. * @ingroup tripal_core
  383. */
  384. function tripal_mviews_form(&$form_state = NULL,$mview_id = NULL){
  385. if(!$mview_id){
  386. $action = 'Add';
  387. } else {
  388. $action = 'Edit';
  389. }
  390. // get this requested view
  391. if(strcmp($action,'Edit')==0){
  392. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  393. $mview = db_fetch_object(db_query($sql));
  394. # set the default values. If there is a value set in the
  395. # form_state then let's use that, otherwise, we'll pull
  396. # the values from the database
  397. $default_name = $form_state['values']['name'];
  398. $default_mv_table = $form_state['values']['mv_table'];
  399. $default_mv_specs = $form_state['values']['mv_specs'];
  400. $default_indexed = $form_state['values']['indexed'];
  401. $default_mvquery = $form_state['values']['mvquery'];
  402. $default_special_index = $form_state['values']['special_index'];
  403. $default_comment = $form_state['values']['cpmment'];
  404. if(!$default_name){
  405. $default_name = $mview->name;
  406. }
  407. if(!$default_mv_table){
  408. $default_mv_table = $mview->mv_table;
  409. }
  410. if(!$default_mv_specs){
  411. $default_mv_specs = $mview->mv_specs;
  412. }
  413. if(!$default_indexed){
  414. $default_indexed = $mview->indexed;
  415. }
  416. if(!$default_mvquery){
  417. $default_mvquery = $mview->query;
  418. }
  419. if(!$default_special_index){
  420. $default_special_index = $mview->special_index;
  421. }
  422. if(!$default_comment){
  423. $default_comment = $mview->comment;
  424. }
  425. }
  426. // Build the form
  427. $form['action'] = array(
  428. '#type' => 'value',
  429. '#value' => $action
  430. );
  431. $form['mview_id'] = array(
  432. '#type' => 'value',
  433. '#value' => $mview_id
  434. );
  435. $form['name']= array(
  436. '#type' => 'textfield',
  437. '#title' => t('View Name'),
  438. '#description' => t('Please enter the name for this materialized view.'),
  439. '#required' => TRUE,
  440. '#default_value' => $default_name,
  441. '#weight' => 1
  442. );
  443. $form['mv_table']= array(
  444. '#type' => 'textfield',
  445. '#title' => t('Table Name'),
  446. '#description' => t('Please enter the Postgres table name that this view will generate in the database. You can use the schema and table name for querying the view'),
  447. '#required' => TRUE,
  448. '#default_value' => $default_mv_table,
  449. '#weight' => 3
  450. );
  451. $form['mv_specs']= array(
  452. '#type' => 'textarea',
  453. '#title' => t('Table Definition'),
  454. '#description' => t('Please enter the field definitions for this view. Each field should be separated by a comma or enter each field definition on each line.'),
  455. '#required' => TRUE,
  456. '#default_value' => $default_mv_specs,
  457. '#weight' => 4
  458. );
  459. $form['indexed']= array(
  460. '#type' => 'textarea',
  461. '#title' => t('Indexed Fields'),
  462. '#description' => t('Please enter the field names (as provided in the table definition above) that will be indexed for this view. Separate by a comma or enter each field on a new line.'),
  463. '#required' => FALSE,
  464. '#default_value' => $default_indexed,
  465. '#weight' => 5
  466. );
  467. $form['mvquery']= array(
  468. '#type' => 'textarea',
  469. '#title' => t('Query'),
  470. '#description' => t('Please enter the SQL statement used to populate the table.'),
  471. '#required' => TRUE,
  472. '#default_value' => $default_mvquery,
  473. '#weight' => 6
  474. );
  475. /**
  476. $form['special_index']= array(
  477. '#type' => 'textarea',
  478. '#title' => t('View Name'),
  479. '#description' => t('Please enter the name for this materialized view.'),
  480. '#required' => TRUE,
  481. '#default_value' => $default_special_index,
  482. '#weight' => 7
  483. );
  484. */
  485. $form['comment']= array(
  486. '#type' => 'textarea',
  487. '#title' => t('MView Description'),
  488. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  489. '#required' => FALSE,
  490. '#default_value' => $default_comment,
  491. '#weight' => 8
  492. );
  493. if($action == 'Edit'){
  494. $value = 'Save';
  495. }
  496. if($action == 'Add'){
  497. $value = 'Add';
  498. }
  499. $form['submit'] = array (
  500. '#type' => 'submit',
  501. '#value' => t($value),
  502. '#weight' => 9,
  503. '#executes_submit_callback' => TRUE,
  504. );
  505. $form['#redirect'] = 'admin/tripal/mviews';
  506. return $form;
  507. }
  508. /**
  509. *
  510. *
  511. * @ingroup tripal_core
  512. */
  513. function tripal_mviews_form_submit($form, &$form_state){
  514. $action = $form_state['values']['action'];
  515. $mview_id = $form_state['values']['mview_id'];
  516. $name = $form_state['values']['name'];
  517. $mv_table = $form_state['values']['mv_table'];
  518. $mv_specs = $form_state['values']['mv_specs'];
  519. $indexed = $form_state['values']['indexed'];
  520. $query = $form_state['values']['mvquery'];
  521. $special_index = $form_state['values']['special_index'];
  522. $comment = $form_state['values']['comment'];
  523. if(strcmp($action,'Edit')==0){
  524. tripal_edit_mview($mview_id,$name, 'tripal_core',$mv_table, $mv_specs,$indexed,$query,$special_index,$comment);
  525. }
  526. else if(strcmp($action,'Add')==0){
  527. tripal_add_mview ($name, 'tripal_core',$mv_table, $mv_specs,$indexed,$query,$special_index,$comment);
  528. }
  529. else {
  530. drupal_set_message("No action performed.");
  531. }
  532. return '';
  533. }