<commit_before><?php

namespace Yeah\Fw\Db;

/**
 * Class for creating query objects used in a simple CRUD
 * 
 * @author <NAME>
 */
class PdoQuery {

    private $parts = array();

    /**
     * Adds SELECT portion of the query
     * 
     * @param string $fields Fields to be selected
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function Select($fields) {
        $this->parts[0] = 'select ' . $fields;
        return $this;
    }

    /**
     * Adds FROM portion of the query
     * 
     * @param string $tables List of tables for FROM statement
     */
    public function From($tables) {
        $this->parts[1] = ' from ' . $tables;
        return $this;
    }

    /**
     * Adds INNER JOIN portion of the query
     * 
     * @param string $table Table to join with
     * @param string $condition Join condition
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function InnerJoin($table, $condition) {
        $this->parts[2][] = ' inner join ' . $table . ' on ' . $condition;
        return $this;
    }

    /**
     * Adds conditional part of the query
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function Where($field, $param = null) {
        $part = ' where ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Appends conditional statements to already existing WHERE portion of the 
     * query with AND operator
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function AndWhere($field, $param) {
        $part = ' and ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Appends conditional statements to already existing WHERE portion of the 
     * query with OR operator
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function OrWhere($field, $param) {
        $part = ' or ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Requrns all query parts
     * 
     * @return mixed
     */
    public function getParts() {
        return $this->parts;
    }

    /**
     * Fetches assembles query
     * 
     * @return string
     */
    public function getSql() {
        return $this->buildQueryString($this->getParts());
    }

    /**
     * Runs assembly on query parts
     * 
     * @param mixed $parts
     * @return string
     */
    private function buildQueryString($parts) {
        $sql = '';
        foreach($parts as $part) {
            if(is_array($part)) {
                $sql .= $this->buildQueryString($part);
            } else {
                $sql .= $part;
            }
        }
        return $sql;
    }

    /**
     * Executes final query
     * 
     * @return mixed
     */
    public function execute() {
        $db = PdoConnection::getInstance();
        $stmt = $db->prepare($this->getSql());
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

}
<commit_msg>Added array support for table and field list in PdoQuery Added fallback to AndWhere and OrWhere when no previous conditional parts are present<commit_after><?php

namespace Yeah\Fw\Db;

/**
 * Class for creating query objects used in a simple CRUD
 * 
 * @author <NAME>
 */
class PdoQuery {

    private $parts = array();

    /**
     * @return string
     */
    public function combineArray($array) {
        return implode(',', $array);
    }
    
    /**
     * Adds SELECT portion of the query
     * 
     * @param string $fields Fields to be selected
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function Select($fields) {
        $this->parts[0] = 'select ' . $this->combineArray($fields);
        return $this;
    }

    /**
     * Adds FROM portion of the query
     * 
     * @param string $tables List of tables for FROM statement
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function From($tables) {
        $this->parts[1] = ' from ' . $this->combineArray($tables);
        return $this;
    }

    /**
     * Adds INNER JOIN portion of the query
     * 
     * @param string $table Table to join with
     * @param string $condition Join condition
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function InnerJoin($table, $condition) {
        $this->parts[2][] = ' inner join ' . $table . ' on ' . $condition;
        return $this;
    }

    /**
     * Adds conditional part of the query
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function Where($field, $param = null) {
        $part = ' where ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Appends conditional statements to already existing WHERE portion of the 
     * query with AND operator
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function AndWhere($field, $param) {
        if(!isset($this->parts[3])) {
            return $this->Where($field);
        }
        $part = ' and ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Appends conditional statements to already existing WHERE portion of the 
     * query with OR operator
     * 
     * @param string $field Field to consider
     * @param string $param Value to consider
     * @return \Yeah\Fw\Db\PdoQuery
     */
    public function OrWhere($field, $param) {
        if(!isset($this->parts[3])) {
            return $this->Where($field, $param);
        }
        $part = ' or ' . $field;
        if(isset($param)) {
            if(is_array($param)) {
                $part .= ' in (' . implode(',', $param) . ')';
            } else {
                $part .= '=' . $param;
            }
        }
        $this->parts[3][] = $part;
        return $this;
    }

    /**
     * Requrns all query parts
     * 
     * @return mixed
     */
    public function getParts() {
        return $this->parts;
    }

    /**
     * Fetches assembles query
     * 
     * @return string
     */
    public function getSql() {
        return $this->buildQueryString($this->getParts());
    }

    /**
     * Runs assembly on query parts
     * 
     * @param mixed $parts
     * @return string
     */
    private function buildQueryString($parts) {
        $sql = '';
        foreach($parts as $part) {
            if(is_array($part)) {
                $sql .= $this->buildQueryString($part);
            } else {
                $sql .= $part;
            }
        }
        return $sql;
    }

    /**
     * Executes final query
     * 
     * @return mixed
     */
    public function execute() {
        $db = new PdoConnection(array());
        $stmt = $db->prepare($this->getSql());
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

}

{'fraction_non_alphanumeric': 0.14257860102364123, 'fraction_numerical': 0.0017060687301974165, 'mean_word_length': 1.6078805211312361, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '6503145', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2417, 'n_words': 634}