protected function subProcess(JoinCapableQuery $query, DBField $field) { // if need to exclude change field to NULL if (array_key_exists($field->getField(), $this->excludedFields)) { $value = $this->excludedFields[$field->getField()]; $query->get(is_null($value) ? new DBNull() : new DBValue($value), $field->getField()); } else { $query->get($field); } }
/** * @return JoinCapableQuery **/ public function process(Criteria $criteria, JoinCapableQuery $query) { $dao = call_user_func([$this->className, 'dao']); foreach ($dao->getFields() as $field) { $this->subProcess($query, DBField::create($field, $dao->getTable())); } return $query; }
/** * @return SelectQuery **/ public function makeCountQuery() { $query = $this->makeFetchQuery(); if ($query->isDistinct()) { $countFunction = SQLFunction::create('count', DBField::create($this->container->getDao()->getIdName(), $this->container->getDao()->getTable()))->setAggregateDistinct(); $query->unDistinct(); } else { $countFunction = SQLFunction::create('count', DBValue::create('*')); } return $query->dropFields()->dropOrder()->dropLimit()->get($countFunction->setAlias('count')); }
public function getById($id, $expires = Cache::EXPIRES_MEDIUM) { if ($expires !== Cache::DO_NOT_CACHE && ($object = $this->getCachedById($id))) { if ($object === Cache::NOT_FOUND) { throw new CachedObjectNotFoundException("there is no such object for '" . $this->dao->getObjectName() . "' with id=" . $id); } return $this->dao->completeObject($object); } else { $query = $this->dao->makeSelectHead()->andWhere(Expression::eq(DBField::create($this->dao->getIdName(), $this->dao->getTable()), $id)); if ($expires === Cache::DO_NOT_CACHE) { $object = $this->fetchObject($query); } else { $object = $this->cachedFetchObject($query, $expires, true); } if ($object) { return $object; } else { throw new ObjectNotFoundException("there is no such object for '" . $this->dao->getObjectName() . "' with query == " . $query->toDialectString(DBPool::me()->getByDao($this->dao)->getDialect())); } } }
private function processPath(AbstractProtoClass $proto, $probablyPath, JoinCapableQuery $query, $table, $parentRequired = true, $prefix = null) { $path = explode('.', $probablyPath); try { $property = $proto->getPropertyByName($path[0]); } catch (MissingElementException $e) { // oh, it's a value, not a property return new DBValue($probablyPath); } unset($path[0]); Assert::isTrue($property->getRelationId() != null && !$property->isGenericType()); Assert::classExists($property->getClassName()); // checking whether we're playing with value object if (!method_exists($property->getClassName(), 'dao')) { if (method_exists($property->getClassName(), 'proto') && count($path) > 1) { return $this->processPath($property->getProto(), implode('.', $path), $query, $table); } else { return $this->guessAtom(implode('.', $path), $query, $table, $prefix); } } else { $propertyDao = call_user_func([$property->getClassName(), 'dao']); Assert::isNotNull($propertyDao, 'can not find target dao for "' . $property->getName() . '" property at "' . get_class($proto) . '"'); } $alias = $propertyDao->getJoinName($property->getColumnName(), $prefix); if ($property->getRelationId() == MetaRelation::ONE_TO_MANY || $property->getRelationId() == MetaRelation::MANY_TO_MANY) { $remoteName = $property->getClassName(); $selfName = $this->getObjectName(); $self = new $selfName(); $getter = $property->getGetter(); $dao = call_user_func([$remoteName, 'dao']); if ($property->getRelationId() == MetaRelation::MANY_TO_MANY) { $helperTable = $self->{$getter}()->getHelperTable(); $helperAlias = $helperTable; if (!$query->hasJoinedTable($helperAlias)) { $logic = Expression::eq(DBField::create($this->getIdName(), $table), DBField::create($self->{$getter}()->getParentIdField(), $helperAlias)); if ($property->isRequired()) { $query->join($helperTable, $logic, $helperAlias); } else { $query->leftJoin($helperTable, $logic, $helperAlias); } } $logic = Expression::eq(DBField::create($propertyDao->getIdName(), $alias), DBField::create($self->{$getter}()->getChildIdField(), $helperAlias)); } else { $logic = Expression::eq(DBField::create($self->{$getter}()->getParentIdField(), $alias), DBField::create($this->getIdName(), $table)); } if (!$query->hasJoinedTable($alias)) { if ($property->isRequired() && $parentRequired) { $query->join($dao->getTable(), $logic, $alias); } else { $query->leftJoin($dao->getTable(), $logic, $alias); } } } else { // OneToOne, lazy OneToOne // prevents useless joins if (isset($path[1]) && count($path) == 1 && $path[1] == $propertyDao->getIdName()) { return new DBField($property->getColumnName(), $table); } if (!$query->hasJoinedTable($alias)) { $logic = Expression::eq(DBField::create($property->getColumnName(), $table), DBField::create($propertyDao->getIdName(), $alias)); if ($property->isRequired() && $parentRequired) { $query->join($propertyDao->getTable(), $logic, $alias); } else { $query->leftJoin($propertyDao->getTable(), $logic, $alias); } } } if ($path) { return $propertyDao->guessAtom(implode('.', $path), $query, $alias, $property->isRequired() && $parentRequired, $propertyDao->getJoinPrefix($property->getColumnName(), $prefix)); } // ok, we're done }
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; }
private function joinProperties(SelectQuery $query, ProtoDAO $parentDao, $parentTable, $parentRequired, $prefix = null) { $proto = call_user_func([$parentDao->getObjectName(), 'proto']); foreach ($proto->getPropertyList() as $property) { if ($property instanceof LightMetaProperty && $property->getRelationId() == MetaRelation::ONE_TO_ONE && !$property->isGenericType() && (!$property->getFetchStrategyId() && $this->getFetchStrategy()->getId() == FetchStrategy::JOIN || $property->getFetchStrategyId() == FetchStrategy::JOIN)) { if (is_subclass_of($property->getClassName(), Enumeration::class) || is_subclass_of($property->getClassName(), Enum::class) || is_subclass_of($property->getClassName(), Registry::class)) { // field already added by makeSelectHead continue; } elseif ($property->isInner()) { $proto = call_user_func([$property->getClassName(), 'proto']); foreach ($proto->getPropertyList() as $innerProperty) { $query->get(new DBField($innerProperty->getColumnName(), $parentTable)); } continue; } $propertyDao = call_user_func([$property->getClassName(), 'dao']); // add's custom dao's injection possibility if (!$propertyDao instanceof ProtoDAO) { continue; } $tableAlias = $propertyDao->getJoinName($property->getColumnName(), $prefix); $fields = $propertyDao->getFields(); if (!$query->hasJoinedTable($tableAlias)) { $logic = Expression::eq(DBField::create($property->getColumnName(), $parentTable), DBField::create($propertyDao->getIdName(), $tableAlias)); if ($property->isRequired() && $parentRequired) { $query->join($propertyDao->getTable(), $logic, $tableAlias); } else { $query->leftJoin($propertyDao->getTable(), $logic, $tableAlias); } } foreach ($fields as $field) { $query->get(new DBField($field, $tableAlias), $propertyDao->getJoinPrefix($property->getColumnName(), $prefix) . $field); } $this->joinProperties($query, $propertyDao, $tableAlias, $property->isRequired() && $parentRequired, $propertyDao->getJoinPrefix($property->getColumnName(), $prefix)); } } }