Parses and builds AST for the given Query.

@return \Doctrine\ORM\Query\AST\SelectStatement |
\Doctrine\ORM\Query\AST\UpdateStatement |
\Doctrine\ORM\Query\AST\DeleteStatement
163747
public function getAST()
    {
        // Parse & build AST
        $AST = $this->QueryLanguage();

        // Process any deferred validations of some nodes in the AST.
        // This also allows post-processing of the AST for modification purposes.
        $this->processDeferredIdentificationVariables();

        if ($this->deferredPartialObjectExpressions) {
            $this->processDeferredPartialObjectExpressions();
        }

        if ($this->deferredPathExpressions) {
            $this->processDeferredPathExpressions($AST);
        }

        if ($this->deferredResultVariables) {
            $this->processDeferredResultVariables();
        }

        if ($this->deferredNewObjectExpressions) {
            $this->processDeferredNewObjectExpressions($AST);
        }

        $this->processRootEntityAliasSelected();

        // TODO: Is there a way to remove this? It may impact the mixed hydration resultset a lot!
        $this->fixIdentificationVariableOrder($AST);

        return $AST;
    }
1313132
public function generateDeleteSQL()
        {
            if ($this->sql === null) {
                $this->detectDistinct();
                $this->action = QueryInterface::ACTION_DELETE;
                $sql_generator = new SqlGenerator($this);

                $this->sql = $sql_generator->getDeleteSQL();
            }

            return $this->sql;
        }
925640
protected function buildSelectQueryBody($query)
    {
        $distinctSql = $query->getDistinct() ? ' DISTINCT' : '';
        $whatSql = $this->buildSelectWhatPart($query);
        $fromSql = $this->buildSelectFromPart($query);
        $whereSql = $this->buildWherePart($query);
        $groupBySql = $this->buildSelectGroupByPart($query);
        $havingSql = $this->buildSelectHavingPart($query);
        $joinSql = $this->buildSelectJoinPart($query);

        $result = 'SELECT' . $distinctSql . ' ' . $whatSql
            . $fromSql
            . $joinSql
            . $whereSql
            . $groupBySql
            . $havingSql;

        return $result;
    }
986234
public function delete() {
        $statement = new DeleteStatement();
        $statement->setStatementContext($this->statementContext);
        $statement->setCondition($this->condition);
        $sql = $statement->parse();

        $rowCount = $this->connection->execute($sql, $this->statementContext->getParameters());

        $this->clear();

        return $rowCount;
    }
1159604
protected function build()
    {
        $sql = "SELECT\n\t"
            . $this->columns . "\n"
            . $this->from . "\n"
            . ($this->join    ? $this->join           : null)
            . ($this->where   ? $this->where . "\n"   : null)
            . ($this->groupby ? $this->groupby . "\n" : null)
            . ($this->orderby ? $this->orderby . "\n" : null)
            . ($this->limit   ? $this->limit . "\n"   : null)
            . ($this->offset  ? $this->offset . "\n"  : null)
            . ";";

        return $sql;
    }
545716
public function parse($query)
    {
        $node = new Nodes\RootNode();
        $node->setValue($query);

        $l = new Nodes\UndefinedLogicNode();
        $node->addChild($l);

        $t = new Nodes\TextNode();
        $t->setValue($query);
        $l->addChild($t);

        // $this->addResolver(new Resolvers\TextResolver());

        foreach ($this->resolvers as $token) {
            $token->resolve($node);
        }

        // From Root to Logic
        $node = $node->getChildByIndex(0);

        if ($node === null) {
            throw new Exceptions\QuerySyntaxException($query);
        }

        $node->setParent(null);

        // If logic has only one child, skip to first key node
        if (count($node->getChildren()) === 1) {
            $node = $node->getChildByIndex(0);
            $node->setParent(null);
        }

        if ($node instanceof Nodes\UndefinedLogicNode) {
            throw new Exceptions\QuerySyntaxException($query);
        }

        return $node;
    }
938361
protected function _buildSQL()
	{
		if( $this->_stmt !== null ) {
			return $this->_stmt;
		}

		$i = 1;
		foreach( $this->_parts as $part )
		{
			$this->_stmt .= $part;
			if( isset( $this->_binds[$i] ) ) {
				$this->_stmt .= $this->_binds[$i];
			}
			$i++;
		}

		return $this->_stmt;
	}
1311939
private function parse()
    {
        // Skip if already parsed!
        if ($this->parsed) {
            return;
        }

        // Type switch
        switch($this->query->mode){
            case Query::MODE_SELECT:
                $this->parseSelectQuery(); break;
            case Query::MODE_INSERT:
                $this->parseInsertQuery(); break;
            case Query::MODE_UPDATE:
                $this->parseUpdateQuery(); break;
            case Query::MODE_DELETE:
                $this->parseDeleteQuery(); break;
            case Query::MODE_TRUNCATE:
                $this->parseTruncateQuery(); break;
            case Query::MODE_ADVANCED:
                throw new QueryBuilderParseException("Query mode not yet implemented!"); break; // @codeCoverageIgnore
            case Query::MODE_NONE:
                throw new QueryBuilderParseException("Query has no mode yet!"); break; // @codeCoverageIgnore
            default:
                throw new QueryBuilderParseException("Query mode is invalid!"); break; // @codeCoverageIgnore
        }


        // Set parsed to true.
        $this->parsed = true;
    }
125295
private function getUpdateQuery()
    {
        if (isset($this->parts['from'][0]['alias'])) {
            $fromClause = $this->parts['from'][0]['alias'].' FROM '.$this->getFromClause();
        } else {
            $fromClause = $this->parts['from'][0]['table'];
        }

        $setClause = array();

        foreach ($this->parts['set'] as $idenfier => $value) {
            $setClause[] = $this->getExpressionBuilder()->equal($idenfier, $value);
        }

        return 'UPDATE '.$fromClause.
               ' SET '.implode(', ', $setClause).
               (($this->parts['where'] !== null) ? ' WHERE '.$this->parts['where'] : null);
    }
67976
public function from($query)
    {
        $clause = '';

        $resource = $query->getRepository()->getResources()->main();

        $query->from($resource->getName().' AS '.$this->_store->quoteName($resource->getAlias()));

        if (!empty($query->from)) {
            $clause = ' FROM '.implode(' , ', $query->from);
        }

        return $clause;
    }
648502
protected function _buildUpdate()
        {
            $query = [];
            $query[] = "UPDATE {$this->_quoteIdentifier($this->_table_name)} SET";

            $field_list = [];

            foreach ($this->_dirty_fields as $key => $value) {
                if(!Arrays::exists($key, $this->_expr_fields)) {
                    $value = '?';
                }

                $field_list[] = "{$this->_quoteIdentifier($key)} = $value";
            }

            $query[] = join(", ", $field_list);
            $this->_addIdColumnConditions($query);

            return join(" ", $query);
        }
1322711
public function getDeleteQuery()
    {
        $sql = 'DELETE FROM ' . $this->getTable();
        if ($this->where) {
            $sql .= "\nWHERE " . $this->getWhere();
        }
        if ($this->limit) {
            $sql .= "\nLIMIT " . $this->getLimit();
        }

        return $sql;
    }
169263
private function getDeleteQuery()
	{

		$result = $this->getDeleteClause();

		if ($where = $this->getWhereClause()) {
			$result .= ' ' . $where;
		}

		if ($limit = $this->getLimitClause()) {
			$result .= ' ' . $limit;
		}

		return $result;
	}
385914
public function getStatement(): string
    {
        $statement = '';
        if ($this->isSelect() || $this->isAggregate()) {
            $statement = $this->getSelectStatement();
        } elseif ($this->isInsert()) {
            $statement = $this->getInsertStatement();
        } elseif ($this->isUpdate()) {
            $statement = $this->getUpdateStatement();
        } elseif ($this->isDelete()) {
            $statement = $this->getDeleteStatement();
        }

        return $statement;
    }
929963
public function buildQuery($query = null)
    {
        if (null === $query) {
            $query = $this->query;
        }

        $sql = "SELECT ";
        if (isset($query['distinct']) && $query['distinct']) {
            $sql .= 'DISTINCT ';
        }
        $sql .= isset($query['select']) ? $query['select'] : '*';

        if (!isset($query['from'])) {
            return false;
        }

        $sql .= "\nFROM " . $query['from'];
        if (isset($query['join'])) {
            $sql .= "\n" . (is_array($query['join']) ? implode("\n", $query['join']) : $query['join']);
        }
        if (isset($query['where']) && $query['where'] !== '') {
            $sql .= "\nWHERE " . $query['where'];
        }
        if (isset($query['group'])) {
            $sql .= "\nGROUP BY " . $query['group'];
            if (isset($query['having'])) {
                $sql .= "\nHAVING " . $query['having'];
            }
        }
        if (isset($query['order'])) {
            $sql .= "\n ORDER BY " . $query['order'];
        }
        $limit = isset($query['limit']) ? $query['limit'] : 0;
        $offset = isset($query['offset']) ? $query['offset'] : 0;
        $sql = $this->db->buildLimitOffset($sql, $limit, $offset);

        if (isset($query['union'])) {
            $sql .= "\n" . (is_array($query['union']) ? implode("\n", $query['union']) : $query['union']);
        }

        return $sql;
    }
1015453
protected function translateDelete()
    {
        $build = 'delete from ' . $this->escapeTable(false);

        // build the where statements
        if ($wheres = $this->attr('wheres'))
        {
            $build .= $this->translateWhere($wheres);
        }
    
        // build offset and limit
        if ($this->attr('limit'))
        {
             $build .= $this->translateLimit();
        }

        return $build;
    }
750454
public function delete(): IResult
    {
        $builder = new DeleteBuilder($this);
        $sql = $builder->build();
        $result = $this->execute($sql);
        $this->__init();
        return $result;
    }