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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DeleteQuery::$condition protected property The condition object for this query.
DeleteQuery::$table protected property The table from which to delete.
DeleteQuery::arguments public function Implements QueryConditionInterface::arguments(). Overrides QueryConditionInterface::arguments
DeleteQuery::compile public function Implements QueryConditionInterface::compile(). Overrides QueryConditionInterface::compile
DeleteQuery::compiled public function Implements QueryConditionInterface::compiled(). Overrides QueryConditionInterface::compiled
DeleteQuery::condition public function Implements QueryConditionInterface::condition(). Overrides QueryConditionInterface::condition
DeleteQuery::conditions public function Implements QueryConditionInterface::conditions(). Overrides QueryConditionInterface::conditions
DeleteQuery::execute public function Executes the DELETE query. Overrides Query::execute 1
DeleteQuery::exists public function Implements QueryConditionInterface::exists(). Overrides QueryConditionInterface::exists
DeleteQuery::isNotNull public function Implements QueryConditionInterface::isNotNull(). Overrides QueryConditionInterface::isNotNull
DeleteQuery::isNull public function Implements QueryConditionInterface::isNull(). Overrides QueryConditionInterface::isNull
DeleteQuery::notExists public function Implements QueryConditionInterface::notExists(). Overrides QueryConditionInterface::notExists
DeleteQuery::where public function Implements QueryConditionInterface::where(). Overrides QueryConditionInterface::where
DeleteQuery::__construct public function Constructs a DeleteQuery object. Overrides Query::__construct
DeleteQuery::__toString public function Implements PHP magic __toString method to convert the query to a string. Overrides Query::__toString
Query::$comments protected property An array of comments that can be prepended to a query.
Query::$connection protected property The connection object on which to run this query.
Query::$connectionKey protected property The key of the connection object.
Query::$connectionTarget protected property The target of the connection object.
Query::$nextPlaceholder protected property The placeholder counter.
Query::$queryOptions protected property The query options to pass on to the connection object.
Query::$uniqueIdentifier protected property A unique identifier for this query object.
Query::comment public function Adds a comment to the query.
Query::getComments public function Returns a reference to the comments array for the query.
Query::nextPlaceholder public function Gets the next placeholder value for this query object. Overrides QueryPlaceholderInterface::nextPlaceholder
Query::uniqueIdentifier public function Returns a unique identifier for this object. Overrides QueryPlaceholderInterface::uniqueIdentifier
Query::__clone public function Implements the magic __clone function. 1
Query::__sleep public function Implements the magic __sleep function to disconnect from the database.
Query::__wakeup public function Implements the magic __wakeup function to reconnect to the database.

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