class DeleteQuery

General class for an abstracted DELETE operation.

Hierarchy

Expanded class hierarchy of DeleteQuery

Related topics

1 string reference to 'DeleteQuery'
DatabaseConnection::delete in includes/database/database.inc
Prepares and returns a DELETE query object.

File

includes/database/query.inc, line 733

View source
class DeleteQuery extends Query implements QueryConditionInterface {
  
  /**
   * The table from which to delete.
   *
   * @var string
   */
  protected $table;
  
  /**
   * The condition object for this query.
   *
   * Condition handling is handled via composition.
   *
   * @var DatabaseCondition
   */
  protected $condition;
  
  /**
   * Constructs a DeleteQuery object.
   *
   * @param DatabaseConnection $connection
   *   A DatabaseConnection object.
   * @param string $table
   *   Name of the table to associate with this query.
   * @param array $options
   *   Array of database options.
   */
  public function __construct(DatabaseConnection $connection, $table, array $options = array()) {
    $options['return'] = Database::RETURN_AFFECTED;
    parent::__construct($connection, $options);
    $this->table = $table;
    $this->condition = new DatabaseCondition('AND');
  }
  
  /**
   * Implements QueryConditionInterface::condition().
   */
  public function condition($field, $value = NULL, $operator = NULL) {
    $this->condition
      ->condition($field, $value, $operator);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::isNull().
   */
  public function isNull($field) {
    $this->condition
      ->isNull($field);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::isNotNull().
   */
  public function isNotNull($field) {
    $this->condition
      ->isNotNull($field);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::exists().
   */
  public function exists(SelectQueryInterface $select) {
    $this->condition
      ->exists($select);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::notExists().
   */
  public function notExists(SelectQueryInterface $select) {
    $this->condition
      ->notExists($select);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::conditions().
   */
  public function &conditions() {
    return $this->condition
      ->conditions();
  }
  
  /**
   * Implements QueryConditionInterface::arguments().
   */
  public function arguments() {
    return $this->condition
      ->arguments();
  }
  
  /**
   * Implements QueryConditionInterface::where().
   */
  public function where($snippet, $args = array()) {
    $this->condition
      ->where($snippet, $args);
    return $this;
  }
  
  /**
   * Implements QueryConditionInterface::compile().
   */
  public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
    return $this->condition
      ->compile($connection, $queryPlaceholder);
  }
  
  /**
   * Implements QueryConditionInterface::compiled().
   */
  public function compiled() {
    return $this->condition
      ->compiled();
  }
  
  /**
   * Executes the DELETE query.
   *
   * @return int
   *   The number of rows affected by the delete query.
   */
  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)) {
      try {
        $this->condition
          ->compile($this->connection, $this);
      } catch (InvalidQueryConditionOperatorException $e) {
        drupal_trigger_fatal_error($e->getMessage());
      }
      $query .= "\nWHERE " . $this->condition;
    }
    return $query;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.