<commit_before><?php

namespace OtherCode\Database\Query;

/**
 * Class Query
 * @package OtherCode\Database\Query
 */
class Query
{
    /**
     * Allowed clauses
     * @var array
     */
    protected $statements = array(
        'SELECT',
        'INSERT',
        'UPDATE',
        'DELETE',
        'FROM',
        'WHERE'
    );

    /**
     * Allowed operators
     * @var array
     */
    protected $operators = array(
        '=', '<', '>', '<=', '>=', '<>', '!=',
        'like', 'like binary', 'not like', 'between', 'ilike',
        '&', '|', '^', '<<', '>>',
        'rlike', 'regexp', 'not regexp',
        '~', '~*', '!~', '!~*', 'similar to',
        'not similar to'
    );

    /**
     * Select values
     * @var array
     */
    private $select;

    /**
     * Update values
     * @var array
     */
    private $update;

    /**
     * Delete values
     * @var array
     */
    private $delete;

    /**
     * From values
     * @var array
     */
    private $from;

    /**
     * Where conditions
     * @var array
     */
    private $where;

    /**
     * Groups values
     * @var array
     */
    private $groups;

    /**
     * Order by values
     * @var array
     */
    private $orders;

    /**
     * Limit values
     * @var array
     */
    private $limit;

    /**
     * Add a select clause
     * @param array $columns
     * @return $this
     */
    public function select(Array $columns = array('*'))
    {
        $this->select = $columns;
        return $this;
    }

    /**
     * Add a delete clause
     * @param null $id
     * @return $this
     */
    public function delete($id = null)
    {
        $this->delete = $id;
        return $this;
    }

    /**
     * Add an update clause
     * @param array $values
     * @return $this
     */
    public function update(array $values)
    {
        $this->update = $values;
        return $this;
    }

    /**
     * @param array|string $tables
     * @return $this
     */
    public function from($tables)
    {
        if (is_array($tables)) {
            $this->from = $tables;
        } else {
            $this->from = array($tables);
        }
        return $this;
    }

    /**
     * Add a where clause
     * @param $column
     * @param $operator
     * @param $value
     * @return $this
     */
    public function where($column, $operator, $value)
    {
        $this->where[] = array(
            'column' => $column,
            'operator' => $operator,
            'value' => $value,
        );
        return $this;
    }

    /**
     * Compile each clause of the query to SQL
     * @return string
     * @throws \OtherCode\Database\Exceptions\QueryException
     */
    public function compile()
    {
        $sql = array();

        if (count($this->select) > 0) {
            $sql[] = "SELECT " . implode(',', $this->select);
        }

        if (count($this->update) > 0) {
            $sql[] = "UPDATE " . implode(",", $this->update);
        }

        if ($this->delete) {
            $sql[] = "DELETE ";
        }

        if (count($this->select) > 0) {
            $sql[] = "FROM " . implode(',', $this->from);
        }

        foreach ($this->where as $index => $where) {

            if (!is_string($where['column'])) {
                throw new \OtherCode\Database\Exceptions\QueryException("The column field is not a string in where clause.");
            }

            if (!is_string($where['operator']) || !in_array($where['operator'], $this->operators)) {
                throw new \OtherCode\Database\Exceptions\QueryException("Invalid operator in where clause.");
            }

            if (gettype($where['value']) == 'string' && stripos($where['value'],':') === false) {
                $where['value'] = '"' . $where['value'] . '"';
            }

            $sentence = ($index == 0) ? "WHERE " : "AND ";
            $sql[] = $sentence . implode(" ", $where);

        }

        return implode(" ", $sql);
    }

    /**
     * Return the final sql string
     * @return string
     */<commit_msg>Fixed the error when you don't use the where statement<commit_after><?php

namespace OtherCode\Database\Query;

/**
 * Class Query
 * @package OtherCode\Database\Query
 */
class Query
{
    /**
     * Allowed clauses
     * @var array
     */
    protected $statements = array(
        'SELECT',
        'INSERT',
        'UPDATE',
        'DELETE',
        'FROM',
        'WHERE'
    );

    /**
     * Allowed operators
     * @var array
     */
    protected $operators = array(
        '=', '<', '>', '<=', '>=', '<>', '!=',
        'like', 'like binary', 'not like', 'between', 'ilike',
        '&', '|', '^', '<<', '>>',
        'rlike', 'regexp', 'not regexp',
        '~', '~*', '!~', '!~*', 'similar to',
        'not similar to'
    );

    /**
     * Select values
     * @var array
     */
    private $select;

    /**
     * Update values
     * @var array
     */
    private $update;

    /**
     * Delete values
     * @var array
     */
    private $delete;

    /**
     * From values
     * @var array
     */
    private $from;

    /**
     * Where conditions
     * @var array
     */
    private $where;

    /**
     * Groups values
     * @var array
     */
    private $groups;

    /**
     * Order by values
     * @var array
     */
    private $orders;

    /**
     * Limit values
     * @var array
     */
    private $limit;

    /**
     * Add a select clause
     * @param array $columns
     * @return $this
     */
    public function select(Array $columns = array('*'))
    {
        $this->select = $columns;
        return $this;
    }

    /**
     * Add a delete clause
     * @param null $id
     * @return $this
     */
    public function delete($id = null)
    {
        $this->delete = $id;
        return $this;
    }

    /**
     * Add an update clause
     * @param array $values
     * @return $this
     */
    public function update(array $values)
    {
        $this->update = $values;
        return $this;
    }

    /**
     * @param array|string $tables
     * @return $this
     */
    public function from($tables)
    {
        if (is_array($tables)) {
            $this->from = $tables;
        } else {
            $this->from = array($tables);
        }
        return $this;
    }

    /**
     * Add a where clause
     * @param $column
     * @param $operator
     * @param $value
     * @return $this
     */
    public function where($column, $operator, $value)
    {
        $this->where[] = array(
            'column' => $column,
            'operator' => $operator,
            'value' => $value,
        );
        return $this;
    }

    /**
     * Compile each clause of the query to SQL
     * @return string
     * @throws \OtherCode\Database\Exceptions\QueryException
     */
    public function compile()
    {
        $sql = array();

        if (count($this->select) > 0) {
            $sql[] = "SELECT " . implode(',', $this->select);
        }

        if (count($this->update) > 0) {
            $sql[] = "UPDATE " . implode(",", $this->update);
        }

        if ($this->delete) {
            $sql[] = "DELETE ";
        }

        if (count($this->select) > 0) {
            $sql[] = "FROM " . implode(',', $this->from);
        }

        if (isset($this->where)) {
            foreach ($this->where as $index => $where) {

                if (!is_string($where['column'])) {
                    throw new \OtherCode\Database\Exceptions\QueryException("The column field is not a string in where clause.");
                }

                if (!is_string($where['operator']) || !in_array($where['operator'], $this->operators)) {
                    throw new \OtherCode\Database\Exceptions\QueryException("Invalid operator in where clause.");
                }

                if (gettype($where['value']) == 'string' && stripos($where['value'], ':') === false) {
                    $where['value'] = '"' . $where['value'] . '"';
                }

                $sentence = ($index == 0) ? "WHERE " : "AND ";
                $sql[] = $sentence . implode(" ", $where);

            }
        }

        return implode(" ", $sql);
    }

    /**
     * Return the final sql string
     * @return string
     */
{'fraction_non_alphanumeric': 0.1714354702402511, 'fraction_numerical': 0.0009658336351563443, 'mean_word_length': 1.5240706886045094, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 58, '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': '7088002', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2450, 'n_words': 587}