Index: search_by_page.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/search_by_page/search_by_page.module,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 search_by_page.module
--- search_by_page.module	11 Jun 2009 17:10:51 -0000	1.1.2.3
+++ search_by_page.module	7 Aug 2009 19:39:09 -0000
@@ -129,6 +129,56 @@
 }
 
 /**
+ * Returns unique where and join clauses, similar to _db_rewrite_sql().
+ *
+ * Use this function as a direct replacement for _db_rewrite_sql().
+ * It works the same way, except that all table name aliases are prefixed
+ * with the passed-in prefix string. For instance, if the prefix string
+ * is 'abc_', then the node table will be aliased as 'abc_n', and the
+ * node access table as 'abc_na'. This will allow Search by Page to
+ * collect multiple query pieces together without conflict.
+ *
+ * @param $prefix
+ *   Prefix to use for all aliases.
+ * @return
+ *   Array with join statement and where statement.
+ */
+function search_by_page_unique_rewrite( $prefix ) {
+  $stuff = _db_rewrite_sql( '', $prefix . 'n' );
+
+  // Prefix any non-prefixed tables in there
+
+  $tablechars = '[a-zA-Z0-9_]'; 
+  // Regexp looks for table aliases in Drupal terms like {node_access} na
+  // Note that this only occurs in the join statement!
+  $regex = "|\{$tablechars+\}\s+($tablechars+)\s+|";
+  $matches = array();
+  $pos = 0;
+
+  while( preg_match( $regex, $stuff[0], $matches, PREG_OFFSET_CAPTURE, $pos )) {
+    $oldtable = $matches[1][0];
+    $pos = $matches[1][1];
+    if( preg_match( '|$' . $prefix . '|', $oldtable )) {
+      // this has already been prefixed
+      continue;
+    }
+
+    // Replace all occurrences of this table alias in join and where
+
+    // When replacing, table name could have a paren or space in front,
+    // or an = sign, or be at the beginning of the line. Followed by a 
+    // space or a .
+    $pat = '|([$\(\s])' . $oldtable . '([\s\.])|';
+    $repl = '$1' . $prefix . $oldtable. '$2';
+
+    $stuff[0] = preg_replace( $pat, $repl, $stuff[0] );
+    $stuff[1] = preg_replace( $pat, $repl, $stuff[1] );
+  }
+  
+  return $stuff;
+}
+
+/**
  * Implementation of hook_search().
  *
  * Defines how to search by page.
Index: sbp_attach.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/search_by_page/Attic/sbp_attach.module,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 sbp_attach.module
--- sbp_attach.module	9 Jun 2009 21:24:28 -0000	1.1.2.1
+++ sbp_attach.module	7 Aug 2009 19:39:09 -0000
@@ -207,13 +207,13 @@
 
 
   // Get node access permissions from db_rewrite_sql().
-  $stuff =  _db_rewrite_sql( '', 'n2' );
+  $stuff =  search_by_page_unique_rewrite( 'sbpa_' );
 
   // Add node information to query -- only allowing published nodes, plus
   // db_rewrite_sql stuff
 
-  $join .= ' LEFT JOIN {node} n2 ON n2.nid = saf.nid AND n2.vid = saf.vid ' . $stuff[0];
-  $where .= ' AND n2.status = 1';
+  $join .= ' LEFT JOIN {node} sbpa_n ON sbpa_n.nid = saf.nid AND sbpa_n.vid = saf.vid ' . $stuff[0];
+  $where .= ' AND sbpa_n.status = 1';
   if ( $stuff[1] ) {
     $where .= ' AND ' . $stuff[1];
   }
Index: sbp_nodes.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/search_by_page/sbp_nodes.module,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 sbp_nodes.module
--- sbp_nodes.module	9 Jun 2009 21:15:04 -0000	1.1.2.1
+++ sbp_nodes.module	7 Aug 2009 19:39:09 -0000
@@ -64,13 +64,13 @@
  */
 function sbp_nodes_sbp_query_modify() {
   // Get node access permissions from db_rewrite_sql()
-  $stuff =  _db_rewrite_sql();
+  $stuff =  search_by_page_unique_rewrite('sbpn_' );
 
   // Form join/where clauses, only allowing published nodes, plus
   // node access stuff
 
-  $join = 'LEFT JOIN {node} n ON n.nid = sp.modid ' . $stuff[0];
-  $where = 'n.status = 1';
+  $join = 'LEFT JOIN {node} sbpn_n ON sbpn_n.nid = sp.modid ' . $stuff[0];
+  $where = 'sbpn_n.status = 1';
   if ( $stuff[1] ) {
     $where .= ' AND ' . $stuff[1];
   }
Index: API_README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/search_by_page/API_README.txt,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 API_README.txt
--- API_README.txt	9 Jun 2009 21:20:39 -0000	1.1.2.1
+++ API_README.txt	7 Aug 2009 19:39:09 -0000
@@ -15,6 +15,8 @@
    search_by_page_force_reindex()
    search_by_page_force_remove()
    search_by_page_rebuild_paths()
+   search_by_page_path_parts()
+   search_by_page_unique_rewrite()
 */
 
 /**
