3 namespace Drupal\Core\Database\Query;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Connection;
9 * General class for an abstracted DELETE operation.
13 class Delete extends Query implements ConditionInterface {
15 use QueryConditionTrait;
18 * The table from which to delete.
25 * Constructs a Delete object.
27 * @param \Drupal\Core\Database\Connection $connection
28 * A Connection object.
29 * @param string $table
30 * Name of the table to associate with this query.
31 * @param array $options
32 * Array of database options.
34 public function __construct(Connection $connection, $table, array $options = []) {
35 $options['return'] = Database::RETURN_AFFECTED;
36 parent::__construct($connection, $options);
37 $this->table = $table;
39 $this->condition = new Condition('AND');
43 * Executes the DELETE query.
46 * The number of rows affected by the delete query.
48 public function execute() {
50 if (count($this->condition)) {
51 $this->condition->compile($this->connection, $this);
52 $values = $this->condition->arguments();
55 return $this->connection->query((string) $this, $values, $this->queryOptions);
59 * Implements PHP magic __toString method to convert the query to a string.
62 * The prepared statement.
64 public function __toString() {
65 // Create a sanitized comment string to prepend to the query.
66 $comments = $this->connection->makeComment($this->comments);
68 $query = $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
70 if (count($this->condition)) {
72 $this->condition->compile($this->connection, $this);
73 $query .= "\nWHERE " . $this->condition;