<commit_before><?php

namespace Joseki\LeanMapper;

use LeanMapper\Entity;
use LeanMapper\Repository as LR;
use Nette\Utils\Paginator;

/**
 * @property array $onBeforePersist
 * @property array $onBeforeCreate
 * @property array $onBeforeUpdate
 * @property array $onBeforeDelete
 * @property array $onAfterPersist
 * @property array $onAfterCreate
 * @property array $onAfterUpdate
 * @property array $onAfterDelete
 */
abstract class Repository extends LR
{

    protected function apply(Query $query)
    {
        $fluent = $this->createFluent();
        $query->applyQuery($fluent, $this->mapper);
        return $fluent;
    }



    /**
     * @return Query
     */
    public function createQuery()
    {
        return new Query();
    }



    /**
     * @param Query|string $query
     * @return Entity[]
     * @throws InvalidArgumentException
     */
    public function findBy($query)
    {
        if (func_num_args() > 1) {
            $query = $this->createQuery();
            call_user_func_array(array($query, 'where'), func_get_args());
        }
        if (!$query instanceof Query) {
            if (is_object($query)) {
                $class = get_class($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', instance of '$class' given.");
            } else {
                $type = gettype($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', '$type' given.");
            }
        }

        return $this->createEntities($this->apply($query)->fetchAll());
    }



    /**
     * @param Query|string $query
     * @return Entity
     * @throws InvalidArgumentException
     * @throws NotFoundException
     */
    public function findOneBy($query)
    {
        if (func_num_args() > 1) {
            $query = $this->createQuery();
            call_user_func_array(array($query, 'where'), func_get_args());
        }
        if (!$query instanceof Query) {
            if (is_object($query)) {
                $class = get_class($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', instance of '$class' given.");
            } else {
                $type = gettype($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', '$type' given.");
            }
        }
        $row = $this->apply($query)
            ->removeClause('limit')
            ->removeClause('offset')
            ->fetch();
        if ($row === false) {
            throw new NotFoundException('Entity not found.');
        }
        return $this->createEntity($row);
    }



    /**
     * @param int|null $limit
     * @param int|null $offset
     * @return Entity[]
     */
    public function findAll($limit = null, $offset = null)
    {
        $query = $this->createQuery();
        if ($limit) {
            $query->limit($limit);
        }
        if ($offset) {
            $query->offset($offset);
        }
        return $this->findBy($query);
    }



    /**
     * @param Query $query
     * @return int
     */
    public function findCountBy(Query $query)
    {
        return $this->apply($query)->count();
    }



    /**
     * @param Query $query
     * @param $page
     * @param $itemsPerPage
     * @return int
     */
    public function findPageBy(Query $query = null, $page, $itemsPerPage)
    {
        if ($query === null) {
            $query = new Query();
        }
        $paginator = new Paginator();
        $paginator->itemCount = $this->findCountBy($query);
        $paginator->itemsPerPage = $itemsPerPage;
        $paginator->page = $page;
        $query->limit($itemsPerPage)->offset($paginator->offset);
        return $this->findBy($query);
    }



    /**
     * @param $id
     * @return Entity
     * @throws NotFoundException
     */
    public function get($id)
    {
        $PK = $this->mapper->getPrimaryKey($this->getTable());
        $query = $this->createQuery()->where("@$PK", $id);
        return $this->findOneBy($query);
    }
}<commit_msg>Repository: added support for transactions via callback<commit_after><?php

namespace Joseki\LeanMapper;

use LeanMapper\Entity;
use LeanMapper\Repository as LR;
use Nette\Utils\Callback;
use Nette\Utils\Paginator;

/**
 * @property array $onBeforePersist
 * @property array $onBeforeCreate
 * @property array $onBeforeUpdate
 * @property array $onBeforeDelete
 * @property array $onAfterPersist
 * @property array $onAfterCreate
 * @property array $onAfterUpdate
 * @property array $onAfterDelete
 */
abstract class Repository extends LR
{

    protected function apply(Query $query)
    {
        $fluent = $this->createFluent();
        $query->applyQuery($fluent, $this->mapper);
        return $fluent;
    }



    /**
     * @return Query
     */
    public function createQuery()
    {
        return new Query();
    }



    /**
     * @param Query|string $query
     * @return Entity[]
     * @throws InvalidArgumentException
     */
    public function findBy($query)
    {
        if (func_num_args() > 1) {
            $query = $this->createQuery();
            call_user_func_array(array($query, 'where'), func_get_args());
        }
        if (!$query instanceof Query) {
            if (is_object($query)) {
                $class = get_class($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', instance of '$class' given.");
            } else {
                $type = gettype($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', '$type' given.");
            }
        }

        return $this->createEntities($this->apply($query)->fetchAll());
    }



    /**
     * @param Query|string $query
     * @return Entity
     * @throws InvalidArgumentException
     * @throws NotFoundException
     */
    public function findOneBy($query)
    {
        if (func_num_args() > 1) {
            $query = $this->createQuery();
            call_user_func_array(array($query, 'where'), func_get_args());
        }
        if (!$query instanceof Query) {
            if (is_object($query)) {
                $class = get_class($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', instance of '$class' given.");
            } else {
                $type = gettype($query);
                throw new InvalidArgumentException("Exptected instance of '\\Joseki\\LeanMapper\\Query', '$type' given.");
            }
        }
        $row = $this->apply($query)
            ->removeClause('limit')
            ->removeClause('offset')
            ->fetch();
        if ($row === false) {
            throw new NotFoundException('Entity not found.');
        }
        return $this->createEntity($row);
    }



    /**
     * @param int|null $limit
     * @param int|null $offset
     * @return Entity[]
     */
    public function findAll($limit = null, $offset = null)
    {
        $query = $this->createQuery();
        if ($limit) {
            $query->limit($limit);
        }
        if ($offset) {
            $query->offset($offset);
        }
        return $this->findBy($query);
    }



    /**
     * @param Query $query
     * @return int
     */
    public function findCountBy(Query $query)
    {
        return $this->apply($query)->count();
    }



    /**
     * @param Query $query
     * @param $page
     * @param $itemsPerPage
     * @return int
     */
    public function findPageBy(Query $query = null, $page, $itemsPerPage)
    {
        if ($query === null) {
            $query = new Query();
        }
        $paginator = new Paginator();
        $paginator->itemCount = $this->findCountBy($query);
        $paginator->itemsPerPage = $itemsPerPage;
        $paginator->page = $page;
        $query->limit($itemsPerPage)->offset($paginator->offset);
        return $this->findBy($query);
    }



    /**
     * @param $id
     * @return Entity
     * @throws NotFoundException
     */
    public function get($id)
    {
        $PK = $this->mapper->getPrimaryKey($this->getTable());
        $query = $this->createQuery()->where("@$PK", $id);
        return $this->findOneBy($query);
    }



    public function inTransaction($callback, array $args = [])
    {
        Callback::check($callback);
        $this->connection->begin();
        try {
            $result = Callback::invokeArgs($callback, $args);
        } catch (\Exception $e) {
            $this->connection->rollback();
            throw $e;
        }
        $this->connection->commit();

        return $result;
    }
}
{'fraction_non_alphanumeric': 0.14532160402147834, 'fraction_numerical': 0.0004569861761681709, 'mean_word_length': 2.05976931142957, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '7470023', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2437, 'n_words': 540}