<commit_before><?php
namespace GtnPersistZendDb\Infrastructure\ZendDb;

use GtnPersistBase\Model\AggregateRootInterface;
use GtnPersistBase\Model\RepositoryInterface;
use GtnPersistZendDb\Db\Adapter\MasterSlavesAdapterInterface;
use GtnPersistZendDb\Service\AggregateRootProxyFactoryInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Expression as SqlExpression;
use Zend\Db\Sql\PreparableSqlInterface;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Stdlib\Hydrator\HydratorInterface;

class Repository implements RepositoryInterface
{
    /** @var MasterSlavesAdapterInterface */
    protected $dbAdapter;

    /** @var Sql */
    protected $masterSql;

    /** @var Sql */
    protected $slaveSql;

    /** @var string */
    protected $tableName;

    /** @var mixed */
    protected $tableId;

    /** @var string */
    protected $tableSequenceName;

    /** @var string */
    protected $aggregateRootClass;

    /** @var AggregateRootProxyFactoryInterface */
    protected $aggregateRootProxyFactory;

    /** @var HydratorInterface */
    protected $aggregateRootHydrator;

    /** @var array */
    protected $config;

    /**
     * @param MasterSlavesAdapterInterface $dbAdapter
     */
    public function __construct(MasterSlavesAdapterInterface $dbAdapter)
    {
        $this->setDbAdapter($dbAdapter);
        $this->setMasterSql(new Sql($this->getDbAdapter()));
        $this->setSlaveSql(new Sql($this->getSlaveDbAdapter()));
    }

    /**
     * @return int
     */
    public function size()
    {
        $resultSet = new ResultSet();
        $resultSet->initialize($this->performRead($this->getSelect()->columns(array(
            'size' => new SqlExpression('COUNT(*)')
        ))));
        return $resultSet->current()->size;
    }

    /**
     * @param mixed $id
     * @return AggregateRootInterface
     */
    public function getById($id)
    {
        return $this->getBy(array(
            $this->getTableId() => $id
        ));
    }

    /**
     * @return array
     */
    public function getAll()
    {
        return $this->hydrateAggregateRootsFromResult($this->performRead($this->getSelect()));
    }

    /**
     * @param AggregateRootInterface $aggregateRoot
     * @return RepositoryInterface
     */
    public function add(AggregateRootInterface $aggregateRoot)
    {
        $data = $this->getAggregateRootHydrator()->extract($aggregateRoot);
        $insert = $this->getMasterSql()->insert($this->getTableName())->values($data);
        $this->performWrite($insert);
        if ($aggregateRoot->getId() === null) {
            $aggregateRoot->setId($this->getDbAdapter()->getDriver()->getLastGeneratedValue($this->getTableSequenceName()));
        }
        return $this;
    }

    /**<commit_msg>Prefix with table name in Repository::getById()<commit_after><?php
namespace GtnPersistZendDb\Infrastructure\ZendDb;

use GtnPersistBase\Model\AggregateRootInterface;
use GtnPersistBase\Model\RepositoryInterface;
use GtnPersistZendDb\Db\Adapter\MasterSlavesAdapterInterface;
use GtnPersistZendDb\Service\AggregateRootProxyFactoryInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Expression as SqlExpression;
use Zend\Db\Sql\PreparableSqlInterface;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Stdlib\Hydrator\HydratorInterface;

class Repository implements RepositoryInterface
{
    /** @var MasterSlavesAdapterInterface */
    protected $dbAdapter;

    /** @var Sql */
    protected $masterSql;

    /** @var Sql */
    protected $slaveSql;

    /** @var string */
    protected $tableName;

    /** @var mixed */
    protected $tableId;

    /** @var string */
    protected $tableSequenceName;

    /** @var string */
    protected $aggregateRootClass;

    /** @var AggregateRootProxyFactoryInterface */
    protected $aggregateRootProxyFactory;

    /** @var HydratorInterface */
    protected $aggregateRootHydrator;

    /** @var array */
    protected $config;

    /**
     * @param MasterSlavesAdapterInterface $dbAdapter
     */
    public function __construct(MasterSlavesAdapterInterface $dbAdapter)
    {
        $this->setDbAdapter($dbAdapter);
        $this->setMasterSql(new Sql($this->getDbAdapter()));
        $this->setSlaveSql(new Sql($this->getSlaveDbAdapter()));
    }

    /**
     * @return int
     */
    public function size()
    {
        $resultSet = new ResultSet();
        $resultSet->initialize($this->performRead($this->getSelect()->columns(array(
            'size' => new SqlExpression('COUNT(*)')
        ))));
        return $resultSet->current()->size;
    }

    /**
     * @param mixed $id
     * @return AggregateRootInterface
     */
    public function getById($id)
    {
        return $this->getBy(array(
            $this->getTableName() . '.' . $this->getTableId() => $id
        ));
    }

    /**
     * @return array
     */
    public function getAll()
    {
        return $this->hydrateAggregateRootsFromResult($this->performRead($this->getSelect()));
    }

    /**
     * @param AggregateRootInterface $aggregateRoot
     * @return RepositoryInterface
     */
    public function add(AggregateRootInterface $aggregateRoot)
    {
        $data = $this->getAggregateRootHydrator()->extract($aggregateRoot);
        $insert = $this->getMasterSql()->insert($this->getTableName())->values($data);
        $this->performWrite($insert);
        if ($aggregateRoot->getId() === null) {
            $aggregateRoot->setId($this->getDbAdapter()->getDriver()->getLastGeneratedValue($this->getTableSequenceName()));
        }
        return $this;
    }

    /**
{'fraction_non_alphanumeric': 0.1391877820201319, 'fraction_numerical': 0.0, 'mean_word_length': 3.093039772727273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, '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': '4624417', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1683, 'n_words': 282}