AbstractQueryBuilder::platformInsertBatchHandler

@param \O2System\Database\NoSql\DataStructures\QueryBuilderCache $queryBuilderCache

@return bool
326447
protected function platformInsertBatchHandler(QueryBuilderCache $queryBuilderCache)
    {
        if ($queryBuilderCache->from) {
            return $this->conn->execute($queryBuilderCache, ['method' => 'insert']);
        }

        return false;
    }
273963
private function addConfigurationFilterToQueryBuilder(QueryBuilder $queryBuilder, $configs)
    {
        foreach ($configs as $config) {
            $workspaceId = $config->getWidgetInstance()->getWorkspace()->getId();
            $queryBuilder
                ->where('workspace.id = :workspaceId')
                ->setParameter('workspaceId', $workspaceId);

            if ($config->hasRestriction()) {
                $queryBuilder
                    ->andWhere('log.action IN (:actions)')
                    ->setParameter('actions', $config->getRestrictions());
            }
        }

        return $queryBuilder;
    }
899161
public function setBaseQueryBuilder(QueryBuilder $baseQueryBuilder=null)
    {
        if($baseQueryBuilder === null)
        {
            $this->baseQueryBuilder = $this->getDefaultQueryBuilder();
        }
        else
        {
            $this->baseQueryBuilder = $baseQueryBuilder;
        }
    }
1036906
public function compileResequenceQuery(QueryBuilder $queryBuilder, ResequenceOrderIndexColumn $query) : \Dms\Core\Persistence\Db\Platform\CompiledQuery
    {
        $platform         = $queryBuilder->getConnection()->getDatabasePlatform();
        $outerTable       = $platform->quoteSingleIdentifier($query->getTable()->getName());
        $subSelectTable   = $platform->quoteSingleIdentifier($query->getTable()->getName() . '__inner');
        $orderIndexColumn = $platform->quoteSingleIdentifier($query->getColumn()->getName());

        $subSelect = $queryBuilder->getConnection()->createQueryBuilder();
        $subSelect
                ->from($outerTable, $outerTable . '__inner')
                ->select('COUNT(*) AS row_number')
                ->where($queryBuilder->expr()->lte(
                        $subSelectTable . '.' . $orderIndexColumn,
                        $outerTable . '.' . $orderIndexColumn
                ));

        $queryBuilder
                ->update($outerTable)
                ->set($orderIndexColumn, '(' . $subSelect->getSQL() . ')');


    }
256683
public function getQueryBuilderWithoutSecurity(QueryBuilder $queryBuilder)
    {
        $queryBuilder->addSelect('fileVersion')
            ->addSelect('file')
            ->addSelect('media')
            ->addSelect('collection')
            ->leftJoin('d.fileVersion', 'fileVersion')
            ->leftJoin('fileVersion.file', 'file')
            ->leftJoin('file.media', 'media')
            ->leftJoin('media.collection', 'collection')
            ->leftJoin(
                AccessControl::class,
                'accessControl',
                'WITH',
                'accessControl.entityClass = :entityClass AND accessControl.entityId = collection.id'
            )
            ->where('file.version = fileVersion.version')
            ->andWhere('accessControl.id is null')
            ->setParameter('entityClass', Collection::class);

        return $queryBuilder;
    }
635752
protected function performInsert(QueryBuilder $query)
    {
        // Disparar evento de documento sendo criado
        if ($this->fireModelEvent('creating') === false) {
            return false;
        }

        // Verificar para atualizar os timestamps
        if ($this->usesTimestamps()) {
            $this->updateTimestamps();
        }

        // Inserir documento e capturar ID
        $id = $query->insertGetId($this->attributes);
        $this->setAttribute('_id', $id);

        // Marcar como model já carregou o documento
        $this->exists = true;

        // Disparar evento de documento criado
        $this->fireModelEvent('created', false);

        return true;
    }
1233825
public function setQueryBuilder($queryBuilder)
    {
        if (!is_object($queryBuilder)) {
            throw new InvalidQueryBuilderObject('Parameter must be an object of "\Doctrine\ORM\QueryBuilder" or "Doctrine\ODM\MongoDB\Query\Builder"');
        }

        if (!$queryBuilder instanceof QueryBuilder && !$queryBuilder instanceof Builder) {
            throw new InvalidQueryBuilderObject(sprintf('%s is not valid query builder object', get_class($queryBuilder)));
        }

        $this->queryBuilder = $queryBuilder;
    }
1250998
public function query(QueryBuilderCache $queryBuilderCache)
    {
        // Reconnect the connection in case isn't connected yet.
        $this->reconnect();

        $result = false;
        $queryStatement = new QueryStatement($queryBuilderCache);

        $startTime = microtime(true);

        // Run the query for real
        if ($this->disableQueryExecution === false) {
            $rows = $this->platformQueryHandler($queryStatement);
            $result = new Result($rows);

            if ($this->transactionInProgress) {
                $this->transactionStatus = ($queryStatement->hasErrors() ? false : true);
            }
        }

        $queryStatement->setDuration($startTime);

        $this->queriesCache[] = $queryStatement;

        if ($queryStatement->hasErrors()) {
            if ($this->debugEnabled) {
                throw new RuntimeException($queryStatement->getLatestErrorMessage(),
                    $queryStatement->getLatestErrorCode());
            }

            if ($this->transactionInProgress) {
                $this->transactionStatus = false;
                $this->transactionInProgress = false;
            }

            return false;
        }

        return $result;
    }
464693
private function _doExecuteCached(Query $query)
    {
        if (null !== $resultCache = $this->documentManager->getResultCache()) {
            $item = $resultCache->getItem($this->cacheProfile->getCacheKey());

            if ($item->isHit()) {
                yield from $item->get();

                return;
            }
        }

        $resultSets = iterator_to_array($this->_doExecute($query));

        if (isset($item)) {
            $item->set($resultSets);
            $item->expiresAfter($this->cacheProfile->getTtl());
            $resultCache->save($item);
        }

        yield from $resultSets;
    }
361025
protected function newBaseQueryBuilder()
    {
        $conn = $this->getConnection();

        $grammar = $conn->getQueryGrammar();

        return new QueryCacheBuilder($this->queryCache(), $conn, $grammar, $conn->getPostProcessor());
    }
972415
public static function execute(UnionQueryProvider $queryProvider, $buildDepth=BuildDepth::SINGLE, $orderby=null, PagingInfo $pagingInfo=null) {
    $cache = ObjectFactory::getInstance('dynamicCache');
    $cacheSection = str_replace('\\', '.', __CLASS__);

    $queryIds = $queryProvider->getIds();
    $numQueries = sizeof($queryIds);

    // get cache key for stored offsets of previous page
    $pagingInfo = $pagingInfo ?: new PagingInfo(PagingInfo::SIZE_INFINITE);
    $page = $pagingInfo->getPage();
    $prevPage = $page > 1 ? $page-1 : 1;
    $prevPagingInfo = new PagingInfo($pagingInfo->getPageSize(), true);
    $prevPagingInfo->setPage($prevPage);
    $prevCacheKey = self::getCacheKey($queryIds, $buildDepth, $orderby, $prevPagingInfo);

    // get offsets
    if ($page == 1) {
      $offsets = array_fill(0, $numQueries, 0);
    }
    else {
      if ($cache->exists($cacheSection, $prevCacheKey)) {
        $offsets = $cache->get($cacheSection, $prevCacheKey);
      }
      else {
        // previous offsets must be generated by loading the pages for pages other than first
        for ($i=1; $i<$page; $i++) {
          $tmpPagingInfo = new PagingInfo($pagingInfo->getPageSize(), true);
          $tmpPagingInfo->setPage($i);
          $tmpCacheKey = self::getCacheKey($queryIds, $buildDepth, $orderby, $tmpPagingInfo);
          if (!$cache->exists($cacheSection, $tmpCacheKey)) {
            self::execute($queryProvider, $buildDepth, $orderby, $tmpPagingInfo);
          }
          $offsets = $cache->get($cacheSection, $tmpCacheKey);
        }
      }
    }

    $tmpResult = [];
    $total = 0;
    for ($i=0, $countI=$numQueries; $i<$countI; $i++) {
      // collect n objects from each query
      $queryId = $queryIds[$i];

      // set paging info
      $tmpPagingInfo = new PagingInfo($pagingInfo->getPageSize(), false);
      $tmpPagingInfo->setOffset($offsets[$i]);

      $objects = $queryProvider->execute($queryId, $buildDepth, $orderby, $tmpPagingInfo);
      foreach ($objects as $object) {
        $object->setProperty('queryId', $queryId);
      }
      $tmpResult = array_merge($tmpResult, $objects);
      $total += $tmpPagingInfo->getTotalCount();
    }
    $pagingInfo->setTotalCount($total);

    // sort
    if ($orderby != null) {
      $comparator = new ObjectComparator($orderby);
      usort($tmpResult, [$comparator, 'compare']);
    }

    // truncate
    $result = array_slice($tmpResult, 0, $pagingInfo->getPageSize());

    // update offsets
    $counts = array_fill_keys($queryIds, 0);
    for ($i=0, $count=sizeof($result); $i<$count; $i++) {
      $counts[$result[$i]->getProperty('queryId')]++;
    }
    for ($i=0, $count=$numQueries; $i<$count; $i++) {
      $offsets[$i] += $counts[$queryIds[$i]];
    }
    $cacheKey = self::getCacheKey($queryIds, $buildDepth, $orderby, $pagingInfo);
    $cache->put($cacheSection, $cacheKey, $offsets);
    return $result;
  }
1049497
public function compileResequenceQuery(QueryBuilder $queryBuilder, ResequenceOrderIndexColumn $query) : \Dms\Core\Persistence\Db\Platform\CompiledQuery
    {
        $platform         = $queryBuilder->getConnection()->getDatabasePlatform();
        $tableName        = $platform->quoteSingleIdentifier($query->getTable()->getName());
        $subSelectTable   = $platform->quoteSingleIdentifier($query->getTable()->getName() . '__inner');
        $orderIndexColumn = $platform->quoteSingleIdentifier($query->getColumn()->getName());

        $subSelect = $queryBuilder->getConnection()->createQueryBuilder();
        $subSelect
                ->from($tableName, $subSelectTable)
                ->select('COUNT(*) AS row_number')
                ->where($queryBuilder->expr()->lte(
                        $subSelectTable . '.' . $orderIndexColumn,
                        $tableName . '.' . $orderIndexColumn
                ));

        if ($query->hasGroupingColumn()) {
            $groupingColumn = $platform->quoteSingleIdentifier($query->getGroupingColumn()->getName());

            $subSelect->andWhere($queryBuilder->expr()->eq(
                    $subSelectTable . '.' . $groupingColumn,
                    $tableName . '.' . $groupingColumn
            ));
        }

        $queryBuilder
                ->update($tableName)
                ->set($orderIndexColumn, '(' . $subSelect->getSQL() . ')');

        if ($query->hasWhereCondition()) {
            $queryBuilder->where(
                    $this->expressionCompiler->compileExpression($queryBuilder, $query->getWhereCondition())
            );
        }

        return new CompiledQuery($queryBuilder->getSQL(), $queryBuilder->getParameters());
    }
220898
public static function hasLeftJoin(QueryBuilder $queryBuilder): bool
    {
        foreach ($queryBuilder->getDQLPart('join') as $joins) {
            foreach ($joins as $join) {
                if (Join::LEFT_JOIN === $join->getJoinType()) {
                    return true;
                }
            }
        }

        return false;
    }
742844
protected function getManyFrom(Builder $query, QueryBuilder $builder): LengthAwarePaginator
    {
        $query = $query->with(self::$loadRelations);

        if ($builder->hasRelation('translations')) {
            if (!$builder->getFilter('translations.language_code')) {
                throw new InvalidArgumentException('Language code is required');
            }
            $query->join('block_translations as t', 'blocks.id', '=', 't.block_id');
            $builder->applyRelationFilters('translations', 't', $query);
            $builder->applyRelationSorts('translations', 't', $query);
        }

        if ($builder->hasFilter('type') || $builder->hasSort('type')) {
            $query->join('block_types as bt', 'blocks.type_id', '=', 'bt.id');
            optional($builder->getFilter('type'))->apply($query, 'bt', 'name');
            optional($builder->getSort('type'))->apply($query, 'bt', 'name');
        }

        $builder->applyFilters($query, 'blocks');
        $builder->applySorts($query, 'blocks');

        $count = clone $query->getQuery();

        $results = $query->limit($builder->getPageSize())
            ->offset($builder->getPageSize() * ($builder->getPage() - 1))
            ->get(['blocks.*']);

        return new LengthAwarePaginator(
            $results,
            $count->select('blocks.id')->count(),
            $builder->getPageSize(),
            $builder->getPage()
        );
    }
932960
protected function getPlatformGroupConcat(QueryBuilder $query)
    {
        $platform = $query->getConnection()->getDatabasePlatform()->getName();

        switch ($platform) {
            case 'mysql':
                return "GROUP_CONCAT(DISTINCT CONCAT_WS('_', f.name, f.grouping, f.id))";
            case 'sqlite':
                return "GROUP_CONCAT(DISTINCT f.name||'_'||f.grouping||'_'||f.id)";
            case 'postgresql':
                return "string_agg(concat_ws('_', f.name,f.grouping,f.id), ',' ORDER BY f.grouping)";
        }

        throw new \RuntimeException(sprintf('Configured database platform "%s" is not supported.', $platform));
    }
528542
public function setQueryBuilder(QueryBuilder $queryBuilder, $alias)
    {
        $this->queryBuilder = $queryBuilder;
        $this->alias = $alias;

        return $this;
    }
268997
public function buildStatement(Query $query) {
        $type = $query->getType();
        $method = 'build' . ucfirst($type);
        $dialect = $this->getDialect();

        if (!method_exists($dialect, $method)) {
            throw new UnsupportedQueryStatementException(sprintf('Query statement %s does not exist or has not been implemented', $type));
        }

        return $this->getConnection()->prepare(call_user_func([$dialect, $method], $query));
    }