tripal_core.mviews.api.inc 14 KB

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