Permalink
Browse files

API Add exclude() method to SearchFilters that excludes items that ma…

…tch the filter.
  • Loading branch information...
simonwelsh committed Sep 6, 2012
1 parent 38e7df2 commit 79b3f8ac45483d9dfa4b9ff0a494ae0201beacec
@@ -21,9 +21,9 @@ class EndsWithFilter extends SearchFilter {
/**
* Applies a match on the trailing characters of a field value.
*
- * @return unknown
+ * @return DataQuery
*/
- public function apply(DataQuery $query) {
+ protected function applyOne(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
return $query->where(sprintf(
"%s %s '%%%s'",
@@ -32,8 +32,65 @@ public function apply(DataQuery $query) {
Convert::raw2sql($this->getValue())
));
}
+
+ /**
+ * Applies a match on the trailing characters of a field value.
+ * Matches against one of the many values.
+ *
+ * @return DataQuery
+ */
+ protected function applyMany(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $connectives = array();
+ foreach($this->getValue() as $value) {
+ $connectives[] = sprintf(
+ "%s %s '%%%s'",
+ $this->getDbName(),
+ (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
+ Convert::raw2sql($value)
+ );
+ }
+ $whereClause = implode(' OR ', $connectives);
+ return $query->where($whereClause);
+ }
+
+ /**
+ * Excludes a match on the trailing characters of a field value.
+ *
+ * @return DataQuery
+ */
+ protected function excludeOne(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ return $query->where(sprintf(
+ "%s NOT %s '%%%s'",
+ $this->getDbName(),
+ (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
+ Convert::raw2sql($this->getValue())
+ ));
+ }
+
+ /**
+ * Excludes a match on the trailing characters of a field value.
+ * Excludes a field if it matches any of the values.
+ *
+ * @return DataQuery
+ */
+ protected function excludeMany(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $connectives = array();
+ foreach($this->getValue() as $value) {
+ $connectives[] = sprintf(
+ "%s NOT %s '%%%s'",
+ $this->getDbName(),
+ (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
+ Convert::raw2sql($value)
+ );
+ }
+ $whereClause = implode(' AND ', $connectives);
+ return $query->where($whereClause);
+ }
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -18,18 +18,72 @@ class ExactMatchFilter extends SearchFilter {
/**
* Applies an exact match (equals) on a field value.
*
- * @return unknown
+ * @return DataQuery
*/
- public function apply(DataQuery $query) {
+ protected function applyOne(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
return $query->where(sprintf(
"%s = '%s'",
$this->getDbName(),
Convert::raw2sql($this->getValue())
));
}
+
+ /**
+ * Applies an exact match (equals) on a field value against multiple
+ * possible values.
+ *
+ * @return DataQuery
+ */
+ protected function applyMany(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $values = array();
+ foreach($this->getValue() as $value) {
+ $values[] = Convert::raw2sql($value);
+ }
+ $valueStr = "'" . implode("', '", $values) . "'";
+ return $query->where(sprintf(
+ '%s IN (%s)',
+ $this->getDbName(),
+ $valueStr
+ ));
+ }
+
+ /**
+ * Excludes an exact match (equals) on a field value.
+ *
+ * @return DataQuery
+ */
+ protected function excludeOne(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ return $query->where(sprintf(
+ "%s != '%s'",
+ $this->getDbName(),
+ Convert::raw2sql($this->getValue())
+ ));
+ }
+
+ /**
+ * Excludes an exact match (equals) on a field value against multiple
+ * possible values.
+ *
+ * @return DataQuery
+ */
+ protected function excludeMany(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $values = array();
+ foreach($this->getValue() as $value) {
+ $values[] = Convert::raw2sql($value);
+ }
+ $valueStr = "'" . implode("', '", $values) . "'";
+ return $query->where(sprintf(
+ '%s NOT IN (%s)',
+ $this->getDbName(),
+ $valueStr
+ ));
+ }
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -7,46 +7,46 @@
/**
* Checks if a value is in a given set.
* SQL syntax used: Column IN ('val1','val2')
+ * @deprecated 3.1 Use ExactMatchFilter instead
*
- * @todo Add negation (NOT IN)6
* @package framework
* @subpackage search
*/
class ExactMatchMultiFilter extends SearchFilter {
+ function __construct($fullName, $value = false) {
+ Deprecation::notice('3.1', 'Use ExactMatchFilter instead.');
+ parent::__construct($fullName, $value);
+ }
public function apply(DataQuery $query) {
- $this->model = $query->applyRelation($this->relation);
- // hack
- // PREVIOUS $values = explode(',',$this->getValue());
- $values = array();
- if (is_string($this->getValue())) {
+ if (!is_array($this->getValue())) {
$values = explode(',',$this->getValue());
+ } else {
+ $values = $this->getValue();
}
- else {
- foreach($this->getValue() as $v) {
- $values[] = $v;
- }
- }
-
-
- if(! $values) return false;
- for($i = 0; $i < count($values); $i++) {
- if(! is_numeric($values[$i])) {
- // @todo Fix string replacement to only replace leading and tailing quotes
- $values[$i] = str_replace("'", '', $values[$i]);
- $values[$i] = Convert::raw2sql($values[$i]);
- }
+ $filter = new ExactMatchFilter($this->getFullName(), $values);
+ return $filter->apply($query);
+ }
+
+ protected function applyOne(DataQuery $query) {
+ /* NO OP */
+ }
+
+ public function exclude(DataQuery $query) {
+ if (!is_array($this->getValue())) {
+ $values = explode(',',$this->getValue());
+ } else {
+ $values = $this->getValue();
}
- $SQL_valueStr = "'" . implode("','", $values) . "'";
-
- return $query->where(sprintf(
- "%s IN (%s)",
- $this->getDbName(),
- $SQL_valueStr
- ));
+ $filter = new ExactMatchFilter($this->getFullName(), $values);
+ return $filter->exclude($query);
+ }
+
+ protected function excludeOne(DataQuery $query) {
+ /* NO OP */
}
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -27,15 +27,23 @@
*/
class FulltextFilter extends SearchFilter {
- public function apply(DataQuery $query) {
+ protected function applyOne(DataQuery $query) {
return $query->where(sprintf(
"MATCH (%s) AGAINST ('%s')",
$this->getDbName(),
Convert::raw2sql($this->getValue())
));
}
+ protected function excludeOne(DataQuery $query) {
+ return $query->where(sprintf(
+ "NOT MATCH (%s) AGAINST ('%s')",
+ $this->getDbName(),
+ Convert::raw2sql($this->getValue())
+ ));
+ }
+
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -10,9 +10,9 @@
class GreaterThanFilter extends SearchFilter {
/**
- * @return $query
+ * @return DataQuery
*/
- public function apply(DataQuery $query) {
+ protected function applyOne(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
$value = $this->getDbFormattedValue();
@@ -21,8 +21,21 @@ public function apply(DataQuery $query) {
return $query->where($filter);
}
+
+ /**
+ * @return DataQuery
+ */
+ protected function excludeOne(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $value = $this->getDbFormattedValue();
+
+ if(is_numeric($value)) $filter = sprintf("%s <= %s", $this->getDbName(), Convert::raw2sql($value));
+ else $filter = sprintf("%s <= '%s'", $this->getDbName(), Convert::raw2sql($value));
+
+ return $query->where($filter);
+ }
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -10,9 +10,9 @@
class LessThanFilter extends SearchFilter {
/**
- * @return $query
+ * @return DataQuery
*/
- public function apply(DataQuery $query) {
+ protected function applyOne(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
$value = $this->getDbFormattedValue();
@@ -21,8 +21,21 @@ public function apply(DataQuery $query) {
return $query->where($filter);
}
+
+ /**
+ * @return DataQuery
+ */
+ protected function excludeOne(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ $value = $this->getDbFormattedValue();
+
+ if(is_numeric($value)) $filter = sprintf("%s >= %s", $this->getDbName(), Convert::raw2sql($value));
+ else $filter = sprintf("%s >= '%s'", $this->getDbName(), Convert::raw2sql($value));
+
+ return $query->where($filter);
+ }
public function isEmpty() {
- return $this->getValue() == null || $this->getValue() == '';
+ return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
}
}
@@ -6,6 +6,7 @@
* @subpackage search
*/
class NegationFilter extends SearchFilter {
+ // Deprecate this once modifiers are done
public function apply(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
@@ -16,5 +17,13 @@ public function apply(DataQuery $query) {
));
}
+ public function exclude(DataQuery $query) {
+ $this->model = $query->applyRelation($this->relation);
+ return $query->where(sprintf(
+ "%s = '%s'",
+ $this->getDbName(),
+ Convert::raw2sql($this->getValue())
+ ));
+ }
}
Oops, something went wrong.

0 comments on commit 79b3f8a

Please sign in to comment.