| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 | <?php namespace System\DB;use System\Str;use System\Config;use System\Paginator;class Query {	/**	 * The database connection.	 *	 * @var Connection	 */	public $connection;	/**	 * The SELECT clause.	 *	 * @var string	 */	public $select;	/**	 * Indicates if the query should return distinct results.	 *	 * @var bool	 */	public $distinct = false;	/**	 * The FROM clause.	 *	 * @var string	 */	public $from;	/**	 * The table name.	 *	 * @var string	 */	public $table;	/**	 * The WHERE clause.	 *	 * @var string	 */	public $where = 'WHERE 1 = 1';	/**	 * The ORDER BY columns.	 *	 * @var array	 */	public $orderings = array();	/**	 * The LIMIT value.	 *	 * @var int	 */	public $limit;	/**	 * The OFFSET value.	 *	 * @var int	 */	public $offset;	/**	 * The query value bindings.	 *	 * @var array	 */	public $bindings = array();	/**	 * Create a new query instance.	 *	 * @param  string      $table	 * @param  Connection  $connection	 * @return void	 */	public function __construct($table, $connection)	{		$this->table = $table;		$this->connection = $connection;		$this->from = 'FROM '.$this->wrap($table);	}	/**	 * Create a new query instance.	 *	 * @param  string      $table	 * @param  Connection  $connection	 * @return Query	 */	public static function table($table, $connection)	{		return new static($table, $connection);	}	/**	 * Force the query to return distinct results.	 *	 * @return Query	 */	public function distinct()	{		$this->distinct = true;		return $this;	}	/**	 * Add columns to the SELECT clause.	 *	 * @param  array  $columns	 * @return Query	 */	public function select($columns = array('*'))	{		$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';		$this->select .= implode(', ', array_map(array($this, 'wrap'), $columns));		return $this;	}	/**	 * Set the FROM clause.	 *	 * @param  string  $from	 * @return Query	 */	public function from($from)	{		$this->from = $from;		return $this;	}	/**	 * Add a join to the query.	 *	 * @param  string  $table	 * @param  string  $column1	 * @param  string  $operator	 * @param  string  $column2	 * @param  string  $type	 * @return Query	 */	public function join($table, $column1, $operator, $column2, $type = 'INNER')	{		$this->from .= ' '.$type.' JOIN '.$this->wrap($table).' ON '.$this->wrap($column1).' '.$operator.' '.$this->wrap($column2);		return $this;	}	/**	 * Add a left join to the query.	 *	 * @param  string  $table	 * @param  string  $column1	 * @param  string  $operator	 * @param  string  $column2	 * @return Query	 */	public function left_join($table, $column1, $operator, $column2)	{		return $this->join($table, $column1, $operator, $column2, 'LEFT');	}	/**	 * Reset the where clause to its initial state. All bindings will be cleared.	 *	 * @return void	 */	public function reset_where()	{		$this->where = 'WHERE 1 = 1';		$this->bindings = array();	}	/**	 * Add a raw where condition to the query.	 *	 * @param  string  $where	 * @param  array   $bindings	 * @param  string  $connector	 * @return Query	 */	public function raw_where($where, $bindings = array(), $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$where;		$this->bindings = array_merge($this->bindings, $bindings);		return $this;	}	/**	 * Add a raw or where condition to the query.	 *	 * @param  string  $where	 * @param  array   $bindings	 * @return Query	 */	public function raw_or_where($where, $bindings = array())	{		return $this->raw_where($where, $bindings, 'OR');	}	/**	 * Add a where condition to the query.	 *	 * @param  string  $column	 * @param  string  $operator	 * @param  mixed   $value	 * @param  string  $connector	 * @return Query	 */	public function where($column, $operator, $value, $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$this->wrap($column).' '.$operator.' ?';		$this->bindings[] = $value;		return $this;	}	/**	 * Add an or where condition to the query.	 *	 * @param  string  $column	 * @param  string  $operator	 * @param  mixed   $value	 * @return Query	 */	public function or_where($column, $operator, $value)	{		return $this->where($column, $operator, $value, 'OR');	}	/**	 * Add a where condition for the primary key to the query.	 * This is simply a short-cut method for convenience.	 *	 * @param  mixed  $value	 * @return Query	 */	public function where_id($value)	{		return $this->where('id', '=', $value);	}	/**	 * Add an or where condition for the primary key to the query.	 * This is simply a short-cut method for convenience.	 *	 * @param  mixed  $value	 * @return Query	 */	public function or_where_id($value)	{		return $this->or_where('id', '=', $value);			}	/**	 * Add a where in condition to the query.	 *	 * @param  string  $column	 * @param  array   $values	 * @param  string  $connector	 * @return Query	 */	public function where_in($column, $values, $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$this->wrap($column).' IN ('.$this->parameterize($values).')';		$this->bindings = array_merge($this->bindings, $values);		return $this;	}	/**	 * Add an or where in condition to the query.	 *	 * @param  string  $column	 * @param  array   $values	 * @return Query	 */	public function or_where_in($column, $values)	{		return $this->where_in($column, $values, 'OR');	}	/**	 * Add a where not in condition to the query.	 *	 * @param  string  $column	 * @param  array   $values	 * @param  string  $connector	 * @return Query	 */	public function where_not_in($column, $values, $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$this->wrap($column).' NOT IN ('.$this->parameterize($values).')';		$this->bindings = array_merge($this->bindings, $values);		return $this;	}	/**	 * Add an or where not in condition to the query.	 *	 * @param  string  $column	 * @param  array   $values	 * @return Query	 */	public function or_where_not_in($column, $values)	{		return $this->where_not_in($column, $values, 'OR');	}	/**	 * Add a where null condition to the query.	 *	 * @param  string  $column	 * @param  string  $connector	 * @return Query	 */	public function where_null($column, $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$this->wrap($column).' IS NULL';		return $this;	}	/**	 * Add an or where null condition to the query.	 *	 * @param  string  $column	 * @return Query	 */	public function or_where_null($column)	{		return $this->where_null($column, 'OR');	}	/**	 * Add a where not null condition to the query.	 *	 * @param  string  $column	 * @param  string  $connector	 * @return Query	 */	public function where_not_null($column, $connector = 'AND')	{		$this->where .= ' '.$connector.' '.$this->wrap($column).' IS NOT NULL';		return $this;	}	/**	 * Add an or where not null condition to the query.	 *	 * @param  string  $column	 * @return Query	 */	public function or_where_not_null($column)	{		return $this->where_not_null($column, 'OR');	}	/**	 * Add dynamic where conditions to the query.	 *	 * Dynamic queries are caught by the __call magic method and are parsed here.	 * They provide a convenient, expressive API for building simple conditions.	 *	 * @param  string  $method	 * @param  array   $parameters	 * @return Query	 */	private function dynamic_where($method, $parameters)	{		// Strip the "where_" off of the method.		$finder = substr($method, 6);		// Split the column names from the connectors.		$segments = preg_split('/(_and_|_or_)/i', $finder, -1, PREG_SPLIT_DELIM_CAPTURE);		// The connector variable will determine which connector will be used for the condition.		// We'll change it as we come across new connectors in the dynamic method string.		//		// The index variable helps us get the correct parameter value for the where condition.		// We increment it each time we add a condition.		$connector = 'AND';		$index = 0;		foreach ($segments as $segment)		{			if ($segment != '_and_' and $segment != '_or_')			{				$this->where($segment, '=', $parameters[$index], $connector);				$index++;			}			else			{				$connector = trim(strtoupper($segment), '_');			}		}		return $this;	}	/**	 * Add an ordering to the query.	 *	 * @param  string  $column	 * @param  string  $direction	 * @return Query	 */	public function order_by($column, $direction)	{		$this->orderings[] = $this->wrap($column).' '.strtoupper($direction);		return $this;	}	/**	 * Set the query offset.	 *	 * @param  int  $value	 * @return Query	 */	public function skip($value)	{		$this->offset = $value;		return $this;	}	/**	 * Set the query limit.	 *	 * @param  int  $value	 * @return Query	 */	public function take($value)	{		$this->limit = $value;		return $this;	}	/**	 * Set the limit and offset values for a given page.	 *	 * @param  int    $page	 * @param  int    $per_page	 * @return Query	 */	public function for_page($page, $per_page)	{		return $this->skip(($page - 1) * $per_page)->take($per_page);	}	/**	 * Find a record by the primary key.	 *	 * @param  int     $id	 * @param  array   $columns	 * @return object	 */	public function find($id, $columns = array('*'))	{		return $this->where('id', '=', $id)->first($columns);	}	/**	 * Get an aggregate value.	 *	 * @param  string  $aggregate	 * @param  string  $column	 * @return mixed	 */	private function aggregate($aggregator, $column)	{		$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');		return $this->first()->aggregate;	}	/**	 * Get paginated query results.	 *	 * @param  int        $per_page	 * @param  array      $columns	 * @return Paginator	 */	public function paginate($per_page, $columns = array('*'))	{		$total = $this->count();		return Paginator::make($this->for_page(Paginator::page($total, $per_page), $per_page)->get($columns), $total, $per_page);	}	/**	 * Execute the query as a SELECT statement and return the first result.	 *	 * @param  array   $columns	 * @return object	 */	public function first($columns = array('*'))	{		return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;	}	/**	 * Execute the query as a SELECT statement.	 *	 * @param  array  $columns	 * @return array	 */	public function get($columns = array('*'))	{		if (is_null($this->select))		{			$this->select($columns);		}		$results = $this->connection->query($this->compile_select(), $this->bindings);		// Reset the SELECT clause so more queries can be performed using the same instance.		// This is helpful for getting aggregates and then getting actual results.		$this->select = null;		return $results;	}	/**	 * Compile the query into a SQL SELECT statement.	 *	 * @return string	 */	private function compile_select()	{		$sql = $this->select.' '.$this->from.' '.$this->where;		if (count($this->orderings) > 0)		{			$sql .= ' ORDER BY '.implode(', ', $this->orderings);		}		if ( ! is_null($this->limit))		{			$sql .= ' LIMIT '.$this->limit;		}		if ( ! is_null($this->offset))		{			$sql .= ' OFFSET '.$this->offset;		}		return $sql;	}	/**	 * Execute an INSERT statement.	 *	 * @param  array  $values	 * @return bool	 */	public function insert($values)	{		return $this->connection->query($this->compile_insert($values), array_values($values));	}	/**	 * Execute an INSERT statement and get the insert ID.	 *	 * @param  array  $values	 * @return int	 */	public function insert_get_id($values)	{		$sql = $this->compile_insert($values);		if ($this->connection->driver() == 'pgsql')		{			$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));			$query->execute(array_values($values));			return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;		}		$this->connection->query($sql, array_values($values));		return $this->connection->pdo->lastInsertId();	}	/**	 * Compile the query into a SQL INSERT statement.	 *	 * @param  array   $values	 * @return string	 */	private function compile_insert($values)	{		$sql = 'INSERT INTO '.$this->wrap($this->table);		$columns = array_map(array($this, 'wrap'), array_keys($values));		return $sql .= ' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';	}	/**	 * Execute the query as an UPDATE statement.	 *	 * @param  array  $values	 * @return bool	 */	public function update($values)	{		$sql = 'UPDATE '.$this->wrap($this->table).' SET ';		foreach (array_keys($values) as $column)		{			$sets[] = $this->wrap($column).' = ?';		}		return $this->connection->query($sql.implode(', ', $sets).' '.$this->where, array_merge(array_values($values), $this->bindings));	}	/**	 * Execute the query as a DELETE statement.	 *	 * @param  int   $id	 * @return bool	 */	public function delete($id = null)	{		if ( ! is_null($id)) $this->where('id', '=', $id);		return $this->connection->query('DELETE FROM '.$this->wrap($this->table).' '.$this->where, $this->bindings);			}	/**	 * Wrap a value in keyword identifiers.	 *	 * @param  string      $value	 * @return string	 */	private function wrap($value)	{		if (strpos(strtolower($value), ' as ') !== false)		{			return $this->wrap_alias($value);		}		$wrap = $this->connection->wrapper();		foreach (explode('.', $value) as $segment)		{			$wrapped[] = ($segment != '*') ? $wrap.$segment.$wrap : $segment;		}		return implode('.', $wrapped);	}	/**	 * Wrap an alias in keyword identifiers.	 *	 * @param  string      $value	 * @return string	 */	private function wrap_alias($value)	{		$segments = explode(' ', $value);		return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);	}	/**	 * Create query parameters from an array of values.	 *	 * @param  array  $values	 * @return string	 */	private function parameterize($values)	{		return implode(', ', array_fill(0, count($values), '?'));	}	/**	 * Magic Method for handling dynamic functions.	 */	public function __call($method, $parameters)	{		if (strpos($method, 'where_') === 0)		{			return $this->dynamic_where($method, $parameters, $this);		}		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))		{			return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]);		}		throw new \Exception("Method [$method] is not defined on the Query class.");	}}
 |