class Query

Same name in other branches
  1. 9 core/modules/workspaces/src/EntityQuery/Query.php \Drupal\workspaces\EntityQuery\Query
  2. 9 core/lib/Drupal/Core/Config/Entity/Query/Query.php \Drupal\Core\Config\Entity\Query\Query
  3. 9 core/lib/Drupal/Core/Database/Query/Query.php \Drupal\Core\Database\Query\Query
  4. 9 core/lib/Drupal/Core/Entity/Query/Sql/Query.php \Drupal\Core\Entity\Query\Sql\Query
  5. 9 core/lib/Drupal/Core/Entity/Query/Null/Query.php \Drupal\Core\Entity\Query\Null\Query
  6. 9 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php \Drupal\Core\Entity\KeyValueStore\Query\Query
  7. 8.9.x core/modules/workspaces/src/EntityQuery/Query.php \Drupal\workspaces\EntityQuery\Query
  8. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Query.php \Drupal\Core\Config\Entity\Query\Query
  9. 8.9.x core/lib/Drupal/Core/Database/Query/Query.php \Drupal\Core\Database\Query\Query
  10. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php \Drupal\Core\Entity\Query\Sql\Query
  11. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/Query.php \Drupal\Core\Entity\Query\Null\Query
  12. 8.9.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php \Drupal\Core\Entity\KeyValueStore\Query\Query
  13. 10 core/modules/workspaces/src/EntityQuery/Query.php \Drupal\workspaces\EntityQuery\Query
  14. 10 core/lib/Drupal/Core/Config/Entity/Query/Query.php \Drupal\Core\Config\Entity\Query\Query
  15. 10 core/lib/Drupal/Core/Database/Query/Query.php \Drupal\Core\Database\Query\Query
  16. 10 core/lib/Drupal/Core/Entity/Query/Sql/Query.php \Drupal\Core\Entity\Query\Sql\Query
  17. 10 core/lib/Drupal/Core/Entity/Query/Null/Query.php \Drupal\Core\Entity\Query\Null\Query
  18. 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php \Drupal\Core\Entity\KeyValueStore\Query\Query
  19. 11.x core/modules/workspaces/src/EntityQuery/Query.php \Drupal\workspaces\EntityQuery\Query
  20. 11.x core/lib/Drupal/Core/Config/Entity/Query/Query.php \Drupal\Core\Config\Entity\Query\Query
  21. 11.x core/lib/Drupal/Core/Database/Query/Query.php \Drupal\Core\Database\Query\Query
  22. 11.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php \Drupal\Core\Entity\Query\Sql\Query
  23. 11.x core/lib/Drupal/Core/Entity/Query/Null/Query.php \Drupal\Core\Entity\Query\Null\Query
  24. 11.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php \Drupal\Core\Entity\KeyValueStore\Query\Query

Base class for query builders.

Note that query builders use PHP's magic __toString() method to compile the query object into a prepared statement.

Hierarchy

Expanded class hierarchy of Query

Related topics

3 string references to 'Query'
SelectQuery::preExecute in includes/database/select.inc
Generic preparation and validation for a SELECT query.
system_update_7076 in modules/system/system.install
Convert menu_links query strings into arrays.
_drupal_decode_exception in includes/errors.inc
Decodes an exception and retrieves the correct caller.

File

includes/database/query.inc, line 275

View source
abstract class Query implements QueryPlaceholderInterface {
    
    /**
     * The connection object on which to run this query.
     *
     * @var DatabaseConnection
     */
    protected $connection;
    
    /**
     * The target of the connection object.
     *
     * @var string
     */
    protected $connectionTarget;
    
    /**
     * The key of the connection object.
     *
     * @var string
     */
    protected $connectionKey;
    
    /**
     * The query options to pass on to the connection object.
     *
     * @var array
     */
    protected $queryOptions;
    
    /**
     * A unique identifier for this query object.
     */
    protected $uniqueIdentifier;
    
    /**
     * The placeholder counter.
     */
    protected $nextPlaceholder = 0;
    
    /**
     * An array of comments that can be prepended to a query.
     *
     * @var array
     */
    protected $comments = array();
    
    /**
     * Constructs a Query object.
     *
     * @param DatabaseConnection $connection
     *   Database connection object.
     * @param array $options
     *   Array of query options.
     */
    public function __construct(DatabaseConnection $connection, $options) {
        $this->uniqueIdentifier = uniqid('', TRUE);
        $this->connection = $connection;
        $this->connectionKey = $this->connection
            ->getKey();
        $this->connectionTarget = $this->connection
            ->getTarget();
        $this->queryOptions = $options;
    }
    
    /**
     * Implements the magic __sleep function to disconnect from the database.
     */
    public function __sleep() {
        $keys = get_object_vars($this);
        unset($keys['connection']);
        return array_keys($keys);
    }
    
    /**
     * Implements the magic __wakeup function to reconnect to the database.
     */
    public function __wakeup() {
        $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
    }
    
    /**
     * Implements the magic __clone function.
     */
    public function __clone() {
        $this->uniqueIdentifier = uniqid('', TRUE);
    }
    
    /**
     * Runs the query against the database.
     */
    protected abstract function execute();
    
    /**
     * Implements PHP magic __toString method to convert the query to a string.
     *
     * The toString operation is how we compile a query object to a prepared
     * statement.
     *
     * @return
     *   A prepared statement query string for this object.
     */
    public abstract function __toString();
    
    /**
     * Returns a unique identifier for this object.
     */
    public function uniqueIdentifier() {
        return $this->uniqueIdentifier;
    }
    
    /**
     * Gets the next placeholder value for this query object.
     *
     * @return int
     *   Next placeholder value.
     */
    public function nextPlaceholder() {
        return $this->nextPlaceholder++;
    }
    
    /**
     * Adds a comment to the query.
     *
     * By adding a comment to a query, you can more easily find it in your
     * query log or the list of active queries on an SQL server. This allows
     * for easier debugging and allows you to more easily find where a query
     * with a performance problem is being generated.
     *
     * The comment string will be sanitized to remove * / and other characters
     * that may terminate the string early so as to avoid SQL injection attacks.
     *
     * @param $comment
     *   The comment string to be inserted into the query.
     *
     * @return Query
     *   The called object.
     */
    public function comment($comment) {
        $this->comments[] = $comment;
        return $this;
    }
    
    /**
     * Returns a reference to the comments array for the query.
     *
     * Because this method returns by reference, alter hooks may edit the comments
     * array directly to make their changes. If just adding comments, however, the
     * use of comment() is preferred.
     *
     * Note that this method must be called by reference as well:
     * @code
     * $comments =& $query->getComments();
     * @endcode
     *
     * @return
     *   A reference to the comments array structure.
     */
    public function &getComments() {
        return $this->comments;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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::execute abstract protected function Runs the query against the database. 6
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::__construct public function Constructs a Query object. 6
Query::__sleep public function Implements the magic __sleep function to disconnect from the database.
Query::__toString abstract public function Implements PHP magic __toString method to convert the query to a string. 6
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.