Apply where clauses on the subquery.

@param  \Sofa\Eloquence\Searchable\Subquery $subquery
@param  \Sofa\Eloquence\Searchable\ColumnCollection $columns
@param  array $words
@return void
555504
protected function searchWhere(
        SearchableSubquery $subquery,
        ColumnCollection $columns,
        array $words,
        array $bindings
    ) {
        $operator = $this->getLikeOperator();

        $wheres = [];

        foreach ($columns as $column) {
            $wheres[] = implode(
                ' or ',
                array_fill(0, count($words), sprintf('%s %s ?', $column->getWrapped(), $operator))
            );
        }

        $where = implode(' or ', $wheres);

        $subquery->whereRaw("({$where})");

        $this->addBinding($bindings, 'select');
    }
963717
protected function addSearchClauses(
        SearchableSubquery $subquery,
        ColumnCollection $columns,
        array $words,
        $threshold
    ) {
        $whereBindings = $this->searchSelect($subquery, $columns, $words, $threshold);

        // For morphOne/morphMany support we need to port the bindings from JoinClauses.
        $joinBindings = collect($subquery->getQuery()->joins)->flatMap(function ($join) {
            return $join->getBindings();
        })->all();

        $this->addBinding($joinBindings, 'select');

        // Developer may want to skip the score threshold filtering by passing zero
        // value as threshold in order to simply order full result by relevance.
        // Otherwise we are going to add where clauses for speed improvement.
        if ($threshold > 0) {
            $this->searchWhere($subquery, $columns, $words, $whereBindings);
        }

        $this->query->where('relevance', '>=', new Expression(number_format($threshold, 2)));

        $this->query->orders = array_merge(
            [['column' => 'relevance', 'direction' => 'desc']],
            (array) $this->query->orders
        );
    }
710908
private void appendSubQuery(Query subQuery, StringBuffer buf)
    {
        buf.append(" (");
        buf.append(getSubQuerySQL(subQuery));
        buf.append(") ");
    }
1375049
private function whereSearch(Builder $query, $field, array $search_terms): void
    {
        if ($field instanceof Expression) {
            $field = $field->getValue();
        }

        foreach ($search_terms as $search_term) {
            $query->whereContains(DB::raw($field), $search_term);
        }
    }
793096
public function select($columns = "*")
    {
        $this->start = $columns;
        $this->queryType = self::QUERY_SELECT;
        return $this;
    }
394683
final public function setConditions(array $conditions)
    {
        if ($this->hasRun) {
            throw new \RuntimeException("Cursor query has already run");
        }

        $this->conditions = $this->applyConditions($conditions);
    }
812486
public function deleteColumns($columns, $deleteRecursiveInSubtables = false)
    {
        foreach ($this->getDataTables() as $table) {
            $table->deleteColumns($columns);
        }
    }
1198716
private function applyFilters() : void
    {
        foreach ($this->criteria as $criteria) {
            $filterClass = $criteria->getOperator();

            $filter = new $filterClass(
                $this->table->aliasField($criteria->getField()),
                $criteria->getValue()
            );

            $filter->apply($this->query);
        }
    }
982527
protected function joinForSearch($mappings, $subquery)
    {
        $mappings = is_array($mappings) ? $mappings : (array) $mappings;

        $columns = new ColumnCollection;

        $grammar = $this->query->getGrammar();

        $joiner = static::$joinerFactory->make($subquery->getQuery(), $this->model);

        // Here we loop through the search mappings in order to join related tables
        // appropriately and build a searchable column collection, which we will
        // use to build select and where clauses with correct table prefixes.
        foreach ($mappings as $mapping => $weight) {
            if (strpos($mapping, '.') !== false) {
                list($relation, $column) = $this->model->parseMappedColumn($mapping);

                $related = $joiner->leftJoin($relation);

                $columns->add(
                    new Column($grammar, $related->getTable(), $column, $mapping, $weight)
                );
            } else {
                $columns->add(
                    new Column($grammar, $this->model->getTable(), $mapping, $mapping, $weight)
                );
            }
        }

        return $columns;
    }
1228275
public function filterQuery(QueryBuilder &$query, &$selects = null)
    {
        //run the parent method
        parent::filterQuery($query, $selects);

        //if there is no value, return
        if (!$this->getOption('value')) {
            return;
        }

        $query->where($this->getOption('foreign_key'), '=', $this->getOption('value'));
    }
1259543
protected function buildSubquery(array $words, array $mappings, $threshold)
    {
        $subquery = new SearchableSubquery($this->query->newQuery(), $this->model->getTable());

        $columns = $this->joinForSearch($mappings, $subquery);

        $threshold = (is_null($threshold))
                        ? array_sum($columns->getWeights()) / 4
                        : (float) $threshold;

        $subquery->select($this->model->getTable() . '.*')
                 ->from($this->model->getTable())
                 ->groupBy($this->model->getQualifiedKeyName());

        $this->addSearchClauses($subquery, $columns, $words, $threshold);

        return $subquery;
    }
830854
private function addInheritedSqlResultSetMappings(ClassMetadata $subClass, ClassMetadata $parentClass)
    {
        foreach ($parentClass->sqlResultSetMappings as $name => $mapping) {
            if ( ! isset ($subClass->sqlResultSetMappings[$name])) {
                $entities = array();
                foreach ($mapping['entities'] as $entity) {
                    $entities[] = array(
                        'fields'                => $entity['fields'],
                        'isSelfClass'           => $entity['isSelfClass'],
                        'discriminatorColumn'   => $entity['discriminatorColumn'],
                        'entityClass'           => $entity['isSelfClass'] ? $subClass->name : $entity['entityClass'],
                    );
                }

                $subClass->addSqlResultSetMapping(array(
                    'name'          => $mapping['name'],
                    'columns'       => $mapping['columns'],
                    'entities'      => $entities,
                ));
            }
        }
    }
394481
public function selectQuery(string $prefix, array $from = [], array $columns = []): SelectQuery
    {
        return new SelectQuery($this, $this->getCompiler($prefix), $from, $columns);
    }
538977
public function innerJoinSubquery(Subquery $subquery, $on)
    {
        return $this->joinSubquery($subquery, $on, Joins::INNER);
    }
1054822
private function buildSearchQuery(array $words)
    {
        $qb = $this->app->make(PackageRepository::class)->createQueryBuilder('p');
        $expr = $qb->expr();
        $orFields = $expr->orX();
        foreach (['p.handle', 'p.name'] as $fieldName) {
            $and = $expr->andX();
            foreach ($words as $word) {
                $and->add($expr->like($fieldName, $expr->literal('%' . $word . '%')));
            }
            $orFields->add($and);
        }
        $qb->where($orFields);
        if ($this->maximumSearchResults) {
            $qb->setMaxResults($this->maximumSearchResults);
        }

        return $qb;
    }
564782
private function remapColumns(array &$columns, $alias = true): void {
    foreach($columns as &$column) {
      if($this->to_db[$column] !== $column) {
        if($alias) {
          $column = $this->to_db[$column] . ' as ' . $column;
        } else {
          $column = $this->to_db[$column];
        }
      }
    }
  }
251498
public function select($columns = "*") : QueryBuilder
    {
        $this->_query[] = "SELECT";

        //Parse column(s)
        if(is_array($columns)) {
            $select_columns = "";

            $size = sizeof($columns);
            for($i = 0; $i < $size; $i++) {
                if($i > 0) {
                    $select_columns .= ', ';
                }

                $select_columns .= '`'. $this->sanitize((string)$columns[$i]) .'`';
            }

            $this->_query[] = $select_columns;

            return $this;
        }

        $select_columns = "*";
        if(
            !empty($columns) &&
            $columns !== "*"
        ) {
            $select_columns = '`'. $this->sanitize((string)$columns) .'`';
        }

        $this->_query[] = $select_columns;

        return $this;
    }