/**
  * @return SelectQuery
  **/
 protected function joinHelperTable(SelectQuery $query)
 {
     $uc = $this->container;
     if (!$query->hasJoinedTable($uc->getHelperTable())) {
         $query->join($uc->getHelperTable(), Expression::eq(new DBField($uc->getParentTableIdField(), $uc->getDao()->getTable()), new DBField($uc->getChildIdField(), $uc->getHelperTable())));
     }
     return $query->andWhere(Expression::eq(new DBField($uc->getParentIdField(), $uc->getHelperTable()), new DBValue($uc->getParentObject()->getId())));
 }
 protected function cacheByQuery(SelectQuery $query, $object, $expires = Cache::EXPIRES_FOREVER)
 {
     $queryId = $query->getId();
     $semKey = $this->keyToInt($this->indexKey);
     $key = $this->makeQueryKey($query, self::SUFFIX_QUERY);
     $pool = SemaphorePool::me();
     if ($pool->get($semKey)) {
         $this->syncMap($key);
         Cache::me()->mark($this->className)->add($key, $object, $expires);
         $pool->free($semKey);
     }
     return $object;
 }
Exemple #3
0
 protected function toDialectStringSelect($query, Dialect $dialect)
 {
     $fields = [];
     foreach ($this->fields as $var => $val) {
         $fields[] = $dialect->quoteField($var);
     }
     if (!$fields) {
         throw new WrongStateException('what should i insert?');
     }
     if ($this->select->getFieldsCount() != count($fields)) {
         throw new WrongStateException('count of select fields must be equal with count of insert fields');
     }
     $fields = implode(', ', $fields);
     return $query . "({$fields}) (" . $this->select->toDialectString($dialect) . ")";
 }
 /**
  * @return SelectQuery
  **/
 protected function targetize(SelectQuery $query)
 {
     return $query->andWhere(Expression::eqId(new DBField($this->container->getParentIdField(), $this->container->getDao()->getTable()), $this->container->getParentObject()));
 }
 protected final function getTagByBinaryExpression(BinaryExpression $expression, SelectQuery $query, &$className, &$columns)
 {
     $tag = null;
     if ($expression->getLogic() == BinaryExpression::EQUALS) {
         $first = $expression->getLeft();
         $second = $expression->getRight();
         if ($second instanceof DBField || $first instanceof DBValue) {
             $first = $expression->getRight();
             $second = $expression->getLeft();
         }
         $columnClassName = null;
         $idValue = null;
         if ($first instanceof DBField && isset($columns[$first->getField()])) {
             if ($first->getTable() === null) {
                 $table = $query->getFirstTable();
             } elseif ($first->getTable() instanceof FromTable) {
                 $table = $first->getTable();
             }
             if ($table instanceof FromTable) {
                 $table = $table->getTable();
             }
             if ($table !== null && $table == $this->getTableByClassName($className)) {
                 $columnClassName = $columns[$first->getField()];
             }
         } elseif (is_string($first) && $first && isset($columns[$first])) {
             $table = $query->getFirstTable();
             if ($table instanceof FromTable) {
                 $table = $table->getTable();
             }
             if ($table !== null && $table == $this->getTableByClassName($className)) {
                 $columnClassName = $columns[$first];
             }
         }
         if ($second instanceof DBValue) {
             $idValue = $second->getValue();
         } elseif ((is_integer($second) || is_string($second)) && $second) {
             $idValue = $second;
         }
         if ($columnClassName && $idValue) {
             $tag = $this->getTagByClassAndId($columnClassName, $idValue);
         }
     }
     return $tag;
 }
Exemple #6
0
 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));
         }
     }
 }
Exemple #7
0
 protected function makeQueryKey(SelectQuery $query, $suffix)
 {
     return $this->className . $suffix . $query->getId() . $this->watermark;
 }
 public function getCustomRowList(SelectQuery $query, $expires = Cache::DO_NOT_CACHE)
 {
     if ($query->getFieldsCount() !== 1) {
         throw new WrongArgumentException('you should select only one row when using this method');
     }
     if ($expires !== Cache::DO_NOT_CACHE && ($list = $this->getCachedByQuery($query))) {
         if ($list === Cache::NOT_FOUND) {
             throw new CachedObjectNotFoundException();
         }
         return $list;
     } elseif ($list = DBPool::getByDao($this->dao)->queryColumn($query)) {
         if (Cache::DO_NOT_CACHE === $expires) {
             return $list;
         } else {
             return $this->cacheByQuery($query, $list, $expires);
         }
     } else {
         throw new ObjectNotFoundException("empty list" . (defined('__LOCAL_DEBUG__') ? " for such query - " . $query->toDialectString(DBPool::me()->getByDao($this->dao)->getDialect()) : null));
     }
 }