tripal_core.chado_schema.api.inc 11 KB

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