? .svn
? tw_crap.patch
? tw_external_tables.patch
? tw_external_tables.patch.1
? tw_external_tables_2.patch
? help/.svn
? tests/.svn
? tw_import/.svn
? tw_import/tests/.svn
Index: tw.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw.module,v
retrieving revision 1.1.2.44
diff -u -p -r1.1.2.44 tw.module
--- tw.module	17 Oct 2009 23:32:11 -0000	1.1.2.44
+++ tw.module	18 Nov 2009 01:22:36 -0000
@@ -517,6 +517,72 @@ function tw_quote_identifier($identifier
   return $quote . $identifier . $quote;
 }
 
+
+
+/**
+ * Check if the external database and the default database are compatible
+ * enough to share tables.
+ *
+ * @param $dbkey
+ *   The non-default database key set in the settings.php file.
+ * @return
+ *   TRUE if they are compatible, FALSE if they are not.
+ */
+function tw_check_dbcompat($dbkey) {
+  static $checked_compats = array();
+  
+  //if we have already checked this, just return it.
+  if (isset($checked_compats[$dbkey])) {
+    return $checked_compats[$dbkey];
+  }
+  
+  //this is a new check
+  $externaldb = tw_get_dbinfo($dbkey);
+  $internaldb = tw_get_dbinfo('default');
+  
+  //they must have the same scheme, username, password, host?
+  $checks = array('scheme', 'username', 'password', 'host');
+  //only works with mysql, so let's check that right away.
+  $compatible = ($externaldb['scheme'] == 'mysql' || $externaldb['scheme'] == 'mysqli') ;
+  $i = 0;
+  
+  while($compatible === TRUE && $i < 4) {
+    $compatible = ($externaldb[$checks[$i]] == $internaldb[$checks[$i]]);
+    $i++;
+  }
+  $checked_compats[$dbkey] = $compatible;
+  return $compatible;
+}
+
+/**
+ * Get the database properties from it's dbconnection key in $db_url, 
+ * and cache it.
+ *
+ * @param $dbconnection
+ *   The database key set in the settings.php file ('default').
+ * @param $property
+ *   Optional - The individual property you want returned.
+ * @return
+ *   If property was given, then the property value. Otherwise the whole
+ *   array of properties plus 'name'.
+ */
+function tw_get_dbinfo($dbkey, $property = NULL) {
+  //cache the parsed db properties.
+  static $db_url_parsed = array();
+  if (!isset($db_url_parsed[$dbkey])) {
+    global $db_url;
+    $db_url_parsed[$dbkey] = parse_url($db_url[$dbkey]);
+    $db_url_parsed[$dbkey]['name'] = substr($db_url_parsed[$dbkey]['path'], 1);
+  }
+  // if a preporty was requested, then return just that property.
+  if(isset($property)) {
+    return $db_url_parsed[$dbkey][$property];
+  }
+  //else just return the whole array.
+  return $db_url_parsed[$dbkey];  
+}
+  
+
 //////////////////////////////////////////////////////////
 // Drupal core hooks
 //
Index: tw.views_default.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw.views_default.inc,v
retrieving revision 1.1.2.19
diff -u -p -r1.1.2.19 tw.views_default.inc
--- tw.views_default.inc	17 Oct 2009 23:28:47 -0000	1.1.2.19
+++ tw.views_default.inc	18 Nov 2009 01:22:36 -0000
@@ -37,9 +37,8 @@ function tw_views_default_views() {
     }
     else {
       global $db_url;
-      $url = parse_url($db_url[$dbconnection ]);
-      $truedbname = substr($url['path'], 1);
-      $rawtablename = $tablename;
+      $truedbname = tw_get_dbinfo($dbconnection, 'name');
+      $rawtablename = $truedbname .'.'. $tablename;
       $cleantablename = $truedbname . '_' . $tablename;
     }
     // Create a basic table view, with exclusion flags, for each import table
Index: tw_pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/tw_pages.inc,v
retrieving revision 1.1.2.52
diff -u -p -r1.1.2.52 tw_pages.inc
--- tw_pages.inc	17 Oct 2009 23:28:47 -0000	1.1.2.52
+++ tw_pages.inc	18 Nov 2009 01:22:37 -0000
@@ -830,11 +830,11 @@ function tw_relationships_form($form_sta
     $checks[$row->twrid] = '';
     $form['twrid'][$row->twrid] = array('#value' => $row->twrid);
     if ($use_connection) {
-      $leftcol = $row->leftconn . '.' . $leftcol;
+      $leftcol = '('.$row->leftconn .') '. $leftcol;
     }
     $form['leftcol'][$row->twrid] = array('#value' => $leftcol);
     if ($use_connection) {
-      $rightcol = $row->rightconn . '.' . $rightcol;
+      $rightcol = '('. $row->rightconn . ') ' . $rightcol;
     }
     $form['rightcol'][$row->twrid] = array('#value' => $rightcol);
     $form['automatic'][$row->twrid] = array('#value' =>
@@ -866,7 +866,7 @@ function tw_relationships_form($form_sta
   $options = array();
   while ($row = db_fetch_array($result)) {
     if ($use_connection) {
-      $options[$row['twcid']] = $row['dbconnection'] . '.' . $row['tablename'] .'.'. $row['colname'];
+      $options[$row['twcid']] = '('. $row['dbconnection'] . ') ' . $row['tablename'] .'.'. $row['colname'];
     }
     else {
       $options[$row['twcid']] = $row['tablename'] . '.' . $row['colname'];
Index: tw_tablebuild.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tw/Attic/tw_tablebuild.inc,v
retrieving revision 1.1.2.9
diff -u -p -r1.1.2.9 tw_tablebuild.inc
--- tw_tablebuild.inc	17 Oct 2009 23:28:47 -0000	1.1.2.9
+++ tw_tablebuild.inc	18 Nov 2009 01:22:37 -0000
@@ -57,13 +57,23 @@ function _tw_generate_views_table_data($
     // Load the schema so we can get table comments.
     $schema = drupal_get_schema($tablename);
     $dbconnection = $tblrow->dbconnection;
+    
+    //intialize a blank prefix
+    $db_prefix = '';
+        
     if ($dbconnection == 'default') {
       $rawtablename = schema_unprefix_table($tablename);
       $disptablename = $tablename;
     }
     else {
-      $rawtablename = $tablename;
-      $disptablename = $dbconnection . '.' . $tablename;
+      $dbname = tw_get_dbinfo($dbconnection, 'name');
+      $disptablename = $dbconnection .'_'. $tablename;
+      if (tw_check_dbcompat($dbconnection)) {
+        $db_prefix = $dbname .'.';
+        $disptablename = $dbconnection . $tablename;
+        $dbconnection = 'default';
+      }
+      $rawtablename = $db_prefix . $tablename;
     }
     $twtid = $tblrow->twtid;
     $table = array();
@@ -131,7 +141,7 @@ function _tw_generate_views_relationship
   $t = $export ? '_tw_t_wrap' : 't';
   $where = is_null($twtids) ? '' : 'WHERE twt1.twtid IN (' . db_placeholders($twtids) . ')';
   // Now that all tables are present, fill in relationships defined by foreign keys
-  $sql = "SELECT twt1.tablename AS tbl1, twc1.colname AS col1, twt2.tablename AS tbl2,
+  $sql = "SELECT twt1.tablename AS tbl1, twc1.colname AS col1, twt1.dbconnection AS dbcon1, twt2.tablename AS tbl2,
                   twc2.colname AS col2, twt2.twtid AS twtid2, twt2.dbconnection AS dbcon2,
                   twr.automatic
           FROM {tw_relationships} twr
@@ -148,15 +158,33 @@ function _tw_generate_views_relationship
   $i = 0;
   while ($row = db_fetch_array($result)) {
     extract($row);
-    $rawtbl1 = schema_unprefix_table($tbl1);
-    $rawtbl2 = schema_unprefix_table($tbl2);
+    
+    if (!tw_check_dbcompat($dbcon1) || $dbcon1 == 'default') {
+     
+      $rawtbl1 = schema_unprefix_table($tbl1);
+    }
+    else {
+       $rawtbl1 = tw_get_dbinfo($dbcon1, 'name') .'.' . $tbl1;
+    }  
+    
+    
+    if (!tw_check_dbcompat($dbcon2) || $dbcon2 == 'default') {
+      
+      $rawtbl2 = schema_unprefix_table($tbl2);
+    }
+    else{
+      $rawtbl2 = tw_get_dbinfo($dbcon2, 'name') .'.'. $tbl2;
+      
+    }
+    
     if ($automatic) {
       // Use Views joins (automatically include right-table fields in left-table views)
-      $disptablename = $dbcon2 . '.' . $tbl2;
+      $disptablename = $dbcon2 . '_' . $tbl2;
       if (!isset($tables[$rawtbl2]['table']['group'])) {
         $tables[$rawtbl2]['table']['group'] = $disptablename;
       }
       $tables[$rawtbl2]['table']['join'][$rawtbl1] = array(
+        'table' => $rawtbl2,
         'left_field' => $col1,
         'field' => $col2,
       );
