Skip to content
Snippets Groups Projects
Select Git revision
  • 92286713d58abc1aad1cc55ad7d1a0c59ab758c3
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

Delete.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Delete.php 3.71 KiB
    <?php
    
    namespace Drupal\Core\Database\Query;
    
    use Drupal\Core\Database\Database;
    use Drupal\Core\Database\Connection;
    
    /**
     * General class for an abstracted DELETE operation.
     */
    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
       */
      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(Connection $connection, $table, array $options = array()) {
        $options['return'] = Database::RETURN_AFFECTED;
        parent::__construct($connection, $options);
        $this->table = $table;
    
        $this->condition = new Condition('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(SelectInterface $select) {
        $this->condition->exists($select);
        return $this;
      }
    
      /**
       * Implements QueryConditionInterface::notExists().
       */
      public function notExists(SelectInterface $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(Connection $connection, PlaceholderInterface $queryPlaceholder) {
        return $this->condition->compile($connection, $queryPlaceholder);
      }
    
      /**
       * Implements QueryConditionInterface::compiled().
       */
      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;
      }
    }