Browse Source

Issue #72: Updating API to include 2.1 improvements: 1bf62fe

Stephen Ficklin 7 years ago
parent
commit
bd7d5e8c18

+ 4 - 1
legacy/tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -1046,7 +1046,10 @@ function chado_add_node_form_relationships_name_to_id_callback($base_table, $nam
 
   $base_key = $base_table.'_id';
 
-  $query = db_select('chado.'.$base_table, 'b')
+  // determine the chado schema.
+  $chado = tripal_get_schema_name('chado');
+
+  $query = db_select($chado . '.' . $base_table, 'b')
     ->fields('b', array($base_key, $name_field))
     ->condition($name_field, '%' . db_like($string) . '%', 'LIKE');
 

+ 5 - 2
tripal_chado/api/tripal_chado.custom_tables.api.inc

@@ -127,6 +127,9 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE, $mvi
   $created = 0;
   $recreated = 0;
 
+  $chado_schema = tripal_get_schema_name('chado');
+  $chado_dot = $chado_schema . '.';
+
   $transaction = db_transaction();
   try {
     // see if the table entry already exists in the tripal_custom_tables table.
@@ -139,7 +142,7 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE, $mvi
 
     // if the table does not exist then create it
     if (!$exists) {
-      $ret = db_create_table('chado.' . $table, $schema);
+      $ret = db_create_table($chado_dot . $table, $schema);
       $created = 1;
     }
 
@@ -154,7 +157,7 @@ function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE, $mvi
       if (array_key_exists('referring_tables', $new_schema)) {
         unset($new_schema['referring_tables']);
       }
-      db_create_table('chado.' . $table, $new_schema);
+      db_create_table($chado_dot . $table, $new_schema);
       $recreated = 1;
     }
 

+ 46 - 11
tripal_chado/api/tripal_chado.query.api.inc

@@ -113,17 +113,19 @@ function chado_set_active($dbname = 'default') {
     $GLOBALS['chado_active_db'] = 'default';
   }
 
-  $previous_db = $GLOBALS['chado_active_db'];
-
+  $previous_db = $active_db = $GLOBALS['chado_active_db'];
+  $search_path = tripal_get_schema_name('drupal');
 
   // Change only if 'chado' has been specified.
+  // Notice that we leave the active_db set as chado but use the possibly user-altered
+  // schema name for the actual search path. This is to keep outward facing mentions of
+  // chado as "chado" while still allowing the user to alter the schema name used.
   if ($dbname == 'chado') {
     $active_db = 'chado';
-    $search_path = 'chado,public';
+    $search_path = tripal_get_schema_name('chado') . ',' . tripal_get_schema_name('drupal');
   }
   else {
     $active_db = $dbname;
-    $search_path = 'public';
   }
 
   $settings = array(
@@ -136,10 +138,10 @@ function chado_set_active($dbname = 'default') {
   // note: hooks can alter $active_db and $search_path.
   drupal_alter('chado_connection', $settings);
 
-  // Set chado_active_db to remember active db.
+  // set chado_active_db to remember active db
   $GLOBALS['chado_active_db'] = $active_db;
 
-  // Set PostgreSQL search_path.
+  // set PostgreSQL search_path
   db_query('SET search_path TO ' . $search_path);
 
   return $previous_db;
@@ -1634,15 +1636,15 @@ function chado_query($sql, $args = array()) {
     // Chado tables should be enclosed in curly brackets (ie: {feature} )
     // and Drupal tables should be enclosed in square brackets
     // (ie: [tripal_jobs] ).
-    // @todo: remove assumption that the chado schema is called 'chado' and the
-    // drupal schema is called 'public'.
-    $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
-    $sql = preg_replace('/\[(\w+)\]/', 'public.$1', $sql);
+    $chado_schema_name = tripal_get_schema_name('chado');
+    $drupal_schema_name = tripal_get_schema_name('drupal');
+    $sql = preg_replace('/\{(.*?)\}/', $chado_schema_name.'.$1', $sql);
+    $sql = preg_replace('/\[(\w+)\]/', $drupal_schema_name.'.$1', $sql);
 
     // The featureloc table has some indexes that use function that call other
     // functions and those calls do not reference a schema, therefore, any tables with featureloc
     // must automaticaly have the chado schema set as active to find
-    if (preg_match('/chado.featureloc/i', $sql) or preg_match('/chado.feature/i', $sql)) {
+    if (preg_match('/'.$chado_schema_name . '.featureloc/i', $sql) or preg_match('/' . $chado_schema_name . '.feature/i', $sql)) {
       $previous_db = chado_set_active('chado') ;
       try {
         $results = db_query($sql, $args);
@@ -1905,7 +1907,40 @@ function tripal_get_schema_name($schema = 'chado') {
 
   return $schema_name;
 }
+/**
+ * Alter the name of the schema housing Chado and/or Drupal.
+ *
+ * This example implementation shows a solution for the case where your chado database
+ * was well established in the "public" schema and you added Drupal later in a
+ * "drupal" schema. Please note that this has not been tested and while we can ensure
+ * that Tripal will work as expected, we have no control over whether Drupal is
+ * compatible with not being in the public schema. That's why we recommened the
+ * organization we have (ie: Chado in a "chado" schema and Drupal in the "public schema).
+ *
+ * @param $schema_name
+ *   The current name of the schema as known by Tripal. This is likely the default
+ *   set in tripal_get_schema_name() but in the case of multiple alter hooks, it might
+ *   be different.
+ * @param $context
+ *   This is an array of items to provide context.
+ *     - schema: this is the schema that was passed to tripal_get_schema_name() and will
+ *       be either "chado" or "drupal". This should be used to determine you are changing
+ *       the name of the correct schema.
+ */
+function hook_tripal_get_schema_name_alter($schema_name, $context) {
 
+  // First we check which schema was passed to chado_get_schema().
+  // Notice that we do not use $schema_name since it may already have
+  // been altered by another module.
+  if ($context['schema'] == 'chado') {
+    $schema_name = 'public';
+  }
+  // Notice that we use elseif to capture the second case rather than else. This
+  // avoids the assumption that there is only one chado and one drupal schema.
+  elseif ($context['schema'] == 'drupal') {
+    $schema_name = 'drupal';
+  }
+}
 /**
  * A replacment for db_select when querying Chado.
  *

+ 10 - 7
tripal_chado/api/tripal_chado.schema.api.inc

@@ -31,13 +31,15 @@
  * This function is necessary because Drupal's db_table_exists() function will
  * not look in any other schema but the one were Drupal is installed
  *
+ * @param $table
+ *   The name of the chado table whose existence should be checked.
+ *
  * @return
- *   TRUE/FALSE depending upon whether it exists
+ *   TRUE if the table exists in the chado schema and FALSE if it does not.
  *
  * @ingroup tripal_chado_schema_api
  */
 function chado_table_exists($table) {
-  global $databases;
 
   global $databases;
 
@@ -264,9 +266,9 @@ function chado_is_local() {
     FROM pg_namespace
     WHERE
       has_schema_privilege(nspname, 'USAGE') AND
-      nspname = 'chado'
+      nspname = :chado
   ";
-  $results = db_query($sql);
+  $results = db_query($sql, array(':chado' => tripal_get_schema_name('chado')));
   $name = $results->fetchObject();
   if ($name) {
     variable_set('chado_schema_exists', FALSE);
@@ -290,7 +292,7 @@ function chado_is_installed() {
   global $databases;
 
   // first check if chado is in the $databases variable of the settings.php file
-  if (array_key_exists('chado', $databases)) {
+  if (array_key_exists(tripal_get_schema_name('chado'), $databases)) {
     return TRUE;
   }
 
@@ -332,7 +334,7 @@ function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
   if (!$chado_exists) {
     // if it's not in the drupal database check to see if it's specified in the $db_url
     // in the settings.php
-    if (!array_key_exists('chado', $databases)) {
+    if (!array_key_exists(tripal_get_schema_name('chado'), $databases)) {
       // if it's not in the drupal database or specified in the $db_url then
       // return uninstalled as the version
       return 'not installed';
@@ -344,7 +346,8 @@ function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
   }
   else {
     $is_local = 1;
-    $prop_exists = chado_table_exists('chadoprop');
+    // @todo we need a chado aware db_table_exists.
+    $prop_exists = db_table_exists(tripal_get_schema_name('chado') . '.chadoprop');
   }
 
   // if the table doesn't exist then we don't know what version but we know

+ 2 - 5
tripal_chado/includes/tripal_chado.cv.inc

@@ -331,11 +331,8 @@ function tripal_cv_cvterm_edit_form($form, &$form_state) {
     $cv_id = $form_state['build_info']['args'][0];
     $cvterm_id = $form_state['build_info']['args'][1];
     if ($form_state['build_info']['args'][1]) {
-      $result = db_select('chado.cvterm','c')
-      ->fields('c', array('name'))
-      ->condition('c.cvterm_id',$cvterm_id)
-      ->execute();
-      $cvterm_name = $result->fetchObject()->name;
+      $cvterm_name = chado_query('SELECT name FROM {cvterm} WHERE cvterm_id = :id',
+          array(':id' => $cvterm_id))->fetchField();
       $step = 1;
     }
   }

+ 2 - 4
tripal_chado/includes/tripal_chado.install.inc

@@ -493,10 +493,8 @@ function tripal_chado_upgrade_chado_1_2_to_1_3_pre_alter() {
 function tripal_chado_upgrade_chado_1_11_to_1_2() {
 
   // Get the path to the schema diff and upgarde SQL files.
-  $schema_file = drupal_get_path('module', 'tripal_chado') .
-    '/chado_schema/default_schema-1.11-1.2-diff.sql';
-  $init_file = drupal_get_path('module', 'tripal_chado') .
-    '/chado_schema/upgrade-1.11-1.2.sql';
+  $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
+  $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/upgrade-1.11-1.2.sql';
 
   $success = tripal_chado_install_sql($schema_file);
   if ($success) {

+ 5 - 2
tripal_chado_views/tripal_chado_views.views.inc

@@ -117,6 +117,9 @@ function tripal_chado_views_views_data() {
   // Manually integrate the drupal.tripal_views* tables
   $data = tripal_chado_views_views_data_tripal_views_tables($data);
 
+  // Determine the name of the chado schema
+  $chado_schema = tripal_get_schema_name('chado');
+
   // MAKE SURE ALL CHADO TABLES ARE INTEGRATED
   tripal_chado_views_integrate_all_chado_tables();
 
@@ -239,7 +242,7 @@ function tripal_chado_views_views_data() {
         'group' => "$tvi_row->name",
         'title' => "$tvi_row->name",
         'help'  => $tvi_row->comment,
-        'search_path' => 'chado'
+        'search_path' => $chado_schema
       );
     }
     else {
@@ -247,7 +250,7 @@ function tripal_chado_views_views_data() {
         'group' => "$tvi_row->name",
         'title' => "$tvi_row->name",
         'help'  => $tvi_row->comment,
-        'search_path' => 'chado'
+        'search_path' => $chado_schema
       );
     }