<commit_before><?php
namespace Squid\MySql\Impl\Connectors;


use Squid\MySql\IMySqlConnector;
use Squid\MySql\Utils\ClassName;
use Squid\MySql\Command\IWithWhere;
use Squid\MySql\Command\ICmdSelect;
use Squid\MySql\Connectors\IMySqlObjectConnector;

use Squid\Object\AbstractObjectConnector;

use Objection\LiteObject;


class MySqlObjectConnector extends AbstractObjectConnector implements IMySqlObjectConnector
{
	private $tableName;
	
	/** @var IMySqlConnector */
	private $connector;
	
	
	/**
	 * @param LiteObject[] $objects
	 * @return array
	 */
	protected function objectsToData(array $objects)
	{
		if ($this->hasMapper())
		{
			$data = $this->getMapper()->getArray($objects);
			
			foreach($data as $key => $object)
			{
				$data[$key] = array_diff_key($object, array_flip($this->getIgnoreFields()));
			}
		}
		else
		{
			$data = LiteObject::allToArray($objects, [], $this->getIgnoreFields());
		}
		
		return $data;
	}
	
	
	/**
	 * @param IWithWhere $query
	 * @param array $where
	 */
	protected function createFilter(IWithWhere $query, $where) 
	{
		foreach ($where as $field => $value) 
		{
			if (is_array($value))
			{
				$query->whereIn($field, $value);
			}
			else 
			{
				$query->byField($field, $value);
			}
		}
	}
	
	/**
	 * @param array $fields
	 * @param array|string|bool $order
	 * @return ICmdSelect
	 */
	protected function createQuery(array $fields, $order = false) 
	{
		$query = $this->connector
			->select()
			->from($this->tableName);
		
		$this->createFilter($query, $fields);
		
		if (is_string($order)) 
		{
			$query->orderBy($order);
		}
		else if (is_array($order)) 
		{
			foreach ($order as $field => $type) 
			{
				$query->orderBy($field, $type);
			}
		}
		
		return $query;
	}
	
	
	/**
	 * @return IMySqlConnector
	 */
	public function getConnector()
	{
		return $this->connector;
	}
	
	/**
	 * @inheritdoc
	 */
	public function setConnector(IMySqlConnector $connector) 
	{ 
		$this->connector = $connector;
		return $this; 
	}

	/**
	 * @inheritdoc
	 */
	public function setTable($tableName) 
	{
		$this->tableName = $tableName;
		return $this;
	}
	
	/**
	 * @inheritdoc
	 */
	public function setDomain($className)
	{
		parent::setDomain($className);
		
		if (!$this->tableName)
		{
			$this->tableName = ClassName::getClassNameOnly($className);
		}
		
		return $this;
	}
	
	/**
	 * @param LiteObject[] $objects
	 * @param array $excludeFields
	 * @return int|bool
	 */
	public function insertAll(array $objects, array $excludeFields = [])
	{
		if ($excludeFields)
		{
			$this->addIgnoreFields($excludeFields);
		}
		
		$data = $this->objectsToData($objects);
		
		return $this->connector
			->insert()
			->into($this->tableName)
			->valuesBulk($data)
			->executeDml(true);
	}
	
	/**
	 * @inheritdoc
	 */
	public function loadOneByFields(array $byFields, array $orderFields = [])
	{
		$data = $this
			->createQuery($byFields, $orderFields)
			->queryRow(true, true);
		
		return (!$data ? $data : $this->createInstance($data));
	}
	
	/**
	 * @param array $byFields
	 * @param array $orderFields
	 * @return LiteObject|null
	 */
	public function loadFirstByFields(array $byFields, array $orderFields = [])
	{
		$data = $this
			->createQuery($byFields, $orderFields)
			->limitBy(1)
			->queryRow(true, true);
		
		return (!$data ? $data : $this->createInstance($data));
	}

	/**
	 * @param array $byFields
	 * @param array $orderFields
	 * @param int $limit
	 * @return LiteObject[]|null
	 */
	public function loadAllByFields(array $byFields, array $orderFields = [], $limit = 32)
	{
		$query = $this->createQuery($byFields, $orderFields);
		return $this->createAllInstances($query->limitBy($limit)->queryAll(true) ?: []);
	}
	
	/**
	 * @inheritdoc
	 */
	public function updateByFields(array $set, array $byFields)<commit_msg>Fixed error when more than one row found in loadFirstByFields<commit_after><?php
namespace Squid\MySql\Impl\Connectors;


use Squid\MySql\IMySqlConnector;
use Squid\MySql\Utils\ClassName;
use Squid\MySql\Command\IWithWhere;
use Squid\MySql\Command\ICmdSelect;
use Squid\MySql\Connectors\IMySqlObjectConnector;

use Squid\Object\AbstractObjectConnector;

use Objection\LiteObject;


class MySqlObjectConnector extends AbstractObjectConnector implements IMySqlObjectConnector
{
	private $tableName;
	
	/** @var IMySqlConnector */
	private $connector;
	
	
	/**
	 * @param LiteObject[] $objects
	 * @return array
	 */
	protected function objectsToData(array $objects)
	{
		if ($this->hasMapper())
		{
			$data = $this->getMapper()->getArray($objects);
			
			foreach($data as $key => $object)
			{
				$data[$key] = array_diff_key($object, array_flip($this->getIgnoreFields()));
			}
		}
		else
		{
			$data = LiteObject::allToArray($objects, [], $this->getIgnoreFields());
		}
		
		return $data;
	}
	
	
	/**
	 * @param IWithWhere $query
	 * @param array $where
	 */
	protected function createFilter(IWithWhere $query, $where) 
	{
		foreach ($where as $field => $value) 
		{
			if (is_array($value))
			{
				$query->whereIn($field, $value);
			}
			else 
			{
				$query->byField($field, $value);
			}
		}
	}
	
	/**
	 * @param array $fields
	 * @param array|string|bool $order
	 * @return ICmdSelect
	 */
	protected function createQuery(array $fields, $order = false) 
	{
		$query = $this->connector
			->select()
			->from($this->tableName);
		
		$this->createFilter($query, $fields);
		
		if (is_string($order)) 
		{
			$query->orderBy($order);
		}
		else if (is_array($order)) 
		{
			foreach ($order as $field => $type) 
			{
				$query->orderBy($field, $type);
			}
		}
		
		return $query;
	}
	
	
	/**
	 * @return IMySqlConnector
	 */
	public function getConnector()
	{
		return $this->connector;
	}
	
	/**
	 * @inheritdoc
	 */
	public function setConnector(IMySqlConnector $connector) 
	{ 
		$this->connector = $connector;
		return $this; 
	}

	/**
	 * @inheritdoc
	 */
	public function setTable($tableName) 
	{
		$this->tableName = $tableName;
		return $this;
	}
	
	/**
	 * @inheritdoc
	 */
	public function setDomain($className)
	{
		parent::setDomain($className);
		
		if (!$this->tableName)
		{
			$this->tableName = ClassName::getClassNameOnly($className);
		}
		
		return $this;
	}
	
	/**
	 * @param LiteObject[] $objects
	 * @param array $excludeFields
	 * @return int|bool
	 */
	public function insertAll(array $objects, array $excludeFields = [])
	{
		if ($excludeFields)
		{
			$this->addIgnoreFields($excludeFields);
		}
		
		$data = $this->objectsToData($objects);
		
		return $this->connector
			->insert()
			->into($this->tableName)
			->valuesBulk($data)
			->executeDml(true);
	}
	
	/**
	 * @inheritdoc
	 */
	public function loadOneByFields(array $byFields, array $orderFields = [])
	{
		$data = $this
			->createQuery($byFields, $orderFields)
			->queryRow(true, true);
		
		return (!$data ? $data : $this->createInstance($data));
	}
	
	/**
	 * @param array $byFields
	 * @param array $orderFields
	 * @return LiteObject|null
	 */
	public function loadFirstByFields(array $byFields, array $orderFields = [])
	{
		$data = $this
			->createQuery($byFields, $orderFields)
			->limitBy(1)
			->queryRow(true, false);
		
		return (!$data ? $data : $this->createInstance($data));
	}

	/**
	 * @param array $byFields
	 * @param array $orderFields
	 * @param int $limit
	 * @return LiteObject[]|null
	 */
	public function loadAllByFields(array $byFields, array $orderFields = [], $limit = 32)
	{
		$query = $this->createQuery($byFields, $orderFields);
		return $this->createAllInstances($query->limitBy($limit)->queryAll(true) ?: []);
	}
	
	/**
	 * @inheritdoc
	 */
	public function updateByFields(array $set, array $byFields)
{'fraction_non_alphanumeric': 0.16201337003539126, 'fraction_numerical': 0.0007864726700747149, 'mean_word_length': 3.5470798569725863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, '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': '3831129', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2563, 'n_words': 500}