Browse Source

Some changes to tripal_core_chado_create_table API function

Stephen Ficklin 12 years ago
parent
commit
e03f5d2e67

+ 15 - 2
tripal_core/tripal_core.api.inc

@@ -1796,7 +1796,9 @@ function tripal_get_max_chado_rank ($tablename, $where_options) {
 	}
 }
 /**
- * Add a new table to the Chado database
+ * Add a new table to the Chado schema. This function is simply a wrapper for
+ * the db_create_table() function of Drupal, but ensures the table is created
+ * inside the Chado schema rather than the Drupal schema.
  *
  * @param $ret
  *   Array to which query results will be added.
@@ -1814,8 +1816,19 @@ function tripal_create_chado_table (&$ret,$table,$schema) {
    $ret = array();
    
    $previous_db = tripal_db_set_active('chado');  // use chado database
-   db_create_table($ret,$table,$schema[$table]);
+   db_create_table($ret,$table,$schema);
    tripal_db_set_active($previous_db);  // now use drupal database
    
+   // if the table creation was succesful then add an entry
+   // in the tripal_custom_table
+   if($ret){
+      $record = new stdClass();
+      $record->table_name = $table;
+      $record->schema = serialize($schema);
+      $success = drupal_write_record('tripal_custom_tables',$record);
+      if(!$success){
+         drupal_set_message($message,"Error adding custom table: $table");   
+      }
+   }
    return $ret;
 }

+ 32 - 2
tripal_core/tripal_core.install

@@ -31,12 +31,19 @@ function tripal_core_update_6000(){
    // recreate the materialized view
    db_add_field($ret, 'tripal_mviews', 'status', array('type' => 'text', 'size' => 'normal', 'not null' => FALSE));
    db_add_field($ret, 'tripal_mviews', 'comment', array('type' => 'text', 'size' => 'normal', 'not null' => FALSE));
+   
+   // create the custom tables table
+   $ret = array();
+   $schema = tripal_core_custom_tables_schema();
+   db_create_table($ret,'tripal_custom_tables',$schema['tripal_custom_tables']);
+   
    $ret = array(
       '#finished' => 1,
    );
-   
+
    return $ret;
 }
+
 /************************************************************************
 * Implementation of hook_schema().
 *
@@ -75,7 +82,10 @@ function tripal_core_get_schemas (){
    foreach ($temp as $table => $arr){ 
       $schema[$table] = $arr; 
    }
-
+   $temp = tripal_core_custom_tables_schema();
+   foreach ($temp as $table => $arr){ 
+      $schema[$table] = $arr; 
+   }
 	return $schema;
 }
 /************************************************************************
@@ -144,4 +154,24 @@ function tripal_core_jobs_schema(){
    );
    return $schema;
 }
+/************************************************************************
+* 
+*
+* @ingroup tripal_core
+*/
+function tripal_core_custom_tables_schema(){
+   $schema = array();
+   $schema['tripal_custom_tables'] = array(
+      'fields' => array(
+         'table_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+         'table_name' => array ('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+         'schema' => array('type' => 'text','not null' => TRUE),
+      ),
+      'indexes' => array(
+         'table_id' => array('table_id'),
+      ),
+      'primary key' => array('table_id'),
+   );
+   return $schema;
+}
 ?>

+ 23 - 2
tripal_core/tripal_core.schema.api.inc

@@ -37,10 +37,20 @@
  */
 
 /**
+ * Retrieves the list tables in the Chado schema.  By default it only retursn
+ * the default Chado tables, but may also return custom tables added to the 
+ * Chado schema as well.
  *
+ * @param $include_custom
+ *   Optional.  Set as TRUE to include any custom tables created in the
+ *   Chado schema. Custom tables are added to Chado using the
+ *   tripal_core_chado_create_table() function.
+ *
+ * @returns
+ *   An associative array where the key and value pairs are the Chado table names.
  * @ingroup tripal_schema_api
  */
-function tripal_core_get_chado_tables() {
+function tripal_core_get_chado_tables($include_custom = NULL) {
   if(is_array($db_url) and array_key_exists('chado',$db_url)){
     $previous_db = tripal_db_set_active('chado');
     $sql = 'SELECT tablename FROM pg_tables';
@@ -50,11 +60,22 @@ function tripal_core_get_chado_tables() {
     $sql = "SELECT tablename FROM pg_tables WHERE schemaname='chado'";
     $resource = db_query($sql);
   }
-  
   $tables = array();
   while ($r = db_fetch_object($resource)) {
     $tables[$r->tablename] = $r->tablename;
+  }  
+  
+  
+  // now add in the custom tables too
+  if($include_custom){
+    $sql = "SELECT table_name FROM tripal_custom_tables";
+    $resource = db_query($sql);
   }
+  while ($r = db_fetch_object($resource)) {
+    $tables[$r->table_name] = $r->table_name;
+  } 
+  
+
   asort($tables);
   return $tables;
 }