tripal_chado.schema.api.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * A Chado-aware replacement for the db_index_exists() function.
  56. *
  57. * @param $table
  58. * The table to be altered.
  59. * @param $name
  60. * The name of the index.
  61. */
  62. function chado_index_exists($table, $name) {
  63. global $databases;
  64. $indexname = $table . '_' . $name . '_idx';
  65. $default_db = $databases['default']['default']['database'];
  66. $sql = "SELECT 1 as exists FROM pg_indexes WHERE indexname = :indexname";
  67. $result = db_query($sql, array(':indexname' => $indexname));
  68. $exists = $result->fetchObject();
  69. return $exists->exists;
  70. }
  71. /**
  72. * A Chado-aware wrapper for the db_add_index() function.
  73. *
  74. * @param $table
  75. * The table to be altered.
  76. * @param $name
  77. * The name of the index.
  78. * @param $fields
  79. * An array of field names.
  80. */
  81. function chado_add_index($table, $name, $fields) {
  82. $indexname = $table . '_' . $name . '_idx';
  83. $query = 'CREATE INDEX "' . $indexname . '" ON {' . $table . '} ';
  84. $query .= '(';
  85. $temp = array();
  86. foreach ($fields as $field) {
  87. if (is_array($field)) {
  88. $temp[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')';
  89. }
  90. else {
  91. $temp[] = '"' . $field . '"';
  92. }
  93. }
  94. $query .= implode(', ', $temp);
  95. $query .= ')';
  96. return chado_query($query);
  97. }
  98. /**
  99. * Check that any given schema exists.
  100. *
  101. * @param $schema
  102. * The name of the schema to check the existence of
  103. *
  104. * @return
  105. * TRUE/FALSE depending upon whether or not the schema exists
  106. *
  107. * @ingroup tripal_chado_schema_api
  108. */
  109. function chado_dbschema_exists($schema) {
  110. $sql = "
  111. SELECT nspname
  112. FROM pg_namespace
  113. WHERE
  114. has_schema_privilege(nspname, 'USAGE') AND
  115. nspname = :nspname
  116. ORDER BY nspname
  117. ";
  118. $schema = db_query($sql, array(':nspname' => $schema))->fetchField();
  119. if ($schema) {
  120. return TRUE;
  121. }
  122. return FALSE;
  123. }
  124. /**
  125. * Check that the Chado schema exists within the local database
  126. *
  127. * @return
  128. * TRUE/FALSE depending upon whether it exists
  129. *
  130. * @ingroup tripal_chado_schema_api
  131. */
  132. function chado_is_local() {
  133. // This is postgresql-specific code to check the existence of the chado schema
  134. // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
  135. $sql = "
  136. SELECT nspname
  137. FROM pg_namespace
  138. WHERE
  139. has_schema_privilege(nspname, 'USAGE') AND
  140. nspname = 'chado'
  141. ";
  142. $results = db_query($sql);
  143. $name = $results->fetchObject();
  144. if ($name) {
  145. variable_set('chado_schema_exists', FALSE);
  146. return TRUE;
  147. }
  148. else {
  149. variable_set('chado_schema_exists', TRUE);
  150. return FALSE;
  151. }
  152. }
  153. /**
  154. * Check whether chado is installed (either in the same or a different database)
  155. *
  156. * @return
  157. * TRUE/FALSE depending upon whether chado is installed.
  158. *
  159. * @ingroup tripal_chado_schema_api
  160. */
  161. function chado_is_installed() {
  162. global $databases;
  163. // first check if chado is in the $databases variable of the settings.php file
  164. if (array_key_exists('chado', $databases)) {
  165. return TRUE;
  166. }
  167. // check to make sure the chado schema exists
  168. return chado_is_local();
  169. }
  170. /**
  171. * Returns the version number of the currently installed Chado instance.
  172. * It can return the real or effective version. Note, this function
  173. * is executed in the hook_init() of the tripal_chado module which then
  174. * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
  175. * variable. You can access these variables rather than calling this function.
  176. *
  177. * @param $exact
  178. * Set this argument to 1 to retrieve the exact version that is installed.
  179. * Otherwise, this function will set the version to the nearest 'tenth'.
  180. * Chado versioning numbers in the hundreds represent changes to the
  181. * software and not the schema. Changes in the tenth's represent changes
  182. * in the schema.
  183. *
  184. * @param $warn_if_unsupported
  185. * If the currently installed version of Chado is not supported by Tripal
  186. * this generates a Drupal warning.
  187. *
  188. * @returns
  189. * The version of Chado
  190. *
  191. * @ingroup tripal_chado_schema_api
  192. */
  193. function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  194. global $databases;
  195. $version = '';
  196. $is_local = 0;
  197. // check that Chado is installed if not return 'uninstalled as the version'
  198. $chado_exists = chado_is_local();
  199. if (!$chado_exists) {
  200. // if it's not in the drupal database check to see if it's specified in the $db_url
  201. // in the settings.php
  202. if (!array_key_exists('chado', $databases)) {
  203. // if it's not in the drupal database or specified in the $db_url then
  204. // return uninstalled as the version
  205. return 'not installed';
  206. }
  207. $is_local = 0;
  208. $previous_db = chado_set_active('chado');
  209. $prop_exists = db_table_exists('chadoprop');
  210. chado_set_active($previous_db);
  211. }
  212. else {
  213. $is_local = 1;
  214. $prop_exists = db_table_exists('chado.chadoprop');
  215. }
  216. // if the table doesn't exist then we don't know what version but we know
  217. // it must be 1.11 or older.
  218. if (!$prop_exists) {
  219. $version = "1.11 or older";
  220. }
  221. else {
  222. $sql = "
  223. SELECT value
  224. FROM {chadoprop} CP
  225. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
  226. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  227. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  228. ";
  229. if (!$is_local) {
  230. $previous_db = chado_set_active('chado');
  231. $results = db_query($sql);
  232. chado_set_active($previous_db);
  233. }
  234. else {
  235. $results = chado_query($sql);
  236. }
  237. $v = $results->fetchObject();
  238. // if we don't have a version in the chadoprop table then it must be
  239. // v1.11 or older
  240. if (!$v) {
  241. $version = "1.11 or older";
  242. }
  243. else {
  244. $version = $v->value;
  245. }
  246. }
  247. // next get the exact Chado version that is installed
  248. $exact_version = $version;
  249. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  250. // but at the time the v1.11 schema API was written we didn't know that so
  251. // we'll return the version 1.11 so the schema API will work.
  252. if (strcmp($exact_version, '1.11 or older') == 0) {
  253. $exact_version = "1.11";
  254. if ($warn_if_unsupported) {
  255. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  256. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  257. 'warning');
  258. }
  259. }
  260. // if not returing an exact version, return the version to the nearest 10th.
  261. // return 1.2 for all versions of 1.2x
  262. $effective_version = $exact_version;
  263. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  264. $effective_version = "1.2";
  265. }
  266. if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
  267. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  268. }
  269. // if the callee has requested the exact version then return it
  270. if ($exact) {
  271. return $exact_version;
  272. }
  273. return $effective_version;
  274. }
  275. /**
  276. * Retrieves the list of tables in the Chado schema. By default it only returns
  277. * the default Chado tables, but can return custom tables added to the
  278. * Chado schema if requested
  279. *
  280. * @param $include_custom
  281. * Optional. Set as TRUE to include any custom tables created in the
  282. * Chado schema. Custom tables are added to Chado using the
  283. * tripal_chado_chado_create_table() function.
  284. *
  285. * @returns
  286. * An associative array where the key and value pairs are the Chado table names.
  287. *
  288. * @ingroup tripal_chado_schema_api
  289. */
  290. function chado_get_table_names($include_custom = NULL) {
  291. // first get the chado version that is installed
  292. $v = $GLOBALS["chado_version"];
  293. $tables = array();
  294. if ($v == '1.2') {
  295. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  296. foreach ($tables_v1_2 as $table) {
  297. $tables[$table] = $table;
  298. }
  299. }
  300. if ($v == '1.11' or $v == '1.11 or older') {
  301. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  302. foreach ($tables_v1_11 as $table) {
  303. $tables[$table] = $table;
  304. }
  305. }
  306. // now add in the custom tables too if requested
  307. if ($include_custom) {
  308. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  309. $resource = db_query($sql);
  310. foreach ($resource as $r) {
  311. $tables[$r->table_name] = $r->table_name;
  312. }
  313. }
  314. asort($tables);
  315. return $tables;
  316. }
  317. /**
  318. * Retrieves the chado tables Schema API array.
  319. *
  320. * @param $table
  321. * The name of the table to retrieve. The function will use the appopriate
  322. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  323. *
  324. * @returns
  325. * A Drupal Schema API array defining the table.
  326. *
  327. * @ingroup tripal_chado_schema_api
  328. */
  329. function chado_get_schema($table) {
  330. // first get the chado version that is installed
  331. $v = $GLOBALS["chado_version"];
  332. // get the table array from the proper chado schema
  333. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  334. $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
  335. // if the table_arr is empty then maybe this is a custom table
  336. if (!is_array($table_arr) or count($table_arr) == 0) {
  337. $table_arr = chado_get_custom_table_schema($table);
  338. }
  339. return $table_arr;
  340. }
  341. /**
  342. * Retrieves the schema in an array for the specified custom table.
  343. *
  344. * @param $table
  345. * The name of the table to create.
  346. *
  347. * @return
  348. * A Drupal-style Schema API array definition of the table. Returns
  349. * FALSE on failure.
  350. *
  351. * @ingroup tripal_chado_schema_api
  352. */
  353. function chado_get_custom_table_schema($table) {
  354. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  355. $results = db_query($sql, array(':table_name' => $table));
  356. $custom = $results->fetchObject();
  357. if (!$custom) {
  358. return FALSE;
  359. }
  360. else {
  361. return unserialize($custom->schema);
  362. }
  363. }