Esempio n. 1
0
 /**
  * @return QueryBuilder
  */
 public function builder()
 {
     $tableName = $this->getTableName();
     $builder = new QueryBuilder($this->connection->getDriver());
     $builder->from("[{$tableName}]", QueryBuilderHelper::getAlias($tableName));
     return $builder;
 }
Esempio n. 2
0
 /**
  * @param QueryBuilder $builder
  * @param string $orderBy
  */
 public function applyOrder(QueryBuilder $builder, $orderBy)
 {
     switch ($orderBy) {
         case 'push':
             $builder->orderBy('[g.pushed_at] DESC');
             break;
         case 'newest':
             $builder->orderBy('[a.created_at] DESC');
             break;
         case 'popularity':
             $builder->orderBy('IFNULL([g.stars], 0) * 2 + IFNULL([g.watchers], 0) + IFNULL([g.forks], 0) DESC');
             break;
     }
 }
 protected function getIteratorCount()
 {
     if ($this->resultCount === NULL) {
         if ($this->queryBuilder->hasLimitOffsetClause()) {
             $sql = 'SELECT COUNT(*) FROM (' . $this->queryBuilder->getQuerySql() . ') temp';
             $args = $this->queryBuilder->getQueryParameters();
         } else {
             $builder = clone $this->queryBuilder;
             $builder->select('COUNT(*)');
             $sql = $builder->getQuerySql();
             $args = $builder->getQueryParameters();
         }
         $this->resultCount = $this->connection->queryArgs($sql, $args)->fetchField();
     }
     return $this->resultCount;
 }
 protected function calculateCacheKey(QueryBuilder $builder, IEntityPreloadContainer $preloadIterator = NULL, $parent)
 {
     return md5($builder->getQuerySQL() . json_encode($builder->getQueryParameters()) . ($preloadIterator ? $preloadIterator->getIdentification() : json_encode($parent->getRawValue($this->metadata->name))));
 }
 protected function assertBuilder($expected, QueryBuilder $builder)
 {
     $args = $builder->getQueryParameters();
     array_unshift($args, $builder->getQuerySql());
     Assert::same($expected, $args);
 }
 protected function calculateCacheKey(QueryBuilder $builder, array $values)
 {
     return md5($builder->getQuerySQL() . json_encode($builder->getQueryParameters()) . json_encode($values));
 }
Esempio n. 7
0
 private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, &$distinctNeeded = false, &$value = null, &$modifier = '%any')
 {
     $column = array_pop($levels);
     $sourceMapper = $this->mapper;
     $sourceAlias = $builder->getFromAlias();
     $sourceReflection = $sourceMapper->getStorageReflection();
     $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $sourceMapper->getRepository()->getEntityClassNames()[0]);
     foreach ($levels as $levelIndex => $level) {
         $property = $sourceEntityMeta->getProperty($level);
         if ($property->relationship === null) {
             throw new InvalidArgumentException("Entity {$sourceEntityMeta->className}::\${$level} does not contain a relationship.");
         }
         $targetMapper = $this->model->getRepository($property->relationship->repository)->getMapper();
         $targetReflection = $targetMapper->getStorageReflection();
         $targetEntityMetadata = $this->metadataStorage->get($property->relationship->entity);
         $relType = $property->relationship->type;
         if ($relType === Relationship::ONE_HAS_MANY || $relType === Relationship::ONE_HAS_ONE && !$property->relationship->isMain) {
             $targetColumn = $targetReflection->convertEntityToStorageKey($property->relationship->property);
             $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0];
             $distinctNeeded = $relType === Relationship::ONE_HAS_MANY;
         } elseif ($relType === Relationship::MANY_HAS_MANY) {
             if ($property->relationship->isMain) {
                 list($joinTable, list($inColumn, $outColumn)) = $sourceMapper->getManyHasManyParameters($property, $targetMapper);
             } else {
                 $sourceProperty = $targetEntityMetadata->getProperty($property->relationship->property);
                 list($joinTable, list($outColumn, $inColumn)) = $targetMapper->getManyHasManyParameters($sourceProperty, $sourceMapper);
             }
             $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0];
             $builder->leftJoin($sourceAlias, $joinTable, self::getAlias($joinTable), "[{$sourceAlias}.{$sourceColumn}] = [{$joinTable}.{$inColumn}]");
             $sourceAlias = $joinTable;
             $sourceColumn = $outColumn;
             $targetColumn = $targetReflection->getStoragePrimaryKey()[0];
             $distinctNeeded = true;
         } else {
             $targetColumn = $targetReflection->getStoragePrimaryKey()[0];
             $sourceColumn = $sourceReflection->convertEntityToStorageKey($level);
         }
         $targetTable = $targetMapper->getTableName();
         $targetAlias = $level . str_repeat('_', $levelIndex);
         $builder->leftJoin($sourceAlias, $targetTable, $targetAlias, "[{$sourceAlias}.{$sourceColumn}] = [{$targetAlias}.{$targetColumn}]");
         $sourceAlias = $targetAlias;
         $sourceMapper = $targetMapper;
         $sourceReflection = $targetReflection;
         $sourceEntityMeta = $targetEntityMetadata;
     }
     $targetProperty = $sourceEntityMeta->getProperty($column);
     if ($targetProperty->isPrimary && $targetProperty->isVirtual) {
         // primary-proxy
         $primaryKey = $sourceEntityMeta->getPrimaryKey();
         if (count($primaryKey) > 1) {
             // composite primary key
             $modifier = '%any';
             list($expression, $value) = $this->processMultiColumn($sourceReflection, $primaryKey, $value, $sourceAlias);
             return $expression;
         } else {
             $column = reset($primaryKey);
         }
     }
     list($expression, $modifier, $value) = $this->processColumn($sourceReflection, $column, $value, $sourceAlias);
     return $expression;
 }
 private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, &$distinctNeeded = FALSE, &$value = NULL)
 {
     $column = array_pop($levels);
     $sourceMapper = $this->mapper;
     $sourceAlias = $builder->getFromAlias();
     $sourceReflection = $sourceMapper->getStorageReflection();
     $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $sourceMapper->getRepository()->getEntityClassNames()[0]);
     foreach ($levels as $levelIndex => $level) {
         $property = $sourceEntityMeta->getProperty($level);
         if ($property->relationship === NULL) {
             throw new InvalidArgumentException("Entity {$sourceEntityMeta->className}::\${$level} does not contain a relationship.");
         }
         $targetMapper = $this->model->getRepository($property->relationship->repository)->getMapper();
         $targetReflection = $targetMapper->getStorageReflection();
         $targetEntityMetadata = $this->metadataStorage->get($property->relationship->entity);
         $relType = $property->relationship->type;
         if ($relType === Relationship::ONE_HAS_MANY || $relType === Relationship::ONE_HAS_ONE_DIRECTED && !$property->relationship->isMain) {
             $targetColumn = $targetReflection->convertEntityToStorageKey($property->relationship->property);
             $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0];
             $distinctNeeded = TRUE;
         } elseif ($relType === Relationship::MANY_HAS_MANY) {
             if ($property->relationship->isMain) {
                 list($joinTable, list($inColumn, $outColumn)) = $sourceMapper->getManyHasManyParameters($property, $targetMapper);
             } else {
                 $sourceProperty = $targetEntityMetadata->getProperty($property->relationship->property);
                 list($joinTable, list($outColumn, $inColumn)) = $targetMapper->getManyHasManyParameters($sourceProperty, $sourceMapper);
             }
             $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0];
             $builder->leftJoin($sourceAlias, $joinTable, self::getAlias($joinTable), "[{$sourceAlias}.{$sourceColumn}] = [{$joinTable}.{$inColumn}]");
             $sourceAlias = $joinTable;
             $sourceColumn = $outColumn;
             $targetColumn = $targetReflection->getStoragePrimaryKey()[0];
             $distinctNeeded = TRUE;
         } else {
             $targetColumn = $targetReflection->getStoragePrimaryKey()[0];
             $sourceColumn = $sourceReflection->convertEntityToStorageKey($level);
         }
         $targetTable = $targetMapper->getTableName();
         $targetAlias = $level . str_repeat('_', $levelIndex);
         $builder->leftJoin($sourceAlias, $targetTable, $targetAlias, "[{$sourceAlias}.{$sourceColumn}] = [{$targetAlias}.{$targetColumn}]");
         $sourceAlias = $targetAlias;
         $sourceMapper = $targetMapper;
         $sourceReflection = $targetReflection;
         $sourceEntityMeta = $targetEntityMetadata;
     }
     $sourceEntityMeta->getProperty($column);
     // check if property exists
     if ($column === 'id' && count($sourceEntityMeta->getPrimaryKey()) > 1) {
         $pair = [];
         foreach ($sourceEntityMeta->getPrimaryKey() as $column) {
             $column = $sourceReflection->convertEntityToStorageKey($column);
             $pair[] = "{$sourceAlias}.{$column}";
         }
         if (!isset($value[0][0])) {
             $value = [$value];
         }
         return '(' . implode(', ', $pair) . ')';
     } else {
         $column = $sourceReflection->convertEntityToStorageKey($column);
         return "{$sourceAlias}.{$column}";
     }
 }
 protected function calculateCacheKey(QueryBuilder $builder, $preloadIterator, $parent)
 {
     return md5($builder->getQuerySql() . json_encode($builder->getQueryParameters()) . ($preloadIterator ? spl_object_hash($preloadIterator) : json_encode($parent->id)));
 }
 protected function calculateCacheKey(QueryBuilder $builder, $preloadIterator, $parent)
 {
     return md5($builder->getQuerySQL() . json_encode($builder->getQueryParameters()) . ($preloadIterator ? spl_object_hash($preloadIterator) : json_encode($parent->getRawValue($this->metadata->name))));
 }
 protected function calculateCacheKey(QueryBuilder $builder, IEntityPreloadContainer $preloadIterator = NULL, IEntity $parent)
 {
     return md5($builder->getQuerySql() . json_encode($builder->getQueryParameters()) . ($preloadIterator ? $preloadIterator->getIdentification() : json_encode($parent->getValue('id'))));
 }