Beispiel #1
0
 /**
  * Adds filtering onto specified column
  *
  * @param string $column name
  * @param string $operation filter
  * @param string|array $value operation mode
  * @param string $chainType (if third argument is array)
  * @return IDataSource
  * @throws \Nette\InvalidArgumentException
  */
 public function filter($column, $operation = IDataSource::EQUAL, $value = NULL, $chainType = NULL)
 {
     if (is_array($operation)) {
         if ($chainType !== self::CHAIN_AND && $chainType !== self::CHAIN_OR) {
             throw new \Nette\InvalidArgumentException('Invalid chain operation type.');
         }
         $conds = [];
         foreach ($operation as $t) {
             $this->validateFilterOperation($t);
             if ($t === self::IS_NULL || $t === self::IS_NOT_NULL) {
                 $conds[] = ['%n', $column, $t];
             } else {
                 $modifier = is_double($value) ? \dibi::FLOAT : \dibi::TEXT;
                 if ($operation === self::LIKE || $operation === self::NOT_LIKE) {
                     $value = WildcardHelper::formatLikeStatementWildcards($value);
                 }
                 $conds[] = ['%n', $column, $t, "%{$modifier}", $value];
             }
         }
         if ($chainType === self::CHAIN_AND) {
             foreach ($conds as $cond) {
                 $this->ds->where($cond);
             }
         } elseif ($chainType === self::CHAIN_OR) {
             $this->ds->where('%or', $conds);
         }
     } else {
         $this->validateFilterOperation($operation);
         if ($operation === self::IS_NULL || $operation === self::IS_NOT_NULL) {
             $this->ds->where('%n', $column, $operation);
         } else {
             $modifier = is_double($value) ? \dibi::FLOAT : \dibi::TEXT;
             if ($operation === self::LIKE || $operation === self::NOT_LIKE) {
                 $value = WildcardHelper::formatLikeStatementWildcards($value);
             }
             $this->ds->where('%n', $column, $operation, "%{$modifier}", $value);
         }
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Filter items in data source
  *
  * @param string $column
  * @param string $operation
  * @param string $value
  * @param string $chainType
  * @return QueryBuilder (fluent)
  * @throws \Nette\InvalidArgumentException
  */
 public function filter($column, $operation = self::EQUAL, $value = NULL, $chainType = NULL, $colfunc = NULL)
 {
     if (!$this->hasColumn($column)) {
         throw new \Nette\InvalidArgumentException('Trying to filter data source by unknown column.');
     }
     $nextParamId = count($this->qb->getParameters()) + 1;
     if (is_array($operation)) {
         if ($chainType !== self::CHAIN_AND && $chainType !== self::CHAIN_OR) {
             throw new \Nette\InvalidArgumentException('Invalid chain operation type.');
         }
         $conds = [];
         foreach ($operation as $t) {
             $this->validateFilterOperation($t);
             if ($t === self::IS_NULL || $t === self::IS_NOT_NULL) {
                 $conds[] = $colfunc ? "{$colfunc}({$this->mapping[$column]}) {$t}" : $this->mapping[$column] . " {$t}";
             } else {
                 $conds[] = $colfunc ? "{$colfunc}({$this->mapping[$column]}) {$t} ?{$nextParamId}" : $this->mapping[$column] . " {$t} ?{$nextParamId}";
                 $this->qb->setParameter($nextParamId++, $t === self::LIKE || $t === self::NOT_LIKE ? DataSources\Utils\WildcardHelper::formatLikeStatementWildcards($value) : $value);
             }
         }
         if ($chainType === self::CHAIN_AND) {
             foreach ($conds as $cond) {
                 $this->qb->andWhere($cond);
             }
         } elseif ($chainType === self::CHAIN_OR) {
             $this->qb->andWhere(new \Doctrine\ORM\Query\Expr\Orx($conds));
         }
     } else {
         $this->validateFilterOperation($operation);
         if ($operation === self::IS_NULL || $operation === self::IS_NOT_NULL) {
             $where = $colfunc ? "{$colfunc}({$this->mapping[$column]}) {$operation}" : $this->mapping[$column] . " {$operation}";
             $this->qb->andWhere($where);
         } else {
             $where = $colfunc ? "{$colfunc}({$this->mapping[$column]}) {$operation} ?{$nextParamId}" : $this->mapping[$column] . " {$operation} ?{$nextParamId}";
             $this->qb->andWhere($where);
             $this->qb->setParameter($nextParamId, $operation === self::LIKE || $operation === self::NOT_LIKE ? DataSources\Utils\WildcardHelper::formatLikeStatementWildcards($value) : $value);
         }
     }
     return $this;
 }