2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-09 08:40:50 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects textual content with an exact match between columnname and keyword.
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
|
|
|
* @todo case sensitivity switch
|
|
|
|
* @todo documentation
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-06 05:28:25 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
class ExactMatchFilter extends SearchFilter {
|
2012-12-10 23:35:04 +01:00
|
|
|
|
|
|
|
public function setModifiers(array $modifiers) {
|
2012-09-20 09:26:05 +02:00
|
|
|
if(($extras = array_diff($modifiers, array('not', 'nocase', 'case'))) != array()) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
get_class($this) . ' does not accept ' . implode(', ', $extras) . ' as modifiers');
|
|
|
|
}
|
2012-12-10 23:35:04 +01:00
|
|
|
|
|
|
|
parent::setModifiers($modifiers);
|
2012-09-20 09:26:05 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Applies an exact match (equals) on a field value.
|
|
|
|
*
|
2012-09-06 09:54:44 +02:00
|
|
|
* @return DataQuery
|
2008-08-09 06:06:52 +02:00
|
|
|
*/
|
2012-09-06 09:54:44 +02:00
|
|
|
protected function applyOne(DataQuery $query) {
|
2015-05-19 03:39:39 +02:00
|
|
|
return $this->oneFilter($query, true);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-19 03:39:39 +02:00
|
|
|
* Excludes an exact match (equals) on a field value.
|
2012-09-06 09:54:44 +02:00
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
2015-05-19 03:39:39 +02:00
|
|
|
protected function excludeOne(DataQuery $query) {
|
|
|
|
return $this->oneFilter($query, false);
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 03:39:39 +02:00
|
|
|
* Applies a single match, either as inclusive or exclusive
|
2012-09-06 09:54:44 +02:00
|
|
|
*
|
2015-05-19 03:39:39 +02:00
|
|
|
* @param DataQuery $query
|
|
|
|
* @param bool $inclusive True if this is inclusive, or false if exclusive
|
2012-09-06 09:54:44 +02:00
|
|
|
* @return DataQuery
|
|
|
|
*/
|
2015-05-19 03:39:39 +02:00
|
|
|
protected function oneFilter(DataQuery $query, $inclusive) {
|
2012-09-06 09:54:44 +02:00
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2015-05-19 03:39:39 +02:00
|
|
|
$field = $this->getDbName();
|
|
|
|
$value = $this->getValue();
|
|
|
|
|
|
|
|
// Null comparison check
|
|
|
|
if($value === null) {
|
|
|
|
$where = DB::get_conn()->nullCheckClause($field, $inclusive);
|
|
|
|
return $query->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value comparison check
|
2013-06-21 00:32:08 +02:00
|
|
|
$where = DB::get_conn()->comparisonClause(
|
2015-05-19 03:39:39 +02:00
|
|
|
$field,
|
2013-06-21 00:32:08 +02:00
|
|
|
null,
|
2012-12-10 23:35:04 +01:00
|
|
|
true, // exact?
|
2015-05-19 03:39:39 +02:00
|
|
|
!$inclusive, // negate?
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
2012-12-10 23:35:04 +01:00
|
|
|
);
|
2015-05-19 03:39:39 +02:00
|
|
|
// for != clauses include IS NULL values, since they would otherwise be excluded
|
|
|
|
if(!$inclusive) {
|
|
|
|
$nullClause = DB::get_conn()->nullCheckClause($field, true);
|
|
|
|
$where .= " OR {$nullClause}";
|
|
|
|
}
|
|
|
|
return $query->where(array($where => $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies an exact match (equals) on a field value against multiple
|
|
|
|
* possible values.
|
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function applyMany(DataQuery $query) {
|
|
|
|
return $this->manyFilter($query, true);
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Excludes an exact match (equals) on a field value against multiple
|
|
|
|
* possible values.
|
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function excludeMany(DataQuery $query) {
|
2015-05-19 03:39:39 +02:00
|
|
|
return $this->manyFilter($query, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies matches for several values, either as inclusive or exclusive
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @param bool $inclusive True if this is inclusive, or false if exclusive
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function manyFilter(DataQuery $query, $inclusive) {
|
2012-09-06 09:54:44 +02:00
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2013-06-21 00:32:08 +02:00
|
|
|
$caseSensitive = $this->getCaseSensitive();
|
2015-05-19 03:39:39 +02:00
|
|
|
|
|
|
|
// Check values for null
|
|
|
|
$field = $this->getDbName();
|
2013-06-21 00:32:08 +02:00
|
|
|
$values = $this->getValue();
|
2015-05-19 03:39:39 +02:00
|
|
|
$hasNull = in_array(null, $values, true);
|
|
|
|
if($hasNull) {
|
|
|
|
$values = array_filter($values, function($value) {
|
|
|
|
return $value !== null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$connective = '';
|
|
|
|
if(empty($values)) {
|
|
|
|
$predicate = '';
|
|
|
|
} elseif($caseSensitive === null) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// For queries using the default collation (no explicit case) we can use the WHERE .. NOT IN .. syntax,
|
|
|
|
// providing simpler SQL than many WHERE .. AND .. fragments.
|
|
|
|
$column = $this->getDbName();
|
|
|
|
$placeholders = DB::placeholders($values);
|
2015-05-19 03:39:39 +02:00
|
|
|
if($inclusive) {
|
|
|
|
$predicate = "$column IN ($placeholders)";
|
|
|
|
} else {
|
|
|
|
$predicate = "$column NOT IN ($placeholders)";
|
|
|
|
}
|
2012-09-20 09:26:05 +02:00
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
// Generate reusable comparison clause
|
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
|
|
|
$this->getDbName(),
|
|
|
|
null,
|
|
|
|
true, // exact?
|
2015-05-19 03:39:39 +02:00
|
|
|
!$inclusive, // negate?
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$count = count($values);
|
2015-05-19 03:39:39 +02:00
|
|
|
if($count > 1) {
|
|
|
|
$connective = $inclusive ? ' OR ' : ' AND ';
|
|
|
|
$conditions = array_fill(0, $count, $comparisonClause);
|
|
|
|
$predicate = implode($connective, $conditions);
|
|
|
|
} else {
|
|
|
|
$predicate = $comparisonClause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always check for null when doing exclusive checks (either AND IS NOT NULL / OR IS NULL)
|
|
|
|
// or when including the null value explicitly (OR IS NULL)
|
|
|
|
if($hasNull || !$inclusive) {
|
|
|
|
// If excluding values which don't include null, or including
|
|
|
|
// values which include null, we should do an `OR IS NULL`.
|
|
|
|
// Otherwise we are excluding values that do include null, so `AND IS NOT NULL`.
|
|
|
|
// Simplified from (!$inclusive && !$hasNull) || ($inclusive && $hasNull);
|
|
|
|
$isNull = !$hasNull || $inclusive;
|
|
|
|
$nullCondition = DB::get_conn()->nullCheckClause($field, $isNull);
|
|
|
|
|
|
|
|
// Determine merge strategy
|
|
|
|
if(empty($predicate)) {
|
|
|
|
$predicate = $nullCondition;
|
|
|
|
} else {
|
|
|
|
// Merge null condition with predicate
|
|
|
|
if($isNull) {
|
|
|
|
$nullCondition = " OR {$nullCondition}";
|
|
|
|
} else {
|
|
|
|
$nullCondition = " AND {$nullCondition}";
|
|
|
|
}
|
|
|
|
// If current predicate connective doesn't match the same as the null connective
|
|
|
|
// make sure to group the prior condition
|
|
|
|
if($connective && (($connective === ' OR ') !== $isNull)) {
|
|
|
|
$predicate = "({$predicate})";
|
|
|
|
}
|
|
|
|
$predicate .= $nullCondition;
|
|
|
|
}
|
2012-09-20 09:26:05 +02:00
|
|
|
}
|
2015-05-19 03:39:39 +02:00
|
|
|
|
|
|
|
return $query->where(array($predicate => $values));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
public function isEmpty() {
|
2012-09-06 09:54:44 +02:00
|
|
|
return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
|
2008-08-11 02:03:57 +02:00
|
|
|
}
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|