Tripal v1.0 (6.x-1.0)
get_FKs.php
Go to the documentation of this file.
00001 <?php
00002 // This script will add FK relatinsions to an existing schema API array for each 
00003 // Chado table.  It requires Chado is installed in a 'chado' schema of 
00004 // the drupal database.  It also requires existing schema hooks for 
00005 // version of Chado.  The goal is to use the output of this script to 
00006 // update the existing schema hooks.  Redirect the output of this script to 
00007 // a file and then replace the existing schema API include file (e.g. 
00008 // tripal_core.schema_v1.2.api.inc).  Be sure to check it before replacing
00009 
00010 // this script requires a single argument (-v) which is the Chado version
00011 //
00012 // example usage in drupal directory root:
00013 //
00014 // php ./sites/all/modules/tripal/tripal_core/api/get_FKs.php -v 1.11 > \
00015 //   ./sites/all/modules/tripal/tripal_core/apitripal_core.schema_v1.11.api.inc.new
00016 //
00017 // php ./sites/all/modules/tripal/tripal_core/api/get_FKs.php -v 1.2 > \
00018 //   ./sites/all/modules/tripal/tripal_core/api/tripal_core.schema_v1.2.api.inc.new
00019 
00020 
00021 $arguments = getopt("v:");
00022 
00023 if (isset($arguments['v'])) {
00024   $drupal_base_url = parse_url('http://www.example.com');
00025   $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
00026   $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
00027   $_SERVER['REMOTE_ADDR'] = NULL;
00028   $_SERVER['REQUEST_METHOD'] = NULL;
00029 
00030   require_once 'includes/bootstrap.inc';
00031   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
00032 
00033   $version = $arguments['v'];
00034   get_chado_fk_relationships($version);
00035 }
00036 
00040 function get_chado_fk_relationships($version) {
00041 
00042   // convert the version to a form suitable for function names
00043   $v = $version;
00044   $v = preg_replace("/\./","_",$v);
00045  
00046   $tables = tripal_core_get_chado_tables(); 
00047   $sql ="
00048     SELECT
00049         tc.constraint_name, tc.table_name, kcu.column_name, 
00050         ccu.table_name AS foreign_table_name,
00051         ccu.column_name AS foreign_column_name 
00052     FROM 
00053         information_schema.table_constraints AS tc 
00054         JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
00055         JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
00056     WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='%s'
00057   ";
00058     
00059   // iterate through the tables and get the foreign keys
00060   print "<?php
00061 /* @file: This file contains default schema definitions for all chado v$version tables
00062  *        to be used by other function. Specifically these functions are used
00063  *        by the tripal_core select/insert/update API functions and by
00064  *        the Tripal Views module.
00065  *
00066  *        These schema definitions can be augmented by another modules
00067  *        (specifically to add missing definitions) by implementing
00068  *        hook_chado_schema_v" . $v . "_<table name>().
00069  *
00070  * @defgroup tripal_schema_api Core Module Schema API
00071  * @{
00072  * Provides an application programming interface (API) for describing Chado tables.
00073  * This API consists of a set of functions, one for each table in Chado.  Each
00074  * function simply returns a Drupal style array that defines the table.
00075  *
00076  * Because Drupal 6 does not handle foreign key (FK) relationships, however FK 
00077  * relationships are needed to for Tripal Views.  Therefore, FK relationships
00078  * have been added to the schema defintitions below.
00079  *
00080  * The functions provided in this documentation should not be called as is, but if you need
00081  * the Drupal-style array definition for any table, use the following function
00082  * call:
00083  *
00084  *   \$table_desc = tripal_core_get_chado_table_schema(\$table)
00085  *
00086  * where the variable \$table contains the name of the table you want to
00087  * retireve.  The tripal_core_get_chado_table_schema function determines the appropriate version of 
00088  * Chado and uses the Drupal hook infrastructure to call the appropriate 
00089  * hook function to retrieve the table schema.
00090  *
00091  * @}
00092  * @ingroup tripal_api
00093  */
00094 ";
00095   $referring = array();
00096   $tables_def = array();
00097   foreach ($tables as $table) {
00098 
00099     // get the existing table array
00100     $table_arr = tripal_core_get_chado_table_schema($table);
00101     
00102     if (empty($table_arr)) {
00103        print "ERROR: empty table definition $table\n";
00104        continue;
00105     }
00106     
00107     // add the table name to the array
00108     $table_arr['table'] = $table;
00109     
00110     // get the foreign keys and add them to the array
00111     $fks = db_query($sql,$table);
00112     while ($fk = db_fetch_object($fks)) {
00113       $table_arr['foreign keys'][$fk->foreign_table_name]['table'] = $fk->foreign_table_name;
00114       $table_arr['foreign keys'][$fk->foreign_table_name]['columns'][$fk->column_name] = $fk->foreign_column_name;
00115       $reffering[$fk->foreign_table_name][] = $table;
00116     }
00117     $tables_def[] = $table_arr;
00118   }
00119   
00120   // now add in the referring tables and print
00121   foreach ($tables_def as $table_arr) {
00122     $table = $table_arr['table'];
00123     
00124     // add in the referring tables
00125     $table_referring = array_unique($reffering[$table]);
00126     $table_arr['referring_tables'] = $table_referring;
00127       
00128     // reformat the array to be more legible
00129     $arr = var_export($table_arr, 1);
00130     $arr = preg_replace("/\n\s+array/","array", $arr); // move array( to previous line
00131     $arr = preg_replace("/\n/","\n  ", $arr); // add indentation
00132     $arr = preg_replace("/true/","TRUE", $arr); // add indentation
00133     $arr = preg_replace("/false/","FALSE", $arr); // add indentation
00134     $arr = preg_replace("/array \(/","array(", $arr); // add indentation      
00135       
00136       // print out the new Schema API function for this table
00137 print "/**
00138  * Implements hook_chado_schema_v".$v."_".$table."()
00139  * Purpose: To describe the structure of '$table' to tripal
00140  * @see tripal_core_chado_insert()
00141  * @see tripal_core_chado_update()
00142  * @see tripal_core_chado_select()
00143  *
00144  * @return
00145  *    An array describing the '$table' table
00146  *
00147  * @ingroup tripal_chado_v".$version."_schema_api
00148  *
00149  */
00150 function tripal_core_chado_schema_v".$v."_".$table."() {
00151   \$description =  $arr;
00152   return \$description;
00153 }
00154 ";
00155   }
00156 }
 All Classes Files Functions Variables