1
0
mirror of synced 2024-12-14 23:26:04 +03:00
This commit is contained in:
zYne 2007-08-01 22:53:33 +00:00
parent c643385fce
commit ebfc0971c6

View File

@ -44,6 +44,8 @@ class Doctrine_Search_Query
protected $_table = array(); protected $_table = array();
protected $_sql = ''; protected $_sql = '';
protected $_condition;
/** /**
* @param octrine_Table $_table the index table * @param octrine_Table $_table the index table
*/ */
@ -56,6 +58,9 @@ class Doctrine_Search_Query
$this->_table = $table; $this->_table = $table;
$this->_query = new Doctrine_Query(); $this->_query = new Doctrine_Query();
$foreignId = current(array_diff($this->_table->getColumnNames(), array('keyword', 'field', 'position')));
$this->_condition = $foreignId . ' IN (SELECT ' . $foreignId . ' FROM ' . $this->_table->getTableName() . ' WHERE ';
} }
/** /**
* getQuery * getQuery
@ -71,7 +76,7 @@ class Doctrine_Search_Query
{ {
$text = strtolower(trim($text)); $text = strtolower(trim($text));
$foreignId = current(array_diff($this->_table->getColumnNames(), array('keyword', 'field', 'position')));
$weighted = false; $weighted = false;
if (strpos($text, '^') === false) { if (strpos($text, '^') === false) {
@ -93,66 +98,69 @@ class Doctrine_Search_Query
$this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . ' ' . $orderby; $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . ' ' . $orderby;
} }
public function tokenizeClause($clause)
public function parseClause($originalClause, $addCondition = false)
{ {
$clause = strtolower(trim($clause)); $clause = Doctrine_Tokenizer::bracketTrim($originalClause);
$clause = Doctrine_Tokenizer::bracketTrim($clause);
$terms = Doctrine_Tokenizer::sqlExplode($clause, ' ', '(', ')'); $brackets = false;
$operator = self::OPERATOR_AND; if ($clause !== $originalClause) {
$brackets = true;
$ret = array();
$pending = false;
$i = 0;
$prev = false;
foreach ($terms as $k => $term) {
$term = trim($term);
if ($term === 'and') {
$operator = self::OPERATOR_AND;
} elseif ($term === 'or') {
$operator = self::OPERATOR_OR;
} else {
if ($operator === self::OPERATOR_OR) {
$ret[$i] = $term;
$i++;
} else {
if ($k === 0) {
$ret[$i] = $term;
$i++;
} else {
if ( ! is_array($ret[($i - 1)])) {
$ret[($i - 1)] = array_merge(array($ret[($i - 1)]), array($term));
} else {
$ret[($i - 1)][] = $term;
} }
}
}
$operator = self::OPERATOR_AND;
}
}
return $ret;
}
public function parseClause($clause)
{
$clause = Doctrine_Tokenizer::bracketTrim($clause);
$foreignId = current(array_diff($this->_table->getColumnNames(), array('keyword', 'field', 'position'))); $foreignId = current(array_diff($this->_table->getColumnNames(), array('keyword', 'field', 'position')));
$terms = $this->tokenizeClause($clause); $terms = Doctrine_Tokenizer::sqlExplode($clause, ' OR ', '(', ')');
if (count($terms) > 1) {
$ret = array(); $ret = array();
foreach ($terms as $term) { if (count($terms) > 1) {
if (is_array($term)) {
$parsed = $this->parseTerms($term); $leavesOnly = true;
foreach ($terms as $k => $term) {
if ($this->isExpression($term)) {
$ret[$k] = $this->parseClause($term);
$leavesOnly = false;
} else { } else {
$ret[$k] = $this->parseTerm($term);
}
}
$return = implode(' OR ', $ret);
if ($leavesOnly) {
$return = $this->_condition . $return;
}
$brackets = false;
} else {
$terms = Doctrine_Tokenizer::sqlExplode($clause, ' ', '(', ')');
foreach ($terms as $k => $term) {
$term = trim($term);
if ($term === 'AND') {
continue;
}
if ($this->isExpression($term)) {
$ret[$k] = $this->parseClause($term, true);
} else {
$ret[$k] = $this->_condition . $this->parseTerm($term);
}
$ret[$k] .= ')';
}
$return = implode(' AND ', $ret);
}
if ($brackets) {
return '(' . $return . ')';
} else {
return $return;
}
/**
if (strpos($term, '(') === false) { if (strpos($term, '(') === false) {
if (substr($term, 0, 1) === '-') { if (substr($term, 0, 1) === '-') {
$operator = 'NOT IN'; $operator = 'NOT IN';
@ -164,22 +172,17 @@ class Doctrine_Search_Query
} else { } else {
$parsed = $this->parseClause($term); $parsed = $this->parseClause($term);
} }
*/
} }
public function isExpression($term)
if (strlen($parsed) > 20) { {
$ret[] = '(' . $parsed . ')'; if (strpos($term, '(') !== false) {
return true;
} else { } else {
$ret[] = $parsed; $terms = Doctrine_Tokenizer::quoteExplode($term);
}
}
$r = implode(' AND ', $ret); return (count($terms) > 1);
} else {
$terms = (is_array($terms[0])) ? $terms[0] : array($terms[0]);
return $this->parseTerms($terms);
} }
return $r;
} }
public function parseTerms(array $terms) public function parseTerms(array $terms)
{ {
@ -188,13 +191,13 @@ class Doctrine_Search_Query
if (count($terms) > 1) { if (count($terms) > 1) {
$ret = array(); $ret = array();
foreach ($terms as $term) { foreach ($terms as $term) {
$ret[] = $this->parseClause($term); if (strpos($term, '(') === false) {
} $term = $this->parseClause($term);
$parsed = implode(' OR ', $ret);
if (strpos($parsed, '(') === false) { $ret[] = $foreignId . ' IN (SELECT ' . $foreignId . ' FROM ' . $this->_table->getTableName() . ' WHERE ' . $term . ')';
$parsed = $foreignId . ' IN (SELECT ' . $foreignId . ' FROM ' . $this->_table->getTableName() . ' WHERE ' . $parsed . ')';
} }
}
$parsed = implode(' AND ', $ret);
return $parsed; return $parsed;
} else { } else {
@ -202,13 +205,16 @@ class Doctrine_Search_Query
return $ret[0]; return $ret[0];
} }
} }
public function parseExpression($expr)
{
$expr = Doctrine_Tokenizer::bracketTrim($expr);
}
public function parseTerm($term) public function parseTerm($term)
{ {
$negation = false; $negation = false;
if (strpos($term, "'") === false) { if (strpos($term, "'") === false) {
$where = 'keyword = ?'; $where = 'keyword = ?';
$params = array($term); $params = array($term);
@ -225,7 +231,7 @@ class Doctrine_Search_Query
$where .= ' AND (position + ' . $k . ') = (SELECT position FROM ' . $this->_table->getTableName() . ' WHERE keyword = ?)'; $where .= ' AND (position + ' . $k . ') = (SELECT position FROM ' . $this->_table->getTableName() . ' WHERE keyword = ?)';
} }
} }
return array($where, $params); return $where;
} }
public function getSql() public function getSql()