ChadoSchema.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Provides an application programming interface (API) for describing Chado tables.
  4. *
  5. * The functions provided in this documentation should not be called as is, but if you need
  6. * the Drupal-style array definition for any table, use the following function
  7. * call:
  8. *
  9. * $table_desc = chado_get_schema($table)
  10. *
  11. * where the variable $table contains the name of the table you want to
  12. * retireve. The chado_get_schema function determines the appropriate version of
  13. * Chado and uses the Drupal hook infrastructure to call the appropriate
  14. * hook function to retrieve the table schema.
  15. */
  16. class ChadoSchema {
  17. /**
  18. * @var string
  19. * The current version for this site. E.g. "1.3".
  20. */
  21. protected $version = '';
  22. /**
  23. * @var string
  24. * The name of the schema chado was installed in.
  25. */
  26. protected $schema_name = 'chado';
  27. /**
  28. * The ChadoSchema constructor.
  29. *
  30. * @param string $version
  31. * The current version for this site. E.g. "1.3". If a version is not provided, the
  32. * version of the current database will be looked up.
  33. */
  34. public function __construct($version = NULL, $schema_name = NULL) {
  35. // Set the version of the schema.
  36. if ($version === NULL) {
  37. $this->version = chado_get_version(TRUE);
  38. }
  39. else {
  40. $this->version = $version;
  41. }
  42. // Set the name of the schema.
  43. if ($schema_name === NULL) {
  44. $this->schema_name = chado_get_schema_name('chado');
  45. }
  46. else {
  47. $this->schema_name = $schema_name;
  48. }
  49. }
  50. /**
  51. * Returns the version number of the Chado this object references.
  52. *
  53. * @returns
  54. * The version of Chado
  55. */
  56. public function getVersion() {
  57. return $this->version;
  58. }
  59. /**
  60. * Retrieve the name of the PostgreSQL schema housing Chado.
  61. *
  62. * @return
  63. * The name of the schema.
  64. */
  65. public function getSchemaName() {
  66. return $this->schema_name;
  67. }
  68. /**
  69. * Retrieves the list of tables in the Chado schema. By default it only returns
  70. * the default Chado tables, but can return custom tables added to the
  71. * Chado schema if requested.
  72. *
  73. * @param $include_custom
  74. * Optional. Set as TRUE to include any custom tables created in the
  75. * Chado schema. Custom tables are added to Chado using the
  76. * tripal_chado_chado_create_table() function.
  77. *
  78. * @returns
  79. * An associative array where the key and value pairs are the Chado table names.
  80. */
  81. public function getTableNames($include_custom = FALSE) {
  82. $tables = array();
  83. if ($this->version == '1.3') {
  84. $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
  85. foreach ($tables_v1_3 as $table) {
  86. $tables[$table] = $table;
  87. }
  88. }
  89. if ($this->version == '1.2') {
  90. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  91. foreach ($tables_v1_2 as $table) {
  92. $tables[$table] = $table;
  93. }
  94. }
  95. if ($this->version == '1.11' or $v == '1.11 or older') {
  96. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  97. foreach ($tables_v1_11 as $table) {
  98. $tables[$table] = $table;
  99. }
  100. }
  101. // now add in the custom tables too if requested
  102. if ($include_custom) {
  103. $sql = "SELECT table FROM {tripal_custom_tables}";
  104. $resource = db_query($sql);
  105. foreach ($resource as $r) {
  106. $tables[$r->table] = $r->table;
  107. }
  108. }
  109. asort($tables);
  110. return $tables;
  111. }
  112. /**
  113. * Retrieves the chado tables Schema API array.
  114. *
  115. * @param $table
  116. * The name of the table to retrieve. The function will use the appopriate
  117. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  118. *
  119. * @returns
  120. * A Drupal Schema API array defining the table.
  121. */
  122. public function getTableSchema($table) {
  123. // first get the chado version.
  124. $v = $this->version;
  125. // get the table array from the proper chado schema
  126. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  127. // Call the module_invoke_all.
  128. $hook_name = "chado_schema_v" . $v . "_" . $table;
  129. $table_arr = module_invoke_all($hook_name);
  130. // If the module_invoke_all returned nothing then let's make sure there isn't
  131. // An API call we can call directly. The only time this occurs is
  132. // during an upgrade of a major Drupal version and tripal_core is disabled.
  133. if ((!$table_arr or !is_array($table_arr)) and
  134. function_exists('tripal_chado_' . $hook_name)) {
  135. $api_hook = "tripal_chado_" . $hook_name;
  136. $table_arr = $api_hook();
  137. }
  138. // if the table_arr is empty then maybe this is a custom table
  139. if (!is_array($table_arr) or count($table_arr) == 0) {
  140. $table_arr = chado_get_custom_table_schema($table);
  141. }
  142. return $table_arr;
  143. }
  144. /**
  145. * Check that any given Chado table exists.
  146. *
  147. * This function is necessary because Drupal's db_table_exists() function will
  148. * not look in any other schema but the one where Drupal is installed
  149. *
  150. * @param $table
  151. * The name of the chado table whose existence should be checked.
  152. *
  153. * @return
  154. * TRUE if the table exists in the chado schema and FALSE if it does not.
  155. */
  156. public function checkTableExists($table) {
  157. return chado_table_exists($table);
  158. }
  159. /**
  160. * Check that any given column in a Chado table exists.
  161. *
  162. * This function is necessary because Drupal's db_field_exists() will not
  163. * look in any other schema but the one were Drupal is installed
  164. *
  165. * @param $table
  166. * The name of the chado table.
  167. * @param $column
  168. * The name of the column in the chado table.
  169. *
  170. * @return
  171. * TRUE if the column exists for the table in the chado schema and
  172. * FALSE if it does not.
  173. *
  174. * @ingroup tripal_chado_schema_api
  175. */
  176. public function checkColumnExists($table, $column) {
  177. return chado_column_exists($table, $column);
  178. }
  179. /**
  180. * Check that any given column in a Chado table exists.
  181. *
  182. * This function is necessary because Drupal's db_field_exists() will not
  183. * look in any other schema but the one were Drupal is installed
  184. *
  185. * @param sequence
  186. * The name of the sequence
  187. * @return
  188. * TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
  189. *
  190. * @ingroup tripal_chado_schema_api
  191. */
  192. public function checkSequenceExists($sequence) {
  193. return chado_sequence_exists($sequence);
  194. }
  195. /**
  196. * A Chado-aware replacement for the db_index_exists() function.
  197. *
  198. * @param $table
  199. * The table to be altered.
  200. * @param $name
  201. * The name of the index.
  202. */
  203. function checkIndexExists($table, $name) {
  204. return chado_index_exists($table, $name);
  205. }
  206. }