tripal_core_mviews.api.inc

Contains functions for the Materialized Views API

tripal_mviews_api Materalized Views API

Provides an application programming interface (API) to manage materialized views in Chado. The Perl-based chado comes with an interface for managing materialzed views. This API provides an alternative Drupal-based method.

File

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

Related topics