<commit_before><?php
namespace Pym;

use Doctrine\DBAL\Connection;

class Table
{
    protected $db = null;
    protected $tableName;
    protected $tableAlias;
    protected $table;
    protected $isTimestampable = false;
    protected $isSoftdeletable = false;
    protected $queryParams = array();

    protected $select = '';
    protected $wheres = array();
    protected $leftJoin = '';
    protected $groupBy = '';
    protected $orderBy = '';

    public function __construct(Connection $db, $tableName, $tableAlias = null)
    {
        $this->db = $db;
        $this->tableName = $tableName;
        $this->tableAlias = $tableAlias;
        $this->table = $this->tableAlias !== null ? $this->tableAlias : '`'.$this->tableName.'`';
        $tableColumns = $db->executeQuery("DESCRIBE $tableName")->fetchAll(\PDO::FETCH_COLUMN);
        $this->isTimestampable = count(array_intersect(['created_at', 'updated_at'], $tableColumns)) == 2;
        $this->isSoftdeletable = in_array('deleted_at', $tableColumns);
    }

    protected function getQuery()
    {
        if (empty($this->select)) $this->select();

        if ($this->isSoftdeletable) {
            $this->where([$this->table.'.deleted_at' => null]);
        }

        return $this->buildSQL();
    }

    protected function escapeColumns(array $columns) {
        array_walk($columns, function(&$value) {
            if (preg_match('/^(\w+)\((\*|\w+)\.?(\w+)?\)$/i', $value, $matches)) {
                if ($matches[2] != '*') {
                    $_matches[] = [];
                    for ($i=1; $i <= 3; $i++) {
                        $_matches[$i] = '`'.$matches[$i].'`';
                    }
                    $column = isset($matches[3]) ? implode('.', array_slice($_matches, 2)) : $_matches[2];
                    $value = sprintf('%s(%s) AS %s', $matches[1], $column, strtolower($matches[2].'_'.$matches[1]));
                }
            } elseif (preg_match('/^(\w+)\.(\w+)(?:\sAS\s)?(.+)?$/i', $value, $matches)) {
                $table = $matches[1] == $this->tableAlias ? $matches[1] : '`'.$matches[1].'`';
                $alias = isset($matches[3]) ? ' AS ' . $matches[3] : ' ';
                $value = sprintf('%s.`%s`%s', $table, $matches[2], $alias);
            } else {
                $value = '`'.$value.'`';
            }
        });

        return $columns;
    }

    protected function cleanData(array $data) {
        $keys = array_keys($data);
        array_walk($keys, function(&$value) {
            $value = '`'.$value.'`';
        });
<commit_msg>Add: tablesAliases param<commit_after><?php
namespace Pym;

use Doctrine\DBAL\Connection;

class Table
{
    protected $db = null;
    protected $tableName;
    protected $tableAlias;
    protected $table;
    protected $isTimestampable = false;
    protected $isSoftdeletable = false;
    protected $queryParams = array();

    protected $select = '';
    protected $wheres = array();
    protected $leftJoin = '';
    protected $groupBy = '';
    protected $orderBy = '';

    public function __construct(Connection $db, $tableName, $tableAlias = null, $tablesAliases = null)
    {
        $this->db = $db;
        $this->tableName = $tableName;
        $this->tableAlias = $tableAlias;
        $this->tablesAliases = $tablesAliases;
        $this->table = $this->tableAlias !== null ? $this->tableAlias : '`'.$this->tableName.'`';
        $tableColumns = $db->executeQuery("DESCRIBE $tableName")->fetchAll(\PDO::FETCH_COLUMN);
        $this->isTimestampable = count(array_intersect(['created_at', 'updated_at'], $tableColumns)) == 2;
        $this->isSoftdeletable = in_array('deleted_at', $tableColumns);
    }

    protected function getQuery()
    {
        if (empty($this->select)) $this->select();

        if ($this->isSoftdeletable) {
            $this->where([$this->table.'.deleted_at' => null]);
        }

        return $this->buildSQL();
    }

    protected function escapeColumns(array $columns) {
        array_walk($columns, function(&$value) {
            if (preg_match('/^(\w+)\((\*|\w+)\.?(\w+)?\)$/i', $value, $matches)) {
                if ($matches[2] != '*') {
                    $_matches[] = [];
                    for ($i=1; $i <= 3; $i++) {
                        $_matches[$i] = '`'.$matches[$i].'`';
                    }
                    $column = isset($matches[3]) ? implode('.', array_slice($_matches, 2)) : $_matches[2];
                    $value = sprintf('%s(%s) AS %s', $matches[1], $column, strtolower($matches[2].'_'.$matches[1]));
                }
            } elseif (preg_match('/^(\w+)\.(\w+|\*{1})(?:\sAS\s)?(.+)?$/i', $value, $matches)) {
                $table = in_array($matches[1], $this->tablesAliases) ? $matches[1] : '`'.$matches[1].'`';
                $alias = isset($matches[3]) ? ' AS ' . $matches[3] : ' ';
                $value = sprintf('%s.%s%s', $table, $matches[2] === '*' ? '*' : "`$matches[2]`", $alias);
            } else {
                $value = '`'.$value.'`';
            }
        });

        return $columns;
    }

    protected function cleanData(array $data) {
        $keys = array_keys($data);
        array_walk($keys, function(&$value) {
            $value = '`'.$value.'`';
        });

{'fraction_non_alphanumeric': 0.2041205646699733, 'fraction_numerical': 0.0064860740175505536, 'mean_word_length': 2.2364197530864196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 42, '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': '303783', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1647, 'n_words': 293}