<commit_before><?php

namespace App\Services;

class EntityService
{
    private $app;
    protected $db;
    protected $route;
    protected $joinedRoute = [];

    public function __construct($app, $db, $route)
    {
        $this->app = $app;
        $this->db = $db;
        $this->route = $route;
    }

    public function getById($id)
    {
        $sql = "SELECT * FROM ".$this->route['tableName']." WHERE ".$this->route['idColumn']." = ?";
        return $this->db->fetchAssoc($sql, array((int) $id));
    }

    public function getByIdWithJoin($id, $join = null)
    {
        $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join)." WHERE ".$this->route['tableName'].".".$this->route['idColumn']." = ?";
        return $this->fetchJoined($this->db->fetchAll($sql, array((int) $id)), $join);
    }

    public function getAll()
    {
        return $this->db->fetchAll("SELECT * FROM ".$this->route['tableName']);
    }

    public function getAllWithJoin($join = null)
    {
        $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join);
        return $this->fetchJoined($this->db->fetchAll($sql), $join);
    }

    public function save($values)
    {
        if (!empty($values)) {
            $this->db->insert($this->route['tableName'], $values);
            return $this->db->lastInsertId();
        }
    }
    
    public function search($criteria, $maxLine = false, $order = false, $group = false)
    {
        if (!empty($criteria)) {
            $sql = "SELECT * FROM ".$this->route['tableName']." WHERE ".$this->prepareSql($criteria).
                $this->group($group).$this->order($order).$this->limit($maxLine);
            return $this->db->fetchAll($sql, $this->array_values_recursive($criteria));
        }
    }

    public function searchWithJoin($criteria, $join = null, $maxLine = false, $order = false, $group = false)
    {
        if (!empty($criteria)) {
            $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join)." WHERE ".$this->prepareSql($criteria).
                $this->group($group).$this->order($order).$this->limit($maxLine);
            return $this->fetchJoined($this->db->fetchAll($sql, array_values_recursive($criteria)), $join);
        }
    }

    public function update($id, $values)
    {<commit_msg>Add limit, order, group request params<commit_after><?php

namespace App\Services;

class EntityService
{
    private $app;
    protected $db;
    protected $route;
    protected $joinedRoute = [];

    public function __construct($app, $db, $route)
    {
        $this->app = $app;
        $this->db = $db;
        $this->route = $route;
    }

    public function getById($id)
    {
        $sql = "SELECT * FROM ".$this->route['tableName']." WHERE ".$this->route['idColumn']." = ?";
        return $this->db->fetchAssoc($sql, array((int) $id));
    }

    public function getByIdWithJoin($id, $join = null)
    {
        $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join)." WHERE ".$this->route['tableName'].".".$this->route['idColumn']." = ?";
        return $this->fetchJoined($this->db->fetchAll($sql, array((int) $id)), $join);
    }

    public function getAll()
    {
        return $this->db->fetchAll("SELECT * FROM ".$this->route['tableName']);
    }

    public function getAllWithJoin($join = null)
    {
        $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join);
        return $this->fetchJoined($this->db->fetchAll($sql), $join);
    }

    public function save($values)
    {
        if (!empty($values)) {
            $this->db->insert($this->route['tableName'], $values);
            return $this->db->lastInsertId();
        }
    }
    
    public function search($criteria, $maxLine = false, $order = false, $group = false)
    {
        if (!empty($criteria)) {
            $sql = "SELECT * FROM ".$this->route['tableName']." WHERE ".$this->prepareSql($criteria).
                $this->group($group).$this->order($order).$this->limit($maxLine);
        } else {
            $sql = "SELECT * FROM ".$this->route['tableName'].
                $this->group($group).$this->order($order).$this->limit($maxLine);
        }
        return $this->db->fetchAll($sql, $this->array_values_recursive($criteria));
    }

    public function searchWithJoin($criteria, $join = null, $maxLine = false, $order = false, $group = false)
    {
        if (!empty($criteria)) {
            $sql = "SELECT ".$this->preprareColumns($join)." FROM ".$this->route['tableName']." ".$this->prepareJoin($join)." WHERE ".$this->prepareSql($criteria).
                $this->group($group).$this->order($order).$this->limit($maxLine);
            return $this->fetchJoined($this->db->fetchAll($sql, array_values_recursive($criteria)), $join);
        }
    }

    public function update($id, $values)
    {
{'fraction_non_alphanumeric': 0.20569057498518079, 'fraction_numerical': 0.0, 'mean_word_length': 2.8117469879518073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, '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': '2472362', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1548, 'n_words': 281}