Compile the "where" portions of the query.

@param  \Illuminate\Database\Query\Builder  $query
@return string
180572
protected function compileWheres(Builder $query)
    {
        // Each type of where clauses has its own compiler function which is responsible
        // for actually creating the where clauses SQL. This helps keep the code nice
        // and maintainable since each clause has a very small method that it uses.
        if (is_null($query->wheres)) {
            return '';
        }

        // If we actually have some where clauses, we will strip off the first boolean
        // operator, which is added by the query builders for convenience so we can
        // avoid checking for the first clauses in each of the compilers methods.
        if (count($sql = $this->compileWheresToArray($query)) > 0) {
            return $this->concatenateWhereClauses($query, $sql);
        }

        return '';
    }
1061443
private function getUpdateWhere($query, &$params)
    {
        $whereArray = [];

        if (isset($query['where'])) {
            $whereArray[] = '('.$query['where'].')';
            unset($query['where']);
        }

        foreach ($query as $field => $value) {
            if ($field[0] == ':') {
                $params[$field] = $value;
                continue;
            }
            $token = ':'.$field;
            $whereArray[] = "{$field} = {$token}";
            $params[$token] = $value;
        }

        return implode(' AND ', $whereArray);
    }
415632
public function compileSelect(Builder $query)
	{
		$original = $query->columns;

		if (is_null($query->columns)) {
			$query->columns = ['*'];
		}

		$components = $this->compileComponents($query);

		// If an offset is present on the query, we will need to wrap the query in
		// a big "ANSI" offset syntax block. This is very nasty compared to the
		// other database systems but is necessary for implementing features.
		if ($query->offset > 0) {
			return $this->compileAnsiOffset($query, $components);
		}

		$sql = $this->concatenate($components);

		$query->columns = $original;

		return $sql;
	}
1092597
protected function getConditionStringFromQueryBuilder(QueryBuilder $query)
    {
        $initialQueryChunks = explode('where ', $query->toSql());

        if (count($initialQueryChunks) == 1) {
            throw new InvalidArgumentException(
                'The query builder instance must have a where clause to build a condition string from'
            );
        }

        $queryChunks = explode('?', $initialQueryChunks[1]);
        $chunkCount  = count($queryChunks);
        $bindings    = $query->getBindings();
        $whereString = '';

        for ($i = 0; $i < $chunkCount; $i++) {

            $whereString .= $queryChunks[ $i ];

            if (isset($bindings[ $i ])) {

                if (gettype($bindings[ $i ]) === 'string') {
                    $whereString .= '"' . $bindings[ $i ] . '"';
                }
            }
        }

        return $whereString;
    }
159809
private function getSearchQueryWhere($query, $where)
	{
		foreach ($where as $key => $value) {
			$query->where($value[0], $value[1], $value[2]);
		}

		return $query;
	}
503900
public function compileSelect(Builder $query) {
    $this->beforeGet($query);
    return $this->grammar->compileSelect($query);
  }
804325
public function compileExists(Builder $query)
    {
        $existsQuery = clone $query;

        $existsQuery->columns = [];

        return $this->compileSelect($existsQuery->selectRaw('1 [exists]')->limit(1));
    }
33778
public function compileMatches(Builder $query, $matches)
    {
        if ( ! is_array($matches) || empty($matches)) return '';

        $prepared = array();

        foreach ($matches as $match)
        {
            $method = 'prepareMatch'. ucfirst($match['type']);
            $prepared[] = $this->$method($match);
        }

        return "MATCH " . implode(', ', $prepared);
    }
1225363
protected function compileUpdateFrom(Builder $query)
	{
		if ( ! isset($query->joins)) return '';

		$froms = array();

		// When using Postgres, updates with joins list the joined tables in the from
		// clause, which is different than other systems like MySQL. Here, we will
		// compile out the tables that are joined and add them to a from clause.
		foreach ($query->joins as $join)
		{
			$froms[] = $this->wrapTable($join->table);
		}

		if (count($froms) > 0) return ' from '.implode(', ', $froms);
	}
1178886
protected function whereSub(Builder $query, $where)
    {
        $select = $this->compileSelect($where['query']);

        return $this->wrap($where['column']).' '.$where['operator']." ($select)";
    }
1369672
protected function compileRowConstraint($query)
    {
        $start = $query->offset + 1;

        if ($query->limit > 0) {
            $finish = $query->offset + $query->limit;

            return "between {$start} and {$finish}";
        }

        return ">= {$start}";
    }
7777
protected function _buildWhere($query, $where)
    {
        $clause = '';

        if (isset($where['condition'])) {
            $clause .= ' '.$where['condition'];
        }

        //converts subclause to string only if there's at least one where statement in it
        if (isset($where['clause'])) {
            if (empty($where['clause'])) {
                return '';
            }

            $clause .= ' (';
            foreach ($where['clause'] as $subwhere) {
                $clause .= $this->_buildWhere($query, $subwhere);
            }
            $clause .= ' )';

            return $clause;
        }

        list($columns, $property) = array_values(AnDomainQueryHelper::parseColumn($query, $where['property']));

        $value = isset($where['value']) ?  $where['value'] : null;
        $where['property'] = pick($columns, $where['property']);

        if ($property && !$value instanceof AnDomainQuery) {
            $constraint = $where['constraint'];

            if (is_object($value) || is_array($columns)) {
                if ($value instanceof AnObjectSet || is_array($value)) {
                    $values = $value;
                    $keys = array();
                    $clauses = array();
                    foreach ($values as $value) {
                        $columns = $property->serialize($value);
                        foreach ($columns as $column => $value) {
                            $keys[$column][] = $value;
                        }
                    }

                    foreach ($keys as $key => $values) {
                        $clauses[] = $this->_buildWhere($query, array('property' => $key, 'constraint' => $constraint, 'value' => $values));
                    }

                    $clause .= ' ('.implode(' AND ', $clauses).')';

                    return $clause;
                } else {
                    $columns = $property->serialize($value);

                    $clauses = array();
                    foreach ($columns as $column => $value) {
                        $clauses[] = $this->_buildWhere($query, array('property' => $column, 'constraint' => $constraint, 'value' => $value));
                    }

                    $clause .= ' ('.implode(' AND ', $clauses).')';

                    return $clause;
                }
            }
        }

        $clause  .= ' '.$where['property'];

        if (isset($where['constraint'])) {
            $value = $this->_store->quoteValue($where['value']);

            if ($value === 'NULL') {
                //force correct constraint
                if (strpos($where['constraint'], 'IS') === false) {
                    if (strpos($where['constraint'], '<>') !== false) {
                        $where['constraint'] = ' IS NOT ';
                    } elseif (strpos($where['constraint'], '=') !== false) {
                        $where['constraint'] = ' IS ';
                    }
                }
            }

            if (in_array($where['constraint'], array('IN', 'NOT IN'))) {
                $value = ' ( '.$value.' ) ';
            }

            $clause .= ' '.$where['constraint'].' '.$value;
        }

        return $clause;
    }
982012
public function compile_select( &$query )
	{
		$build = ( $query->distinct ? 'select distinct' : 'select' ).' ';

		if ( !empty( $query->fields ) )
		{
			foreach( $query->fields as $key => $field )
			{
				if ( !is_numeric( $key ) )
				{
					$build .= $this->escape( $key ).' as '.$this->escape( $field );
				}
				elseif ( is_array( $field ) )
				{
					$build .= $this->escape( $field[0] ).' as '.$this->escape( $field[1] );
				}
				else 
				{
					$build .= $this->escape( $field );
				}
				$build .= ', ';
			}

			$build = substr( $build, 0, -2 );
		}
		else 
		{
			$build .= '*';
		}

		// append the table
		$build .= ' from '.$this->escape_table( $query );

		// build the where stuff
		$build .= $this->compile_where( $query );
		$build .= $this->compile_group( $query );
		$build .= $this->compile_order( $query );
		$build .= $this->compile_limit_with_offset( $query );

		return $build;
	}
751145
public function compileSelect(Builder $query)
	{
		$components = $this->compileComponents($query);

		// If an offset is present on the query, we will need to wrap the query in
		// a big "ANSI" offset syntax block. This is very nasty compared to the
		// other database systems but is necessary for implementing features.
		if ($query->offset > 0)
		{
			return $this->compileAnsiOffset($query, $components);
		}

		return $this->concatenate($components);
	}
699689
public function compile_where( &$query )
	{
		$build = '';

		foreach( $query->wheres as $where ) 
		{
			// to make nested wheres possible you can pass an closure 
			// wich will create a new query where you can add your nested wheres
			if ( !isset( $where[2] ) && is_closure( $where[1] ) )
			{
				$sub_query = new Query;

				call_user_func( $where[1], $sub_query );

				// The parameters get added by the call of compile where
				$build .= ' '.$where[0].' ( '.substr( $this->compile_where( $sub_query ), 7 ).' )';

				continue;
			}

			// when we have an array as where values we have 
			// to parameterize them
			if ( is_array( $where[3] ) ) 
			{
				$where[3] = '('.$this->parameterize( $where[3] ).')';
			}
			else
			{
				$where[3] = $this->param( $where[3] );
			}

			// we always need to escepe where 1 wich referrs to the key
			$where[1] = $this->escape( $where[1] );

			// implode the beauty
			$build .= ' '.implode( ' ', $where );
		}

		return $build;
	}
293047
protected function compileUpdateWheres(Builder $query)
    {
        $baseWheres = $this->compileWheres($query);

        if (!isset($query->joins)) {
            return $baseWheres;
        }

        // Once we compile the join constraints, we will either use them as the where
        // clause or append them to the existing base where clauses. If we need to
        // strip the leading boolean we will do so when using as the only where.
        $joinWheres = $this->compileUpdateJoinWheres($query);

        if (trim($baseWheres) === '') {
            return 'where ' . $this->removeLeadingBoolean($joinWheres);
        }

        return $baseWheres . ' ' . $joinWheres;
    }
89149
protected function applyWhere(Builder $query, array $where)
    {
        foreach ($where as $key => $value) {
            if (is_null($value)) {
                $query = $query->whereNull($key);
            } else {
                if (is_array($value)) {
                    $query = $query->where($key, $value[0], $value[1]);
                } else {
                    $query = $query->where($key, '=', $value);
                }
            }
        }

        return $query;
    }