Decorate a query using pre or post query template
Template add query before / after current query
Use query keyword to retrieve template

@param Query $query
@return array decorated queries
496922
public function decorate(Query $query)
    {
        $queries = $this->process(
            $query,
            sprintf(
                self::PRE_TEMPLATE_PATH,
                $this->templatePath,
                $query->getType()
            )
        );

        $queries[] = $query;

        return array_merge(
            $queries,
            $this->process(
                $query,
                sprintf(
                    self::POST_TEMPLATE_PATH,
                    $this->templatePath,
                    $query->getType()
                )
            )
        );
    }
23785
public function apply(Query $query) : Query
    {
        return $query->where(
            (new QueryExpression())->gt(
                $this->field,
                $this->value,
                $query->getTypeMap()->type($this->field)
            )
        );
    }
425137
public function allArray(Query $query = null) {
		$iterator = $this->getIterator();
		$array = $iterator->all($query);
		$array = $this->forgeEntity($array);
		return $array;
	}
258264
public function merge(Query $query): Query
    {
        foreach ($query->getCriteria() as $criterion) {
            $criterion->attach($this);
        }

        return $this->attach($query);
    }
689630
private function prepare($query)
    {
        foreach ($this->aggregations as $agg) {
            $agg->prepareQuery($this, $query);
        }

        // Handle paging
        $this->setPage(
            $this->getQueryParam($query, $this->pageParameterName, 0)
                + $this->pageDelta
        );
    }
727989
public function override($query)
    {
        foreach ($this->data as $key => $value) {
            $query = str_replace(
                sprintf('{%s}', $key),
                $value,
                $query
            );
        }

        return $query;
    }
1318486
protected static function preprocessQuery($query, $parameters = [])
    {
        if (is_array($parameters) && count($parameters)) {
            return vsprintf($query, $parameters);
        } else {
            if (isset($parameters)) {
                return sprintf($query, $parameters);
            } else {
                return $query;
            }
        }
    }
650943
public function modifyQuery(Query $query)
    {
        $typoScriptConfiguration = $this->searchRequest->getContextTypoScriptConfiguration();
        $faceting = FacetingBuilder::fromTypoScriptConfiguration($typoScriptConfiguration);

        $allFacets = $typoScriptConfiguration->getSearchFacetingFacets();
        $facetParameters = $this->buildFacetingParameters($allFacets, $typoScriptConfiguration);
        foreach ($facetParameters as $facetParameter => $value) {
            if(strtolower($facetParameter) === 'facet.field') {
                $faceting->setFields($value);
            } else {
                $faceting->addAdditionalParameter($facetParameter, $value);
            }
        }

        $searchArguments = $this->searchRequest->getArguments();
        if (!is_array($searchArguments)) {
            return $query;
        }

        $keepAllFacetsOnSelection = $typoScriptConfiguration->getSearchFacetingKeepAllFacetsOnSelection();
        $facetFilters = $this->addFacetQueryFilters($searchArguments, $allFacets, $keepAllFacetsOnSelection);

        $queryBuilder = new QueryBuilder($typoScriptConfiguration);
        $queryBuilder->startFrom($query)->useFaceting($faceting)->useFilterArray($facetFilters);
        return $query;
    }
135595
public function buildUpdate(Query $query) {
        return $this->renderStatement(Query::UPDATE, [
            'fields' => $this->formatFields($query),
            'table' => $this->formatTable($query->getTable(), $query->getAlias()),
            'joins' => $this->formatJoins($query->getJoins()),
            'where' => $this->formatWhere($query->getWhere()),
            'orderBy' => $this->formatOrderBy($query->getOrderBy()),
            'limit' => $this->formatLimit($query->getLimit()),
        ] + $this->formatAttributes($query->getAttributes()));
    }
301255
public function buildSelect(Query $query) {
        return $this->renderStatement(Query::SELECT, [
            'fields' => $this->formatFields($query),
            'table' => $this->formatTable($query->getTable(), $query->getAlias()),
            'joins' => $this->formatJoins($query->getJoins()),
            'where' => $this->formatWhere($query->getWhere()),
            'groupBy' => $this->formatGroupBy($query->getGroupBy()),
            'having' => $this->formatHaving($query->getHaving()),
            'compounds' => $this->formatCompounds($query->getCompounds()),
            'orderBy' => $this->formatOrderBy($query->getOrderBy()),
            'limit' => $this->formatLimitOffset($query->getLimit(), $query->getOffset()),
        ] + $this->formatAttributes($query->getAttributes()));
    }
1201336
public function modifyQuery(Query $query)
    {
        $query = $this->queryBuilder->startFrom($query)->useElevationFromTypoScript()->getQuery();
        return $query;
    }
1154946
private function addQuery($query, array $params) {
		$q = $this->queryBuilder->interpolateQuery($query, $params);
		self::$queries[] = $q;

		$trace = $q . "\n\n";
		foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $i => $t) {
			$trace .= '#' . $i . ' ';
			if (isset($t['file'])) {
				$trace .= $t['file'];
			}
			if (isset($t['line'])) {
				$trace .= '(' . $t['line'] . ')';
			}
			$trace .= ': ';
			if (isset($t['class'])) {
				$trace .= $t['class'];
			}
			if (isset($t['function'])) {
				$trace .= '->' . $t['function'];
			}
			$trace .= "\n";
		}
		self::$trace[] = $trace;
	}
1109973
public function prepareQuery(Query $query)
    {
        $parameters = new Parameters($this->driver);

        if ($this->pdo === null)
            $this->connect();

        $sql = $this->driver->toSQL($parameters, $query);

        $statement = $this->prepare($sql);
        $parameters->bindParameters($statement);
        return $statement;
    }
1049442
public function doPostOrdering($query)
    {
        // Do not execute this in admin view
        if (is_admin() || !(is_archive() || is_home()) || !$query->is_main_query()) {
            return $query;
        }

        $isMetaQuery = false;

        $posttype = $query->get('post_type');
        if (empty($posttype)) {
            $posttype = 'post';
        }

        // Get orderby key, default to post_date
        $orderby = (isset($_GET['orderby']) && !empty($_GET['orderby'])) ? sanitize_text_field($_GET['orderby']) : get_field('archive_' . sanitize_title($posttype) . '_sort_key',
            'option');
        if (empty($orderby)) {
            $orderby = 'post_date';
        }

        if (in_array($orderby, array('post_date', 'post_modified', 'post_title'))) {
            $orderby = str_replace('post_', '', $orderby);
        } else {
            $isMetaQuery = true;
        }

        // Get orderby order, default to desc
        $order = (isset($_GET['order']) && !empty($_GET['order'])) ? sanitize_text_field($_GET['order']) : get_field('archive_' . sanitize_title($posttype) . '_sort_order',
            'option');
        if (empty($order) || !in_array(strtolower($order), array('asc', 'desc'))) {
            $order = 'desc';
        }

        $query->set('order', $order);

        // Return if not meta query
        if (!$isMetaQuery) {
            $query->set('orderby', $orderby);
            return $query;
        }

        if (isset($_GET['orderby']) && $_GET['orderby'] == 'meta_key' && isset($_GET['meta_key']) && !empty($_GET['meta_key'])) {
            $orderby = sanitize_text_field($_GET['meta_key']);
        }

        // Continue if meta query
        $query->set('meta_key', $orderby);
        $query->set('orderby', 'meta_value');

        return $query;
    }
62533
public function unprepared($query)
    {
        $start = $this->getLogTime();

        try {

            $result = $this->wpdb->query($query);

            if ($result === false || $this->wpdb->last_error) {
                throw new QueryException($query, [], new \Exception($this->wpdb->last_error));
            }

            return $result;

        } catch (QueryException $e) {
            throw $e;
        } finally {
            $this->logQuery($query, [], $this->getElapsedTime($start));
        }

    }
310977
function prefixQuery( $regex, $query, $prefix = NULL ) {
		if( !isset( $prefix ) ) {
			return $query;
		}

		if( preg_match( $regex, $query, $match ) ) {
			$preamble = $match[1];
			$postamble = $match[5];
			$objectList = explode( ',', $match[3] );
			// $prefix = $prefix . '_';

			$prefixedList = '';

			foreach( $objectList as $object ) {
				if( $prefixedList !== '' ) {
					$prefixedList .= ', ';
				}

				$prefixedList .= $prefix . trim( $object );
			}

			$query = $preamble . ' ' . $prefixedList . ' ' . $postamble;
		}

		return $query;
	}
413071
final public function queryArray(string $query)
    {
        $result = $this->queryResult($query);
        if (false === $result) {
            return false;
        }

        $return = [];
        while (false !== $row = $result->fetchRow()) {
            $return[] = $row;
        }
        return $return;
    }