<commit_before><?php

namespace Library\Persistence;

class MySqlPersistenceManager implements \Library\Persistence\IPersistenceManager
{

    private $_statementsToCommit;
    private $_host;
    private $_user;
    private $_password;
    private $_mappers;
    private $_connection;

    public function __construct($host, $user, $password, \Library\Persistence\IMapperDictionary $mappers)
    {
        $this->_host = $host;
        $this->_user = $user;
        $this->_password = $password;
        $this->_mappers = $mappers;
        $this->_statementsToCommit = array();
    }

    public function Add($objectToAdd)
    {
        $mapper = $this->_mappers->GetMapper(new \ReflectionClass($objectToAdd));

        array_push($this->_statementsToCommit, $mapper->GetAddQueries($objectToAdd));
    }

    public function Change($objectToChange)
    {
        $mapper = $this->_mappers->GetMapper(new \ReflectionClass($objectToChange));

        array_push($this->_statementsToCommit, $mapper->GetChangeQueries($objectToChange));
    }

    public function Commit()
    {
        try
        {
            $this->GetConnection()->autocommit(FALSE);

            foreach ($this->_statementsToCommit as $statement)
            {
                $this->GetConnection()->query($statement);
            }

            $this->GetConnection()->commit();
            $this->GetConnection()->autocommit(TRUE);
        }
        catch (Exception $e)
        {
            $this->GetConnection()->rollback();
            $this->GetConnection()->autocommit(TRUE);
        }
    }

    public function Get(IPersistenceSearcher $search)
    {
        $mapper = $this->_mappers->GetMapper($search->TypeToSearch());

        $query = $this->GetConnection()->prepare('CALL ' . $mapper->GetFindQuery($search));

        $query->execute();

        $query->store_result();

        $result = $query->get_result();
        while ($row = $result->fetch_array(MYSQLI_NUM))
        {
            $mappedObject = $mapper->MapObject($row);
        }

        $query->free_result();

        $query->close();

        return $mappedObject;
    }

    public function GetCollection(IPersistenceSearcher $search)
    {
        $mapper = $this->_mappers->GetMapper($search->TypeToSearch());

        $query = $this->GetConnection()->prepare('CALL ' . $mapper->GetFindQuery($search));

        if (!$query->execute())
        {
            throw new \Exception($query->error, $query->errno);
        }

        $query->store_result();

        $mappedObjects = array();

        $results = $this->BindResults($query);
        foreach ($results as $row)
        {
            $mappedObject = $mapper->MapObject($row);

            array_push($mappedObjects, $mappedObject);
        }

        $query->free_result();

        $query->close();

        return $mappedObjects;
    }

    private function GetConnection()
    {
        if ($this->_connection == null)
        {
            $this->_connection = new \mysqli($this->_host, $this->_user, $this->_password, '<PASSWORD>');
        }

        return $this->_connection;
    }
}
?><commit_msg>Using PDO for MySQL access.<commit_after><?php

namespace Library\Persistence;

class MySqlPersistenceManager implements \Library\Persistence\IPersistenceManager
{
    private $_statementsToCommit;
    private $_host;
    private $_user;
    private $_password;
    private $_mappers;
    private $_connection;

    public function __construct($host, $user, $password, \Library\Persistence\IMapperDictionary $mappers)
    {
        $this->_host = $host;
        $this->_user = $user;
        $this->_password = $password;
        $this->_mappers = $mappers;
        $this->_statementsToCommit = array();
    }

    public function Add($objectToAdd)
    {
        $mapper = $this->_mappers->GetMapper(new \ReflectionClass($objectToAdd));

        array_push($this->_statementsToCommit, $mapper->GetAddQueries($objectToAdd));
    }

    public function Change($objectToChange)
    {
        $mapper = $this->_mappers->GetMapper(new \ReflectionClass($objectToChange));

        array_push($this->_statementsToCommit, $mapper->GetChangeQueries($objectToChange));
    }

    public function Commit()
    {
        try
        {
            $this->GetConnection()->autocommit(FALSE);

            foreach ($this->_statementsToCommit as $statement)
            {
                $this->GetConnection()->query($statement);
            }

            $this->GetConnection()->commit();
            $this->GetConnection()->autocommit(TRUE);
        }
        catch (Exception $e)
        {
            $this->GetConnection()->rollback();
            $this->GetConnection()->autocommit(TRUE);
        }
    }

    public function Get(IPersistenceSearcher $search)
    {        
        $mapper = $this->_mappers->GetMapper($search->TypeToSearch());

        $query = $this->GetConnection()->query($mapper->GetFindQuery($search) . ';');
        $query->setFetchMode(\PDO::FETCH_OBJ);
                
        while ($row = $query->fetch())
        {
            return $mapper->MapObject($row);
        }

        return null;
    }

    public function GetCollection(IPersistenceSearcher $search)
    {
        $mapper = $this->_mappers->GetMapper($search->TypeToSearch());

        $query = $this->GetConnection()->query($mapper->GetFindQuery($search) . ';');
        $query->setFetchMode(\PDO::FETCH_OBJ);

        $mappedObjects = array();

        while($row = $query->fetch())
        {
            $mappedObject = $mapper->MapObject($row);

            array_push($mappedObjects, $mappedObject);
        }

        return $mappedObjects;
    }

    private function GetConnection()
    {
        if ($this->_connection == null)
        {
            $this->_connection = new \PDO("mysql:host=$this->_host;dbname=fet10009689", $this->_user, $this->_password);  
        }

        return $this->_connection;
    }
}
?>
{'fraction_non_alphanumeric': 0.14834794335805798, 'fraction_numerical': 0.0013486176668914363, 'mean_word_length': 2.3922241280731846, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 115, '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': '5928448', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1652, 'n_words': 258}