<commit_before><?php
namespace Mapbender\SearchBundle\Component;

use Eslider\Driver\HKVStorage;
use Eslider\Entity\HKV;
use Mapbender\CoreBundle\Component\SecurityContext;
use Mapbender\SearchBundle\Entity\Query;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class QueryManager
 *
 * @package Mapbender\SearchBundle\Component
 * @author  <NAME> <<EMAIL>>
 */
class QueryManager extends BaseManager
{
    /** @var HKVStorage $db */
    protected $db;


    /** @var Configuration $configuration */
    protected $configuration;


    const SERVICE_NAME = "mapbender.query.manager";


    /**
     * QueryManager constructor.
     *
     * @param ContainerInterface
     * $container
     */
    public function __construct(ContainerInterface $container = null)
    {
        parent::__construct($container, "queries");
    }


    /**
     * Save query
     *
     * @param array $array
     * @return HKV
     */
    public function saveArray($array, $scope = null, $parentId = null)
    {
        $query = $this->create($array);
        $HKV   = $this->save($query, $scope, $parentId);
        return $HKV;
    }

    /**
     * Save query
     *
     * @param Query $query
     * @return HKV
     */
    public function save($query, $scope = null, $parentId = null)
    {
        $queries = $this->listQueries();

        if ($queries == null) {
            $queries = array();
        }

        $queries[ $query->getId() ] = $query;
        $result                     = $this->db->saveData($this->tableName, $queries, $scope, $parentId, $this->getUserId());

        $children = $result->getChildren();

        foreach ($children as $key => $child) {
            if ($child->getKey() == $query->getId()) {
                return $child;
            }
        }

        return null;

    }


    /**
     * Get query by id
     *
     * @param int    $id
     * @param string $userId
     * @return HKV|null
     */
    public function getById($id)
    {
        $queries = $this->listQueries();
        return isset($queries[ $id ]) ? $queries[ $id ] : null;
    }


    /**
     * List all queries
     *
     * @param int $id
     * @return HKV[]
     */
    public function listQueries()
    {
        return $this->db->getData($this->tableName, null, null, $this->getUserId());
    }


    /**
     * @param $args
     * @return Query
     */
    public function create($args)<commit_msg>Add getting Features for Queries to QueryManager for ALL Queries<commit_after><?php
namespace Mapbender\SearchBundle\Component;

use Eslider\Driver\HKVStorage;
use Eslider\Entity\HKV;
use Mapbender\DataSourceBundle\Component\FeatureType;
use Mapbender\DataSourceBundle\Component\FeatureTypeService;
use Mapbender\DataSourceBundle\Entity\Feature;
use Mapbender\SearchBundle\Entity\Query;
use Mapbender\SearchBundle\Entity\QueryCondition;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class QueryManager
 *
 * @package Mapbender\SearchBundle\Component
 * @author  <NAME> <<EMAIL>>
 */
class QueryManager extends BaseManager
{
    /** @var HKVStorage $db */
    protected $db;


    /** @var Configuration $configuration */
    protected $configuration;


    const SERVICE_NAME = "mapbender.query.manager";


    /**
     * QueryManager constructor.
     *
     * @param ContainerInterface|null $container
     */
    public function __construct(ContainerInterface $container = null)
    {
        parent::__construct($container, "queries");
    }


    /**
     * Save query
     *
     * @param array $array
     * @return HKV
     */
    public function saveArray($array, $scope = null, $parentId = null)
    {
        $query = $this->create($array);
        $HKV   = $this->save($query, $scope, $parentId);
        return $HKV;
    }

    /**
     * Save query
     *
     * @param Query $query
     * @return HKV
     */
    public function save($query, $scope = null, $parentId = null)
    {
        $queries = $this->listQueries();

        if ($queries == null) {
            $queries = array();
        }

        $queries[ $query->getId() ] = $query;
        $result                     = $this->db->saveData($this->tableName, $queries, $scope, $parentId, $this->getUserId());

        $children = $result->getChildren();

        foreach ($children as $key => $child) {
            if ($child->getKey() == $query->getId()) {
                return $child;
            }
        }

        return null;

    }


    /**
     * Get query by id
     *
     * @param int    $id
     * @param string $userId
     * @return HKV|null
     */
    public function getById($id)
    {
        $queries = $this->listQueries();
        return isset($queries[ $id ]) ? $queries[ $id ] : null;
    }


    /**
     * List all queries
     *
     * @return \Mapbender\SearchBundle\Entity\Query[]
     * @internal param int $id
     */
    public function listQueries()
    {
        return $this->db->getData($this->tableName, null, null, $this->getUserId());
    }

    /**
     * Returns all
     *
     * @param FeatureTypeService $featureService
     * @return Feature[]
     */
    public function listQueriesByFeatureType($featureService = null)
    {
        $queries = $this->listQueries();
        $results = array();
        if ($featureService != null) {
            foreach ($featureService->getFeatureTypeDeclarations() as $i => $feature) {
                foreach ($queries as $j => $query) {
                    $this->addFeatureType($results, $featureService, $feature, $query);
                }
            }
        }

        return $results;
    }


    /**
     * @param array              $results
     * @param FeatureTypeService $featureService
     * @param Feature            $feature
     * @param Query              $query
     */
    private function addFeatureType(&$results, $featureService, $feature, $query)
    {
        if ($feature->getType() == $query->getFeatureType()) {

            $featureType     = $featureService->get($feature->getType());
            $queryConditions = $query->getConditions();

            $criteria = $this->buildCriteria($queryConditions, $featureType);
            array_merge($results, $featureType->search($criteria));

        }
    }

    /**
     * @param QueryCondition[] $queryConditions
     * @param FeatureType      $featureType
     * @return array
     */
    private function buildCriteria($queryConditions, $featureType)
    {
        $queryParts = array();
        $escaper    = $featureType->getConnection();
        foreach ($queryConditions as $k => $condition) {
            $queryParts[] = $escaper->quoteIdentifier($condition->getFieldName())
                . " " . $condition->getOperator()
                . " " . $escaper->quote($condition->getValue())
                . " ";

        }
        return array("where" => implode(" AND ", $queryParts));
    }

    /**
     * @param $args
     * @return Query
     */
    public function create($args)
{'fraction_non_alphanumeric': 0.1405041535376683, 'fraction_numerical': 0.0, 'mean_word_length': 2.131390134529148, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2101900', 'n_tokens_mistral': 2060, 'n_tokens_neox': 2004, 'n_words': 431}