Example #1
0
 public function testResolveBindObjects()
 {
     $this->assertEquals(['abc', PDO::PARAM_STR], $this->object->resolveBind(Query::func('SUBSTRING()'), 'abc'));
     $this->assertEquals([123, PDO::PARAM_INT], $this->object->resolveBind('foo', Query::expr('foo', '+', 123)));
 }
Example #2
0
 /**
  * Prepares a node for removal by moving all following nodes up.
  *
  * @param int $id
  * @param int $index
  */
 protected function _removeNode($id, $index)
 {
     $pk = $this->getRepository()->getPrimaryKey();
     foreach ([$this->getConfig('leftField'), $this->getConfig('rightField')] as $field) {
         $this->_moveNode(function (Query $query) use($field, $index, $id, $pk) {
             $query->where($field, '>=', $index);
             if ($id) {
                 $query->where($pk, '!=', $id);
             }
         }, [$field => Query::expr($field, '-', 2)]);
     }
 }
Example #3
0
 public function testUpdateWithExpressions()
 {
     $this->loadFixtures('Stats');
     $stat = new Stat();
     $this->assertEquals(new EntityCollection([new Entity(['id' => 1, 'name' => 'Warrior', 'health' => 1500]), new Entity(['id' => 2, 'name' => 'Ranger', 'health' => 800]), new Entity(['id' => 3, 'name' => 'Mage', 'health' => 600])]), $stat->select('id', 'name', 'health')->orderBy('id', 'asc')->all());
     $query = $stat->query(Query::UPDATE);
     $query->data(['health' => Query::expr('health', '+', 75)]);
     $this->assertEquals(3, $query->save());
     $this->assertEquals(new EntityCollection([new Entity(['id' => 1, 'name' => 'Warrior', 'health' => 1575]), new Entity(['id' => 2, 'name' => 'Ranger', 'health' => 875]), new Entity(['id' => 3, 'name' => 'Mage', 'health' => 675])]), $stat->select('id', 'name', 'health')->orderBy('id', 'asc')->all());
     $this->assertEquals(1, $stat->update(2, ['health' => Query::expr('health', '-', 125)]));
     $this->assertEquals(new EntityCollection([new Entity(['id' => 1, 'name' => 'Warrior', 'health' => 1575]), new Entity(['id' => 2, 'name' => 'Ranger', 'health' => 750]), new Entity(['id' => 3, 'name' => 'Mage', 'health' => 675])]), $stat->select('id', 'name', 'health')->orderBy('id', 'asc')->all());
 }
Example #4
0
 /**
  * Increment the value of a field(s) using a step number.
  * Will update all records, or a single record.
  *
  * @param int|int[]|\Closure $id
  * @param array $fields
  * @return int
  */
 public function increment($id, array $fields)
 {
     $data = [];
     foreach ($fields as $field => $step) {
         $data[$field] = Query::expr($field, '+', $step);
     }
     $query = $this->query(Query::UPDATE);
     if ($id instanceof Closure) {
         $query->bindCallback($id);
     } else {
         if ($id) {
             $query->where($this->getPrimaryKey(), $id);
         }
     }
     return $query->save($data);
 }
Example #5
0
 public function testExpr()
 {
     $this->assertInstanceOf('Titon\\Db\\Query\\Expr', Query::expr('column', '+', 5));
 }
Example #6
0
 public function testFormatUpdateFieldsExprs()
 {
     $fields = $this->object->formatUpdateFields(['foo' => 'bar', 'views' => Query::expr('views', '+', 5)]);
     $this->assertRegExp('/(`|\\")?foo(`|\\")? = \\?/', $fields[0]);
     $this->assertRegExp('/(`|\\")?views(`|\\")? = (`|\\")?views(`|\\")? \\+ \\?/', $fields[1]);
 }