get_FKs.php 5.9 KB

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