Compile a delete statement into SQL

@param  Builder  $query

@return  string
160683
public function compileDelete(Builder $query) {
    $this->remapWheres($query);
    return $this->grammar->compileDelete($query);
  }
1080191
protected function compileDeleteWithoutJoins(Builder $query, $table, $where) {
        $sql = trim("delete from {$table} {$where}");

        // When using MySQL, delete statements may contain order by statements and limits
        // so we will compile both of those here. Once we have finished compiling this
        // we will return the completed SQL statement so it will be executed for us.
        if (! empty($this->orders)) {
            $sql .= $this->compileOrders($query);
        }

        if (isset($this->limit)) {
            $sql .= $this->compileLimit($query);
        }
        return $sql;
    }
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;
	}
1055606
public function rawDelete(Query $query)
        {
            $query->setTable($this);
            $query->generateDeleteSQL();

            $value = Statement::getPreparedStatement($query)->execute();
            $this->clearB2DBCachedObjects();

            return $value;
        }
845951
protected function compileHavings(Builder $query): string {
        if (empty($query->having)) {
            return '';
        }
        $sql = implode(' ', array_map([$this, 'compileHaving'], $query->having));
        return ' HAVING '.$this->removeLeadingBoolean($sql);
    }
714900
public function compileTruncate(Builder $query)
	{
		$sql = array('delete from sqlite_sequence where name = ?' => array($query->from));

		$sql['delete from '.$this->wrapTable($query->from)] = array();

		return $sql;
	}
1284835
public function compileSelect(Builder $query)
    {
        if ($query->unions && $query->aggregate) {
            return $this->compileUnionAggregate($query);
        }

        // If the query does not have any columns set, we'll set the columns to the
        // * character to just get all of the columns from the database. Then we
        // can build the query and concatenate all the pieces together as one.
        $original = $query->columns;

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

        // To compile the query, we'll spin through each component of the query and
        // see if that component exists. If it does we'll just call the compiler
        // function for the component which is responsible for making the SQL.
        $sql = trim($this->concatenate(
            $this->compileComponents($query))
        );

        $query->columns = $original;

        return $sql;
    }
37057
public function compileUpdate(Builder $query, $values)
    {
        $table = $this->wrapTable($query->from);

        $columns = collect($values)->map(function ($value, $key) use ($query) {
            return $this->wrap(Str::after($key, $query->from.'.')).' = '.$this->parameter($value);
        })->implode(', ');

        if (isset($query->joins) || isset($query->limit)) {
            $selectSql = parent::compileSelect($query->select("{$query->from}.rowid"));

            return "update {$table} set $columns where {$this->wrap('rowid')} in ({$selectSql})";
        }

        return trim("update {$table} set {$columns} {$this->compileWheres($query)}");
    }
211147
protected function compileUnions(Builder $query)
    {
        $sql = '';

        foreach ($query->unions as $union) {
            $sql .= $this->compileUnion($union);
        }

        if (! empty($query->unionOrders)) {
            $sql .= ' '.$this->compileOrders($query, $query->unionOrders);
        }

        if (isset($query->unionLimit)) {
            $sql .= ' '.$this->compileLimit($query, $query->unionLimit);
        }

        if (isset($query->unionOffset)) {
            $sql .= ' '.$this->compileOffset($query, $query->unionOffset);
        }

        return ltrim($sql);
    }
159805
public function searchDelete($query) {
		$request = new CPS_SearchDeleteRequest($query);
		$this->_lastResponse = $this->_connection->sendRequest($request);
		return $this->_lastResponse->getHits();
	}
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;
	}
1291586
protected function compileAggregate(Builder $query, $aggregate)
    {
        $column = $this->columnize($aggregate['columns']);

        // If the query has a "distinct" constraint and we're not asking for all columns
        // we need to prepend "distinct" onto the column name so that the query takes
        // it into account when it performs the aggregating operations on the data.
        if ($query->distinct && $column !== '*') {
            $column = 'distinct '.$column;
        }

        return 'select '.$aggregate['function'].'('.$column.') as aggregate';
    }
982541
public function compileDelete(Builder $query)
    {
        $table = $this->wrapTable($query->from);

        return isset($query->joins)
            ? $this->compileDeleteWithJoins($query, $table)
            : parent::compileDelete($query);
    }
870709
protected function compileJoins(Builder $query, $joins)
    {
        $sql = array();

        $query->setBindings(array(), 'join');

        foreach ($joins as $join) {
            $table = $this->wrapTable($join->table);

            $clauses = array();

            foreach ($join->clauses as $clause) {
                $clauses[] = $this->compileJoinConstraint($clause);
            }

            foreach ($join->bindings as $binding) {
                $query->addBinding($binding, 'join');
            }

            $clauses[0] = $this->removeLeadingBoolean($clauses[0]);

            $clauses = implode(' ', $clauses);

            $type = $join->type;

            //
            $sql[] = "$type join $table on $clauses";
        }

        return implode(' ', $sql);
    }
435258
protected function compileComponents(Builder $query)
    {
        $sql = [];

        foreach ($this->selectComponents as $component) {
            // To compile the query, we'll spin through each component of the query and
            // see if that component exists. If it does we'll just call the compiler
            // function for the component which is responsible for making the SQL.
            if (isset($query->$component) && ! is_null($query->$component)) {
                $method = 'compile'.ucfirst($component);

                $sql[$component] = $this->$method($query, $query->$component);
            }
        }

        return $sql;
    }
167774
public function queriedDelete(Query $query)
    {
        if ($query->getLimit()
            || $query->getSort()
            || ($query instanceof RqlQuery && $query->getGroupBy())
            || $query->getSelect()) {
            throw new InvalidArgumentException('Only where clause allowed for delete');
        }

        $conditionBuilder = new SqlConditionBuilder(
            $this->getDbTable()->getAdapter(),
            $this->getDbTable()->getTable()
        );

        $sql = new Sql($this->getDbTable()->getAdapter());
        $delete = $sql->delete($this->getDbTable()->getTable());
        $delete->where($conditionBuilder->__invoke($query->getQuery()));
        $sqlString = $sql->buildSqlString($delete);

        $adapter = $this->dbTable->getAdapter();
        $shouldDeletedIds = array_map(function ($item) {
            return $item[$this->getIdentifier()];
        }, $this->query($query));

        try {
            $statement = $adapter->getDriver()->createStatement($sqlString);
            $result = $statement->execute();
        } catch (\Throwable $e) {
            throw new DataStoreException("Can't delete records using query", 0, $e);
        }

        if (count($shouldDeletedIds) === $result->getAffectedRows()) {
            return $shouldDeletedIds;
        } else {
            $result = $this->query($query);

            foreach ($result as $record) {
                if (in_array($record[$this->getIdentifier()], $shouldDeletedIds)) {
                    unset($shouldDeletedIds[$record[$this->getIdentifier()]]);
                }
            }
        }

        return $shouldDeletedIds;
    }
784699
protected function compileUpdateWheres(Builder $query)
	{
		$baseWhere = $this->compileWheres($query);

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

		// 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.
		$joinWhere = $this->compileUpdateJoinWheres($query);

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

		return $baseWhere.' '.$joinWhere;
	}