<commit_before><?php

namespace Digilist\SnakeDumper\Dumper\Sql;

use Digilist\SnakeDumper\Configuration\Table\Filter\DataDependentFilter;
use Digilist\SnakeDumper\Configuration\Table\Filter\DefaultFilter;
use Digilist\SnakeDumper\Configuration\Table\Filter\FilterInterface;
use Digilist\SnakeDumper\Configuration\Table\TableConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Table;
use InvalidArgumentException;

/**
 * This class helps to query the appropriate data that should be dumped.
 */
class DataSelector
{

    /**
     * @var Connection
     */
    private $connection;

    /**
     * @param Connection $connection
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Executes the generated sql statement.
     *
     * @param TableConfiguration $tableConfig
     * @param Table              $table
     * @param array              $harvestedValues
     *
     * @return \Doctrine\DBAL\Driver\Statement
     * @throws \Doctrine\DBAL\DBALException
     */
    public function executeSelectQuery(TableConfiguration $tableConfig, Table $table, array $harvestedValues)
    {
        list($query, $parameters) = $this->buildSelectQuery($tableConfig, $table, $harvestedValues);

        $result = $this->connection->prepare($query);
        $result->execute($parameters);

        return $result;
    }

    /**
     * Count the number of rows for the generated select statement.
     *
     * @param TableConfiguration $tableConfig
     * @param Table              $table
     * @param array              $harvestedValues
     *
     * @return int
     */
    public function countRows(TableConfiguration $tableConfig, Table $table, array $harvestedValues)
    {
        list($query, $parameters) = $this->buildSelectQuery($tableConfig, $table, $harvestedValues);

        $query = sprintf('SELECT COUNT(*) FROM (%s) AS tmp', $query);

        $result = $this->connection->prepare($query);
        $result->execute($parameters);<commit_msg>Make the row counting less error-prone<commit_after><?php

namespace Digilist\SnakeDumper\Dumper\Sql;

use Digilist\SnakeDumper\Configuration\Table\Filter\DataDependentFilter;
use Digilist\SnakeDumper\Configuration\Table\Filter\DefaultFilter;
use Digilist\SnakeDumper\Configuration\Table\Filter\FilterInterface;
use Digilist\SnakeDumper\Configuration\Table\TableConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Table;
use InvalidArgumentException;

/**
 * This class helps to query the appropriate data that should be dumped.
 */
class DataSelector
{

    /**
     * @var Connection
     */
    private $connection;

    /**
     * @param Connection $connection
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Executes the generated sql statement.
     *
     * @param TableConfiguration $tableConfig
     * @param Table              $table
     * @param array              $harvestedValues
     *
     * @return \Doctrine\DBAL\Driver\Statement
     * @throws \Doctrine\DBAL\DBALException
     */
    public function executeSelectQuery(TableConfiguration $tableConfig, Table $table, array $harvestedValues)
    {
        list($query, $parameters) = $this->buildSelectQuery($tableConfig, $table, $harvestedValues);

        $result = $this->connection->prepare($query);
        $result->execute($parameters);

        return $result;
    }

    /**
     * Count the number of rows for the generated select statement.
     *
     * @param TableConfiguration $tableConfig
     * @param Table              $table
     * @param array              $harvestedValues
     *
     * @return int
     */
    public function countRows(TableConfiguration $tableConfig, Table $table, array $harvestedValues)
    {
        list($query, $parameters) = $this->buildSelectQuery($tableConfig, $table, $harvestedValues);

        // Remove everything before the first FROM, to replace it with a SELECT 1
        $query = 'SELECT 1 ' . substr($query, stripos($query, 'FROM'));

        // The actual select is wrapped in a subquery, to consider groupings, limits etc.
        // We only want to get the number of rows that will be dumped later.
        $query = sprintf('SELECT COUNT(*) FROM (%s) AS tmp', $query);

        $result = $this->connection->prepare($query);
        $result->execute($parameters);
{'fraction_non_alphanumeric': 0.10941306755260244, 'fraction_numerical': 0.00044296788482834997, 'mean_word_length': 2.9648814749780508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '4915687', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1224, 'n_words': 304}