@param QueryBuilder $builder
@param string       $modelClass

@return mixed|null

@throws DBALException

@SuppressWarnings(PHPMD.ElseExpression)
516912
private function fetchResourceWithoutRelationships(QueryBuilder $builder, string $modelClass)
    {
        $model     = null;
        $statement = $builder->execute();

        if ($this->isFetchTyped() === true) {
            $statement->setFetchMode(PDOConnection::FETCH_ASSOC);
            if (($attributes = $statement->fetch()) !== false) {
                $platform  = $builder->getConnection()->getDatabasePlatform();
                $typeNames = $this->getModelSchemas()->getAttributeTypes($modelClass);
                $model     = $this->readResourceFromAssoc($modelClass, $attributes, $typeNames, $platform);
            }
        } else {
            $statement->setFetchMode(PDOConnection::FETCH_CLASS, $modelClass);
            if (($fetched = $statement->fetch()) !== false) {
                $model = $fetched;
            }
        }

        return $model;
    }
485724
public function getClassMetadata(Driver $driver, $modelClass)
    {
        $manager = $this->managerFactory->getManager($driver);
        $manager->setModelClass($modelClass);

        return $manager->getClassMetadata();
    }
249697
protected function createDeleteModelBuilder(): ModelQueryBuilder
    {
        $builder = $this
            ->createBuilder($this->getModelClass())
            ->deleteModels();

        $this->applyTableFilters($builder);

        $result = $this->builderOnDelete($builder);

        $this->clearBuilderParameters();

        return $result;
    }
1069765
public function findBy(
        string $modelClass,
        array $criteria,
        array $orderBy = null,
        int $limit = null,
        int $offset = null
    ): array {
        return $this->getRepositoryByClass($modelClass)->findBy($criteria, $orderBy, $limit, $offset);
    }
351224
public function find(string $modelClass, string $id = null)
    {
        if (null === $id) {
            return null;
        }

        return $this->getRepositoryByClass($modelClass)->find($id);
    }
850787
public function apply(Builder $query, Model $model)
    {
        Models::scope()->applyToModelQuery($query, $model->getTable());
    }
209762
public function newQuery() : QueryBuilder
    {
        $model = $this->container->make($this->getModelName());

        if (!$model instanceof Model) {
            throw new RepositoryException("Class {$this->model()} must be an instance of " . Model::class);
        }

        return $model->newQuery();
    }
514060
public function getModelColumns(string $tableAlias = null, string $modelClass = null): array
    {
        $modelClass = $modelClass ?? $this->getModelClass();
        $tableAlias = $tableAlias ?? $this->getAlias();

        $quotedColumns = [];

        $columnMapper    = $this->getColumnToDatabaseMapper();
        $selectedColumns = $this->getModelSchemas()->getAttributes($modelClass);
        foreach ($selectedColumns as $column) {
            $quotedColumns[] = call_user_func($columnMapper, $tableAlias, $column, $this);
        }

        $rawColumns = $this->getModelSchemas()->getRawAttributes($modelClass);
        foreach ($rawColumns as $columnOrCallable) {
            $quotedColumns[] = is_callable($columnOrCallable) === true ?
                call_user_func($columnOrCallable, $tableAlias, $this) : $columnOrCallable;
        }

        return $quotedColumns;
    }
862916
public function search(QueryBuilderInterface $Builder) {
        $this->serialyse($Builder);
        $statement = $this->connector->query($this->parsedQuery);
        $result    = $this->statementToArray($statement);
        $resultSetClass = $this->resultSetClassName;
        $resultSet = new $resultSetClass($result);
        $queryCount = $this->getSerialyser()->setCriteriaList($Builder)->count(true)->serialyse();
        $resultSet->setParent($this)->setCountQuery($queryCount);
        return $resultSet;
    }
1263886
public function getQuery($model = 'db', $queryClass = '')
    {
        if (!isset($this->query[$model])) {
            $class               = $queryClass ?: $this->config['query'];

            $this->query[$model] = new $class($this, 'db' == $model ? '' : $model);
        }
        return $this->query[$model];
    }
732770
public function returnFilteredEntity(QueryBuilder $builder, AbstractFilter $filter) {
        $query = $builder->getQuery();
        $query->setHydrationMode($filter->getHydrationMode());
        try {
            $entity = $query->getSingleResult();
        } catch (NoResultException $e) {
            return null;
        }
        return $entity;
    }
814947
public function getByEntityBuilder($entityClass, $entityId, array $properties = [])
    {
        $qb = $this->createDefaultBuilder()
            ->andWhere('o.objectClass = :entity_class')
            ->setParameter('entity_class', $entityClass)
            ->andWhere('o.objectId = :entity_id')
            ->setParameter('entity_id', $entityId);

        return !empty($properties) ? $qb->andWhere($qb->expr()->in('o.property', $properties)) : $qb;
    }
701319
public function queryMethods(QueryBuilder $builder)
    {
        $script = '';
        $script .= $this->addArchive($builder);
        if ($this->behavior->isArchiveOnUpdate()) {
            $script .= $this->addSetArchiveOnUpdate($builder);
            $script .= $this->addUpdateWithoutArchive($builder);
        }
        if ($this->behavior->isArchiveOnDelete()) {
            $script .= $this->addSetArchiveOnDelete($builder);
            $script .= $this->addDeleteWithoutArchive($builder);
        }

        return $script;
    }
477506
protected function getQueryBuilderForModelClass($modelClass)
    {
        if ( ! is_a($modelClass, Model::class, true)) {
            throw new UnexpectedValueException("{$modelClass} is not an Eloquent model.");
        }

        // If the targeted model is a CMS model, we can default back to its reference data
        // if not specifics are given, otherwise, a custom fallback reference must be used.
        if ($info = $this->getCmsModelInformation($modelClass)) {

            // Get model records, filtered as necessary
            $repository = $this->getModelRepositoryForInformation($info);

            /** @var Model[] $models */
            return $repository->query();
        }

        // The model is not part of the CMS
        return $this->getQueryBuilderForNonCmsModelClass($modelClass);
    }
616987
public function getEntityBuilder($className = null, ClassMetadata $classMetadata = null)
    {
        $builder = new Entity\Builder($this->getMapper(), $this->getFieldManager());

        if ($className !== null) {
            $builder->setClass($className);
        }

        if ($classMetadata !== null) {
            $builder->setClassMetadata($classMetadata);
        }

        return $builder;
    }
492205
private function fetchResourceCollection(QueryBuilder $builder, string $modelClass): array
    {
        $statement = $builder->execute();

        $models           = [];
        $counter          = 0;
        $hasMoreThanLimit = false;
        $limit            = $builder->getMaxResults() !== null ? $builder->getMaxResults() - 1 : null;

        if ($this->isFetchTyped() === true) {
            $platform  = $builder->getConnection()->getDatabasePlatform();
            $typeNames = $this->getModelSchemas()->getAttributeTypes($modelClass);
            $statement->setFetchMode(PDOConnection::FETCH_ASSOC);
            while (($attributes = $statement->fetch()) !== false) {
                $counter++;
                if ($limit !== null && $counter > $limit) {
                    $hasMoreThanLimit = true;
                    break;
                }
                $models[] = $this->readResourceFromAssoc($modelClass, $attributes, $typeNames, $platform);
            }
        } else {
            $statement->setFetchMode(PDOConnection::FETCH_CLASS, $modelClass);
            while (($fetched = $statement->fetch()) !== false) {
                $counter++;
                if ($limit !== null && $counter > $limit) {
                    $hasMoreThanLimit = true;
                    break;
                }
                $models[] = $fetched;
            }
        }

        return [$models, $hasMoreThanLimit, $limit, $builder->getFirstResult()];
    }
532694
public function getConverter(string $modelClass, string $originalClass = '')
    {
        if ('' === $originalClass) {
            $originalClass = $modelClass;
        }

        $converterClass = 'NerdsAndCompany\\Schematic\\Converters\\'.ucfirst(str_replace('craft\\', '', $modelClass));
        $event = new ConverterEvent([
            'modelClass' => $modelClass,
            'converterClass' => $converterClass,
        ]);

        $this->trigger(self::EVENT_RESOLVE_CONVERTER, $event);
        $converterClass = $event->converterClass;

        if (class_exists($converterClass)) {
            $converter = new $converterClass();
            if ($converter instanceof ConverterInterface) {
                return $converter;
            }
        }

        $parentClass = get_parent_class($modelClass);
        if (!$parentClass) {
            Schematic::error('No converter found for '.$originalClass);

            return null;
        }

        return $this->getConverter($parentClass, $originalClass);
    }