<commit_before><?php

namespace Graze\Dal\Adapter\Pdo\Persister;

use Aura\Sql\ExtendedPdo;
use Graze\Dal\Configuration\ConfigurationInterface;
use Graze\Dal\Persister\AbstractPersister;
use Graze\Dal\UnitOfWork\UnitOfWorkInterface;

class EntityPersister extends AbstractPersister
{
    /**
     * @var ExtendedPdo
     */
    private $db;

    /**
     * EntityPersister constructor.
     *
     * @param string $entityName
     * @param string $recordName
     * @param UnitOfWorkInterface $unitOfWork
     * @param ConfigurationInterface $config
     * @param ExtendedPdo $db
     */
    public function __construct(
        $entityName,
        $recordName,
        UnitOfWorkInterface $unitOfWork,
        ConfigurationInterface $config,
        ExtendedPdo $db
    ) {
        parent::__construct($entityName, $recordName, $unitOfWork, $config);
        $this->db = $db;
    }

    /**
     * @param array $record
     *
     * @return array
     */
    protected function saveRecord($record)
    {
        if ($this->getRecordId($record)) {
            return $this->update($record);
        } else {
            unset($record['id']);
            return $this->insert($record);
        }
    }

    /**
     * @param array $record
     *
     * @return array
     */
    private function insert($record)
    {
        $cols = implode(',', array_keys($record));
        $vals = array_values($record);

        $stmt = "INSERT INTO `{$this->getRecordName()}` ({$cols}) VALUES (?)";
        $this->db->perform($stmt, [1 => $vals]);
        $record['id'] = $this->db->lastInsertId();

        return $record;
    }

    /**
     * @param array $record
     *
     * @return array
     */
    private function update($record)
    {
        $cols = array_keys($record);
        $set = '';
        foreach ($cols as $col) {
            $set .= $col . '=' . ':' . $col . ',';
        }
        $set = rtrim($set, ',');

        $stmt = $this->db->prepare("UPDATE `{$this->getRecordName()}` SET {$set}");
        foreach ($record as $col => $value) {
            $stmt->bindValue($col, $value);
        }

        $this->db->exec($stmt);

        return $record;
    }

    /**
     * @param array $record
     */
    protected function deleteRecord($record)
    {
        $stmt = $this->db->prepare("DELETE FROM {$this->getRecordName()} WHERE `id` = :id");
        $stmt->bindValue('id', $this->getRecordId($record));

        $this->db->exec($stmt);
    }

    /**
     * @param array $record
     *
     * @return int
     */
    protected function getRecordId($record)
    {
        return array_key_exists('id', $record) ? $record['id'] : null;
    }

    /**
     * @param array $criteria
     * @param object $entity
     * @param array $orderBy
     *
     * @return object
     */
    protected function loadRecord(array $criteria, $entity = null, array $orderBy = null)
    {
        $where = $this->buildWhereClause($criteria);

        $stmt = "SELECT * FROM `{$this->getRecordName()}` WHERE {$where} LIMIT 1";

        return $this->db->fetchOne($stmt, $criteria);
    }

    /**
     * @param array $criteria
     * @param array $orderBy
     * @param int $limit
     * @param int $offset
     *
     * @return array
     */
    protected function loadAllRecords(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $where = $this->buildWhereClause($criteria);

        $stmt = "SELECT * FROM `{$this->getRecordName()}` WHERE {$where}";

        if ($offset) {
            $stmt .= " OFFSET {$offset}";
        }

        if ($limit) {
            $stmt .= " LIMIT {$limit}";
        }

        return $this->db->fetchAll($stmt, $criteria);
    }<commit_msg>Fixing issue with loadAllRecords() with empty criteria<commit_after><?php

namespace Graze\Dal\Adapter\Pdo\Persister;

use Aura\Sql\ExtendedPdo;
use Graze\Dal\Configuration\ConfigurationInterface;
use Graze\Dal\Persister\AbstractPersister;
use Graze\Dal\UnitOfWork\UnitOfWorkInterface;

class EntityPersister extends AbstractPersister
{
    /**
     * @var ExtendedPdo
     */
    private $db;

    /**
     * EntityPersister constructor.
     *
     * @param string $entityName
     * @param string $recordName
     * @param UnitOfWorkInterface $unitOfWork
     * @param ConfigurationInterface $config
     * @param ExtendedPdo $db
     */
    public function __construct(
        $entityName,
        $recordName,
        UnitOfWorkInterface $unitOfWork,
        ConfigurationInterface $config,
        ExtendedPdo $db
    ) {
        parent::__construct($entityName, $recordName, $unitOfWork, $config);
        $this->db = $db;
    }

    /**
     * @param array $record
     *
     * @return array
     */
    protected function saveRecord($record)
    {
        if ($this->getRecordId($record)) {
            return $this->update($record);
        } else {
            unset($record['id']);
            return $this->insert($record);
        }
    }

    /**
     * @param array $record
     *
     * @return array
     */
    private function insert($record)
    {
        $cols = implode(',', array_keys($record));
        $vals = array_values($record);

        $stmt = "INSERT INTO `{$this->getRecordName()}` ({$cols}) VALUES (?)";
        $this->db->perform($stmt, [1 => $vals]);
        $record['id'] = $this->db->lastInsertId();

        return $record;
    }

    /**
     * @param array $record
     *
     * @return array
     */
    private function update($record)
    {
        $cols = array_keys($record);
        $set = '';
        foreach ($cols as $col) {
            $set .= $col . '=' . ':' . $col . ',';
        }
        $set = rtrim($set, ',');

        $stmt = $this->db->prepare("UPDATE `{$this->getRecordName()}` SET {$set}");
        foreach ($record as $col => $value) {
            $stmt->bindValue($col, $value);
        }

        $this->db->exec($stmt);

        return $record;
    }

    /**
     * @param array $record
     */
    protected function deleteRecord($record)
    {
        $stmt = $this->db->prepare("DELETE FROM {$this->getRecordName()} WHERE `id` = :id");
        $stmt->bindValue('id', $this->getRecordId($record));

        $this->db->exec($stmt);
    }

    /**
     * @param array $record
     *
     * @return int
     */
    protected function getRecordId($record)
    {
        return array_key_exists('id', $record) ? $record['id'] : null;
    }

    /**
     * @param array $criteria
     * @param object $entity
     * @param array $orderBy
     *
     * @return object
     */
    protected function loadRecord(array $criteria, $entity = null, array $orderBy = null)
    {
        $where = $this->buildWhereClause($criteria);

        $stmt = "SELECT * FROM `{$this->getRecordName()}` WHERE {$where} LIMIT 1";

        return $this->db->fetchOne($stmt, $criteria);
    }

    /**
     * @param array $criteria
     * @param array $orderBy
     * @param int $limit
     * @param int $offset
     *
     * @return array
     */
    protected function loadAllRecords(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $where = $this->buildWhereClause($criteria);
        $where = $where ? ' WHERE ' . $where : '';

        $stmt = "SELECT * FROM `{$this->getRecordName()}` WHERE {$where}";

        if ($offset) {
            $stmt .= " OFFSET {$offset}";
        }

        if ($limit) {
            $stmt .= " LIMIT {$limit}";
        }

        return $this->db->fetchAll($stmt, $criteria);
    }
{'fraction_non_alphanumeric': 0.16110962923446254, 'fraction_numerical': 0.0005334755934915977, 'mean_word_length': 2.097480380008261, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '5442141', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2277, 'n_words': 486}