Newer
Older
Larry Garfield
committed
<?php
/**
* @file
* Definition of Drupal\Core\Database\Query\Delete
*/
namespace Drupal\Core\Database\Query;
Larry Garfield
committed
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
Larry Garfield
committed
/**
* General class for an abstracted DELETE operation.
Angie Byron
committed
*
* @ingroup database
Larry Garfield
committed
*/
class Delete extends Query implements ConditionInterface {
/**
* The table from which to delete.
*
* @var string
*/
protected $table;
/**
* The condition object for this query.
*
* Condition handling is handled via composition.
*
* @var Condition
Larry Garfield
committed
*/
protected $condition;
/**
* Constructs a Delete object.
Larry Garfield
committed
*
Jennifer Hodgdon
committed
* @param \Drupal\Core\Database\Connection $connection
* A Connection object.
Larry Garfield
committed
* @param string $table
* Name of the table to associate with this query.
* @param array $options
* Array of database options.
*/
public function __construct(Connection $connection, $table, array $options = array()) {
$options['return'] = Database::RETURN_AFFECTED;
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = new Condition('AND');
Larry Garfield
committed
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::condition().
Larry Garfield
committed
*/
public function condition($field, $value = NULL, $operator = '=') {
Larry Garfield
committed
$this->condition->condition($field, $value, $operator);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::isNull().
Larry Garfield
committed
*/
public function isNull($field) {
$this->condition->isNull($field);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull().
Larry Garfield
committed
*/
public function isNotNull($field) {
$this->condition->isNotNull($field);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::exists().
Larry Garfield
committed
*/
public function exists(SelectInterface $select) {
$this->condition->exists($select);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::notExists().
Larry Garfield
committed
*/
public function notExists(SelectInterface $select) {
$this->condition->notExists($select);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::conditions().
Larry Garfield
committed
*/
public function &conditions() {
return $this->condition->conditions();
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::arguments().
Larry Garfield
committed
*/
public function arguments() {
return $this->condition->arguments();
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::where().
Larry Garfield
committed
*/
public function where($snippet, $args = array()) {
$this->condition->where($snippet, $args);
return $this;
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::compile().
Larry Garfield
committed
*/
public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) {
return $this->condition->compile($connection, $queryPlaceholder);
}
/**
* Implements Drupal\Core\Database\Query\ConditionInterface::compiled().
Larry Garfield
committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
*/
public function compiled() {
return $this->condition->compiled();
}
/**
* Executes the DELETE query.
*
* @return
* The return value is dependent on the database connection.
*/
public function execute() {
$values = array();
if (count($this->condition)) {
$this->condition->compile($this->connection, $this);
$values = $this->condition->arguments();
}
return $this->connection->query((string) $this, $values, $this->queryOptions);
}
/**
* Implements PHP magic __toString method to convert the query to a string.
*
* @return string
* The prepared statement.
*/
public function __toString() {
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
$query = $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
if (count($this->condition)) {
$this->condition->compile($this->connection, $this);
$query .= "\nWHERE " . $this->condition;
}
return $query;
}
}