tripal_core.mviews.api.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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_core_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_table
  27. * The name of the table to add to chado. This is the table that can be queried.
  28. * @param $mv_specs
  29. * The table definition
  30. * @param $indexed
  31. * The columns that are to be indexed
  32. * @param $query
  33. * The SQL query that loads the materialized view with data
  34. * @param $special_index
  35. * currently not used
  36. * @param $comment
  37. * A string containing a description of the materialized view
  38. *
  39. * @ingroup tripal_mviews_api
  40. *
  41. function tripal_add_legacy_mview($name, $modulename, $mv_table, $mv_specs, $indexed,
  42. $query, $special_index, $comment = NULL) {
  43. // Create a new record
  44. $record = new stdClass();
  45. $record->name = $name;
  46. $record->modulename = $modulename;
  47. $record->mv_table = $mv_table;
  48. $record->mv_specs = $mv_specs;
  49. $record->indexed = $indexed;
  50. $record->query = $query;
  51. $record->special_index = $special_index;
  52. $record->comment = $comment;
  53. // add the record to the tripal_mviews table and if successful
  54. // create the new materialized view in the chado schema
  55. if (drupal_write_record('tripal_mviews', $record)) {
  56. // drop the table from chado if it exists
  57. if (chado_table_exists($mv_table)) {
  58. $sql = "DROP TABLE {$mv_table}";
  59. chado_query($sql);
  60. }
  61. // now construct the indexes
  62. $index = '';
  63. if ($indexed) {
  64. // add to the array of values
  65. $vals = preg_split("/[\n,]+/", $indexed);
  66. $index = '';
  67. foreach ($vals as $field) {
  68. $field = trim($field);
  69. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  70. }
  71. }
  72. }
  73. // add the table to the database
  74. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  75. $previous_db = chado_set_active('chado'); // use chado database
  76. $results = db_query($sql);
  77. chado_set_active($previous_db); // now use drupal database
  78. if ($results) {
  79. drupal_set_message(t("View '%name' created", array('%name' => $name)));
  80. }
  81. else {
  82. drupal_set_message(t("Failed to create the materialized view table: '%mv_table'", array('%mv_table' => $mv_table)), 'error');
  83. }
  84. }
  85. */
  86. /**
  87. * Add a materialized view to the chado database to help speed data access. This
  88. * function supports the older style where postgres column specifications
  89. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  90. * supports the newer preferred method where the materialized view is described
  91. * using the Drupal Schema API array.
  92. *
  93. * @param $name
  94. * The name of the materialized view.
  95. * @param $modulename
  96. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  97. * @param $mv_schema
  98. * If using the newer Schema API array to define the materialized view then
  99. * this variable should contain the array or a string representation of the
  100. * array.
  101. * @param $query
  102. * The SQL query that loads the materialized view with data
  103. * @param $comment
  104. * A string containing a description of the materialized view
  105. *
  106. * @ingroup tripal_mviews_api
  107. */
  108. function tripal_add_mview($name, $modulename, $schema_arr, $query, $comment = NULL) {
  109. $mv_table = $schema_arr['table'];
  110. if (!$mv_table) {
  111. tripal_report_error('tripal_core', TRIPAL_ERROR,
  112. 'Must have a table name when creating an mview.', array());
  113. return NULL;
  114. }
  115. // Create a new record
  116. $record = new stdClass();
  117. $record->name = $name;
  118. $record->modulename = $modulename;
  119. $record->mv_table = $mv_table;
  120. $record->query = $query;
  121. $record->comment = $comment;
  122. $record->mv_schema = $mv_schema;
  123. // add the record to the tripal_mviews table and if successful
  124. // create the new materialized view in the chado schema
  125. if (drupal_write_record('tripal_mviews', $record)) {
  126. // drop the table from chado if it exists
  127. if (chado_table_exists($mv_table)) {
  128. $sql = 'DROP TABLE {' . $mv_table . '}';
  129. chado_query($sql);
  130. }
  131. // create the table
  132. if (!chado_create_custom_table ($mv_table, $schema_arr, 0)) {
  133. drupal_set_message(t("Could not create the materialized view. Check Drupal error report logs."), 'error');
  134. }
  135. else {
  136. drupal_set_message(t("View '%name' created", array('%name' => $name)));
  137. }
  138. }
  139. }
  140. /**
  141. * Edits a materialized view to the chado database to help speed data access.This
  142. * function supports the older style where postgres column specifications
  143. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  144. * supports the newer preferred method where the materialized view is described
  145. * using the Drupal Schema API array.
  146. *
  147. * @param $mview_id
  148. * The mview_id of the materialized view to edit
  149. * @param $name
  150. * The name of the materialized view.
  151. * @param $modulename
  152. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  153. * @param $mv_table
  154. * The name of the table to add to chado. This is the table that can be queried.
  155. * @param $mv_specs
  156. * The table definition
  157. * @param $indexed
  158. * The columns that are to be indexed
  159. * @param $query
  160. * The SQL query that loads the materialized view with data
  161. * @param $special_index
  162. * currently not used
  163. * @param $comment
  164. * A string containing a description of the materialized view
  165. * @param $mv_schema
  166. * If using the newer Schema API array to define the materialized view then
  167. * this variable should contain the array.
  168. *
  169. * @ingroup tripal_mviews_api
  170. */
  171. function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  172. $indexed, $query, $special_index, $comment = NULL, $mv_schema = NULL) {
  173. // get the table name from the schema array
  174. $schema_arr = array();
  175. if ($mv_schema) {
  176. // get the schema from the mv_specs and use it to add the custom table
  177. eval("\$schema_arr = $mv_schema;");
  178. $mv_table = $schema_arr['table'];
  179. }
  180. $record = new stdClass();
  181. $record->mview_id = $mview_id;
  182. $record->name = $name;
  183. $record->modulename = $modulename;
  184. $record->query = $query;
  185. $record->last_update = 0;
  186. $record->status = '';
  187. $record->comment = $comment;
  188. // get the view before we update and check to see if the table structure has
  189. // changed. IF so, then we want to drop and recreate the table. If not, then
  190. // just save the updated SQL.
  191. $create_table = 1;
  192. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  193. $results = db_query($sql, array(':mview_id' => $mview_id));
  194. $mview = $results->fetchObject();
  195. if ($mview->mv_schema == $mv_schema and $mview->mv_table == $mv_table and
  196. $mview->mv_specs == $mv_specs and $mview->indexed == $indexed and
  197. $mview->special_index == $special_index) {
  198. // nothing has changed so simpy update the SQL and other fields
  199. $create_table = 0;
  200. }
  201. else {
  202. // add in the table structure fields
  203. $record->mv_schema = $mv_schema;
  204. $record->mv_table = $mv_table;
  205. $record->mv_specs = $mv_specs;
  206. $record->indexed = $indexed;
  207. $record->query = $query;
  208. $record->special_index = $special_index;
  209. }
  210. // if we are going to create the table then we must first drop it if it exists
  211. if ($create_table) {
  212. $previous_db = chado_set_active('chado'); // use chado database
  213. if (db_table_exists($mview->mv_table)) {
  214. $sql = "DROP TABLE :table_name";
  215. db_query($sql, array(':table_name' => $mview->mv_table));
  216. drupal_set_message(t("View '%name' dropped", array('%name' => $name)));
  217. }
  218. chado_set_active($previous_db); // now use drupal database
  219. }
  220. // update the record to the tripal_mviews table and if successful
  221. // create the new materialized view in the chado schema
  222. if (drupal_write_record('tripal_mviews', $record, 'mview_id')) {
  223. // construct the indexes SQL if needed
  224. $index = '';
  225. if ($indexed) {
  226. // add to the array of values
  227. $vals = preg_split("/[\n,]+/", $indexed);
  228. $index = '';
  229. foreach ($vals as $field) {
  230. $field = trim($field);
  231. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  232. }
  233. }
  234. // re-create the table differently depending on if it the traditional method
  235. // or the Drupal Schema API method
  236. if ($create_table and $mv_schema) {
  237. if (!chado_create_custom_table($mv_table, $schema_arr, 0)) {
  238. drupal_set_message(t("Could not create the materialized view. Check Drupal error report logs."));
  239. }
  240. else {
  241. drupal_set_message(t("View '%name' created", array('%name' => $name)));
  242. }
  243. }
  244. if ($create_table and !$mv_schema) {
  245. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  246. $results = chado_query($sql);
  247. if ($results) {
  248. drupal_set_message(t("View '%name' created. All records cleared. Please re-populate the view.",
  249. array('%name' => $name)));
  250. }
  251. else {
  252. drupal_set_message(t("Failed to create the materialized view table: '%mv_table'",
  253. array('%mv_table' => $mv_table)), 'error');
  254. }
  255. }
  256. if (!$create_table) {
  257. $message = "View '%name' updated. All records remain. ";
  258. if ($query != $mview->query) {
  259. $message .= "Please repopulate the view to use updated query.";
  260. }
  261. drupal_set_message(t($message, array('%name' => $name)));
  262. }
  263. }
  264. else {
  265. drupal_set_message(t("Failed to update the materialized view: '%mv_table'",
  266. array('%mv_table' => $mv_table)), 'error');
  267. }
  268. }
  269. /**
  270. * Retrieve the materialized view_id given the name
  271. *
  272. * @param $view_name
  273. * The name of the materialized view
  274. *
  275. * @return
  276. * The unique identifier for the given view
  277. *
  278. * @ingroup tripal_mviews_api
  279. */
  280. function tripal_mviews_get_mview_id($view_name) {
  281. if (db_table_exists('tripal_mviews')) {
  282. $sql = "SELECT * FROM {tripal_mviews} WHERE name = :name";
  283. $results = db_query($sql, array(':name' => $view_name));
  284. $mview = $results->fetchObject();
  285. if ($mview) {
  286. return $mview->mview_id;
  287. }
  288. }
  289. return FALSE;
  290. }
  291. /**
  292. * Does the specified action for the specified Materialized View
  293. *
  294. * @param $mview_id
  295. * The unique ID of the materialized view for the action to be performed on
  296. * @param $redirect
  297. * TRUE/FALSE depending on whether you want to redirect the user to admin/tripal/mviews
  298. *
  299. * @ingroup tripal_mviews_api
  300. */
  301. function tripal_add_populate_mview($mview_id, $redirect = FALSE) {
  302. global $user;
  303. if (!$mview_id) {
  304. return '';
  305. }
  306. // get this mview details
  307. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  308. $results = db_query($sql, array(':mview_id' => $mview_id));
  309. $mview = $results->fetchObject();
  310. // add a job or perform the action based on the given operation
  311. $args = array("$mview_id");
  312. tripal_add_job("Populate materialized view '$mview->name'", 'tripal_core',
  313. 'tripal_populate_mview', $args, $user->uid);
  314. // Redirect the user
  315. if ($redirect) {
  316. drupal_goto("admin/tripal/mviews");
  317. }
  318. }
  319. /**
  320. * Does the specified action for the specified Materialized View
  321. *
  322. * @param $op
  323. * The action to be taken. One of update or delete
  324. * @param $mview_id
  325. * The unique ID of the materialized view for the action to be performed on
  326. * @param $redirect
  327. * TRUE/FALSE depending on whether you want to redirect the user to admin/tripal/mviews
  328. *
  329. * @ingroup tripal_mviews_api
  330. */
  331. function tripal_delete_mview($mview_id, $redirect = FALSE) {
  332. global $user;
  333. if (!$mview_id) {
  334. return '';
  335. }
  336. // get this mview details
  337. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  338. $results = db_query($sql, array(':mview_id' => $mview_id));
  339. $mview = $results->fetchObject();
  340. // if op is to delete then do so
  341. // remove the mview from the tripal_mviews table
  342. $sql = "DELETE FROM {tripal_mviews} WHERE mview_id = $mview_id";
  343. db_query($sql);
  344. // does the table already exist?
  345. $mview_exists = db_table_exists('chado.' . $mview->mv_table);
  346. // drop the table from chado if it exists
  347. if ($mview_exists) {
  348. $sql = "DROP TABLE {" . $mview->mv_table . "}";
  349. chado_query($sql);
  350. }
  351. // Redirect the user
  352. if ($redirect) {
  353. drupal_goto("admin/tripal/mviews");
  354. }
  355. }
  356. /**
  357. * Update a Materialized View
  358. *
  359. * @param $mview_id
  360. * The unique identifier for the materialized view to be updated
  361. *
  362. * @return
  363. * True if successful, FALSE otherwise
  364. *
  365. * @ingroup tripal_mviews_api
  366. */
  367. function tripal_populate_mview($mview_id) {
  368. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  369. $results = db_query($sql, array(':mview_id' => $mview_id));
  370. $mview = $results->fetchObject();
  371. if ($mview) {
  372. // execute the query inside a transaction so that it doesn't destroy existing data
  373. // that may leave parts of the site unfunctional
  374. $transaction = db_transaction();
  375. try {
  376. $previous_db = chado_set_active('chado'); // use chado database
  377. $success = db_query("DELETE FROM {" . $mview->mv_table . "}");
  378. $success = db_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");
  379. chado_set_active($previous_db); // now use drupal database
  380. // if success get the number of results and update the table record
  381. if ($success) {
  382. $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
  383. $results = chado_query($sql);
  384. $count = $results->fetchObject();
  385. $record = new stdClass();
  386. $record->mview_id = $mview_id;
  387. $record->last_update = REQUEST_TIME;
  388. $record->status = "Populated with " . number_format($count->cnt) . " rows";
  389. drupal_write_record('tripal_mviews', $record, 'mview_id');
  390. }
  391. // if not success then throw an error
  392. else {
  393. throw new Exception("ERROR populating the materialized view ". $mview->mv_table . ". See Drupal's recent log entries for details.");
  394. }
  395. }
  396. catch (Exception $e) {
  397. // print and save the error message
  398. $record = new stdClass();
  399. $record->mview_id = $mview_id;
  400. $record->status = "ERROR populating $mview->mv_table. See Drupal's recent log entries for details.\n";
  401. drupal_write_record('tripal_mviews', $record, 'mview_id');
  402. watchdog_exception('tripal_mviews', $e);
  403. $transaction->rollback();
  404. return FALSE;
  405. }
  406. print "Done.\n";
  407. return TRUE;
  408. }
  409. }