<commit_before><?php

namespace Silktide\Reposition\Sql\QueryInterpreter\Type;

use Silktide\Reposition\Exception\InterpretationException;
use Silktide\Reposition\QueryBuilder\QueryToken\Entity;
use Silktide\Reposition\QueryBuilder\QueryToken\Token;
use Silktide\Reposition\QueryBuilder\QueryToken\Value;
use Silktide\Reposition\QueryBuilder\QueryToken\Reference;
use Silktide\Reposition\QueryBuilder\TokenSequencerInterface;
use Silktide\Reposition\Metadata\EntityMetadata;

class FindInterpreter extends AbstractSqlQueryTypeInterpreter
{

    public function supportedQueryType()
    {
        return TokenSequencerInterface::TYPE_FIND;
    }

    public function interpretQuery(TokenSequencerInterface $query)
    {
        $this->reset();
        $this->query = $query;

        $includes = $query->getIncludes();
        $mainMetadata = $query->getEntityMetadata();
        $mainCollection = $mainMetadata->getCollection();

        $token = $query->getNextToken();

        $this->fields = [];
        while (!empty($token) && in_array($token->getType(), ["function", "field"])) {
            if ($token->getType() == "function") {
                /** @var Value $token */
                // render the aggregate function SQL e.g. COUNT(*), SUM(field), etc...
                $fieldSql = $this->renderFunction($token);

                // create an alias for this aggregate and append the SQL
                $collectionAlias = $this->findFreeAlias($token->getValue(), $this->fields);
                $fieldSql .= $this->renderAlias($collectionAlias);
            } else {
                /** @var Reference $token */
                $fieldName = $mainCollection . "." . $token->getValue();
                $collectionAlias = $this->getSelectFieldAlias($fieldName);
                $fieldSql = $this->renderArbitraryReference($fieldName, $collectionAlias);
            }

            // add the sql to the fields array and load the next token
            $this->fields[$collectionAlias] = $fieldSql;

            $token = $query->getNextToken();
        }

        // if we had no aggregate functions in the sequence, then this is a standard select query
        // so, we need to get the fields to return from the entity metadata for each
        if (empty($this->fields)) {
            $metadata = $mainMetadata;
            $collectionAlias = $mainCollection;

            do {
                if (empty($metadata)) {
                    continue;
                }

                // for each entity field, create an aliased SQL reference
                $entityFields = $metadata->getFieldNames();
                sort($entityFields);
                foreach ($entityFields as $field) {
                    $field = $collectionAlias . "." . $field;
                    $thisFieldAlias = $this->getSelectFieldAlias($field);
                    $this->fields[$thisFieldAlias] = $this->renderArbitraryReference($field, $thisFieldAlias);
                }

            } while (list($collectionAlias, $metadata) = each($includes));

        }

        if (empty($this->fields)) {
            throw new InterpretationException("Cannot interpret find query, there are no fields to return");
        }

        // if this query has an include, it's more complex and requires separate processing
        if (count($includes) > 0) {
            return $this->renderIncludeQuery($token);
        }

        // simple, no include query. Render all remaining tokens
        $sql = "SELECT " . implode(", ", $this->fields) . " FROM " . $this->renderArbitraryReference($mainCollection);

        // render all other tokens
        do {
            if (empty($token)) {<commit_msg>fixed call that is deprecated in php7.2<commit_after><?php

namespace Silktide\Reposition\Sql\QueryInterpreter\Type;

use Silktide\Reposition\Exception\InterpretationException;
use Silktide\Reposition\QueryBuilder\QueryToken\Entity;
use Silktide\Reposition\QueryBuilder\QueryToken\Token;
use Silktide\Reposition\QueryBuilder\QueryToken\Value;
use Silktide\Reposition\QueryBuilder\QueryToken\Reference;
use Silktide\Reposition\QueryBuilder\TokenSequencerInterface;
use Silktide\Reposition\Metadata\EntityMetadata;

class FindInterpreter extends AbstractSqlQueryTypeInterpreter
{

    public function supportedQueryType()
    {
        return TokenSequencerInterface::TYPE_FIND;
    }

    public function interpretQuery(TokenSequencerInterface $query)
    {
        $this->reset();
        $this->query = $query;

        $includes = $query->getIncludes();
        $mainMetadata = $query->getEntityMetadata();
        $mainCollection = $mainMetadata->getCollection();

        $token = $query->getNextToken();

        $this->fields = [];
        while (!empty($token) && in_array($token->getType(), ["function", "field"])) {
            if ($token->getType() == "function") {
                /** @var Value $token */
                // render the aggregate function SQL e.g. COUNT(*), SUM(field), etc...
                $fieldSql = $this->renderFunction($token);

                // create an alias for this aggregate and append the SQL
                $collectionAlias = $this->findFreeAlias($token->getValue(), $this->fields);
                $fieldSql .= $this->renderAlias($collectionAlias);
            } else {
                /** @var Reference $token */
                $fieldName = $mainCollection . "." . $token->getValue();
                $collectionAlias = $this->getSelectFieldAlias($fieldName);
                $fieldSql = $this->renderArbitraryReference($fieldName, $collectionAlias);
            }

            // add the sql to the fields array and load the next token
            $this->fields[$collectionAlias] = $fieldSql;

            $token = $query->getNextToken();
        }

        // if we had no aggregate functions in the sequence, then this is a standard select query
        // so, we need to get the fields to return from the entity metadata for each
        if (empty($this->fields)) {
            foreach ([[$mainCollection => $mainMetadata], $includes] as $includeList) {
                foreach ($includeList as $collectionAlias => $metadata) {
                    if (empty($metadata)) {
                        continue;
                    }

                    // for each entity field, create an aliased SQL reference
                    $entityFields = $metadata->getFieldNames();
                    sort($entityFields);
                    foreach ($entityFields as $field) {
                        $field = $collectionAlias . "." . $field;
                        $thisFieldAlias = $this->getSelectFieldAlias($field);
                        $this->fields[$thisFieldAlias] = $this->renderArbitraryReference($field, $thisFieldAlias);
                    }
                }
            }
        }

        if (empty($this->fields)) {
            throw new InterpretationException("Cannot interpret find query, there are no fields to return");
        }

        // if this query has an include, it's more complex and requires separate processing
        if (count($includes) > 0) {
            return $this->renderIncludeQuery($token);
        }

        // simple, no include query. Render all remaining tokens
        $sql = "SELECT " . implode(", ", $this->fields) . " FROM " . $this->renderArbitraryReference($mainCollection);

        // render all other tokens
        do {
            if (empty($token)) {
{'fraction_non_alphanumeric': 0.11790803979564399, 'fraction_numerical': 0.0005377789728421619, 'mean_word_length': 2.2037037037037037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3098016', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1808, 'n_words': 466}