generate_chado_schema_file.php 7.8 KB

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