Skip navigation
Help

DeleteQuery

  1. drupal
    1. 7 drupal/includes/database/query.inc

General class for an abstracted DELETE operation.

Hierarchy

Properties

NameDescription
DeleteQuery::$conditionThe condition object for this query.
DeleteQuery::$tableThe table from which to delete.
Query::$commentsAn array of comments that can be prepended to a query.
Query::$connectionThe connection object on which to run this query.
Query::$connectionKeyThe key of the connection object.
Query::$connectionTargetThe target of the connection object.
Query::$nextPlaceholderThe placeholder counter.
Query::$queryOptionsThe query options to pass on to the connection object.

Functions & methods

NameDescription
DeleteQuery::argumentsImplements QueryConditionInterface::arguments(). Overrides QueryConditionInterface::arguments
DeleteQuery::compileImplements QueryConditionInterface::compile(). Overrides QueryConditionInterface::compile
DeleteQuery::conditionImplements QueryConditionInterface::condition(). Overrides QueryConditionInterface::condition
DeleteQuery::conditionsImplements QueryConditionInterface::conditions(). Overrides QueryConditionInterface::conditions
DeleteQuery::executeExecutes the DELETE query. Overrides Query::execute
DeleteQuery::existsImplements QueryConditionInterface::exists(). Overrides QueryConditionInterface::exists
DeleteQuery::isNotNullImplements QueryConditionInterface::isNotNull(). Overrides QueryConditionInterface::isNotNull
DeleteQuery::isNullImplements QueryConditionInterface::isNull(). Overrides QueryConditionInterface::isNull
DeleteQuery::notExistsImplements QueryConditionInterface::notExists(). Overrides QueryConditionInterface::notExists
DeleteQuery::whereImplements QueryConditionInterface::where(). Overrides QueryConditionInterface::where
DeleteQuery::__constructConstructs a DeleteQuery object. Overrides Query::__construct
DeleteQuery::__toStringImplements PHP magic __toString method to convert the query to a string. Overrides Query::__toString
Query::commentAdds a comment to the query.
Query::getCommentsReturns a reference to the comments array for the query.
Query::nextPlaceholderGets the next placeholder value for this query object. Overrides QueryPlaceholderInterface::nextPlaceholder
Query::__sleepImplements the magic __sleep function to disconnect from the database.
Query::__wakeupImplements the magic __wakeup function to reconnect to the database.

File

drupal/includes/database/query.inc, line 692
Non-specific Database query code. Used by all engines.

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 = NULL) {
    return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
  }

  /**
   * Executes the DELETE query.
   *
   * @return
   *   The return value is dependant 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;
  }
}

Related topics