mviews.inc

Contains functions for viewing and editing of Materialized Views on a Tripal website.

File

tripal_core/includes/mviews.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions for viewing and editing of Materialized Views
  5. * on a Tripal website.
  6. */
  7. /**
  8. * A template function which returns markup to display details for the current materialized view
  9. *
  10. * @param $mview_id
  11. * The unique ID of the materialized view to render
  12. *
  13. * @ingroup tripal_core
  14. */
  15. function tripal_mview_report($mview_id) {
  16. // get this mview details
  17. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
  18. $mview = db_fetch_object(db_query($sql, $mview_id));
  19. $rows = array();
  20. // create a table with each row containig stats for
  21. // an individual job in the results set.
  22. $return_url = url("admin/tripal/mviews/");
  23. $output .= "<p><a href=\"$return_url\">Return to table of materialized views.</a></p>";
  24. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  25. // build the URLs using the url function so we can handle installations where
  26. // clean URLs are or are not used
  27. $update_url = url("admin/tripal/mviews/action/update/$mview->mview_id");
  28. $delete_url = url("admin/tripal/mviews/action/delete/$mview->mview_id");
  29. $edit_url = url("admin/tripal/mviews/edit/$mview->mview_id");
  30. $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>, <a href='$delete_url'>Delete</a>");
  31. if ($mview->last_update > 0) {
  32. $update = format_date($mview->last_update);
  33. }
  34. else {
  35. $update = 'Not yet populated';
  36. }
  37. $rows[] = array('Last Update', $update);
  38. if ($mview->name) {
  39. $rows[] = array('View Name', $mview->name);
  40. }
  41. if ($mview->modulename) {
  42. $rows[] = array('Module Name', $mview->modulename);
  43. }
  44. if ($mview->mv_table) {
  45. $rows[] = array('Table Name', $mview->mv_table);
  46. }
  47. if ($mview->mv_specs) {
  48. $rows[] = array('Table Field Definitions', $mview->mv_specs);
  49. }
  50. if ($mview->query) {
  51. $rows[] = array('Query', "<pre>" . $mview->query . "</pre>");
  52. }
  53. if ($mview->indexed) {
  54. $rows[] = array('Indexed Fields', $mview->indexed);
  55. }
  56. if ($mview->special_index) {
  57. $rows[] = array('Special Indexed Fields', $mview->special_index);
  58. }
  59. if ($mview->mv_schema) {
  60. $rows[] = array('Drupal Schema API Definition', "<pre>" . $mview->mv_schema . "</pre>");
  61. }
  62. $table = theme_table(array(), $rows);
  63. $output .= $table;
  64. return $output;
  65. }
  66. /**
  67. * A template function to render a listing of all Materialized Views
  68. *
  69. * @ingroup tripal_core
  70. */
  71. function tripal_mviews_report() {
  72. $header = array('', 'MView Name', 'Last Update', 'Status', 'Description', '');
  73. $rows = array();
  74. $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
  75. while ($mview = db_fetch_object($mviews)) {
  76. if ($mview->last_update > 0) {
  77. $update = format_date($mview->last_update);
  78. }
  79. else {
  80. $update = 'Not yet populated';
  81. }
  82. $rows[] = array(
  83. l(t('View'), "admin/tripal/mviews/report/$mview->mview_id") ." | ".
  84. l(t('Edit'), "admin/tripal/mviews/edit/$mview->mview_id") ." | ".
  85. l(t('Populate'), "admin/tripal/mviews/action/update/$mview->mview_id"),
  86. $mview->name,
  87. $update,
  88. $mview->status,
  89. $mview->comment,
  90. l(t('Delete'), "admin/tripal/mviews/action/delete/$mview->mview_id"),
  91. );
  92. }
  93. $rows[] = array(
  94. 'data' => array(
  95. array('data' => l(t('Create a new materialized view.'), "admin/tripal/mviews/new"),
  96. 'colspan' => 6),
  97. )
  98. );
  99. $page = '</p>' . t("Materialized Views (MViews) are custom tables populated with a defined SQL statement.
  100. Because Chado is highly normalized and highly constrained it serves as a wonderful
  101. data storage platform, but unfortunately some queries may be slow. MViews alleviate slowness by aggregating data
  102. into tables that are more easy to query. Use MViews to create tables for custom search pages or custom Tripal
  103. module development.") . '</p>';
  104. $page .= '<p><b>' . t("MViews behaves in the following way:") . '</b><ul>'.
  105. '<li>' . t("The SQL statement defined for an MVIEW will be used to populate the table") . '</li>' .
  106. '<li>' . t("Altering the table structure of an MView will cause the MView table to be dropped and recreated. All records in the MView will be lost.") . '</li>' .
  107. '<li>' . t("Altering the query of an existing view will not change the MView table. No records will be lost. ") . '</li>' .
  108. '<li>' . t("Repopulating an MView that is already populated will result in replacement of all records.") . '</li>' .
  109. '<li>' . t("A database transaction will be used when populating MViews. Therefore replacement of records does not occur until the query completes. Any search forms or pages dependent on the MView will continue to function.") . '</li>' .
  110. '</ul></p>';
  111. $page .= '<b>' . t("Existing MViews") . '</b>';
  112. $page .= theme('table', $header, $rows);
  113. return $page;
  114. }
  115. /**
  116. * A Form to Create/Edit a Materialized View
  117. *
  118. * @param $form_state
  119. * The current state of the form (Form API)
  120. * @param $mview_id
  121. * The unique ID of the Materialized View to Edit or NULL if creating a new materialized view
  122. *
  123. * @return
  124. * A form array (Form API)
  125. *
  126. * @ingroup tripal_core
  127. */
  128. function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
  129. if (!$mview_id) {
  130. $action = 'Add';
  131. }
  132. else {
  133. $action = 'Edit';
  134. }
  135. // set defaults for collapsed fieldsets
  136. $schema_collapsed = 0;
  137. $traditional_collapsed = 1;
  138. // get this requested view
  139. if (strcmp($action, 'Edit')==0) {
  140. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
  141. $mview = db_fetch_object(db_query($sql, $mview_id));
  142. // set the default values. If there is a value set in the
  143. // form_state then let's use that, otherwise, we'll pull
  144. // the values from the database
  145. $default_name = $form_state['values']['name'];
  146. $default_mv_table = $form_state['values']['mv_table'];
  147. $default_mv_specs = $form_state['values']['mv_specs'];
  148. $default_indexed = $form_state['values']['indexed'];
  149. $default_mvquery = $form_state['values']['mvquery'];
  150. $default_special_index = $form_state['values']['special_index'];
  151. $default_comment = $form_state['values']['comment'];
  152. $default_modulename = $form_state['values']['modulename'];
  153. if (!$default_name) {
  154. $default_name = $mview->name;
  155. }
  156. if (!$default_mv_table) {
  157. $default_mv_table = $mview->mv_table;
  158. }
  159. if (!$default_mv_specs) {
  160. $default_mv_specs = $mview->mv_specs;
  161. }
  162. if (!$default_indexed) {
  163. $default_indexed = $mview->indexed;
  164. }
  165. if (!$default_mvquery) {
  166. $default_mvquery = $mview->query;
  167. }
  168. if (!$default_special_index) {
  169. $default_special_index = $mview->special_index;
  170. }
  171. if (!$default_comment) {
  172. $default_comment = $mview->comment;
  173. }
  174. if (!$default_schema) {
  175. $default_schema = $mview->mv_schema;
  176. }
  177. if (!$default_modulename) {
  178. $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
  179. }
  180. // the mv_table column of the tripal_mviews table always has the table
  181. // name even if it is a custom table. However, for the sake of the form,
  182. // we do not want this to show up as the mv_table is needed for the
  183. // traditional style input. We'll blank it out if we have a custom
  184. // table and it will get reset in the submit function using the
  185. // 'table' value from the schema array
  186. if ($default_schema) {
  187. $default_mv_table = '';
  188. }
  189. // set which fieldset is collapsed
  190. if (!$default_schema) {
  191. $schema_collapsed = 1;
  192. $traditional_collapsed = 0;
  193. }
  194. }
  195. // Build the form
  196. $form['action'] = array(
  197. '#type' => 'value',
  198. '#value' => $action
  199. );
  200. $form['mview_id'] = array(
  201. '#type' => 'value',
  202. '#value' => $mview_id
  203. );
  204. $form['modulename'] = array(
  205. '#type' => 'value',
  206. '#value' => $default_modulename,
  207. );
  208. $form['name']= array(
  209. '#type' => 'textfield',
  210. '#title' => t('View Name'),
  211. '#description' => t('Please enter the name for this materialized view.'),
  212. '#required' => TRUE,
  213. '#default_value' => $default_name,
  214. );
  215. $form['comment']= array(
  216. '#type' => 'textarea',
  217. '#title' => t('MView Description'),
  218. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  219. '#required' => FALSE,
  220. '#default_value' => $default_comment,
  221. );
  222. // add a fieldset for the Drupal Schema API
  223. $form['schema'] = array(
  224. '#type' => 'fieldset',
  225. '#title' => 'Drupal Schema API Setup',
  226. '#description' => t('Use the Drupal Schema API array to describe a table. The benefit is that it '.
  227. 'can be fully integrated with Tripal Views. Tripal supports an extended '.
  228. 'array format to allow for descriptoin of foreign key relationships.'),
  229. '#collapsible' => 1,
  230. '#collapsed' => $schema_collapsed ,
  231. );
  232. $form['schema']['schema']= array(
  233. '#type' => 'textarea',
  234. '#title' => t('Schema Array'),
  235. '#description' => t('Please enter the Drupal Schema API compatible array that defines the table.'),
  236. '#required' => FALSE,
  237. '#default_value' => $default_schema,
  238. '#rows' => 25,
  239. );
  240. // add a fieldset for the Original Table Description fields
  241. $form['traditional'] = array(
  242. '#type' => 'fieldset',
  243. '#title' => 'Legacy MViews Setup',
  244. '#description' => t('Traditionally MViews were created by specifying PostgreSQL style '.
  245. 'column types. This method can be used but is deprecated in favor of the '.
  246. 'newer Drupal schema API method provided above. In rare cases where the Drupal Schema API ' .
  247. 'does not support a desired data type the Legacy Mviews should be used'),
  248. '#collapsible' => 1,
  249. '#collapsed' => $traditional_collapsed,
  250. );
  251. $form['traditional']['mv_table']= array(
  252. '#type' => 'textfield',
  253. '#title' => t('Table Name'),
  254. '#description' => t('Please enter the table name that this view will generate in the database. You can use the schema and table name for querying the view'),
  255. '#required' => FALSE,
  256. '#default_value' => $default_mv_table,
  257. );
  258. $form['traditional']['mv_specs']= array(
  259. '#type' => 'textarea',
  260. '#title' => t('Table Definition'),
  261. '#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.'),
  262. '#required' => FALSE,
  263. '#default_value' => $default_mv_specs,
  264. );
  265. $form['traditional']['indexed']= array(
  266. '#type' => 'textarea',
  267. '#title' => t('Indexed Fields'),
  268. '#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.'),
  269. '#required' => FALSE,
  270. '#default_value' => $default_indexed,
  271. );
  272. /**
  273. $form['traditional']['special_index']= array(
  274. '#type' => 'textarea',
  275. '#title' => t('View Name'),
  276. '#description' => t('Please enter the name for this materialized view.'),
  277. '#required' => TRUE,
  278. '#default_value' => $default_special_index,
  279. );
  280. */
  281. $form['mvquery']= array(
  282. '#type' => 'textarea',
  283. '#title' => t('Query'),
  284. '#description' => t('Please enter the SQL statement used to populate the table.'),
  285. '#required' => TRUE,
  286. '#default_value' => $default_mvquery,
  287. '#rows' => 25,
  288. );
  289. if ($action == 'Edit') {
  290. $value = 'Save';
  291. }
  292. if ($action == 'Add') {
  293. $value = 'Add';
  294. }
  295. $form['submit'] = array(
  296. '#type' => 'submit',
  297. '#value' => t($value),
  298. '#weight' => 9,
  299. '#executes_submit_callback' => TRUE,
  300. );
  301. $form['#redirect'] = 'admin/tripal/mviews';
  302. return $form;
  303. }
  304. /**
  305. * Validate the Create/Edit Materialized View Form
  306. * Implements hook_form_validate().
  307. *
  308. * @ingroup tripal_core
  309. */
  310. function tripal_mviews_form_validate($form, &$form_state) {
  311. $action = $form_state['values']['action'];
  312. $mview_id = $form_state['values']['mview_id'];
  313. $name = $form_state['values']['name'];
  314. $mv_table = $form_state['values']['mv_table'];
  315. $mv_specs = $form_state['values']['mv_specs'];
  316. $indexed = $form_state['values']['indexed'];
  317. $query = $form_state['values']['mvquery'];
  318. $special_index = $form_state['values']['special_index'];
  319. $comment = $form_state['values']['comment'];
  320. $schema = $form_state['values']['schema'];
  321. if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
  322. form_set_error($form_state['values']['schema'],
  323. t('You can create an MView using the Drupal Schema API method or the '.
  324. 'traditional method but not both.'));
  325. }
  326. if (!$schema) {
  327. if (!$mv_specs) {
  328. form_set_error($form_state['values']['mv_specs'],
  329. t('The Table Definition field is required.'));
  330. }
  331. if (!$mv_table) {
  332. form_set_error($form_state['values']['mv_table'],
  333. t('The Table Name field is required.'));
  334. }
  335. }
  336. // make sure the array is valid
  337. if ($schema) {
  338. $success = eval("\$schema_array = $schema;");
  339. if ($success === FALSE) {
  340. $error = error_get_last();
  341. form_set_error($form_state['values']['schema'],
  342. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  343. }
  344. if (!array_key_exists('table', $schema_array)) {
  345. form_set_error($form_state['values']['schema'],
  346. t("The schema array must have key named 'table'"));
  347. }
  348. // TODO: add in more validation checks of the array to help the user
  349. }
  350. }
  351. /**
  352. * Submit the Create/Edit Materialized View Form
  353. * Implements hook_form_submit().
  354. *
  355. * @ingroup tripal_core
  356. */
  357. function tripal_mviews_form_submit($form, &$form_state) {
  358. $ret = array();
  359. $action = $form_state['values']['action'];
  360. $mview_id = $form_state['values']['mview_id'];
  361. $name = $form_state['values']['name'];
  362. $mv_table = $form_state['values']['mv_table'];
  363. $mv_specs = $form_state['values']['mv_specs'];
  364. $indexed = $form_state['values']['indexed'];
  365. $query = $form_state['values']['mvquery'];
  366. $special_index = $form_state['values']['special_index'];
  367. $comment = $form_state['values']['comment'];
  368. $schema = $form_state['values']['schema'];
  369. $modulename = $form_state['values']['modulename'];
  370. if (!$modulename) {
  371. $modulename = 'tripal_core';
  372. }
  373. if (strcmp($action, 'Edit') == 0) {
  374. tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  375. $indexed, $query, $special_index, $comment, $schema);
  376. }
  377. elseif (strcmp($action, 'Add') == 0) {
  378. tripal_add_mview($name, $modulename, $mv_table, $mv_specs,
  379. $indexed, $query, $special_index, $comment, $schema);
  380. }
  381. else {
  382. drupal_set_message(t("No action performed."));
  383. }
  384. return '';
  385. }