<commit_before><?php
/*
 * Avalon
 * Copyright (C) 2011-2012 <NAME>
 *
 * @author <NAME>. <<EMAIL>>
 * @copyright <NAME>.
 * @license New BSD License
 */

/**
 * PDO Database wrapper query builder
 *
 * @package Avalon
 * @subpackage Database
 * @since 0.1
 * @author <NAME>. <<EMAIL>>
 * @copyright Copyright (c) <NAME>.
 */
class PDO_Query
{
	private $type;
	private $cols;
	private $table;
	private $group_by = array();
	private $where = array();
	private $limit;
	private $order_by = array();
	private $custom_sql = array();
	private $set;
	private $_model;
	
	public function __construct($type, $data = null)
	{
		if ($type == 'SELECT') {
			$this->cols = (is_array($data) ? $data : array('*'));
		} else if ($type == 'INSERT INTO') {
			$this->data = $data;
		} else if ($type == 'UPDATE') {
			$this->table = $data;
		}
		
		$this->type = $type;
		$this->prefix = Database::driver()->prefix;
		return $this;
	}
	
	/**
	 * Enable use of the model object for table rows.
	 *
	 * @param string $model The model class.
	 */
	public function _model($model)
	{
		$this->_model = $model;
		return $this;
	}
	
	public function distinct()
	{
		$this->type = $this->type.' DISTINCT';
		return $this;
	}

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

	public function into($table)
	{
		$this->table = $table;
		return $this;
	}
	
	public function set($data)
	{
		$this->data = $data;
		return $this;
	}
	
	public function order_by($col, $dir = 'ASC')
	{
		$this->order_by = array($col, $dir);
		return $this;
	}
	
	public function custom_sql($sql)
	{
		$this->custom_sql[] = $sql;
		return $this;
	}
	
	/**
	 * Easily add a "table = something" to the query.
	 *
	 * @example
	 *    where("count", 5, ">=")
	 *    or
	 *    where(array(array('count', '>=', 5)));
	 *
	 * @param string $columm Column
	 * @param mixed $value Column value
	 * @param string $cond Condintional (=, !=, >=, <=, !=, etc)
	 */
	public function where($columm, $value = null, $cond = '=')
	{
		if (is_array($columm))
		{
			foreach($columm as $where)
			{
				$this->where($where[0], $where[1], $where[2]);
			}
		}
		else
		{
			$this->where[] = array($columm, $cond, $value === null ? 'NULL' : $value);
		}
		
		return $this;
	}
	
	public function limit($from, $to = null)
	{
		$this->limit = implode(',', func_get_args());
		return $this;
	}
  
	public function exec()
	{
		$result = Database::driver()->prepare($this->_assemble());
		
		if ($this->type != 'INSERT')
		{
			foreach ($this->where as $where)
			{
				$result->bind_value(':'. $where[0], $where[2]);
			}
		}
		
		return $result->_model($this->_model)->exec();
	}
	
	private function _assemble()
	{
		$query = array();
		$query[] = $this->type;
		
		if (in_array($this->type, array("SELECT", "SELECT DISTINCT"))) {
			$cols = array();
			foreach ($this->cols as $col => $as)
			{
				if (!is_numeric($col)) {
					$cols[] = "{$col} AS {$as}";
				} else {
					$cols[] = "{$as}";
				}
			}
			$query[] = implode(', ', $cols);
		}
		
		// Select or Delete query
		if (in_array($this->type, array("SELECT", "SELECT DISTINCT", "DELETE")))
		{
			$query[] = "FROM `{$this->prefix}{$this->table}`";
			
			// Where
			$query = array_merge($query, $this->_build_where());
			
			// Group by
			if (count($this->group_by) > 0)
			{
				$query[] = "GROUP BY " . implode(', ', $this->group_by);
			}
			
			// Custom SQL
			if (count($this->custom_sql))
			{
				$query[] = implode(" ", $this->custom_sql);
			}
			
			// Order by
			if (count($this->order_by) > 0)
			{
				$query[] = "ORDER BY `{$this->table}`.`{$this->order_by[0]}` {$this->order_by[1]}";
			}
			<commit_msg>Wrap column names with back ticks in the "SELECT {x} FROM" part of queries.<commit_after><?php
/*
 * Avalon
 * Copyright (C) 2011-2012 <NAME>
 *
 * @author <NAME>. <<EMAIL>>
 * @copyright <NAME>.
 * @license New BSD License
 */

/**
 * PDO Database wrapper query builder
 *
 * @package Avalon
 * @subpackage Database
 * @since 0.1
 * @author <NAME>. <<EMAIL>>
 * @copyright Copyright (c) <NAME>.
 */
class PDO_Query
{
	private $type;
	private $cols;
	private $table;
	private $group_by = array();
	private $where = array();
	private $limit;
	private $order_by = array();
	private $custom_sql = array();
	private $set;
	private $_model;
	
	public function __construct($type, $data = null)
	{
		if ($type == 'SELECT') {
			$this->cols = (is_array($data) ? $data : array('*'));
		} else if ($type == 'INSERT INTO') {
			$this->data = $data;
		} else if ($type == 'UPDATE') {
			$this->table = $data;
		}
		
		$this->type = $type;
		$this->prefix = Database::driver()->prefix;
		return $this;
	}
	
	/**
	 * Enable use of the model object for table rows.
	 *
	 * @param string $model The model class.
	 */
	public function _model($model)
	{
		$this->_model = $model;
		return $this;
	}
	
	public function distinct()
	{
		$this->type = $this->type.' DISTINCT';
		return $this;
	}

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

	public function into($table)
	{
		$this->table = $table;
		return $this;
	}
	
	public function set($data)
	{
		$this->data = $data;
		return $this;
	}
	
	public function order_by($col, $dir = 'ASC')
	{
		$this->order_by = array($col, $dir);
		return $this;
	}
	
	public function custom_sql($sql)
	{
		$this->custom_sql[] = $sql;
		return $this;
	}
	
	/**
	 * Easily add a "table = something" to the query.
	 *
	 * @example
	 *    where("count", 5, ">=")
	 *    or
	 *    where(array(array('count', '>=', 5)));
	 *
	 * @param string $columm Column
	 * @param mixed $value Column value
	 * @param string $cond Condintional (=, !=, >=, <=, !=, etc)
	 */
	public function where($columm, $value = null, $cond = '=')
	{
		if (is_array($columm))
		{
			foreach($columm as $where)
			{
				$this->where($where[0], $where[1], $where[2]);
			}
		}
		else
		{
			$this->where[] = array($columm, $cond, $value === null ? 'NULL' : $value);
		}
		
		return $this;
	}
	
	public function limit($from, $to = null)
	{
		$this->limit = implode(',', func_get_args());
		return $this;
	}
  
	public function exec()
	{
		$result = Database::driver()->prepare($this->_assemble());
		
		if ($this->type != 'INSERT')
		{
			foreach ($this->where as $where)
			{
				$result->bind_value(':'. $where[0], $where[2]);
			}
		}
		
		return $result->_model($this->_model)->exec();
	}
	
	private function _assemble()
	{
		$query = array();
		$query[] = $this->type;
		
		if (in_array($this->type, array("SELECT", "SELECT DISTINCT"))) {
			$cols = array();
			foreach ($this->cols as $col => $as)
			{
				// Check if we're fetching all columns
				if ($col == '*')
				{
					$cols[] = '*';
				}
				// Check if we're fetching a column as an "alias"
				else if (!is_numeric($col))
				{
					$cols[] = "`{$col}` AS `{$as}`";
				}
				// Normal column
				else
				{
					$cols[] = "`{$as}`";
				}
			}
			$query[] = implode(', ', $cols);
		}
		
		// Select or Delete query
		if (in_array($this->type, array("SELECT", "SELECT DISTINCT", "DELETE")))
		{
			$query[] = "FROM `{$this->prefix}{$this->table}`";
			
			// Where
			$query = array_merge($query, $this->_build_where());
			
			// Group by
			if (count($this->group_by) > 0)
			{
				$query[] = "GROUP BY " . implode(', ', $this->group_by);
			}
			
			// Custom SQL
			if (count($this->custom_sql))
			{
				$query[] = implode(" ", $this->custom_sql);
			}
			
			// Order by
			if (count($this->order_by) > 0)
			{
				$query[] = "ORDER BY `{$this->table}`.`{$this->order_by[0]}` {$this->order_by[1]}";
			}
			
{'fraction_non_alphanumeric': 0.20979299363057324, 'fraction_numerical': 0.005573248407643312, 'mean_word_length': 2.9173596673596673, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1421585', 'n_tokens_mistral': 3222, 'n_tokens_neox': 2751, 'n_words': 658}