tripal_core.chado_schema.api.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 = tripal_core_get_chado_table_schema($table)
  19. *
  20. * where the variable $table contains the name of the table you want to
  21. * retireve. The tripal_core_get_chado_table_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 function
  29. * hardcodes the 'public'
  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 tripal_core_schema_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 tripal_core_chado_schema_exists() {
  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 tripal_core_is_chado_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 tripal_core_chado_schema_exists();
  127. }
  128. /**
  129. * Check whether chado is installed local to the Drupal database
  130. * in its own Chado schema.
  131. *
  132. * @return
  133. * TRUE/FALSE depending upon whether chado is local.
  134. *
  135. * @ingroup tripal_chado_schema_api
  136. */
  137. function tripal_core_is_chado_local() {
  138. global $databases, $db_type;
  139. // first check if chado is in the $databases variable of the settings.php file
  140. if (array_key_exists('chado', $databases)) {
  141. return FALSE;
  142. }
  143. // check to make sure the chado schema exists
  144. return tripal_core_chado_schema_exists();
  145. }
  146. /**
  147. * Returns the version number of the currently installed Chado instance.
  148. * It can return the real or effective version. Note, this function
  149. * is executed in the hook_init() of the tripal_core module which then
  150. * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
  151. * variable. You can access these variables rather than calling this function.
  152. *
  153. * @param $exact
  154. * Set this argument to 1 to retrieve the exact version that is installed.
  155. * Otherwise, this function will set the version to the nearest 'tenth'.
  156. * Chado versioning numbers in the hundreds represent changes to the
  157. * software and not the schema. Changes in the tenth's represent changes
  158. * in the schema.
  159. *
  160. * @param $warn_if_unsupported
  161. * If the currently installed version of Chado is not supported by Tripal
  162. * this generates a Drupal warning.
  163. *
  164. * @returns
  165. * The version of Chado
  166. *
  167. * @ingroup tripal_chado_schema_api
  168. */
  169. function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  170. global $databases;
  171. $version = '';
  172. $is_local = 0;
  173. // check that Chado is installed if not return 'uninstalled as the version'
  174. $chado_exists = tripal_core_chado_schema_exists();
  175. if (!$chado_exists) {
  176. // if it's not in the drupal database check to see if it's specified in the $db_url
  177. // in the settings.php
  178. if (!array_key_exists('chado', $databases)) {
  179. // if it's not in the drupal database or specified in the $db_url then
  180. // return uninstalled as the version
  181. return 'not installed';
  182. }
  183. $is_local = 0;
  184. $previous_db = tripal_db_set_active('chado');
  185. $prop_exists = db_table_exists('chadoprop');
  186. tripal_db_set_active($previous_db);
  187. }
  188. else {
  189. $is_local = 1;
  190. $prop_exists = db_table_exists('chado.chadoprop');
  191. }
  192. // if the table doesn't exist then we don't know what version but we know
  193. // it must be 1.11 or older.
  194. if (!$prop_exists) {
  195. $version = "1.11 or older";
  196. }
  197. else {
  198. $sql = "
  199. SELECT value
  200. FROM {chadoprop} CP
  201. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
  202. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  203. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  204. ";
  205. if (!$is_local) {
  206. $previous_db = tripal_db_set_active('chado');
  207. $results = db_query($sql);
  208. tripal_db_set_active($previous_db);
  209. }
  210. else {
  211. $results = chado_query($sql);
  212. }
  213. $v = $results->fetchObject();
  214. // if we don't have a version in the chadoprop table then it must be
  215. // v1.11 or older
  216. if (!$v) {
  217. $version = "1.11 or older";
  218. }
  219. $version = $v->value;
  220. }
  221. // next get the exact Chado version that is installed
  222. $exact_version = $version;
  223. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  224. // but at the time the v1.11 schema API was written we didn't know that so
  225. // we'll return the version 1.11 so the schema API will work.
  226. if (strcmp($exact_version, '1.11 or older') == 0) {
  227. $exact_version = "1.11";
  228. if ($warn_if_unsupported) {
  229. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  230. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  231. 'warning');
  232. }
  233. }
  234. // if not returing an exact version, return the version to the nearest 10th.
  235. // return 1.2 for all versions of 1.2x
  236. $effective_version = $exact_version;
  237. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  238. $effective_version = "1.2";
  239. }
  240. if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
  241. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  242. }
  243. // if the callee has requested the exact version then return it
  244. if ($exact) {
  245. return $exact_version;
  246. }
  247. return $effective_version;
  248. }
  249. /**
  250. * Retrieves the list tables in the Chado schema. By default it only retursn
  251. * the default Chado tables, but may also return custom tables added to the
  252. * Chado schema as well.
  253. *
  254. * @param $include_custom
  255. * Optional. Set as TRUE to include any custom tables created in the
  256. * Chado schema. Custom tables are added to Chado using the
  257. * tripal_core_chado_create_table() function.
  258. *
  259. * @returns
  260. * An associative array where the key and value pairs are the Chado table names.
  261. *
  262. * @ingroup tripal_chado_schema_api
  263. */
  264. function tripal_core_get_chado_tables($include_custom = NULL) {
  265. // first get the chado version that is installed
  266. $v = $GLOBALS["chado_version"];
  267. $tables = array();
  268. if ($v == '1.2') {
  269. $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
  270. foreach ($tables_v1_2 as $table) {
  271. $tables[$table] = $table;
  272. }
  273. }
  274. if ($v == '1.11' or $v == '1.11 or older') {
  275. $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
  276. foreach ($tables_v1_11 as $table) {
  277. $tables[$table] = $table;
  278. }
  279. }
  280. // now add in the custom tables too if requested
  281. if ($include_custom) {
  282. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  283. $resource = db_query($sql);
  284. foreach ($resource as $r) {
  285. $tables[$r->table_name] = $r->table_name;
  286. }
  287. }
  288. asort($tables);
  289. return $tables;
  290. }
  291. /**
  292. * Retrieves the chado tables Schema API array.
  293. *
  294. * @param $table
  295. * The name of the table to retrieve. The function will use the appopriate
  296. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  297. *
  298. * @returns
  299. * A Drupal Schema API array defining the table.
  300. *
  301. * @ingroup tripal_chado_schema_api
  302. */
  303. function tripal_core_get_chado_table_schema($table) {
  304. // first get the chado version that is installed
  305. $v = $GLOBALS["chado_version"];
  306. // get the table array from the proper chado schema
  307. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  308. $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
  309. // if the table_arr is empty then maybe this is a custom table
  310. if (!is_array($table_arr) or count($table_arr) == 0) {
  311. $table_arr = tripal_get_chado_custom_schema($table);
  312. }
  313. return $table_arr;
  314. }
  315. /**
  316. * Retrieves the schema in an array for the specified custom table.
  317. *
  318. * @param $table
  319. * The name of the table to create.
  320. *
  321. * @return
  322. * A Drupal-style Schema API array definition of the table. Returns
  323. * FALSE on failure.
  324. *
  325. * @ingroup tripal_chado_schema_api
  326. */
  327. function tripal_get_chado_custom_schema($table) {
  328. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  329. $results = db_query($sql, array(':table_name' => $table));
  330. $custom = $results->fetchObject();
  331. if (!$custom) {
  332. return FALSE;
  333. }
  334. else {
  335. return unserialize($custom->schema);
  336. }
  337. }