@param QueryBuilder       $qb
@param StockItemInterface $item
@param BatchInterface     $batch

@return QueryBuilder
494376
private function filterByStockItem(QueryBuilder $qb, $item, $batch = null)
    {
        $alias = $qb->getAllAliases()[0];
        $qb
            ->andWhere($alias . '.stockItem = :item')
            ->setParameter('item', $item);

        if (null !== $batch) {
            $qb
                ->andWhere($alias . '.batch = :batch')
                ->setParameter('batch', $batch);
        }

        return $qb;
    }
566543
private function getQuerySelectQuery(): QueryBuilder
    {
        $query = $this->connection->createQueryBuilder();
        $query->select('DISTINCT q.*, qt.*')
            ->from('ngbm_collection_query', 'q')
            ->innerJoin(
                'q',
                'ngbm_collection_query_translation',
                'qt',
                $query->expr()->andX(
                    $query->expr()->eq('qt.query_id', 'q.id'),
                    $query->expr()->eq('qt.status', 'q.status')
                )
            );

        return $query;
    }
162338
public function createDraft(StockItemInterface $item, UomQty $qty,
        Location $srcLoc, Location $destLoc, ?BatchInterface $batch = null): Movement
    {
        $mvt = $this->movementFactory
            ->createDraft($item, $qty, $srcLoc, $destLoc);

        if (null == $batch) {
            $mvt->setBatch($mvt->getBatch());
        }

        $this->movementRepository->add($mvt);

        return $mvt;
    }
114298
public function getFilter(QueryBuilder $qb, $dqlAlias)
    {
        if (null !== $this->dqlAlias) {
            $dqlAlias = $this->dqlAlias;
        }

        return sprintf('%s INSTANCE OF %s', $dqlAlias, $this->value);
    }
1187974
public function findByStockItemAndLocation(
        StockItemInterface $item,
        Location $location,
        ?BatchInterface $batch = null
    ): array {
        return array_filter(
            $this->findAll(),
            function ($su) use ($item, $location) {
                return $su->getLocation() == $location && $su->getStockItem() == $item;
            }
        );
    }
110809
protected function applyFilters(QueryBuilder $qb, array $filters)
    {
        $definition = $this->objectDefinition;
        foreach ($filters as $field => $value) {
            if (!$definition->hasField($field) || !$prop = $definition->getField($field)->getOriginName()) {
                continue;
            }

            $entityField = sprintf('%s.%s', $this->queryAlias, $prop);

            switch (gettype($value)) {
                case 'string':
                    $qb->andWhere($qb->expr()->eq($entityField, $qb->expr()->literal($value)));
                    break;
                case 'integer':
                case 'double':
                    $qb->andWhere($qb->expr()->eq($entityField, $value));
                    break;
                case 'boolean':
                    $qb->andWhere($qb->expr()->eq($entityField, (int) $value));
                    break;
                case 'array':
                    if (empty($value)) {
                        $qb->andWhere($qb->expr()->isNull($entityField));
                    } else {
                        $qb->andWhere($qb->expr()->in($entityField, $value));
                    }
                    break;
                case 'NULL':
                    $qb->andWhere($qb->expr()->isNull($entityField));
                    break;
            }
        }
    }
215199
public function setOrderBy(QueryBuilder $qb)
    {
        if (isset($this->request['iSortCol_0'])) {
            for ($i = 0; $i < intval($this->request['iSortingCols']); $i++) {
                if ($this->request['bSortable_'.intval($this->request['iSortCol_'. $i])] == "true") {
                    $qb->addOrderBy(
                        $this->associations[$this->request['iSortCol_'.$i]]['fullName'],
                        $this->request['sSortDir_'.$i]
                    );
                }
            }
        }
    }
130661
public function createPager(QueryBuilder $qb, Request $request)
    {
        // Too bad we can't use the Sidus/FilterBundle DoctrineORMAdapter instead but it's not a dependency
        $pager = new Pagerfanta(new DoctrineORMAdapter($qb, false));
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
        $pager->setMaxPerPage(10);
        try {
            $pager->setCurrentPage($request->get('page', 1));
        } catch (NotValidCurrentPageException $e) {
            // Silence exception, fallback to first page
        }

        return $pager;
    }
227261
public function getFilter(QueryBuilder $qb, $dqlAlias)
    {
        $children = [];
        foreach ($this->children as $spec) {
            if ($spec instanceof Filter) {
                $children[] = $spec->getFilter($qb, $dqlAlias);
            }
        }

        return call_user_func_array(array($qb->expr(), $this->expression), $children);
    }
1076380
protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
    {
        $query = $this->setTranslationHints($qb->getQuery(), $locale);

        return $query;
    }
561939
public final function queue(string $query, array $queryParams = null): BatchInterface
    {
        $this->queue[] = $this->agent->prepare($query, $queryParams);

        return $this;
    }
32816
protected function executeWithPartialLoad(QueryBuilder $qb)
    {
        $query = $qb->getQuery();
        $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);

        return $query->getOneOrNullResult();
    }
662409
private function filterByAvailability(QueryBuilder $qb)
    {
        $alias = $qb->getAllAliases()[0];
        $qb->andWhere($alias . '.reservationMovement IS NULL');

        return $qb;
    }
1037419
private function applyFilters (QueryBuilder $qb, array $filters)
    {

        if (!isset($filters['groupOp']) || !in_array($filters['groupOp'], array('AND', 'OR'))){
            throw new \InvalidArgumentException('Operator does not match OR | AND');
        }

        if (!isset($filters['rules']) || !is_array($filters['rules'])){
            throw new \InvalidArgumentException('Rules are not set.');
        }
        
        $groupOp = $filters['groupOp'];
    
        $andXWithWhere = $qb->expr()->andX();
        $andXWithHaving = $qb->expr()->andX();
        
        $orXWithWhere = $qb->expr()->orX();
        $orXWithHaving = $qb->expr()->orX();

        $supportedAggregateOperators = array('eq', 'ne', 'lt', 'le', 'gt', 'ge');
    
        foreach ($filters['rules'] as $rule) {
            $rule = $this->getResolvedRule((array) $rule);
            
            $field = $this->getFieldQuery($rule['field'], $rule['op'], $rule['data'], $qb);
            $isAgrigated = $this->isAggregated($rule['field']);
            
            if ($groupOp === 'AND'){
                if (false === $isAgrigated){
                    $andXWithWhere->add($field);
                } else if ($isAgrigated && in_array($rule['op'], $supportedAggregateOperators)){
                    $andXWithHaving->add($field);
                }  
            } elseif ($groupOp === 'OR'){
                if (false === $isAgrigated){
                    $orXWithWhere->add($field);
                } else if ($isAgrigated && in_array($rule['op'], $supportedAggregateOperators)){
                    $orXWithHaving->add($field);
                }
            }
        }
    
        
          
        (count($andXWithWhere->getParts()) > 0) ? $qb->andWhere($andXWithWhere) : null;
        (count($andXWithHaving->getParts()) > 0) ? $qb->andHaving($andXWithHaving) : null; 
        
        // we use addWhere/addHaving because if we add a where/having clause beforehand then it will be ignored.
        (count($orXWithWhere->getParts()) > 0) ? $qb->andWhere($orXWithWhere) : null;
        (count($orXWithHaving->getParts()) > 0) ? $qb->andHaving($orXWithHaving) : null;     
    }
838332
protected function applyPagination(RestRequestContract $request, QueryBuilder $qb)
    {
        $qb->addCriteria(
            new Criteria(null,
                $request->getOrderBy(),
                $request->getStart(),
                $request->getLimit()
            )
        );

        return $this;
    }
1136102
public final function do(): BatchInterface
    {
        // no need to get excited
        if (empty($this->queue)) {
            return $this;
        }

        // check transaction status
        $resource = $this->agent->getResource();
        /* if ($resource->getType() == Resource::TYPE_MYSQL_LINK) {
            // nope..
        } else */ if ($resource->getType() == Resource::TYPE_PGSQL_LINK) {
            $resource = $resource->getObject();
            $resourceStatus = pg_transaction_status($resource);
            if ($resourceStatus !== PGSQL_TRANSACTION_IDLE) do {
                time_nanosleep(0, 100000);
                $resourceStatus = pg_transaction_status($resource);
            } while ($resourceStatus === PGSQL_TRANSACTION_ACTIVE);
        }

        $startTime = microtime(true);

        $this->start(); // begin

        foreach ($this->queue as $query) {
            // @important (clone)
            $result = clone $this->agent->query($query);

            if ($result->getRowsAffected() > 0) {
                $this->results[] = $result;
            }

            unset($result);
        }

        $this->end(); // commit, go go go!

        $this->totalTime = (float) number_format(microtime(true) - $startTime, 10);

        $this->agent->getResult()->reset();

        return $this;
    }
812256
protected function getQueryTotal(QueryBuilder $qb): int
    {
        $countQuery = clone $qb;

        if (\count($qb->getParameters()) > 0) {
            $countQuery->setParameters($qb->getParameters());
        }

        if ($countQuery->getDQLPart('orderBy')) {
            $countQuery->resetDQLPart('orderBy');
        }

        $countQuery->setMaxResults(null);
        $countQuery->setFirstResult(0);

        $queryAlias = $qb->getAllAliases()[0];
        $countQuery->select(sprintf('count(DISTINCT %s.%s) as total', $queryAlias, 'id'));

        return $countQuery->getQuery()->getSingleScalarResult();
    }