tripal_chado.mviews.api.inc 13 KB

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