tripal_core.chado_schema.api.inc 9.7 KB

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