tripal_core.chado_schema.api.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * @defgroup tripal_chado_schema_api Chado Schema API
  4. * @ingroup tripal_chado_api
  5. * @{
  6. * Provides an application programming interface (API) for describing Chado tables.
  7. * This API consists of a set of functions, one for each table in Chado. Each
  8. * function simply returns a Drupal style array that defines the table.
  9. *
  10. * Because Drupal 6 does not handle foreign key (FK) relationships, however FK
  11. * relationships are needed to for Tripal Views. Therefore, FK relationships
  12. * have been added to the schema defintitions below.
  13. *
  14. * The functions provided in this documentation should not be called as is, but if you need
  15. * the Drupal-style array definition for any table, use the following function
  16. * call:
  17. *
  18. * $table_desc = chado_get_schema($table)
  19. *
  20. * where the variable $table contains the name of the table you want to
  21. * retireve. The chado_get_schema function determines the appropriate version of
  22. * Chado and uses the Drupal hook infrastructure to call the appropriate
  23. * hook function to retrieve the table schema.
  24. * @}
  25. */
  26. /**
  27. * Check that any given Chado table exists. This function
  28. * is necessary because Drupa's db_table_exists will not
  29. * look in any other schema but the one were Drupal is installed
  30. *
  31. * @return
  32. * TRUE/FALSE depending upon whether it exists
  33. *
  34. * @ingroup tripal_chado_schema_api
  35. */
  36. function chado_table_exists($table) {
  37. global $databases;
  38. $default_db = $databases['default']['default']['database'];
  39. $sql = "
  40. SELECT 1
  41. FROM information_schema.tables
  42. WHERE
  43. table_name = :table_name AND
  44. table_schema = 'chado' AND
  45. table_catalog = '$default_db'
  46. ";
  47. $results = db_query($sql, array(':table_name' => $table));
  48. $exists = $results->fetchObject();
  49. if (!$exists) {
  50. return FALSE;
  51. }
  52. return TRUE;
  53. }
  54. /**
  55. * Check that any given schema exists
  56. *
  57. * @param $schema
  58. * The name of the schema to check the existence of
  59. *
  60. * @return
  61. * TRUE/FALSE depending upon whether or not the schema exists
  62. *
  63. * @ingroup tripal_chado_schema_api
  64. */
  65. function chado_dbschema_exists($schema) {
  66. // check that the chado schema now exists
  67. $sql = "
  68. SELECT nspname
  69. FROM pg_namespace
  70. WHERE
  71. has_schema_privilege(nspname, 'USAGE') AND
  72. nspname = :nspname
  73. ORDER BY nspname
  74. ";
  75. $results = db_query($sql, array(':nspname' => $schema));
  76. $name = $results->fetchObject();
  77. if (strcmp($name->nspname, $schema) != 0) {
  78. return FALSE;
  79. }
  80. return TRUE;
  81. }
  82. /**
  83. * Check that the Chado schema exists within the local database
  84. *
  85. * @return
  86. * TRUE/FALSE depending upon whether it exists
  87. *
  88. * @ingroup tripal_chado_schema_api
  89. */
  90. function chado_is_local() {
  91. // This is postgresql-specific code to check the existence of the chado schema
  92. // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
  93. $sql = "
  94. SELECT nspname
  95. FROM pg_namespace
  96. WHERE
  97. has_schema_privilege(nspname, 'USAGE') AND
  98. nspname = 'chado'
  99. ";
  100. $results = db_query($sql);
  101. $name = $results->fetchObject();
  102. if ($name) {
  103. variable_set('chado_schema_exists', FALSE);
  104. return TRUE;
  105. }
  106. else {
  107. variable_set('chado_schema_exists', TRUE);
  108. return FALSE;
  109. }
  110. }
  111. /**
  112. * Check whether chado is installed (either in the same or a different database)
  113. *
  114. * @return
  115. * TRUE/FALSE depending upon whether chado is installed.
  116. *
  117. * @ingroup tripal_chado_schema_api
  118. */
  119. function chado_is_installed() {
  120. global $databases;
  121. // first check if chado is in the $databases variable of the settings.php file
  122. if (array_key_exists('chado', $databases)) {
  123. return TRUE;
  124. }
  125. // check to make sure the chado schema exists
  126. return chado_is_local();
  127. }
  128. /**
  129. * Returns the version number of the currently installed Chado instance.
  130. * It can return the real or effective version. Note, this function
  131. * is executed in the hook_init() of the tripal_core module which then
  132. * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
  133. * variable. You can access these variables rather than calling this function.
  134. *
  135. * @param $exact
  136. * Set this argument to 1 to retrieve the exact version that is installed.
  137. * Otherwise, this function will set the version to the nearest 'tenth'.
  138. * Chado versioning numbers in the hundreds represent changes to the
  139. * software and not the schema. Changes in the tenth's represent changes
  140. * in the schema.
  141. *
  142. * @param $warn_if_unsupported
  143. * If the currently installed version of Chado is not supported by Tripal
  144. * this generates a Drupal warning.
  145. *
  146. * @returns
  147. * The version of Chado
  148. *
  149. * @ingroup tripal_chado_schema_api
  150. */
  151. function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  152. global $databases;
  153. $version = '';
  154. $is_local = 0;
  155. // check that Chado is installed if not return 'uninstalled as the version'
  156. $chado_exists = chado_is_local();
  157. if (!$chado_exists) {
  158. // if it's not in the drupal database check to see if it's specified in the $db_url
  159. // in the settings.php
  160. if (!array_key_exists('chado', $databases)) {
  161. // if it's not in the drupal database or specified in the $db_url then
  162. // return uninstalled as the version
  163. return 'not installed';
  164. }
  165. $is_local = 0;
  166. $previous_db = chado_set_active('chado');
  167. $prop_exists = db_table_exists('chadoprop');
  168. chado_set_active($previous_db);
  169. }
  170. else {
  171. $is_local = 1;
  172. $prop_exists = db_table_exists('chado.chadoprop');
  173. }
  174. // if the table doesn't exist then we don't know what version but we know
  175. // it must be 1.11 or older.
  176. if (!$prop_exists) {
  177. $version = "1.11 or older";
  178. }
  179. else {
  180. $sql = "
  181. SELECT value
  182. FROM {chadoprop} CP
  183. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
  184. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  185. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  186. ";
  187. if (!$is_local) {
  188. $previous_db = chado_set_active('chado');
  189. $results = db_query($sql);
  190. chado_set_active($previous_db);
  191. }
  192. else {
  193. $results = chado_query($sql);
  194. }
  195. $v = $results->fetchObject();
  196. // if we don't have a version in the chadoprop table then it must be
  197. // v1.11 or older
  198. if (!$v) {
  199. $version = "1.11 or older";
  200. }
  201. $version = $v->value;
  202. }
  203. // next get the exact Chado version that is installed
  204. $exact_version = $version;
  205. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  206. // but at the time the v1.11 schema API was written we didn't know that so
  207. // we'll return the version 1.11 so the schema API will work.
  208. if (strcmp($exact_version, '1.11 or older') == 0) {
  209. $exact_version = "1.11";
  210. if ($warn_if_unsupported) {
  211. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  212. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  213. 'warning');
  214. }
  215. }
  216. // if not returing an exact version, return the version to the nearest 10th.
  217. // return 1.2 for all versions of 1.2x
  218. $effective_version = $exact_version;
  219. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  220. $effective_version = "1.2";
  221. }
  222. if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
  223. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  224. }
  225. // if the callee has requested the exact version then return it
  226. if ($exact) {
  227. return $exact_version;
  228. }
  229. return $effective_version;
  230. }
  231. /**
  232. * Retrieves the list of tables in the Chado schema. By default it only returns
  233. * the default Chado tables, but can return custom tables added to the
  234. * Chado schema if requested
  235. *
  236. * @param $include_custom
  237. * Optional. Set as TRUE to include any custom tables created in the
  238. * Chado schema. Custom tables are added to Chado using the
  239. * tripal_core_chado_create_table() function.
  240. *
  241. * @returns
  242. * An associative array where the key and value pairs are the Chado table names.
  243. *
  244. * @ingroup tripal_chado_schema_api
  245. */
  246. function chado_get_table_names($include_custom = NULL) {
  247. // first get the chado version that is installed
  248. $v = $GLOBALS["chado_version"];
  249. $tables = array();
  250. if ($v == '1.2') {
  251. $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
  252. foreach ($tables_v1_2 as $table) {
  253. $tables[$table] = $table;
  254. }
  255. }
  256. if ($v == '1.11' or $v == '1.11 or older') {
  257. $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
  258. foreach ($tables_v1_11 as $table) {
  259. $tables[$table] = $table;
  260. }
  261. }
  262. // now add in the custom tables too if requested
  263. if ($include_custom) {
  264. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  265. $resource = db_query($sql);
  266. foreach ($resource as $r) {
  267. $tables[$r->table_name] = $r->table_name;
  268. }
  269. }
  270. asort($tables);
  271. return $tables;
  272. }
  273. /**
  274. * Retrieves the chado tables Schema API array.
  275. *
  276. * @param $table
  277. * The name of the table to retrieve. The function will use the appopriate
  278. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  279. *
  280. * @returns
  281. * A Drupal Schema API array defining the table.
  282. *
  283. * @ingroup tripal_chado_schema_api
  284. */
  285. function chado_get_schema($table) {
  286. // first get the chado version that is installed
  287. $v = $GLOBALS["chado_version"];
  288. // get the table array from the proper chado schema
  289. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  290. $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
  291. // if the table_arr is empty then maybe this is a custom table
  292. if (!is_array($table_arr) or count($table_arr) == 0) {
  293. $table_arr = chado_get_custom_table_schema($table);
  294. }
  295. return $table_arr;
  296. }
  297. /**
  298. * Retrieves the schema in an array for the specified custom table.
  299. *
  300. * @param $table
  301. * The name of the table to create.
  302. *
  303. * @return
  304. * A Drupal-style Schema API array definition of the table. Returns
  305. * FALSE on failure.
  306. *
  307. * @ingroup tripal_chado_schema_api
  308. */
  309. function chado_get_custom_table_schema($table) {
  310. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  311. $results = db_query($sql, array(':table_name' => $table));
  312. $custom = $results->fetchObject();
  313. if (!$custom) {
  314. return FALSE;
  315. }
  316. else {
  317. return unserialize($custom->schema);
  318. }
  319. }