tripal_core_mviews.api.inc 14 KB

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