示例#1
0
 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 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;
 }