<commit_before><?php

namespace SypherLev\Blueprint\Elements;

use SypherLev\Blueprint\QueryBuilders\QueryInterface;

class Pattern
{
    private $table;
    private $columns;
    private $joins = [];
    private $group;
    private $aggregates = [];

    public function table($tableName) {
        $this->table = $tableName;
        return $this;
    }

    public function columns($columns) {
        $this->columns = $columns;
        return $this;
    }

    public function join($firsttable, $secondtable, Array $on, $type = 'inner') {
        $this->joins[] = array(
            'firsttable' => $firsttable,
            'secondtable' => $secondtable,
            'on' => $on,
            'type' => $type
        );
        return $this;
    }

    public function groupBy($columnname_or_columnarray) {
        $this->group = $columnname_or_columnarray;
        return $this;
    }

    public function aggregate($function, $columnName_or_columnArray, $alias = false) {
        $this->aggregates = array(
            'function' => $function,
            'columns' => $columnName_or_columnArray,
            'alias' => $alias
        );
    }

    public function setQueryParams(QueryInterface $query) {
        $query->setTable($this->table);
        foreach ($this->joins as $join) {
            $query->setJoin($join['firsttable'], $join['secondtable'], $join['on'], $join['type']);
        }
        foreach ($this->aggregates as $agg) {
            $query->setAggregate($agg['function'], $agg['columns'], $agg['alias']);
        }
        $query->setColumns($this->columns);
        $query->setGroupBy($this->group);
        return $query;
    }
}<commit_msg>Added grouping and aggregate functions<commit_after><?php

namespace SypherLev\Blueprint\Elements;

use SypherLev\Blueprint\QueryBuilders\QueryInterface;

class Pattern
{
    private $table;
    private $columns;
    private $joins = [];
    private $group = null;
    private $aggregates = [];

    public function table($tableName) {
        $this->table = $tableName;
        return $this;
    }

    public function columns($columns) {
        $this->columns = $columns;
        return $this;
    }

    public function join($firsttable, $secondtable, Array $on, $type = 'inner') {
        $this->joins[] = array(
            'firsttable' => $firsttable,
            'secondtable' => $secondtable,
            'on' => $on,
            'type' => $type
        );
        return $this;
    }

    public function groupBy($columnname_or_columnarray) {
        if (!is_array($columnname_or_columnarray)) {
            $columnname_or_columnarray = [$columnname_or_columnarray];
        }
        $this->group = $columnname_or_columnarray;
        return $this;
    }

    public function aggregate($function, $columnName_or_columnArray, $alias = false) {
        $this->aggregates[] = array(
            'function' => $function,
            'columns' => $columnName_or_columnArray,
            'alias' => $alias
        );
        return $this;
    }

    public function setQueryParams(QueryInterface $query) {
        $query->setTable($this->table);
        foreach ($this->joins as $join) {
            $query->setJoin($join['firsttable'], $join['secondtable'], $join['on'], $join['type']);
        }
        foreach ($this->aggregates as $agg) {
            $query->setAggregate(strtoupper($agg['function']), $agg['columns'], $agg['alias']);
        }
        $query->setColumns($this->columns);
        if(!is_null($this->group)) {
            $query->setGroupBy($this->group);
        }
        return $query;
    }
}
{'fraction_non_alphanumeric': 0.14954051796157058, 'fraction_numerical': 0.0, 'mean_word_length': 2.4015151515151514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '4859110', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1044, 'n_words': 204}