get_FKs.php

File

tripal_core/api/get_FKs.php
View source
  1. <?php
  2. // This script will add FK relatinsions to an existing schema API array for each
  3. // Chado table. It requires Chado is installed in a 'chado' schema of
  4. // the drupal database. It also requires existing schema hooks for
  5. // version of Chado. The goal is to use the output of this script to
  6. // update the existing schema hooks. Redirect the output of this script to
  7. // a file and then replace the existing schema API include file (e.g.
  8. // tripal_core.schema_v1.2.api.inc). Be sure to check it before replacing
  9. // this script requires a single argument (-v) which is the Chado version
  10. //
  11. // example usage in drupal directory root:
  12. //
  13. // php ./sites/all/modules/tripal/tripal_core/api/get_FKs.php -v 1.11 > \
  14. // ./sites/all/modules/tripal/tripal_core/apitripal_core.schema_v1.11.api.inc.new
  15. //
  16. // php ./sites/all/modules/tripal/tripal_core/api/get_FKs.php -v 1.2 > \
  17. // ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.2.api.inc.new
  18. $arguments = getopt("v:");
  19. if (isset($arguments['v'])) {
  20. $drupal_base_url = parse_url('http://www.example.com');
  21. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  22. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  23. $_SERVER['REMOTE_ADDR'] = NULL;
  24. $_SERVER['REQUEST_METHOD'] = NULL;
  25. require_once 'includes/bootstrap.inc';
  26. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  27. $version = $arguments['v'];
  28. get_chado_fk_relationships($version);
  29. }
  30. /**
  31. *
  32. */
  33. function get_chado_fk_relationships($version) {
  34. // convert the version to a form suitable for function names
  35. $v = $version;
  36. $v = preg_replace("/\./","_",$v);
  37. $tables = tripal_core_get_chado_tables();
  38. $sql ="
  39. SELECT
  40. tc.constraint_name, tc.table_name, kcu.column_name,
  41. ccu.table_name AS foreign_table_name,
  42. ccu.column_name AS foreign_column_name
  43. FROM
  44. information_schema.table_constraints AS tc
  45. JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
  46. JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
  47. WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='%s'
  48. ";
  49. // iterate through the tables and get the foreign keys
  50. print "<?php
  51. /* @file: This file contains default schema definitions for all chado v$version tables
  52. * to be used by other function. Specifically these functions are used
  53. * by the tripal_core select/insert/update API functions and by
  54. * the Tripal Views module.
  55. *
  56. * These schema definitions can be augmented by another modules
  57. * (specifically to add missing definitions) by implementing
  58. * hook_chado_schema_v" . $v . "_<table name>().
  59. *
  60. * @defgroup tripal_schema_api Core Module Schema API
  61. * @{
  62. * Provides an application programming interface (API) for describing Chado tables.
  63. * This API consists of a set of functions, one for each table in Chado. Each
  64. * function simply returns a Drupal style array that defines the table.
  65. *
  66. * Because Drupal 6 does not handle foreign key (FK) relationships, however FK
  67. * relationships are needed to for Tripal Views. Therefore, FK relationships
  68. * have been added to the schema defintitions below.
  69. *
  70. * The functions provided in this documentation should not be called as is, but if you need
  71. * the Drupal-style array definition for any table, use the following function
  72. * call:
  73. *
  74. * \$table_desc = tripal_core_get_chado_table_schema(\$table)
  75. *
  76. * where the variable \$table contains the name of the table you want to
  77. * retireve. The tripal_core_get_chado_table_schema function determines the appropriate version of
  78. * Chado and uses the Drupal hook infrastructure to call the appropriate
  79. * hook function to retrieve the table schema.
  80. *
  81. * @}
  82. * @ingroup tripal_api
  83. */
  84. ";
  85. $referring = array();
  86. $tables_def = array();
  87. foreach ($tables as $table) {
  88. // get the existing table array
  89. $table_arr = tripal_core_get_chado_table_schema($table);
  90. if (empty($table_arr)) {
  91. print "ERROR: empty table definition $table\n";
  92. continue;
  93. }
  94. // add the table name to the array
  95. $table_arr['table'] = $table;
  96. // get the foreign keys and add them to the array
  97. $fks = db_query($sql,$table);
  98. while ($fk = db_fetch_object($fks)) {
  99. $table_arr['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name;
  100. $table_arr['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name;
  101. $reffering[$fk->foreign_table_name][] = $table;
  102. }
  103. $tables_def[] = $table_arr;
  104. }
  105. // now add in the referring tables and print
  106. foreach ($tables_def as $table_arr) {
  107. $table = $table_arr['table'];
  108. // add in the referring tables
  109. $table_referring = array_unique($reffering[$table]);
  110. $table_arr['referring_tables'] = $table_referring;
  111. // reformat the array to be more legible
  112. $arr = var_export($table_arr, 1);
  113. $arr = preg_replace("/\n\s+array/","array", $arr); // move array( to previous line
  114. $arr = preg_replace("/\n/","\n ", $arr); // add indentation
  115. $arr = preg_replace("/true/","TRUE", $arr); // add indentation
  116. $arr = preg_replace("/false/","FALSE", $arr); // add indentation
  117. $arr = preg_replace("/array \(/","array(", $arr); // add indentation
  118. // print out the new Schema API function for this table
  119. print "/**
  120. * Implements hook_chado_schema_v".$v."_".$table."()
  121. * Purpose: To describe the structure of '$table' to tripal
  122. * @see tripal_core_chado_insert()
  123. * @see tripal_core_chado_update()
  124. * @see tripal_core_chado_select()
  125. *
  126. * @return
  127. * An array describing the '$table' table
  128. *
  129. * @ingroup tripal_chado_v".$version."_schema_api
  130. *
  131. */
  132. function tripal_core_chado_schema_v".$v."_".$table."() {
  133. \$description = $arr;
  134. return \$description;
  135. }
  136. ";
  137. }
  138. }