public static function increment(DAOConnected &$object, array $fields, $refreshCurrent = true, $query = null) { $objectDao = $object->dao(); if ($query) { $updateQuery = $query; } else { $updateQuery = OSQL::update()->setTable($objectDao->getTable())->where(Expression::eqId('id', $object)); } $mapping = $objectDao->getProtoClass()->getMapping(); foreach ($mapping as $field => $column) { if (isset($fields[$field])) { $updateQuery->set($column, Expression::add($column, $fields[$field])); } } $updateCount = DBPool::getByDao($objectDao)->queryCount($updateQuery); if ($query) { $objectDao->uncacheLists(); } else { $objectDao->uncacheById($object->getId()); } if ($refreshCurrent && !$query) { $object = $objectDao->getById($object->getId()); } return $updateCount; }
public function testHaving() { $query = OQL::select('from TestUser'); $criteria = Criteria::create(TestUser::dao()); $this->assertCriteria($query->addHaving(OQL::having('id > 0')), $criteria->addProjection(Projection::having(Expression::gt('id', 0)))); $this->assertCriteria($query->addHaving(OQL::having('name is not null and (id <> $1 or id != $2)')->bindNext(4)->bindNext(8)), $criteria->addProjection(Projection::having(Expression::expAnd(Expression::notNull('name'), Expression::expOr(Expression::notEq('id', 4), Expression::notEq('id', 8)))))); $this->assertEquals(OQL::having('id + $15')->bind(15, 16)->toProjection(), Projection::having(Expression::add('id', 16))); $this->assertCriteria(OQL::select('from TestUser')->addHaving(OQL::having('id = $1')->bindNext(23))->bindNext(42), Criteria::create(TestUser::dao())->addProjection(Projection::having(Expression::eq('id', 42)))); }
public function testFormCalculation() { $form = Form::create()->add(Primitive::string('a'))->add(Primitive::boolean('b'))->add(Primitive::integer('c'))->add(Primitive::integer('d'))->add(Primitive::integer('e'))->add(Primitive::boolean('f'))->import(array('a' => 'asDfg', 'b' => 'true', 'c' => '1', 'd' => '2', 'e' => '3')); $this->assertTrue(Expression::isTrue(new FormField('b'))->toBoolean($form)); $this->assertFalse(Expression::isTrue(new FormField('f'))->toBoolean($form)); $this->assertFalse(Expression::eq('asdf', new FormField('a'))->toBoolean($form)); $this->assertTrue(Expression::eqLower('asdfg', new FormField('a'))->toBoolean($form)); $this->assertTrue(Expression::eq('asDfg', new FormField('a'))->toBoolean($form)); $this->assertTrue(Expression::andBlock(Expression::expOr(new FormField('b'), new FormField('f')), Expression::eq(7, Expression::add(new FormField('c'), Expression::mul(new FormField('d'), new FormField('e')))))->toBoolean($form)); $this->assertTrue(Expression::between(new FormField('d'), new FormField('c'), new FormField('e'))->toBoolean($form)); $this->assertFalse(Expression::between(new FormField('c'), new FormField('d'), new FormField('e'))->toBoolean($form)); $this->assertFalse(Expression::not(new FormField('b'))->toBoolean($form)); $this->assertTrue(Expression::not(new FormField('f'))->toBoolean($form)); }
public function toSelectQuery() { if (!$this->range || !$this->interval) { throw new WrongStateException('define time range and interval units first'); } if (!$this->range->getStart() || !$this->range->getEnd()) { throw new WrongArgumentException('cannot operate with unlimited range'); } $firstIntervalStart = $this->interval->truncate($this->range->getStart(), !$this->overlapped); $maxIntervals = $this->interval->countInRange($this->range, $this->overlapped) - 1; $generator = $this->getSeriesGenerator(0, $maxIntervals); $result = OSQL::select()->from($generator, self::ITERATOR_ALIAS)->get(Expression::add(DBValue::create($firstIntervalStart->toString())->castTo(DataType::create(DataType::TIMESTAMP)->getName()), Expression::mul(DBValue::create("1 {$this->interval->getName()}")->castTo(DataType::create(DataType::INTERVAL)->getName()), DBField::create(self::ITERATOR_ALIAS))), $this->field); return $result; }
public function testHaving() { $this->assertCriteria('from TestUser having (2 + -id --1) / 2 = id', Criteria::create(TestUser::dao())->setProjection(Projection::having(Expression::eq(Expression::div(Expression::sub(Expression::add(2, Expression::minus('id')), -1), 2), 'id'))), array(1 => 'id'))->assertCriteria('from TestUser having (id = 1) != ((1 = id) = (id >= 2))', Criteria::create(TestUser::dao())->setProjection(Projection::having(Expression::notEq(Expression::eq('id', 1), Expression::eq(Expression::eq(1, 'id'), Expression::gtEq('id', 2))))))->assertCriteria('from TestUser having $1 = 1', Criteria::create(TestUser::dao())->setProjection(Projection::having(Expression::eq(SQLFunction::create('count', 'id'), 1))), array(1 => SQLFunction::create('count', 'id'))); }