<commit_before><?php
namespace EngineWorks\DBAL;

/**
 * Pagination
 * @package EngineWorks\DBAL
 */
class Pager
{
    /**
     * This count method is used when another query to retrieve the total records is provided
     */
    const COUNT_METHOD_QUERY = 0;

    /**
     * This count method is used to create a select count(*) with the data query as subquery
     */
    const COUNT_METHOD_SELECT = 1;

    /**
     * This count method is used to retrieve the total records by
     */
    const COUNT_METHOD_RECORDCOUNT = 2;

    /** @var DBAL */
    private $dbal;

    /** @var Recordset|null */
    private $recordset;

    /** @var string SQL to query the data */
    private $queryData;

    /** @var string SQL to query the count */
    private $queryCount;

    /** @var int */
    private $pageSize;

    /**
     * One of COUNT_METHOD_QUERY, COUNT_METHOD_SELECT, COUNT_METHOD_RECORDCOUNT
     * This is set when the object is created,
     * Using its setter for COUNT_METHOD_SELECT, COUNT_METHOD_RECORDCOUNT
     * Using setQueryCount for COUNT_METHOD_QUERY
     *
     * @var int
     */
    private $countMethod;

    /** @var int number of the current page */
    private $page;

    /**
     * If NULL then the value needs to be read from database
     *
     * @var int|null
     */
    private $totalRecords = null;

    /**
     * Instantiate a pager object
     * If the queryCount is not set then it will set the method COUNT_METHOD_SELECT
     * that will query a count(*) using $queryData as a subquery
     *
     * @param DBAL $dbal
     * @param string $queryData The sql sentence to retrieve the data, do not use any LIMIT here
     * @param string $queryCount The sql sentence to retrieve the count of the data
     * @param int $pageSize The page size
     */
    public function __construct(DBAL $dbal, $queryData, $queryCount = '', $pageSize = 20)
    {
        $this->dbal = $dbal;
        $this->queryData = $queryData;
        if ('' !== $queryCount) {
            // this method also calls $this->setCountMethod
            $this->setQueryCount($queryCount);
        } else {
            $this->setCountMethod(self::COUNT_METHOD_SELECT);
        }
        $this->setPageSize($pageSize);
    }

    /**
     * perform the query to get a limited result
     * @param int $requestedPage
     * @return bool
     */
    public function queryPage($requestedPage)
    {
        // clear
        $this->page = 0;
        $this->totalRecords = null;
        $this->recordset = null;
        // request
        $page = (int) min($this->getTotalPages(), max(1, intval($requestedPage)));
        $query = $this->dbal->sqlLimit($this->getQueryData(), $page, $this->getPageSize());
        $recordset = $this->dbal->queryRecordset($query);
        if (! $recordset instanceof Recordset) {
            return false;
        }
        $this->page = $page;
        $this->recordset = $recordset;
        return true;
    }

    /**
     * perform the query to get all the records (without paging)
     * @return bool
     */
    public function queryAll()
    {
        $this->setPageSize($this->getTotalCount());
        return $this->queryPage(1);
    }

    /**
     * The current page number
     * @return int
     */
    public function getPage()
    {
        return $this->page;
    }

    /**
     * The current recordset object
     * @return Recordset|null
     */
    public function getRecordset()
    {
        return $this->recordset;
    }

    /**
     * The SQL to query the data
     * @return string
     */
    public function getQueryData()
    {
        return $this->queryData;
    }

    /**
     * The SQL to query the count of records
     * @return string
     */
    public function getQueryCount()
    {
        return $this->queryCount;
    }

    /**
     * Set the SQL to query the count of records
     * This set the countMethod to COUNT_METHOD_QUERY
     * @param string $query
     */
    protected function setQueryCount($query)
    {
        if (! is_string($query) || empty($query)) {
            throw new \InvalidArgumentException('setQueryCount require a valid string argument');
        }
        $this->queryCount = $query;
        $this->countMethod = self::COUNT_METHOD_QUERY;
    }

    /**
     * Get the page size
     * @return int
     */
    public function getPageSize()
    {
        return $this->pageSize;
    }

    /**
     * Get the total count based on the count method
     * @return int
     */
    public function getTotalCount()
    {
        if (null === $this->totalRecords) {
            if ($this->getCountMethod() === self::COUNT_METHOD_QUERY) {
                $this->totalRecords = $this->getTotalRecordsByQueryCount();
            } elseif ($this->getCountMethod() === self::COUNT_METHOD_SELECT) {
                $this->totalRecords = $this->getTotalRecordsBySelectCount();
            } elseif ($this->getCountMethod() === self::COUNT_METHOD_RECORDCOUNT) {
                $this->totalRecords = $this->getTotalRecordsByRecordCount();
            }<commit_msg>Throw exception if cannot get a method to obtain the total count<commit_after><?php
namespace EngineWorks\DBAL;

/**
 * Pagination
 * @package EngineWorks\DBAL
 */
class Pager
{
    /**
     * This count method is used when another query to retrieve the total records is provided
     */
    const COUNT_METHOD_QUERY = 0;

    /**
     * This count method is used to create a select count(*) with the data query as subquery
     */
    const COUNT_METHOD_SELECT = 1;

    /**
     * This count method is used to retrieve the total records by
     */
    const COUNT_METHOD_RECORDCOUNT = 2;

    /** @var DBAL */
    private $dbal;

    /** @var Recordset|null */
    private $recordset;

    /** @var string SQL to query the data */
    private $queryData;

    /** @var string SQL to query the count */
    private $queryCount;

    /** @var int */
    private $pageSize;

    /**
     * One of COUNT_METHOD_QUERY, COUNT_METHOD_SELECT, COUNT_METHOD_RECORDCOUNT
     * This is set when the object is created,
     * Using its setter for COUNT_METHOD_SELECT, COUNT_METHOD_RECORDCOUNT
     * Using setQueryCount for COUNT_METHOD_QUERY
     *
     * @var int
     */
    private $countMethod;

    /** @var int number of the current page */
    private $page;

    /**
     * If NULL then the value needs to be read from database
     *
     * @var int|null
     */
    private $totalRecords = null;

    /**
     * Instantiate a pager object
     * If the queryCount is not set then it will set the method COUNT_METHOD_SELECT
     * that will query a count(*) using $queryData as a subquery
     *
     * @param DBAL $dbal
     * @param string $queryData The sql sentence to retrieve the data, do not use any LIMIT here
     * @param string $queryCount The sql sentence to retrieve the count of the data
     * @param int $pageSize The page size
     */
    public function __construct(DBAL $dbal, $queryData, $queryCount = '', $pageSize = 20)
    {
        $this->dbal = $dbal;
        $this->queryData = $queryData;
        if ('' !== $queryCount) {
            // this method also calls $this->setCountMethod
            $this->setQueryCount($queryCount);
        } else {
            $this->setCountMethod(self::COUNT_METHOD_SELECT);
        }
        $this->setPageSize($pageSize);
    }

    /**
     * perform the query to get a limited result
     * @param int $requestedPage
     * @return bool
     */
    public function queryPage($requestedPage)
    {
        // clear
        $this->page = 0;
        $this->totalRecords = null;
        $this->recordset = null;
        // request
        $page = (int) min($this->getTotalPages(), max(1, intval($requestedPage)));
        $query = $this->dbal->sqlLimit($this->getQueryData(), $page, $this->getPageSize());
        $recordset = $this->dbal->queryRecordset($query);
        if (! $recordset instanceof Recordset) {
            return false;
        }
        $this->page = $page;
        $this->recordset = $recordset;
        return true;
    }

    /**
     * perform the query to get all the records (without paging)
     * @return bool
     */
    public function queryAll()
    {
        $this->setPageSize($this->getTotalCount());
        return $this->queryPage(1);
    }

    /**
     * The current page number
     * @return int
     */
    public function getPage()
    {
        return $this->page;
    }

    /**
     * The current recordset object
     * @return Recordset|null
     */
    public function getRecordset()
    {
        return $this->recordset;
    }

    /**
     * The SQL to query the data
     * @return string
     */
    public function getQueryData()
    {
        return $this->queryData;
    }

    /**
     * The SQL to query the count of records
     * @return string
     */
    public function getQueryCount()
    {
        return $this->queryCount;
    }

    /**
     * Set the SQL to query the count of records
     * This set the countMethod to COUNT_METHOD_QUERY
     * @param string $query
     */
    protected function setQueryCount($query)
    {
        if (! is_string($query) || empty($query)) {
            throw new \InvalidArgumentException('setQueryCount require a valid string argument');
        }
        $this->queryCount = $query;
        $this->countMethod = self::COUNT_METHOD_QUERY;
    }

    /**
     * Get the page size
     * @return int
     */
    public function getPageSize()
    {
        return $this->pageSize;
    }

    /**
     * Get the total count based on the count method
     * @return int
     */
    public function getTotalCount()
    {
        if (null === $this->totalRecords) {
            if ($this->getCountMethod() === self::COUNT_METHOD_QUERY) {
                $this->totalRecords = $this->getTotalRecordsByQueryCount();
            } elseif ($this->getCountMethod() === self::COUNT_METHOD_SELECT) {
                $this->totalRecords = $this->getTotalRecordsBySelectCount();
            } elseif ($this->getCountMethod() === self::COUNT_METHOD_RECORDCOUNT) {
                $this->totalRecords = $this->getTotalRecordsByRecordCount();
            } else {
                throw new \LogicException('Cannot get a method to obtain the total count');
            }
{'fraction_non_alphanumeric': 0.1127555988315482, 'fraction_numerical': 0.001557935735150925, 'mean_word_length': 2.1266362252663624, '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': '1192743', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2886, 'n_words': 870}