tripal_core.chado_schema.api.inc

File

tripal_core/api/tripal_core.chado_schema.api.inc
View source
  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.
  28. *
  29. * This function is necessary because Drupal's db_table_exists will not
  30. * look in any other schema but the one were Drupal is installed
  31. *
  32. * @param $table
  33. * The name of the chado table whose existence should be checked.
  34. * @return
  35. * TRUE if the table exists in the chado schema and FALSE if it does not.
  36. *
  37. * @ingroup tripal_chado_schema_api
  38. */
  39. function chado_table_exists($table) {
  40. global $databases;
  41. $default_db = $databases['default']['default']['database'];
  42. $sql = "
  43. SELECT 1
  44. FROM information_schema.tables
  45. WHERE
  46. table_name = :table_name AND
  47. table_schema = :chado AND
  48. table_catalog = :default_db
  49. ";
  50. $args = array(
  51. ':table_name' => $table,
  52. ':chado' => tripal_get_schema_name('chado'),
  53. ':default_db' => $default_db
  54. );
  55. $results = db_query($sql, $args);
  56. $exists = $results->fetchObject();
  57. if (!$exists) {
  58. return FALSE;
  59. }
  60. return TRUE;
  61. }
  62. /**
  63. * Check that any given column in a Chado table exists.
  64. *
  65. * This function is necessary because Drupal's db_field_exists() will not
  66. * look in any other schema but the one were Drupal is installed
  67. *
  68. * @param $table
  69. * The name of the chado table.
  70. * @param $column
  71. * The name of the column in the chado table.
  72. * @return
  73. * TRUE if the column exists for the table in the chado schema and
  74. * FALSE if it does not.
  75. *
  76. * @ingroup tripal_chado_schema_api
  77. */
  78. function chado_column_exists($table, $column) {
  79. global $databases;
  80. $default_db = $databases['default']['default']['database'];
  81. $sql = "
  82. SELECT 1
  83. FROM information_schema.columns
  84. WHERE
  85. table_name = :table_name AND
  86. column_name = :column_name AND
  87. table_schema = :chado AND
  88. table_catalog = :default_db
  89. ";
  90. $args = array(
  91. ':table_name' => $table,
  92. ':column_name' => $column,
  93. ':chado' => tripal_get_schema_name('chado'),
  94. ':default_db' => $default_db
  95. );
  96. $results = db_query($sql, $args);
  97. $exists = $results->fetchField();
  98. if (!$exists) {
  99. return FALSE;
  100. }
  101. return TRUE;
  102. }
  103. /**
  104. * Check that any given column in a Chado table exists.
  105. *
  106. * This function is necessary because Drupal's db_field_exists() will not
  107. * look in any other schema but the one were Drupal is installed
  108. *
  109. * @param sequence
  110. * The name of the sequence
  111. * @return
  112. * TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
  113. *
  114. * @ingroup tripal_chado_schema_api
  115. */
  116. function chado_sequence_exists($sequence) {
  117. global $databases;
  118. $default_db = $databases['default']['default']['database'];
  119. $sql = "
  120. SELECT 1
  121. FROM information_schema.sequences
  122. WHERE
  123. sequence_name = :sequence_name AND
  124. sequence_schema = :sequence_schema AND
  125. sequence_catalog = :sequence_catalog
  126. ";
  127. $args = array(
  128. ':sequence_name' => $sequence,
  129. ':sequence_schema' => tripal_get_schema_name('chado'),
  130. ':sequence_catalog' => $default_db
  131. );
  132. $results = db_query($sql, $args);
  133. $exists = $results->fetchField();
  134. if (!$exists) {
  135. return FALSE;
  136. }
  137. return TRUE;
  138. }
  139. /**
  140. * A Chado-aware replacement for the db_index_exists() function.
  141. *
  142. * @param $table
  143. * The table to be altered.
  144. * @param $name
  145. * The name of the index.
  146. */
  147. function chado_index_exists($table, $name) {
  148. global $databases;
  149. $indexname = $table . '_' . $name . '_idx';
  150. $default_db = $databases['default']['default']['database'];
  151. $sql = "
  152. SELECT 1 as exists
  153. FROM pg_indexes
  154. WHERE indexname = :indexname
  155. ";
  156. $result = db_query($sql, array(':indexname' => $indexname));
  157. $exists = $result->fetchObject();
  158. return $exists->exists;
  159. }
  160. /**
  161. * A Chado-aware wrapper for the db_add_index() function.
  162. *
  163. * @param $table
  164. * The table to be altered.
  165. * @param $name
  166. * The name of the index.
  167. * @param $fields
  168. * An array of field names.
  169. */
  170. function chado_add_index($table, $name, $fields) {
  171. $indexname = $table . '_' . $name . '_idx';
  172. $query = 'CREATE INDEX "' . $indexname . '" ON {' . $table . '} ';
  173. $query .= '(';
  174. $temp = array();
  175. foreach ($fields as $field) {
  176. if (is_array($field)) {
  177. $temp[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')';
  178. }
  179. else {
  180. $temp[] = '"' . $field . '"';
  181. }
  182. }
  183. $query .= implode(', ', $temp);
  184. $query .= ')';
  185. return chado_query($query);
  186. }
  187. /**
  188. * Check that any given schema exists.
  189. *
  190. * @param $schema
  191. * The name of the schema to check the existence of
  192. *
  193. * @return
  194. * TRUE/FALSE depending upon whether or not the schema exists
  195. *
  196. * @ingroup tripal_chado_schema_api
  197. */
  198. function chado_dbschema_exists($schema) {
  199. $sql = "
  200. SELECT nspname
  201. FROM pg_namespace
  202. WHERE
  203. has_schema_privilege(nspname, 'USAGE') AND
  204. nspname = :nspname
  205. ORDER BY nspname
  206. ";
  207. $schema = db_query($sql, array(':nspname' => $schema))->fetchField();
  208. if ($schema) {
  209. return TRUE;
  210. }
  211. return FALSE;
  212. }
  213. /**
  214. * Check that the Chado schema exists within the local database
  215. *
  216. * @return
  217. * TRUE/FALSE depending upon whether it exists
  218. *
  219. * @ingroup tripal_chado_schema_api
  220. */
  221. function chado_is_local() {
  222. // This is postgresql-specific code to check the existence of the chado schema
  223. // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
  224. $sql = "
  225. SELECT nspname
  226. FROM pg_namespace
  227. WHERE
  228. has_schema_privilege(nspname, 'USAGE') AND
  229. nspname = :chado
  230. ";
  231. $results = db_query($sql, array(':chado' => tripal_get_schema_name('chado')));
  232. $name = $results->fetchObject();
  233. if ($name) {
  234. variable_set('chado_schema_exists', FALSE);
  235. return TRUE;
  236. }
  237. else {
  238. variable_set('chado_schema_exists', TRUE);
  239. return FALSE;
  240. }
  241. }
  242. /**
  243. * Check whether chado is installed (either in the same or a different database)
  244. *
  245. * @return
  246. * TRUE/FALSE depending upon whether chado is installed.
  247. *
  248. * @ingroup tripal_chado_schema_api
  249. */
  250. function chado_is_installed() {
  251. global $databases;
  252. // first check if chado is in the $databases variable of the settings.php file
  253. if (array_key_exists(tripal_get_schema_name('chado'), $databases)) {
  254. return TRUE;
  255. }
  256. // check to make sure the chado schema exists
  257. return chado_is_local();
  258. }
  259. /**
  260. * Returns the version number of the currently installed Chado instance.
  261. * It can return the real or effective version. Note, this function
  262. * is executed in the hook_init() of the tripal_core module which then
  263. * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
  264. * variable. You can access these variables rather than calling this function.
  265. *
  266. * @param $exact
  267. * Set this argument to 1 to retrieve the exact version that is installed.
  268. * Otherwise, this function will set the version to the nearest 'tenth'.
  269. * Chado versioning numbers in the hundreds represent changes to the
  270. * software and not the schema. Changes in the tenth's represent changes
  271. * in the schema.
  272. *
  273. * @param $warn_if_unsupported
  274. * If the currently installed version of Chado is not supported by Tripal
  275. * this generates a Drupal warning.
  276. *
  277. * @returns
  278. * The version of Chado
  279. *
  280. * @ingroup tripal_chado_schema_api
  281. */
  282. function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  283. global $databases;
  284. $version = '';
  285. $is_local = 0;
  286. // check that Chado is installed if not return 'uninstalled as the version'
  287. $chado_exists = chado_is_local();
  288. if (!$chado_exists) {
  289. // if it's not in the drupal database check to see if it's specified in the $db_url
  290. // in the settings.php
  291. if (!array_key_exists(tripal_get_schema_name('chado'), $databases)) {
  292. // if it's not in the drupal database or specified in the $db_url then
  293. // return uninstalled as the version
  294. return 'not installed';
  295. }
  296. $is_local = 0;
  297. $previous_db = chado_set_active('chado');
  298. $prop_exists = db_table_exists('chadoprop');
  299. chado_set_active($previous_db);
  300. }
  301. else {
  302. $is_local = 1;
  303. // @todo we need a chado aware db_table_exists.
  304. $prop_exists = db_table_exists(tripal_get_schema_name('chado').'.chadoprop');
  305. }
  306. // if the table doesn't exist then we don't know what version but we know
  307. // it must be 1.11 or older.
  308. if (!$prop_exists) {
  309. $version = "1.11 or older";
  310. }
  311. else {
  312. $sql = "
  313. SELECT value
  314. FROM {chadoprop} CP
  315. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
  316. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  317. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  318. ";
  319. if (!$is_local) {
  320. $previous_db = chado_set_active('chado');
  321. $results = db_query($sql);
  322. chado_set_active($previous_db);
  323. }
  324. else {
  325. $results = chado_query($sql);
  326. }
  327. $v = $results->fetchObject();
  328. // if we don't have a version in the chadoprop table then it must be
  329. // v1.11 or older
  330. if (!$v) {
  331. $version = "1.11 or older";
  332. }
  333. else {
  334. $version = $v->value;
  335. }
  336. }
  337. // next get the exact Chado version that is installed
  338. $exact_version = $version;
  339. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  340. // but at the time the v1.11 schema API was written we didn't know that so
  341. // we'll return the version 1.11 so the schema API will work.
  342. if (strcmp($exact_version, '1.11 or older') == 0) {
  343. $exact_version = "1.11";
  344. if ($warn_if_unsupported) {
  345. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  346. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  347. 'warning');
  348. }
  349. }
  350. // if not returing an exact version, return the version to the nearest 10th.
  351. // return 1.2 for all versions of 1.2x
  352. $effective_version = $exact_version;
  353. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  354. $effective_version = "1.2";
  355. }
  356. else if (preg_match('/^1\.3\d+$/', $effective_version)) {
  357. $effective_version = "1.3";
  358. }
  359. if ($warn_if_unsupported and ($effective_version < 1.11 and $effective_version != 'not installed')) {
  360. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  361. }
  362. // if the callee has requested the exact version then return it
  363. if ($exact) {
  364. return $exact_version;
  365. }
  366. return $effective_version;
  367. }
  368. /**
  369. * Retrieves the list of tables in the Chado schema. By default it only returns
  370. * the default Chado tables, but can return custom tables added to the
  371. * Chado schema if requested
  372. *
  373. * @param $include_custom
  374. * Optional. Set as TRUE to include any custom tables created in the
  375. * Chado schema. Custom tables are added to Chado using the
  376. * tripal_core_chado_create_table() function.
  377. *
  378. * @returns
  379. * An associative array where the key and value pairs are the Chado table names.
  380. *
  381. * @ingroup tripal_chado_schema_api
  382. */
  383. function chado_get_table_names($include_custom = NULL) {
  384. // first get the chado version that is installed
  385. $v = $GLOBALS["chado_version"];
  386. $tables = array();
  387. if ($v == '1.3') {
  388. $tables_v1_3 = tripal_core_chado_get_v1_3_tables();
  389. foreach ($tables_v1_3 as $table) {
  390. $tables[$table] = $table;
  391. }
  392. }
  393. if ($v == '1.2') {
  394. $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
  395. foreach ($tables_v1_2 as $table) {
  396. $tables[$table] = $table;
  397. }
  398. }
  399. if ($v == '1.11' or $v == '1.11 or older') {
  400. $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
  401. foreach ($tables_v1_11 as $table) {
  402. $tables[$table] = $table;
  403. }
  404. }
  405. // now add in the custom tables too if requested
  406. if ($include_custom) {
  407. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  408. $resource = db_query($sql);
  409. foreach ($resource as $r) {
  410. $tables[$r->table_name] = $r->table_name;
  411. }
  412. }
  413. asort($tables);
  414. return $tables;
  415. }
  416. /**
  417. * Retrieves the chado tables Schema API array.
  418. *
  419. * @param $table
  420. * The name of the table to retrieve. The function will use the appopriate
  421. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  422. *
  423. * @returns
  424. * A Drupal Schema API array defining the table.
  425. *
  426. * @ingroup tripal_chado_schema_api
  427. */
  428. function chado_get_schema($table) {
  429. // first get the chado version that is installed
  430. $v = $GLOBALS["chado_version"];
  431. // get the table array from the proper chado schema
  432. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  433. // Call the module_invoke_all.
  434. $hook_name = "chado_schema_v" . $v . "_" . $table;
  435. $table_arr = module_invoke_all($hook_name);
  436. // If the module_invoke_all returned nothing then let's make sure there isn't
  437. // An API call we can call directly. The only time this occurs is
  438. // during an upgrade of a major Drupal version and tripal_core is disabled.
  439. if ((!$table_arr or !is_array($table_arr)) and
  440. function_exists('tripal_core_' . $hook_name)) {
  441. $api_hook = "tripal_core_" . $hook_name;
  442. $table_arr = $api_hook();
  443. }
  444. // if the table_arr is empty then maybe this is a custom table
  445. if (!is_array($table_arr) or count($table_arr) == 0) {
  446. $table_arr = chado_get_custom_table_schema($table);
  447. }
  448. return $table_arr;
  449. }
  450. /**
  451. * Retrieves the schema in an array for the specified custom table.
  452. *
  453. * @param $table
  454. * The name of the table to create.
  455. *
  456. * @return
  457. * A Drupal-style Schema API array definition of the table. Returns
  458. * FALSE on failure.
  459. *
  460. * @ingroup tripal_chado_schema_api
  461. */
  462. function chado_get_custom_table_schema($table) {
  463. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  464. $results = db_query($sql, array(':table_name' => $table));
  465. $custom = $results->fetchObject();
  466. if (!$custom) {
  467. return FALSE;
  468. }
  469. else {
  470. return unserialize($custom->schema);
  471. }
  472. }