ChadoSchema.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Provides an application programming interface (API) for describing Chado tables.
  4. *
  5. * If you need the Drupal-style array definition for any table, use the following:
  6. * @code
  7. $chado_schema = new \ChadoSchema();
  8. $table_schema = $chado_schema->getTableSchema($table_name);
  9. * @endcode
  10. *
  11. * where the variable $table contains the name of the table you want to
  12. * retireve. The getTableSchema method 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. // Check functions require the chado schema be local and installed...
  50. // So lets check that now...
  51. if (!chado_is_local()) {
  52. tripal_report_error(
  53. 'ChadoSchema',
  54. TRIPAL_NOTICE,
  55. 'The ChadoSchema class requires chado be installed within the drupal database
  56. in a separate schema for any compliance checking functionality.'
  57. );
  58. }
  59. if (!chado_is_installed()) {
  60. tripal_report_error(
  61. 'ChadoSchema',
  62. TRIPAL_NOTICE,
  63. 'The ChadoSchema class requires chado be installed
  64. for any compliance checking functionality.'
  65. );
  66. }
  67. }
  68. /**
  69. * Returns the version number of the Chado this object references.
  70. *
  71. * @returns
  72. * The version of Chado
  73. */
  74. public function getVersion() {
  75. return $this->version;
  76. }
  77. /**
  78. * Retrieve the name of the PostgreSQL schema housing Chado.
  79. *
  80. * @return
  81. * The name of the schema.
  82. */
  83. public function getSchemaName() {
  84. return $this->schema_name;
  85. }
  86. /**
  87. * Retrieves the list of tables in the Chado schema. By default it only returns
  88. * the default Chado tables, but can return custom tables added to the
  89. * Chado schema if requested.
  90. *
  91. * @param $include_custom
  92. * Optional. Set as TRUE to include any custom tables created in the
  93. * Chado schema. Custom tables are added to Chado using the
  94. * tripal_chado_chado_create_table() function.
  95. *
  96. * @returns
  97. * An associative array where the key and value pairs are the Chado table names.
  98. */
  99. public function getTableNames($include_custom = FALSE) {
  100. $tables = array();
  101. if ($this->version == '1.3') {
  102. $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
  103. foreach ($tables_v1_3 as $table) {
  104. $tables[$table] = $table;
  105. }
  106. }
  107. if ($this->version == '1.2') {
  108. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  109. foreach ($tables_v1_2 as $table) {
  110. $tables[$table] = $table;
  111. }
  112. }
  113. if ($this->version == '1.11' or $v == '1.11 or older') {
  114. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  115. foreach ($tables_v1_11 as $table) {
  116. $tables[$table] = $table;
  117. }
  118. }
  119. // now add in the custom tables too if requested
  120. if ($include_custom) {
  121. $sql = "SELECT table FROM {tripal_custom_tables}";
  122. $resource = db_query($sql);
  123. foreach ($resource as $r) {
  124. $tables[$r->table] = $r->table;
  125. }
  126. }
  127. asort($tables);
  128. return $tables;
  129. }
  130. /**
  131. * Retrieves the chado tables Schema API array.
  132. *
  133. * @param $table
  134. * The name of the table to retrieve. The function will use the appopriate
  135. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  136. *
  137. * @returns
  138. * A Drupal Schema API array defining the table.
  139. */
  140. public function getTableSchema($table) {
  141. // first get the chado version.
  142. $v = $this->version;
  143. // get the table array from the proper chado schema
  144. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  145. // Call the module_invoke_all.
  146. $hook_name = "chado_schema_v" . $v . "_" . $table;
  147. $table_arr = module_invoke_all($hook_name);
  148. // If the module_invoke_all returned nothing then let's make sure there isn't
  149. // An API call we can call directly. The only time this occurs is
  150. // during an upgrade of a major Drupal version and tripal_core is disabled.
  151. if ((!$table_arr or !is_array($table_arr)) and
  152. function_exists('tripal_chado_' . $hook_name)) {
  153. $api_hook = "tripal_chado_" . $hook_name;
  154. $table_arr = $api_hook();
  155. }
  156. // if the table_arr is empty then maybe this is a custom table
  157. if (!is_array($table_arr) or count($table_arr) == 0) {
  158. $table_arr = chado_get_custom_table_schema($table);
  159. }
  160. return $table_arr;
  161. }
  162. /**
  163. * Check that any given Chado table exists.
  164. *
  165. * This function is necessary because Drupal's db_table_exists() function will
  166. * not look in any other schema but the one where Drupal is installed
  167. *
  168. * @param $table
  169. * The name of the chado table whose existence should be checked.
  170. *
  171. * @return
  172. * TRUE if the table exists in the chado schema and FALSE if it does not.
  173. */
  174. public function checkTableExists($table) {
  175. return chado_table_exists($table);
  176. }
  177. /**
  178. * Check that any given column in a Chado table exists.
  179. *
  180. * This function is necessary because Drupal's db_field_exists() will not
  181. * look in any other schema but the one were Drupal is installed
  182. *
  183. * @param $table
  184. * The name of the chado table.
  185. * @param $column
  186. * The name of the column in the chado table.
  187. *
  188. * @return
  189. * TRUE if the column exists for the table in the chado schema and
  190. * FALSE if it does not.
  191. *
  192. * @ingroup tripal_chado_schema_api
  193. */
  194. public function checkColumnExists($table, $column) {
  195. return chado_column_exists($table, $column);
  196. }
  197. /**
  198. * Check that any given column in a Chado table exists.
  199. *
  200. * This function is necessary because Drupal's db_field_exists() will not
  201. * look in any other schema but the one were Drupal is installed
  202. *
  203. * @param sequence
  204. * The name of the sequence
  205. * @return
  206. * TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
  207. *
  208. * @ingroup tripal_chado_schema_api
  209. */
  210. public function checkSequenceExists($sequence) {
  211. return chado_sequence_exists($sequence);
  212. }
  213. /**
  214. * A Chado-aware replacement for the db_index_exists() function.
  215. *
  216. * @param $table
  217. * The table to be altered.
  218. * @param $name
  219. * The name of the index.
  220. */
  221. function checkIndexExists($table, $name) {
  222. return chado_index_exists($table, $name);
  223. }
  224. }