get_FKs.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_core.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_core/api/get_FKs.php -v 1.11 > \
  17. * ./sites/all/modules/tripal/tripal_core/apitripal_core.schema_v1.11.api.inc.new
  18. *
  19. * php ./sites/all/modules/tripal/tripal_core/api/get_FKs.php -v 1.2 > \
  20. * ./sites/all/modules/tripal/tripal_core/api/tripal_core.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. * This function does the actual work of determining the foreign key relationships from
  36. * the database and creating the schema file.
  37. */
  38. function get_chado_fk_relationships($version) {
  39. // convert the version to a form suitable for function names
  40. $v = $version;
  41. $v = preg_replace("/\./","_",$v);
  42. $tables = chado_get_table_names();
  43. $sql ="
  44. SELECT
  45. tc.constraint_name, tc.table_name, kcu.column_name,
  46. ccu.table_name AS foreign_table_name,
  47. ccu.column_name AS foreign_column_name
  48. FROM
  49. information_schema.table_constraints AS tc
  50. JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
  51. JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
  52. WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name=:table_name
  53. ";
  54. // iterate through the tables and get the foreign keys
  55. print "<?php
  56. /* @file: This file contains default schema definitions for all chado v$version tables
  57. * to be used by other function. Specifically these functions are used
  58. * by the tripal_core select/insert/update API functions and by
  59. * the Tripal Views module.
  60. *
  61. * These schema definitions can be augmented by another modules
  62. * (specifically to add missing definitions) by implementing
  63. * hook_chado_schema_v" . $v . "_<table name>().
  64. *
  65. * @defgroup tripal_schema_api Core Module Schema API
  66. * @{
  67. * Provides an application programming interface (API) for describing Chado tables.
  68. * This API consists of a set of functions, one for each table in Chado. Each
  69. * function simply returns a Drupal style array that defines the table.
  70. *
  71. * Because Drupal 6 does not handle foreign key (FK) relationships, however FK
  72. * relationships are needed to for Tripal Views. Therefore, FK relationships
  73. * have been added to the schema defintitions below.
  74. *
  75. * The functions provided in this documentation should not be called as is, but if you need
  76. * the Drupal-style array definition for any table, use the following function
  77. * call:
  78. *
  79. * \$table_desc = chado_get_schema(\$table)
  80. *
  81. * where the variable \$table contains the name of the table you want to
  82. * retireve. The chado_get_schema function determines the appropriate version of
  83. * Chado and uses the Drupal hook infrastructure to call the appropriate
  84. * hook function to retrieve the table schema.
  85. *
  86. * @}
  87. * @ingroup tripal_api
  88. */
  89. ";
  90. $referring = array();
  91. $tables_def = array();
  92. foreach ($tables as $table) {
  93. // get the existing table array
  94. $table_arr = chado_get_schema($table);
  95. if (empty($table_arr)) {
  96. print "ERROR: empty table definition $table\n";
  97. continue;
  98. }
  99. // add the table name to the array
  100. $table_arr['table'] = $table;
  101. // get the foreign keys and add them to the array
  102. $fks = db_query($sql, array(':table_name' => $table));
  103. foreach ($fks as $fk) {
  104. $table_arr['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name;
  105. $table_arr['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name;
  106. $reffering[$fk->foreign_table_name][] = $table;
  107. }
  108. $tables_def[] = $table_arr;
  109. }
  110. // now add in the referring tables and print
  111. foreach ($tables_def as $table_arr) {
  112. $table = $table_arr['table'];
  113. // add in the referring tables
  114. $table_referring = array_unique($reffering[$table]);
  115. $table_arr['referring_tables'] = $table_referring;
  116. // reformat the array to be more legible
  117. $arr = var_export($table_arr, 1);
  118. $arr = preg_replace("/\n\s+array/","array", $arr); // move array( to previous line
  119. $arr = preg_replace("/\n/","\n ", $arr); // add indentation
  120. $arr = preg_replace("/true/","TRUE", $arr); // add indentation
  121. $arr = preg_replace("/false/","FALSE", $arr); // add indentation
  122. $arr = preg_replace("/array \(/","array(", $arr); // add indentation
  123. // print out the new Schema API function for this table
  124. print "/**
  125. * Implements hook_chado_schema_v".$v."_".$table."()
  126. * Purpose: To describe the structure of '$table' to tripal
  127. * @see chado_insert_record()
  128. * @see chado_update_record()
  129. * @see chado_select_record()
  130. *
  131. * @return
  132. * An array describing the '$table' table
  133. *
  134. * @ingroup tripal_chado_v".$version."_schema_api
  135. *
  136. */
  137. function tripal_core_chado_schema_v".$v."_".$table."() {
  138. \$description = $arr;
  139. return \$description;
  140. }
  141. ";
  142. }
  143. }