Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Database / Query / Delete.php
1 <?php
2
3 namespace Drupal\Core\Database\Query;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Connection;
7
8 /**
9  * General class for an abstracted DELETE operation.
10  *
11  * @ingroup database
12  */
13 class Delete extends Query implements ConditionInterface {
14
15   use QueryConditionTrait;
16
17   /**
18    * The table from which to delete.
19    *
20    * @var string
21    */
22   protected $table;
23
24   /**
25    * Constructs a Delete object.
26    *
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.
33    */
34   public function __construct(Connection $connection, $table, array $options = []) {
35     $options['return'] = Database::RETURN_AFFECTED;
36     parent::__construct($connection, $options);
37     $this->table = $table;
38
39     $this->condition = new Condition('AND');
40   }
41
42   /**
43    * Executes the DELETE query.
44    *
45    * @return int
46    *   The number of rows affected by the delete query.
47    */
48   public function execute() {
49     $values = [];
50     if (count($this->condition)) {
51       $this->condition->compile($this->connection, $this);
52       $values = $this->condition->arguments();
53     }
54
55     return $this->connection->query((string) $this, $values, $this->queryOptions);
56   }
57
58   /**
59    * Implements PHP magic __toString method to convert the query to a string.
60    *
61    * @return string
62    *   The prepared statement.
63    */
64   public function __toString() {
65     // Create a sanitized comment string to prepend to the query.
66     $comments = $this->connection->makeComment($this->comments);
67
68     $query = $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
69
70     if (count($this->condition)) {
71
72       $this->condition->compile($this->connection, $this);
73       $query .= "\nWHERE " . $this->condition;
74     }
75
76     return $query;
77   }
78
79 }