tripal_chado.mviews.api.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage materialized
  5. * views in Chado.
  6. */
  7. /**
  8. * @defgroup tripal_mviews_api Chado Materalized Views
  9. * @ingroup tripal_chado_api
  10. * @{
  11. * Provides an application programming interface (API) to manage materialized
  12. * views in Chado The Perl-based chado comes with an interface for managing
  13. * materialzed views. This API provides an alternative Drupal-based method.
  14. * @}
  15. */
  16. /**
  17. * Add a materialized view to the chado database to help speed data access. This
  18. * function supports the older style where postgres column specifications
  19. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  20. * supports the newer preferred method where the materialized view is described
  21. * using the Drupal Schema API array.
  22. *
  23. * @param $name
  24. * The name of the materialized view.
  25. * @param $modulename
  26. * The name of the module submitting the materialized view
  27. * (e.g. 'tripal_library').
  28. * @param $mv_schema
  29. * If using the newer Schema API array to define the materialized view then
  30. * this variable should contain the array or a string representation of the
  31. * array.
  32. * @param $query
  33. * The SQL query that loads the materialized view with data.
  34. * @param $comment
  35. * A string containing a description of the materialized view.
  36. * @param $redirect
  37. * Optional (default: TRUE). By default this function redirects back to
  38. * admin pages. However, when called by Drush we don't want to redirect. This
  39. * parameter allows this to be used as a true API function.
  40. *
  41. * @ingroup tripal_mviews_api
  42. */
  43. function chado_add_mview($name, $modulename, $mv_schema, $query, $comment = NULL, $redirect = TRUE) {
  44. if (!array_key_exists('table', $mv_schema)) {
  45. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  46. 'Must have a table name when creating an mview.', array());
  47. return NULL;
  48. }
  49. $mv_table = $mv_schema['table'];
  50. // See if the mv_table name already exsists.
  51. $mview_id = db_query(
  52. 'SELECT mview_id FROM {tripal_mviews} WHERE name = :name',
  53. array(':name' => $name))->fetchField();
  54. if(!$mview_id) {
  55. $transaction = db_transaction();
  56. try {
  57. // Create a new record.
  58. $record = new stdClass();
  59. $record->name = $name;
  60. $record->modulename = $modulename;
  61. $record->mv_table = $mv_table;
  62. $record->query = $query;
  63. $record->comment = $comment;
  64. // Convert the schema into a string format.
  65. $str_schema = var_export($mv_schema, TRUE);
  66. $str_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $str_schema);
  67. $record->mv_schema = $str_schema;
  68. // Add the record to the tripal_mviews table and if successful
  69. // create the new materialized view in the chado schema.
  70. if (drupal_write_record('tripal_mviews', $record)) {
  71. // Drop the table from chado if it exists.
  72. if (chado_table_exists($mv_table)) {
  73. $sql = 'DROP TABLE {' . $mv_table . '}';
  74. chado_query($sql);
  75. }
  76. // Create the table.
  77. chado_create_custom_table($mv_table, $mv_schema, 0, $record->mview_id, $redirect);
  78. }
  79. }
  80. catch (Exception $e) {
  81. $transaction->rollback();
  82. watchdog_exception('tripal_chado', $e);
  83. $error = _drupal_decode_exception($e);
  84. drupal_set_message(t("Could not create the materialized view %table_name: %message.",
  85. array('%table_name' => $name, '%message' => $error['!message'])), 'error');
  86. return FALSE;
  87. }
  88. drupal_set_message(t("Materialized view '%name' created", array('%name' => $name)));
  89. return TRUE;
  90. }
  91. else {
  92. drupal_set_message(t("Materialized view, $name, already exists. Skipping creation.", array('%name' => $name)));
  93. return FALSE;
  94. }
  95. }
  96. /**
  97. * Edits a materialized view to the chado database to help speed data access.
  98. * This function supports the older style where postgres column specifications
  99. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  100. * supports the newer preferred method where the materialized view is described
  101. * using the Drupal Schema API array.
  102. *
  103. * @param $mview_id
  104. * The mview_id of the materialized view to edit.
  105. * @param $name
  106. * The name of the materialized view.
  107. * @param $modulename
  108. * The name of the module submitting the materialized view
  109. * (e.g. 'tripal_library').
  110. * @param $mv_table
  111. * The name of the table to add to chado. This is the table that can be
  112. * queried.
  113. * @param $mv_specs
  114. * The table definition.
  115. * @param $indexed
  116. * The columns that are to be indexed.
  117. * @param $query
  118. * The SQL query that loads the materialized view with data.
  119. * @param $special_index
  120. * currently not used.
  121. * @param $comment
  122. * A string containing a description of the materialized view.
  123. * @param $mv_schema
  124. * If using the newer Schema API array to define the materialized view then
  125. * this variable should contain the array.
  126. *
  127. * @ingroup tripal_mviews_api
  128. */
  129. function chado_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  130. $indexed, $query, $special_index, $comment = NULL, $mv_schema = NULL) {
  131. $transaction = db_transaction();
  132. try {
  133. // get the table name from the schema array
  134. $schema_arr = array();
  135. if ($mv_schema) {
  136. // get the schema from the mv_specs and use it to add the custom table
  137. eval("\$schema_arr = $mv_schema;");
  138. $mv_table = $schema_arr['table'];
  139. }
  140. $record = new stdClass();
  141. $record->mview_id = $mview_id;
  142. $record->name = $name;
  143. $record->modulename = $modulename;
  144. $record->query = $query;
  145. $record->last_update = 0;
  146. $record->status = '';
  147. $record->comment = $comment;
  148. $record->mv_schema = $mv_schema;
  149. $record->mv_table = $mv_table;
  150. // update the record to the tripal_mviews table
  151. drupal_write_record('tripal_mviews', $record, 'mview_id');
  152. // get the view before we update and check to see if the table structure has
  153. // changed. If so, then we want to drop and recreate the table. If not, then
  154. // just save the updated SQL.
  155. $create_table = 1;
  156. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  157. $results = db_query($sql, array(':mview_id' => $mview_id));
  158. $mview = $results->fetchObject();
  159. if ($mview->mv_schema == $mv_schema and $mview->mv_table == $mv_table) {
  160. chado_create_custom_table($mv_table, $schema_arr, 0, $record->mview_id);
  161. drupal_set_message(t("Materialized view '%name' created", array('%name' => $name)));
  162. }
  163. else {
  164. $message = "View '%name' updated. All records remain. ";
  165. if ($query != $mview->query) {
  166. $message .= "Please repopulate the view to use the updated query.";
  167. }
  168. drupal_set_message(t($message, array('%name' => $name)));
  169. }
  170. // construct the indexes SQL if needed
  171. $index = '';
  172. if ($indexed) {
  173. // add to the array of values
  174. $vals = preg_split("/[\n,]+/", $indexed);
  175. $index = '';
  176. foreach ($vals as $field) {
  177. $field = trim($field);
  178. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  179. }
  180. }
  181. }
  182. catch (Exception $e) {
  183. $transaction->rollback();
  184. watchdog_exception('tripal_chado', $e);
  185. $error = _drupal_decode_exception($e);
  186. drupal_set_message(t("Could not update materialized view '%table_name': %message.",
  187. array('%table_name' => $mv_table, '%message' => $error['!message'])), 'error');
  188. return FALSE;
  189. }
  190. }
  191. /**
  192. * Retrieve the materialized view_id given the name.
  193. *
  194. * @param $view_name
  195. * The name of the materialized view.
  196. *
  197. * @return
  198. * The unique identifier for the given view.
  199. *
  200. * @ingroup tripal_mviews_api
  201. */
  202. function chado_get_mview_id($view_name) {
  203. if (db_table_exists('tripal_mviews')) {
  204. $sql = "SELECT * FROM {tripal_mviews} WHERE name = :name";
  205. $results = db_query($sql, array(':name' => $view_name));
  206. $mview = $results->fetchObject();
  207. if ($mview) {
  208. return $mview->mview_id;
  209. }
  210. }
  211. return FALSE;
  212. }
  213. /**
  214. * Retrieves the list of materialized views in this site.
  215. *
  216. * @returns
  217. * An associative array where the key and value pairs are the table names.
  218. *
  219. * @ingroup tripal_mviews_api
  220. */
  221. function chado_get_mview_table_names() {
  222. $sql = "SELECT name FROM {tripal_mviews}";
  223. $resource = db_query($sql);
  224. $tables = array();
  225. foreach ($resource as $r) {
  226. $tables[$r->name] = $r->name;
  227. }
  228. asort($tables);
  229. return $tables;
  230. }
  231. /**
  232. * Populates the specified Materialized View.
  233. *
  234. * @param $mview_id
  235. * The unique ID of the materialized view for the action to be performed on.
  236. *
  237. * @ingroup tripal_mviews_api
  238. */
  239. function chado_refresh_mview($mview_id) {
  240. global $user;
  241. if (!$mview_id) {
  242. return '';
  243. }
  244. // Get this mview details.
  245. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  246. $results = db_query($sql, array(':mview_id' => $mview_id));
  247. $mview = $results->fetchObject();
  248. // Add a job to populate the mview.
  249. $args = array("$mview_id");
  250. tripal_add_job("Populate materialized view '$mview->name'", 'tripal_chado',
  251. 'chado_populate_mview', $args, $user->uid);
  252. }
  253. /**
  254. * Retrieves the list of materialized view IDs and their names.
  255. *
  256. * @return
  257. * An array of objects with the following properties: mview_id, name.
  258. *
  259. * @ingroup tripal_mviews_api
  260. *
  261. */
  262. function chado_get_mviews() {
  263. $results = db_select('tripal_mviews', 'tm')
  264. ->fields('tm', array('mview_id', 'name'))
  265. ->execute();
  266. $list = array();
  267. while ($mview = $results->fetchObject()) {
  268. $list[] = $mview;
  269. }
  270. return $list;
  271. }
  272. /**
  273. * Does the specified action for the specified Materialized View.
  274. *
  275. * @param $op
  276. * The action to be taken. One of update or delete.
  277. * @param $mview_id
  278. * The unique ID of the materialized view for the action to be performed on.
  279. *
  280. * @ingroup tripal_mviews_api
  281. */
  282. function chado_delete_mview($mview_id) {
  283. global $user;
  284. if (!$mview_id) {
  285. return '';
  286. }
  287. // Get this mview details.
  288. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  289. $results = db_query($sql, array(':mview_id' => $mview_id));
  290. $mview = $results->fetchObject();
  291. // If op is to delete then do so.
  292. // Remove the mview from the tripal_mviews table.
  293. $sql = "DELETE FROM {tripal_mviews} WHERE mview_id = $mview_id";
  294. db_query($sql);
  295. // Does the table already exist?
  296. $mview_exists = chado_table_exists($mview->mv_table);
  297. // Drop the table from chado if it exists.
  298. if ($mview_exists) {
  299. $sql = "DROP TABLE {" . $mview->mv_table . "}";
  300. $success = chado_query($sql);
  301. if ($success) {
  302. drupal_set_message(t("Materialized view, %name, deleted.", array('%name' => $mview->name)));
  303. }
  304. else {
  305. drupal_set_message(t("Problem deleting materialized view, %name.", array('%name' => $mview->name)), 'error');
  306. }
  307. }
  308. }
  309. /**
  310. * Update a Materialized View.
  311. *
  312. * @param $mview_id
  313. * The unique identifier for the materialized view to be updated.
  314. *
  315. * @return
  316. * True if successful, FALSE otherwise.
  317. *
  318. * @ingroup tripal_mviews_api
  319. */
  320. function chado_populate_mview($mview_id) {
  321. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  322. $results = db_query($sql, array(':mview_id' => $mview_id));
  323. $mview = $results->fetchObject();
  324. if ($mview) {
  325. // Execute the query inside a transaction so that it doesn't destroy
  326. // existing data that may leave parts of the site unfunctional.
  327. $transaction = db_transaction();
  328. $previous_db = chado_set_active('chado'); // use chado database
  329. try {
  330. $success = chado_query("DELETE FROM {" . $mview->mv_table . "}");
  331. $success = chado_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");
  332. // If success get the number of results and update the table record.
  333. if ($success) {
  334. $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
  335. $results = chado_query($sql);
  336. $count = $results->fetchObject();
  337. $record = new stdClass();
  338. $record->mview_id = $mview_id;
  339. $record->last_update = time();
  340. $record->status = "Populated with " . number_format($count->cnt) . " rows";
  341. drupal_write_record('tripal_mviews', $record, 'mview_id');
  342. }
  343. // If not success then throw an error.
  344. else {
  345. throw new Exception("ERROR populating the materialized view ". $mview->mv_table . ". See Drupal's recent log entries for details.");
  346. }
  347. chado_set_active($previous_db);
  348. }
  349. catch (Exception $e) {
  350. $transaction->rollback();
  351. chado_set_active($previous_db);
  352. // Print and save the error message.
  353. $record = new stdClass();
  354. $record->mview_id = $mview_id;
  355. $record->status = "ERROR populating $mview->mv_table. See Drupal's recent log entries for details.\n";
  356. drupal_write_record('tripal_mviews', $record, 'mview_id');
  357. watchdog_exception('tripal_mviews', $e);
  358. return FALSE;
  359. }
  360. print "Done.\n";
  361. return TRUE;
  362. }
  363. }