tripal_core.custom_tables.api.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * @defgroup tripal_custom_tables_api Custom Tables API
  4. * @ingroup tripal_core_api
  5. * @{
  6. * Provides an API to manage custom tables in Chado.
  7. * @}
  8. */
  9. /**
  10. * Edits a custom table in the chado database. It supports
  11. * using the Drupal Schema API array.
  12. *
  13. * @param $table_id
  14. * The table_id of the table to edit
  15. * @param $table_name
  16. * The name of the custom table
  17. * @param $schema
  18. * Use the Schema API array to define the custom table.
  19. * @param $skip_creation
  20. * Set as TRUE to skip dropping and re-creation of the table. This is
  21. * useful if the table was already created through another means and you
  22. * simply want to make Tripal aware of the table schema.
  23. *
  24. * @ingroup tripal_custom_tables_api
  25. */
  26. function tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1) {
  27. // Create a new record
  28. $record = new stdClass();
  29. $record->table_id = $table_id;
  30. $record->table_name = $table_name;
  31. $record->schema = serialize($schema);
  32. // get the current custom table record
  33. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  34. $results = db_query($sql, array(':table_id' => $table_id));
  35. $custom_table = $results->fetchObject();
  36. // if the user changed the table name, we want to drop the old one and force
  37. // creation of the new one.
  38. if ($custom_table->table_name != $table_name) {
  39. chado_query("DROP TABLE %s", $custom_table->table_name);
  40. $skip_creation = 0; // we want to create the table
  41. }
  42. // if skip creation is not set, then drop the table from chado if it exists
  43. if (!$skip_creation) {
  44. if (db_table_exists($custom_table->table_name)) {
  45. chado_query("DROP TABLE %s", $custom_table->table_name);
  46. drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
  47. }
  48. }
  49. // update the custom table record and re-create the table in Chado
  50. if (drupal_write_record('tripal_custom_tables', $record, 'table_id')) {
  51. // drop the table from chado if it exists
  52. if (!$skip_creation) {
  53. if (db_table_exists($custom_table->table_name)) {
  54. chado_query("DROP TABLE %s", $custom_table->table_name);
  55. drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
  56. }
  57. // re-create the table
  58. if (!tripal_core_create_custom_table ($table_name, $schema)) {
  59. drupal_set_message(t("Could not create the custom table. Check Drupal error report logs."));
  60. }
  61. else {
  62. drupal_set_message(t("Custom table '%name' created", array('%name' => $table_name)));
  63. }
  64. }
  65. // TODO: add FK constraints
  66. }
  67. }
  68. /**
  69. * Add a new table to the Chado schema. This function is simply a wrapper for
  70. * the db_create_table() function of Drupal, but ensures the table is created
  71. * inside the Chado schema rather than the Drupal schema. If the table already
  72. * exists then it will be dropped and recreated using the schema provided.
  73. * However, it will only drop a table if it exsits in the tripal_custom_tables
  74. * table. This way the function cannot be used to accidentally alter existing
  75. * non custom tables. If $skip_creation is set then the table is simply
  76. * added to the tripal_custom_tables and no table is created in Chado.
  77. *
  78. * @param $table
  79. * The name of the table to create.
  80. * @param $schema
  81. * A Drupal-style Schema API definition of the table
  82. * @param $skip_creation
  83. * Set as TRUE to skip dropping and re-creation of the table if it already
  84. * exists. This is useful if the table was already created through another
  85. * means and you simply want to make Tripal aware of the table schema. If the
  86. * table does not exist it will be created.
  87. *
  88. * @return
  89. * TRUE on success, FALSE on failure
  90. *
  91. * @ingroup tripal_custom_tables_api
  92. */
  93. function tripal_core_create_custom_table($table, $schema, $skip_creation = 1) {
  94. global $databases;
  95. $created = 0;
  96. $recreated = 0;
  97. // see if the table entry already exists in the tripal_custom_tables table.
  98. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
  99. $results = db_query($sql, array(':table_name' => $table));
  100. $centry = $results->fetchObject();
  101. // check to see if the table already exists in the chado schema
  102. $exists = chado_table_exists($table);
  103. // if the table does not exist then create it
  104. if (!$exists) {
  105. try {
  106. $ret = db_create_table('chado.' . $table, $schema);
  107. $created = 1;
  108. }
  109. catch (Exception $e) {
  110. $error = $e->getMessage();
  111. watchdog('tripal_core', "Error adding custom table: @message", array('@message' => $error), WATCHDOG_ERROR);
  112. drupal_set_message("Could not add custom table. $error.", "error");
  113. return FALSE;
  114. }
  115. }
  116. // if the table exists in Chado and in our custom table and
  117. // skip creation is turned off then drop and re-create the table
  118. if ($exists and is_object($centry) and !$skip_creation) {
  119. // drop the table we'll recreate it with the new schema
  120. try {
  121. chado_query('DROP TABLE {' . $table . '}');
  122. db_create_table('chado.' . $table, $schema);
  123. $recreated = 1;
  124. }
  125. catch (Exception $e) {
  126. $error = $e->getMessage();
  127. watchdog('tripal_core', "Error adding custom table: @message",
  128. array('@message' => $error), WATCHDOG_ERROR);
  129. drupal_set_message("Could not add custom table. $error.", "error");
  130. return FALSE;
  131. }
  132. }
  133. // add an entry in the tripal_custom_table
  134. $record = new stdClass();
  135. $record->table_name = $table;
  136. $record->schema = serialize($schema);
  137. // if an entry already exists then remove it
  138. if ($centry) {
  139. $sql = "DELETE FROM {tripal_custom_tables} WHERE table_name = :table_name";
  140. db_query($sql, array(':table_name' => $table));
  141. }
  142. $success = drupal_write_record('tripal_custom_tables', $record);
  143. if (!$success) {
  144. watchdog('tripal_core', "Error adding custom table %table_name.",
  145. array('%table_name' => $table), WATCHDOG_ERROR);
  146. drupal_set_message(t("Could not add custom table %table_name.
  147. Please check the schema array.", array('%table_name' => $table)), 'error');
  148. return FALSE;
  149. }
  150. // now add any foreign key constraints
  151. if (!$skip_creation and array_key_exists('foreign keys', $schema)) {
  152. // iterate through the foreign keys and add each one
  153. $fkeys = $schema['foreign keys'];
  154. foreach ($fkeys as $fktable => $fkdetails) {
  155. $relations = $fkdetails['columns'];
  156. foreach ($relations as $left => $right) {
  157. $sql = '
  158. ALTER TABLE {' . $table . '}
  159. ADD CONSTRAINT ' . $table . '_' . $left . '_fkey FOREIGN KEY (' . $left . ')
  160. REFERENCES {' . $fktable . '} (' . $right . ')
  161. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  162. ';
  163. try {
  164. chado_query($sql);
  165. }
  166. catch (Exception $e) {
  167. $error = $e->getMessage();
  168. watchdog('tripal_core', "Error, could not add foreign key contraint to custom table: %error",
  169. array('%error' => $error), WATCHDOG_ERROR);
  170. drupal_set_message("Could not add foreign key contraint to table: $error", 'error');
  171. return FALSE;
  172. }
  173. }
  174. }
  175. }
  176. if ($created) {
  177. drupal_set_message("Custom table created successfully.", 'status');
  178. }
  179. elseif ($recreated) {
  180. drupal_set_message("Custom table re-created successfully.", 'status');
  181. }
  182. else {
  183. drupal_set_message("Custom table already exists. Table structure not changed, but definition array has been saved.", 'status');
  184. }
  185. return TRUE;
  186. }
  187. /**
  188. * Retrieve the custom table id given the name
  189. *
  190. * @param $table_name
  191. * The name of the custom table
  192. *
  193. * @return
  194. * The unique identifier for the given table
  195. *
  196. * @ingroup tripal_custom_tables_api
  197. */
  198. function tripal_custom_tables_get_table_id($table_name) {
  199. if (db_table_exists('tripal_custom_tables')) {
  200. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
  201. $results = db_query($sql, array(':table_name' => $table_name));
  202. $custom_table = $results->fetchObject();
  203. if ($custom_table) {
  204. return $custom_table->table_id;
  205. }
  206. }
  207. return FALSE;
  208. }