generate_chado_schema_file.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This script will generate the schema file for the Tripal API for an
  6. * installation of Chado. To use the script you must install the version of
  7. * Chado desired using Tripal. Next install and enable the 'schema' module
  8. * from the Drupal module respository. Finally, add a new 'chado'
  9. * entry in the $databases variable of the settings.php file. For
  10. * example:
  11. *
  12. * @code
  13. *'chado' => array(
  14. * 'default' => array(
  15. * 'database' => 'd7x_t2x_c13',
  16. * 'username' => 'chado',
  17. * 'password' => 'testing123',
  18. * 'host' => 'localhost',
  19. * 'port' => '',
  20. * 'driver' => 'pgsql',
  21. * 'prefix' => '',
  22. * ),
  23. * ),
  24. * @endcode
  25. *
  26. * This script requires a single argument (-v) which is the Chado version.
  27. * Redirect output into a new file as desired.
  28. *
  29. * Example usage in drupal directory root:
  30. *
  31. * php
  32. * ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php
  33. * -v 1.11 > \
  34. * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.11.api.inc.new
  35. *
  36. * php
  37. * ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php
  38. * -v 1.2 > \
  39. * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.2.api.inc.new
  40. *
  41. * php
  42. * ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php
  43. * -v 1.3 > \
  44. * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.3.api.inc.new
  45. */
  46. $arguments = getopt("v:");
  47. if (isset($arguments['v'])) {
  48. $drupal_base_url = parse_url('http://www.example.com');
  49. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  50. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  51. $_SERVER['REMOTE_ADDR'] = NULL;
  52. $_SERVER['REQUEST_METHOD'] = NULL;
  53. define('DRUPAL_ROOT', getcwd());
  54. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  55. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  56. $version = $arguments['v'];
  57. $safe_version = preg_replace('/\./', '_', $version);
  58. print("<?php \n" .
  59. "/**\n" .
  60. " * @file\n" .
  61. " * Describes the chado tables in version $version\n" .
  62. " */\n" .
  63. "\n" .
  64. "/**\n" .
  65. " * @defgroup tripal_schema_v" . $safe_version . "_api Chado v" . $version . " Schema API\n" .
  66. " * @ingroup tripal_chado_schema_api\n" .
  67. " * @{\n" .
  68. " * Provides an application programming interface (API) for describing Chado\n" .
  69. " * tables. This API consists of a set of functions, one for each table in Chado.\n" .
  70. " * Each function simply returns a Drupal style array that defines the table.\n" .
  71. " *\n" .
  72. " * Because Drupal does not handle foreign key (FK) relationships, which are\n" .
  73. " * needed to for Tripal Views, they have been added to the schema defintitions\n" .
  74. " * below.\n" .
  75. " *\n" .
  76. " * The functions provided in this documentation should not be called as is,\n" .
  77. " * but if you need the Drupal-style array definition for any table, use the\n" .
  78. " * following function call:\n" .
  79. " *\n" .
  80. " * \$table_desc = chado_get_schema(\$table)\n" .
  81. " *\n" .
  82. " * where the variable \$table contains the name of the table you want to\n" .
  83. " * retireve. The chado_get_schema function determines the appropriate version\n" .
  84. " * of Chado and uses the Drupal hook infrastructure to call the appropriate\n" .
  85. " * hook function to retrieve the table schema.\n" .
  86. " *\n" .
  87. " * If you need to augment these schema definitions within your own module,\n" .
  88. " * you need to implement the hook_chado_schema_v" . $safe_version . "_[table name]() hook where\n" .
  89. " * [table name] is the name of the chado table whose schema definition you\n" .
  90. " * want to augment.\n" .
  91. " * @}\n" .
  92. " */\n"
  93. );
  94. // The SQL for retreiving details about a table.
  95. $fksql = "
  96. SELECT
  97. tc.constraint_name, tc.table_name, kcu.column_name,
  98. ccu.table_name AS foreign_table_name,
  99. ccu.column_name AS foreign_column_name
  100. FROM
  101. information_schema.table_constraints AS tc
  102. JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
  103. JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
  104. WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name=:table_name
  105. ";
  106. // Iterate through the tables of Chado and use the Schema module to
  107. // generate a schema array for each table.
  108. $sql = "
  109. SELECT table_name
  110. FROM information_schema.tables
  111. WHERE
  112. table_schema = 'chado' AND
  113. table_type = 'BASE TABLE' AND
  114. table_name NOT like 'tripal%'
  115. ORDER BY table_name
  116. ";
  117. $result = db_query($sql);
  118. $table_schemas = [];
  119. $referring = [];
  120. while ($table = $result->fetchField()) {
  121. // Get the schema for each table.
  122. $schema = schema_dbobject('chado')->inspect(NULL, $table);
  123. $schema = $schema[$table];
  124. // Get the foreign keys and add them to the array.
  125. $fks = db_query($fksql, [':table_name' => $table]);
  126. $schema['foreign keys'] = [];
  127. foreach ($fks as $fk) {
  128. $schema['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name;
  129. $schema['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name;
  130. $reffering[$fk->foreign_table_name][] = $table;
  131. }
  132. // Add a table and description key to the top.
  133. $schema = ['table' => $table] + $schema;
  134. $schema = ['description' => ''] + $schema;
  135. // Fix the datetime fields and add a description field.
  136. foreach ($schema['fields'] as $fname => $details) {
  137. if ($schema['fields'][$fname]['type'] == "timestamp without time zone") {
  138. $schema['fields'][$fname]['type'] = 'datetime';
  139. }
  140. $schema['fields'][$fname]['description'] = '';
  141. }
  142. // Remove the 'name' key.
  143. unset($schema['name']);
  144. $table_schemas[$table] = $schema;
  145. }
  146. // Now iterate through the tables now that we have all the referring info
  147. // and generate the function strings.
  148. foreach ($table_schemas as $table => $schema) {
  149. $schema['referring_tables'] = [];
  150. if (count($reffering[$table]) > 0) {
  151. $schema['referring_tables'] = array_unique($reffering[$table]);
  152. }
  153. // Reformat the array to be more legible.
  154. $arr = var_export($schema, 1);
  155. // Move array( to previous line.
  156. $arr = preg_replace("/\n\s+array/", "array", $arr);
  157. // Add indentation.
  158. $arr = preg_replace("/\n/", "\n ", $arr);
  159. $arr = preg_replace("/true/", "TRUE", $arr);
  160. $arr = preg_replace("/false/", "FALSE", $arr);
  161. $arr = preg_replace("/array \(/", "array(", $arr);
  162. print (
  163. "/**\n" .
  164. " * Implements hook_chado_schema_v" . $safe_version . "_" . $table . "()\n" .
  165. " * \n" .
  166. " * Purpose: To describe the structure of '$table' to tripal\n" .
  167. " * @see chado_insert_record()\n" .
  168. " * @see chado_update_record()\n" .
  169. " * @see chado_select_record()\n" .
  170. " * @see chado_generate_var()\n" .
  171. " * @see chado_expan_var()\n" .
  172. " *\n" .
  173. " * @return\n" .
  174. " * An array describing the '$table' table\n" .
  175. " *\n" .
  176. " * @ingroup tripal_chado_v" . $version . "_schema_api\n" .
  177. " *\n" .
  178. " */\n" .
  179. "function tripal_core_chado_schema_v" . $safe_version . "_" . $table . "() {\n" .
  180. " \$description = $arr; \n " .
  181. " return \$description;\n" .
  182. "}\n"
  183. );
  184. }
  185. // Finally add the tables function for this version.
  186. $table_list = '';
  187. foreach ($table_schemas as $table => $schema) {
  188. $table_list .= " '$table',\n";
  189. }
  190. print (
  191. "/**\n" .
  192. " * Lists the table names in the v" . $version . " chado schema\n" .
  193. " *\n" .
  194. " * @return\n" .
  195. " * An array containing all of the table names\n" .
  196. " *\n" .
  197. " * @ingroup tripal_chado_v" . $version . "_schema_api\n" .
  198. " *\n" .
  199. " */\n" .
  200. "function tripal_core_chado_get_v" . $safe_version . "_tables() {\n" .
  201. " \$tables = array(\n" .
  202. "$table_list" .
  203. " );\n" .
  204. " return \$tables;\n" .
  205. "}\n"
  206. );
  207. }