tripal_chado.mviews.api.inc 12 KB

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