Exemple #1
0
 /**
  * Modify a predicate by adding additional clauses.
  *
  * @param \Titon\Db\Query\Predicate $predicate
  * @param int $type
  * @param string $field
  * @param mixed $op
  * @param mixed $value
  * @return $this
  * @throws \Titon\Db\Exception\ExistingPredicateException
  */
 protected function _modifyPredicate(&$predicate, $type, $field, $op, $value)
 {
     if (!$predicate) {
         $predicate = new Predicate($type);
     } else {
         if ($predicate->getType() !== $type) {
             throw new ExistingPredicateException(sprintf('Predicate clause already created using "%s" conjunction', $predicate->getType()));
         }
     }
     if ($field instanceof Closure) {
         $predicate->bindCallback($field, $this);
     } else {
         if ($value !== null || in_array($op, [Expr::NULL, Expr::NOT_NULL], true)) {
             $predicate->add($field, $op, $value);
         } else {
             if ($op === '!=') {
                 $predicate->notEq($field, $value);
             } else {
                 $predicate->eq($field, $op);
             }
         }
     }
     return $this;
 }
Exemple #2
0
 /**
  * Format the where clause.
  *
  * @param \Titon\Db\Query\Predicate $where
  * @return string
  */
 public function formatWhere(Predicate $where)
 {
     if ($where->getParams()) {
         return sprintf($this->getClause(self::WHERE), $this->formatPredicate($where));
     }
     return '';
 }
Exemple #3
0
 public function testNeither()
 {
     $neither = new Predicate(Predicate::NEITHER);
     $neither->notIn('color', ['red', 'green'])->notIn('size', ['large', 'small']);
     $this->object->neither(function (Predicate $predicate) {
         $predicate->notIn('color', ['red', 'green'])->notIn('size', ['large', 'small']);
     });
     $this->assertEquals([$neither], $this->object->getParams());
 }
Exemple #4
0
 /**
  * Generate a new sub-grouped NOR predicate.
  *
  * @param \Closure $callback
  * @return $this
  */
 public function neither(Closure $callback)
 {
     $predicate = new Predicate(self::NEITHER);
     $predicate->bindCallback($callback);
     $this->_params[] = $predicate;
     $this->addBinding(null, $predicate);
     return $this;
 }
Exemple #5
0
 public function testFormatPredicate()
 {
     $pred = new Predicate(Predicate::ALSO);
     $pred->eq('id', 1)->gte('age', 12);
     $this->assertRegExp('/(`|\\")?id(`|\\")? = \\? AND (`|\\")?age(`|\\")? >= \\?/', $this->object->formatPredicate($pred));
     $pred->either(function (Predicate $where) {
         $where->like('name', '%Titon%')->notLike('name', '%Symfony%');
         $where->also(function (Predicate $where2) {
             $where2->eq('active', 1)->notEq('status', 2);
         });
     });
     $this->assertRegExp('/(`|\\")?id(`|\\")? = \\? AND (`|\\")?age(`|\\")? >= \\? AND \\((`|\\")?name(`|\\")? LIKE \\? OR (`|\\")?name(`|\\")? NOT LIKE \\? OR \\((`|\\")?active(`|\\")? = \\? AND (`|\\")?status(`|\\")? != \\?\\)\\)/', $this->object->formatPredicate($pred));
     $pred = new Predicate(Predicate::MAYBE);
     $pred->eq('id', 1)->gte('age', 12);
     $this->assertRegExp('/(`|\\")?id(`|\\")? = \\? XOR (`|\\")?age(`|\\")? >= \\?/', $this->object->formatPredicate($pred));
 }