You are here

function search_by_page_unique_rewrite in Search by Page 6

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.

Parameters

$prefix: Prefix to use for all aliases.

Return value

Array with join statement and where statement.

3 calls to search_by_page_unique_rewrite()
sbp_attach_sbp_query_modify in ./sbp_attach.module
Implementation of Search by Page hook_sbp_query_modify().
sbp_nodes_sbp_query_modify in ./sbp_nodes.module
Implementation of Search by Page hook_sbp_query_modify().
SearchByPageUnitTest::testUniqueRewrite in tests/search_by_page.test
Tests the search_by_page_unique_rewrite() function.

File

./search_by_page.module, line 213
Main module file for Drupal module Search by Page.

Code

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;
}