<commit_before><?php
/*
 * This file was delivered to you as part of the Yucca package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Yucca\Component;

use Yucca\Component\ConnectionManager;
use Yucca\Component\ShardingStrategy\ShardingStrategyInterface;

class SchemaManager
{
    protected $schemaConfig;
    protected $shardingStrategies;
    /**
     * @var \Yucca\Component\ConnectionManager
     */
    protected $connectionManager;

    /**
     * @param array $schemaConfig
     */
    public function __construct(array $schemaConfig) {
        $this->schemaConfig = $schemaConfig;
    }

    /**
     * @param ConnectionManager $connectionManager
     */
    public function setConnectionManager(ConnectionManager $connectionManager){
        $this->connectionManager = $connectionManager;
    }

    /**
     * @param $shardingStrategyName
     * @param ShardingStrategy\ShardingStrategyInterface $shardingStrategy
     */
    public function addShardingStrategy($shardingStrategyName, ShardingStrategyInterface $shardingStrategy) {
        $this->shardingStrategies[$shardingStrategyName] = $shardingStrategy;
    }

    /**
     * @param $table
     * @param $shardingKey
     * @param bool $forceFromMaster
     * @return mixed
     * @throws \Exception
     */
    public function getConnectionName($table, $shardingKey, $forceFromMaster=true) {
        $shardingIdentifier = $this->getShardingIdentifier($table, $shardingKey);

        if(is_null($shardingIdentifier)){
            if(1 === count($this->schemaConfig[$table]['shards'])){
                return current($this->schemaConfig[$table]['shards']);
            } else {
                $shardsCount = count($this->schemaConfig[$table]['shards']);
                throw new \Exception("Table $table is not configured as sharded. $shardsCount connections found for table $table and sharding key $shardingKey");
            }
        }

        if(false === isset($this->schemaConfig[$table]['shards'][$shardingIdentifier])) {
            throw new \Exception("No connections found for table $table and shard $shardingIdentifier");
        }

        //Return connection name
        return $this->schemaConfig[$table]['shards'][$shardingIdentifier];
    }

    public function getShardingIdentifier($table, $shardingKey = null){
        if(is_null($shardingKey)){
            return null;
        }

        //Look for table config
        if(false === isset($this->schemaConfig[$table])) {
            throw new \InvalidArgumentException("$table is not in given configuration");
        }

        //Look for sharding strategy
        if(false === isset($this->schemaConfig[$table]['sharding_strategy'])){
            return null;
        }
        $shardingStrategy = $this->schemaConfig[$table]['sharding_strategy'];
        if(false === isset($this->shardingStrategies[$shardingStrategy])){
            throw new \Exception("Sharding strategy $shardingStrategy not found for table $table");
        }

        //Look for sharding identifier
        return $this->shardingStrategies[$shardingStrategy]->getShardingIdentifier(
            $this->schemaConfig[$table],
            $shardingKey
        );
    }

    protected function fetch($tableName, array $criterias, array $fields, $allowEmptyCriterias, $forceFromMaster, array $options=array()) {
        $shardingKey = null;
        if(isset($criterias['sharding_key'])) {
            $shardingKey = $criterias['sharding_key'];
            unset($criterias['sharding_key']);
        }

        if((false === $allowEmptyCriterias) && empty($criterias)){
            if(is_array($tableName)){
                $tableName = implode(',',array_keys($tableName));
            }
            throw new \Exception("Trying to load from $tableName with no identifiers");
        }
        if(empty($tableName)) {
            throw new \RuntimeException('table name must not be empty');
        }

        if(is_array($tableName)){
            $connectionName = $this->getConnectionName(key($tableName), $shardingKey, $forceFromMaster);
            $from = array();
            $join = array();

            foreach($tableName as $table => $tableJoin) {
                if($connectionName != $this->getConnectionName($table, $shardingKey, $forceFromMaster)) {
                    throw new \RuntimeException('Expected connection : '.$connectionName.', but '.$table.' use another one');
                }

                $shardingIdentifier = $this->getShardingIdentifier($table,$shardingKey);
                if($shardingIdentifier) {
                    $table = sprintf('%1$s_%2$s', $table, $shardingIdentifier);
                }

                if(empty($tableJoin['join'])) {
                    $from[] = $table. (isset($tableJoin['alias'])?' AS '.$tableJoin['alias']:'');
                } else {
                    $join[] = sprintf($tableJoin['join'], $table);
                }
            }

            $tables = implode(',',$from).' '.implode(' ',$join);
            $connection = $this->connectionManager->getConnection(
                $connectionName,
                $forceFromMaster
            );
        } else {
            $connection = $this->connectionManager->getConnection(
                $this->getConnectionName($tableName, $shardingKey, $forceFromMaster),
                $forceFromMaster
            );

            $shardingIdentifier = $this->getShardingIdentifier($tableName,$shardingKey);
            if($shardingIdentifier) {
                $tableName = sprintf('%1$s_%2$s', $tableName, $shardingIdentifier);
            }

            $tables = '`'.$tableName.'`';
        }

        $fields = implode(',',$fields);

        $sql = "SELECT $fields FROM $tables";
        $params = array();
        $whereCriterias = array();
        foreach($criterias as $criteriaKey=>$criteriaValue){
            if(is_array($criteriaValue) && 1==count($criteriaValue)){
                $criteriaValue = current($criteriaValue);
            }
            if(is_array($criteriaValue)){
                $parametersNames = array();
                $i = 0;
                foreach($criteriaValue as $v){
                    if($v instanceof \Yucca\Model\ModelInterface) {
                        $params[":$criteriaKey$i"] = $v->getId();
                        $parametersNames[] = ":$criteriaKey$i";
                        $i++;
                    } elseif(is_scalar($v)) {
                        $params[":$criteriaKey$i"] = $v;
                        $parametersNames[] = ":$criteriaKey$i";
                        $i++;
                    } else {
                        throw new \Exception("Don't know what to do with criteria $criteriaKey");
                    }
                }
                $whereCriterias[] = "`$criteriaKey` IN (".implode(',',$parametersNames).")";
            } else {
                if($criteriaValue instanceof \Yucca\Model\ModelInterface) {
                    $whereCriterias[] = "`$criteriaKey`=:$criteriaKey";
                    $params[":$criteriaKey"] = $criteriaValue->getId();
                } elseif(is_null($criteriaValue)) {
                    $whereCriterias[] = "`$criteriaKey` IS NULL";
                } elseif(is_scalar($criteriaValue)) {
                    $whereCriterias[] = "`$criteriaKey`=:$criteriaKey";
                    $params[":$criteriaKey"] = $criteriaValue;
                } elseif($criteriaValue instanceof \Yucca\Component\Selector\Expression) {
                    $whereCriterias[] = $criteriaValue->toString('database');
                } else {
                    throw new \Exception("Don't know what to do with criteria $criteriaKey");
                }
            }
        }
        if(false === empty($whereCriterias)) {
            $sql .= ' WHERE '.implode(" AND ",$whereCriterias);
        }

        if(isset($options['groupBy'])){
            $sql .= ' GROUP BY '.$options['groupBy'];
        }
        if(isset($options['orderBy'])) {
            $sql .= ' ORDER BY '.$options['orderBy'];<commit_msg>[SELECTOR] Can use alias in criterias (like m.id)<commit_after><?php
/*
 * This file was delivered to you as part of the Yucca package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Yucca\Component;

use Yucca\Component\ConnectionManager;
use Yucca\Component\ShardingStrategy\ShardingStrategyInterface;

class SchemaManager
{
    protected $schemaConfig;
    protected $shardingStrategies;
    /**
     * @var \Yucca\Component\ConnectionManager
     */
    protected $connectionManager;

    /**
     * @param array $schemaConfig
     */
    public function __construct(array $schemaConfig) {
        $this->schemaConfig = $schemaConfig;
    }

    /**
     * @param ConnectionManager $connectionManager
     */
    public function setConnectionManager(ConnectionManager $connectionManager){
        $this->connectionManager = $connectionManager;
    }

    /**
     * @param $shardingStrategyName
     * @param ShardingStrategy\ShardingStrategyInterface $shardingStrategy
     */
    public function addShardingStrategy($shardingStrategyName, ShardingStrategyInterface $shardingStrategy) {
        $this->shardingStrategies[$shardingStrategyName] = $shardingStrategy;
    }

    /**
     * @param $table
     * @param $shardingKey
     * @param bool $forceFromMaster
     * @return mixed
     * @throws \Exception
     */
    public function getConnectionName($table, $shardingKey, $forceFromMaster=true) {
        $shardingIdentifier = $this->getShardingIdentifier($table, $shardingKey);

        if(is_null($shardingIdentifier)){
            if(1 === count($this->schemaConfig[$table]['shards'])){
                return current($this->schemaConfig[$table]['shards']);
            } else {
                $shardsCount = count($this->schemaConfig[$table]['shards']);
                throw new \Exception("Table $table is not configured as sharded. $shardsCount connections found for table $table and sharding key $shardingKey");
            }
        }

        if(false === isset($this->schemaConfig[$table]['shards'][$shardingIdentifier])) {
            throw new \Exception("No connections found for table $table and shard $shardingIdentifier");
        }

        //Return connection name
        return $this->schemaConfig[$table]['shards'][$shardingIdentifier];
    }

    public function getShardingIdentifier($table, $shardingKey = null){
        if(is_null($shardingKey)){
            return null;
        }

        //Look for table config
        if(false === isset($this->schemaConfig[$table])) {
            throw new \InvalidArgumentException("$table is not in given configuration");
        }

        //Look for sharding strategy
        if(false === isset($this->schemaConfig[$table]['sharding_strategy'])){
            return null;
        }
        $shardingStrategy = $this->schemaConfig[$table]['sharding_strategy'];
        if(false === isset($this->shardingStrategies[$shardingStrategy])){
            throw new \Exception("Sharding strategy $shardingStrategy not found for table $table");
        }

        //Look for sharding identifier
        return $this->shardingStrategies[$shardingStrategy]->getShardingIdentifier(
            $this->schemaConfig[$table],
            $shardingKey
        );
    }

    protected function fetch($tableName, array $criterias, array $fields, $allowEmptyCriterias, $forceFromMaster, array $options=array()) {
        $shardingKey = null;
        if(isset($criterias['sharding_key'])) {
            $shardingKey = $criterias['sharding_key'];
            unset($criterias['sharding_key']);
        }

        if((false === $allowEmptyCriterias) && empty($criterias)){
            if(is_array($tableName)){
                $tableName = implode(',',array_keys($tableName));
            }
            throw new \Exception("Trying to load from $tableName with no identifiers");
        }
        if(empty($tableName)) {
            throw new \RuntimeException('table name must not be empty');
        }

        if(is_array($tableName)){
            $connectionName = $this->getConnectionName(key($tableName), $shardingKey, $forceFromMaster);
            $from = array();
            $join = array();

            foreach($tableName as $table => $tableJoin) {
                if($connectionName != $this->getConnectionName($table, $shardingKey, $forceFromMaster)) {
                    throw new \RuntimeException('Expected connection : '.$connectionName.', but '.$table.' use another one');
                }

                $shardingIdentifier = $this->getShardingIdentifier($table,$shardingKey);
                if($shardingIdentifier) {
                    $table = sprintf('%1$s_%2$s', $table, $shardingIdentifier);
                }

                if(empty($tableJoin['join'])) {
                    $from[] = $table. (isset($tableJoin['alias'])?' AS '.$tableJoin['alias']:'');
                } else {
                    $join[] = sprintf($tableJoin['join'], $table);
                }
            }

            $tables = implode(',',$from).' '.implode(' ',$join);
            $connection = $this->connectionManager->getConnection(
                $connectionName,
                $forceFromMaster
            );
        } else {
            $connection = $this->connectionManager->getConnection(
                $this->getConnectionName($tableName, $shardingKey, $forceFromMaster),
                $forceFromMaster
            );

            $shardingIdentifier = $this->getShardingIdentifier($tableName,$shardingKey);
            if($shardingIdentifier) {
                $tableName = sprintf('%1$s_%2$s', $tableName, $shardingIdentifier);
            }

            $tables = '`'.$tableName.'`';
        }

        $fields = implode(',',$fields);

        $sql = "SELECT $fields FROM $tables";
        $params = array();
        $whereCriterias = array();
        foreach($criterias as $criteriaKey=>$criteriaValue){
            if(is_array($criteriaValue) && 1==count($criteriaValue)){
                $criteriaValue = current($criteriaValue);
            }
            if(is_array($criteriaValue)){
                $parametersNames = array();
                $i = 0;
                foreach($criteriaValue as $v){
                    if($v instanceof \Yucca\Model\ModelInterface) {
                        $params[":".str_replace('.','_',$criteriaKey)."$i"] = $v->getId();
                        $parametersNames[] = ":".str_replace('.','_',$criteriaKey)."$i";
                        $i++;
                    } elseif(is_scalar($v)) {
                        $params[":".str_replace('.','_',$criteriaKey)."$i"] = $v;
                        $parametersNames[] = ":".str_replace('.','_',$criteriaKey)."$i";
                        $i++;
                    } else {
                        throw new \Exception("Don't know what to do with criteria $criteriaKey");
                    }
                }
                $whereCriterias[] = "`".str_replace('.','`.`',$criteriaKey)."` IN (".implode(',',$parametersNames).")";
            } else {
                if($criteriaValue instanceof \Yucca\Model\ModelInterface) {
                    $whereCriterias[] = "`".str_replace('.','`.`',$criteriaKey)."`=:".str_replace('.','_',$criteriaKey)."";
                    $params[":".str_replace('.','_',$criteriaKey)] = $criteriaValue->getId();
                } elseif(is_null($criteriaValue)) {
                    $whereCriterias[] = "`".str_replace('.','`.`',$criteriaKey)."` IS NULL";
                } elseif(is_scalar($criteriaValue)) {
                    $whereCriterias[] = "`".str_replace('.','`.`',$criteriaKey)."`=:".str_replace('.','_',$criteriaKey)."";
                    $params[":".str_replace('.','_',$criteriaKey)] = $criteriaValue;
                } elseif($criteriaValue instanceof \Yucca\Component\Selector\Expression) {
                    $whereCriterias[] = $criteriaValue->toString('database');
                } else {
                    throw new \Exception("Don't know what to do with criteria $criteriaKey");
                }
            }
        }
        if(false === empty($whereCriterias)) {
            $sql .= ' WHERE '.implode(" AND ",$whereCriterias);
        }

        if(isset($options['groupBy'])){
            $sql .= ' GROUP BY '.$options['groupBy'];
        }
        if(isset($options['orderBy'])) {
            $sql .= ' ORDER BY '.$options['orderBy'];
{'fraction_non_alphanumeric': 0.1438430148823812, 'fraction_numerical': 0.0008401344215074412, 'mean_word_length': 2.1761006289308176, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 69, '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': '7375285', 'n_tokens_mistral': 4467, 'n_tokens_neox': 4307, 'n_words': 832}