array( * 'default' => array( * 'database' => 'd7x_t2x_c13', * 'username' => 'chado', * 'password' => 'testing123', * 'host' => 'localhost', * 'port' => '', * 'driver' => 'pgsql', * 'prefix' => '', * ), * ), * @endcode * * This script requires a single argument (-v) which is the Chado version. * Redirect output into a new file as desired. * * Example usage in drupal directory root: * * php ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php -v 1.11 > \ * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.11.api.inc.new * * php ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php -v 1.2 > \ * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.2.api.inc.new * * php ./sites/all/modules/tripal/tripal_core/api/generate_chado_schema_file.php -v 1.3 > \ * ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.3.api.inc.new */ $arguments = getopt("v:"); if (isset($arguments['v'])) { $drupal_base_url = parse_url('http://www.example.com'); $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; $_SERVER['REMOTE_ADDR'] = NULL; $_SERVER['REQUEST_METHOD'] = NULL; define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); $version = $arguments['v']; $safe_version = preg_replace('/\./', '_', $version); print("fetchField()) { // Get the schema for each table. $schema = schema_dbobject('chado')->inspect(NULL, $table); $schema = $schema[$table]; // Get the foreign keys and add them to the array. $fks = db_query($fksql, array(':table_name' => $table)); $schema['foreign keys'] = array(); foreach ($fks as $fk) { $schema['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name; $schema['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name; $reffering[$fk->foreign_table_name][] = $table; } // Add a table and description key to the top. $schema = array('table' => $table) + $schema; $schema = array('description' => '') + $schema; // Fix the datetime fields and add a description field. foreach ($schema['fields'] as $fname => $details) { if ($schema['fields'][$fname]['type'] == "timestamp without time zone") { $schema['fields'][$fname]['type'] = 'datetime'; } $schema['fields'][$fname]['description'] = ''; } // Remove the 'name' key. unset($schema['name']); $table_schemas[$table] = $schema; } // Now iterate through the tables now that we have all the referring info // and generate the function strings. foreach ($table_schemas as $table => $schema) { $schema['referring_tables'] = array(); if (count($reffering[$table]) > 0) { $schema['referring_tables'] = array_unique($reffering[$table]); } // Reformat the array to be more legible. $arr = var_export($schema, 1); // Move array( to previous line. $arr = preg_replace("/\n\s+array/","array", $arr); // Add indentation. $arr = preg_replace("/\n/","\n ", $arr); $arr = preg_replace("/true/","TRUE", $arr); $arr = preg_replace("/false/","FALSE", $arr); $arr = preg_replace("/array \(/","array(", $arr); print ( "/**\n" . " * Implements hook_chado_schema_v" . $safe_version . "_" . $table . "()\n" . " * \n" . " * Purpose: To describe the structure of '$table' to tripal\n" . " * @see chado_insert_record()\n" . " * @see chado_update_record()\n" . " * @see chado_select_record()\n" . " * @see chado_generate_var()\n" . " * @see chado_expan_var()\n" . " *\n" . " * @return\n" . " * An array describing the '$table' table\n" . " *\n" . " * @ingroup tripal_chado_v" . $version . "_schema_api\n" . " *\n" . " */\n" . "function tripal_core_chado_schema_v" . $safe_version . "_" . $table . "() {\n" . " \$description = $arr; \n " . " return \$description;\n" . "}\n" ); } // Finally add the tables function for this version. $table_list = ''; foreach ($table_schemas as $table => $schema) { $table_list .= " '$table',\n"; } print ( "/**\n" . " * Lists the table names in the v" . $version . " chado schema\n" . " *\n" . " * @return\n" . " * An array containing all of the table names\n" . " *\n" . " * @ingroup tripal_chado_v" . $version . "_schema_api\n" . " *\n" . " */\n" . "function tripal_core_chado_get_v" . $safe_version . "_tables() {\n" . " \$tables = array(\n" . "$table_list" . " );\n" . " return \$tables;\n" . "}\n" ); }