Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Commit

Permalink
# [joomla#28615] Backport union query element from Platform 12 and u…
Browse files Browse the repository at this point in the history
…se it in

Smart Search. Thanks Michael.
  • Loading branch information
mbabker authored and infograf768 committed Jun 4, 2012
1 parent 9e4ce06 commit fbe69c5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 13 deletions.
26 changes: 13 additions & 13 deletions administrator/components/com_finder/helpers/indexer/query.php
Expand Up @@ -1302,20 +1302,20 @@ protected function getTokenData($token)
else
{
// Add the term to the query.
// $query->where('t.term = ' . $db->quote($token->term));
// $query->where('t.phrase = 0');
//
// // Clone the query, replace the WHERE clause.
// $sub = clone($query);
// $sub->clear('where');
// $sub->where('t.stem = '.$db->quote($token->stem));
// $sub->where('t.phrase = 0');
//
// // Union the two queries.
// $query->union($sub);

$query->where('(t.term = ' . $db->quote($token->term) . ' OR t.stem = ' . $db->quote($token->stem) . ')');
$query->where('t.term = ' . $db->quote($token->term));
$query->where('t.phrase = 0');

// Clone the query, replace the WHERE clause.
$sub = clone($query);
$sub->clear('where');
$sub->where('t.stem = '.$db->quote($token->stem));
$sub->where('t.phrase = 0');

// Union the two queries.
$query->union($sub);

// $query->where('(t.term = ' . $db->quote($token->term) . ' OR t.stem = ' . $db->quote($token->stem) . ')');
// $query->where('t.phrase = 0');
}

// Get the terms.
Expand Down
1 change: 1 addition & 0 deletions installation/CHANGELOG
Expand Up @@ -30,6 +30,7 @@ $ -> Language fix or change
+ [#28067] Option to disable the ability to change the Username in Profile
+ [#24510] Implementation of Terms of Service. Thanks Peter.
+ [#28614] Add option to enable/disable logging in Smart Search. Thanks Michael.
# [#28615] Backport union query element from Platform 12 and use it in Smart Search. Thanks Michael.

03-Jun-2012 Mark Dexter
^ [#28450] Set the CSS class for JForm field label. Thanks Viet Vu.
Expand Down
85 changes: 85 additions & 0 deletions libraries/joomla/database/query.php
Expand Up @@ -231,6 +231,12 @@ abstract class JDatabaseQuery
*/
protected $order = null;

/**
* @var JDatabaseQueryElement The union element.
* @since 12.1
*/
protected $union = null;

/**
* @var object The auto increment insert field element.
* @since 11.1
Expand Down Expand Up @@ -333,6 +339,10 @@ public function __toString()

break;

case 'union':
$query .= (string) $this->union;
break;

case 'delete':
$query .= (string) $this->delete;
$query .= (string) $this->from;
Expand Down Expand Up @@ -1200,4 +1210,79 @@ public function __clone()
}
}
}

/**
* Add a query to UNION with the current query.
* Multiple unions each require separate statements and create an array of unions.
*
* Usage:
* $query->union('SELECT name FROM #__foo')
* $query->union('SELECT name FROM #__foo','distinct')
* $query->union(array('SELECT name FROM #__foo','SELECT name FROM #__bar'))
*
* @param mixed $query The JDatabaseQuery object or string to union.
* @param boolean $distinct True to only return distinct rows from the union.
* @param string $glue The glue by which to join the conditions.
*
* @return mixed The JDatabaseQuery object on success or boolean false on failure.
*
* @since 12.1
*/
public function union($query, $distinct = false, $glue = '')
{

// Clear any ORDER BY clause in UNION query
// See http://dev.mysql.com/doc/refman/5.0/en/union.html
if (!is_null($this->order))
{
$this->clear('order');
}

// Set up the DISTINCT flag, the name with parentheses, and the glue.
if ($distinct)
{
$name = 'UNION DISTINCT ()';
$glue = ')' . PHP_EOL . 'UNION DISTINCT (';
}
else
{
$glue = ')' . PHP_EOL . 'UNION (';
$name = 'UNION ()';

}
// Get the JDatabaseQueryElement if it does not exist
if (is_null($this->union))
{
$this->union = new JDatabaseQueryElement($name, $query, "$glue");
}
// Otherwise append the second UNION.
else
{
$glue = '';
$this->union->append($query);
}

return $this;
}

/**
* Add a query to UNION DISTINCT with the current query. Simply a proxy to Union with the Distinct clause.
*
* Usage:
* $query->unionDistinct('SELECT name FROM #__foo')
*
* @param mixed $query The JDatabaseQuery object or string to union.
* @param string $glue The glue by which to join the conditions.
*
* @return mixed The JDatabaseQuery object on success or boolean false on failure.
*
* @since 12.1
*/
public function unionDistinct($query, $glue = '')
{
$distinct = true;

// Apply the distinct flag to the union.
return $this->union($query, $distinct, $glue);
}
}

0 comments on commit fbe69c5

Please sign in to comment.